Exemple #1
0
    def test_load_environment(self):
        local_root = tempfile.mkdtemp()
        config = Configuration("agent", "1.2.3")
        config.system_root = local_root
        self.add_cleanup_path(local_root)
        split = config.split_version()
        filename = config.name + config.file_extension
        paths = [
            join(config.system_root, config.child_dir, split[2], filename),
            join(config.system_root, config.child_dir + os.sep, filename),
        ]

        for i, path in enumerate(paths):
            try:
                os.makedirs(dirname(path))
            except OSError:
                pass

            data = dedent(
                """
            env:
                key%s: %s
                key: %s
            """
                % (i, i, i)
            )

            with open(path, "w") as stream:
                stream.write(data)
                self.add_cleanup_path(stream.name)

        environment = {}
        config.load(environment=environment)
        self.assertEqual(environment, {"key0": 0, "key1": 1, "key": 1})
Exemple #2
0
 def test_files_system_root(self):
     os.environ[Configuration.DEFAULT_ENVIRONMENT_PATH_VARIABLE] = tempfile.mkdtemp()
     config = Configuration("agent", "1.2.3")
     os.environ.pop(Configuration.DEFAULT_ENVIRONMENT_PATH_VARIABLE)
     split = config.split_version()
     filename = config.name + config.file_extension
     all_paths = [
         join(config.system_root, config.child_dir, split[0], filename),
         join(config.system_root, config.child_dir, split[1], filename),
         join(config.system_root, config.child_dir, split[2], filename),
         join(config.system_root, config.child_dir + os.sep, filename),
         join(config.user_root, "." + config.child_dir, split[0], filename),
         join(config.user_root, "." + config.child_dir, split[1], filename),
         join(config.user_root, "." + config.child_dir, split[2], filename),
         join(config.user_root, "." + config.child_dir + os.sep, filename),
         join(config.local_dir, config.child_dir, split[0], filename),
         join(config.local_dir, config.child_dir, split[1], filename),
         join(config.local_dir, config.child_dir, split[2], filename),
         join(config.local_dir, config.child_dir + os.sep, filename),
         join(config.environment_root, config.child_dir, split[0], filename),
         join(config.environment_root, config.child_dir, split[1], filename),
         join(config.environment_root, config.child_dir, split[2], filename),
         join(config.environment_root, config.child_dir + os.sep, filename),
     ]
     self.assertEqual(config.files(validate=False), all_paths)
Exemple #3
0
 def test_recursive_expansion(self):
     envvar1 = "a" + uuid.uuid4().hex
     envvalue1 = "a" + uuid.uuid4().hex
     envvar2 = "b" + uuid.uuid4().hex
     envvalue2 = "b" + uuid.uuid4().hex
     os.environ[envvar1] = envvalue1
     os.environ[envvar2] = envvalue2
     config = Configuration("pyfarm.core")
     config.update(
         foo="foo",
         bar="bar",
         foobar="$foo/$bar",
         home="~/foo",
         path="$foobar/$%s" % envvar1,
         envvar2_expand="$%s" % envvar2,
     )
     config.update({envvar2: "envvar2"})
     self.assertEqual(config["foobar"], "foo/bar")
     self.assertEqual(config["path"], "foo/bar/%s" % envvalue1)
     self.assertEqual(config["home"], expanduser("~/foo"))
     self.assertEqual(config["envvar2_expand"], "envvar2")
Exemple #4
0
    def test_files_filtered_with_files(self):
        local_root = tempfile.mkdtemp()
        config = Configuration("agent", "1.2.3")
        config.system_root = local_root
        self.add_cleanup_path(local_root)
        split = config.split_version()
        filename = config.name + config.file_extension
        paths = [
            join(config.system_root, config.child_dir, split[2], filename),
            join(config.system_root, config.child_dir + os.sep, filename),
        ]

        for path in paths:
            try:
                os.makedirs(dirname(path))
            except OSError:
                pass

            with open(path, "w") as stream:
                self.add_cleanup_path(stream.name)

        self.assertEqual(config.files(), paths)
Exemple #5
0
    def test_load_empty_file(self):
        local_root = tempfile.mkdtemp()
        config = Configuration("agent", "1.2.3")
        config.system_root = local_root
        self.add_cleanup_path(local_root)
        split = config.split_version()
        filename = config.name + config.file_extension
        paths = [
            join(config.system_root, config.child_dir + os.sep, filename),
            join(config.system_root, config.child_dir, split[2], filename),
        ]

        for i, path in enumerate(paths):
            try:
                os.makedirs(dirname(path))
            except OSError:
                pass

            with open(path, "w") as stream:
                stream.write("")
                self.add_cleanup_path(stream.name)

        config.load()
Exemple #6
0
    def __init__(self, data=None, environment=None, load=True):
        super(LoggingConfiguration, self).__init__("pyfarm.agent")
        assert data is None or isinstance(data, dict)
        assert environment is None or isinstance(environment, dict)

        if environment is None:
            environment = os.environ

        if load:
            self.load(environment=environment)
            self.update(
                # A mapping of UUIDs to job type instances.
                jobtypes={},

                # A mapping of tasks to job type instances.
                current_assignments={},

                # The last time we were in touch with the master,
                # or the last time it was in touch with us.
                last_master_contact=None,

                # The last time we announced ourselves to the master.  This
                # may be longer than --master-reannounce if
                # `last_master_contact` caused us to skip an announcement.
                last_announce=None)

        if data is not None:
            self.update(data)

        # Load configuration file(s) for jobtypes and then
        # update the local instance
        if load:
            jobtypes_config = Configuration(
                "pyfarm.jobtypes", version=self.version)
            jobtypes_config.load(environment=environment)
            self.update(jobtypes_config)
Exemple #7
0
 def test_temp_get(self):
     key = uuid.uuid4().hex
     config = Configuration("pyfarm.core")
     config[key] = "$temp/bar"
     self.assertEqual(config.get(key), config.tempdir + "/bar")
Exemple #8
0
 def test_get_default_behavior(self):
     key = uuid.uuid4().hex
     config = Configuration("pyfarm.core")
     self.assertIsNone(config.get(key))
Exemple #9
0
 def test_files_filtered_without_files(self):
     config = Configuration("agent", "1.2.3")
     self.assertEqual(config.files(), [])
Exemple #10
0
 def test_split_empty_version(self):
     config = Configuration("agent", "")
     self.assertEqual(config.split_version(), [])
Exemple #11
0
 def test_split_version(self):
     config = Configuration("agent", "1.2.3")
     self.assertEqual(config.split_version(), ["1.2.3", "1.2", "1"])