Ejemplo n.º 1
0
 def test_list_types(self):
     base_path = op.join(fixtures, 'list_types')
     types = list(core.Type.list_types(base_path))
     types_expected = [
         core.Type(base_path, '__first'),
         core.Type(base_path, '__second'),
         core.Type(base_path, '__third'),
     ]
     self.assertEqual(types, types_expected)
Ejemplo n.º 2
0
 def test_list_objects(self):
     objects = list(
         core.Object.list_objects(object_base_path, type_base_path))
     objects_expected = [
         core.Object(core.Type(type_base_path, '__first'), object_base_path,
                     'man'),
         core.Object(core.Type(type_base_path, '__second'),
                     object_base_path, 'on-the'),
         core.Object(core.Type(type_base_path, '__third'), object_base_path,
                     'moon'),
     ]
     self.assertEqual(objects, objects_expected)
Ejemplo n.º 3
0
 def test_run_type_explorers(self):
     cdist_type = core.Type(self.local.type_path, '__test_type')
     cdist_object = core.Object(cdist_type, self.local.object_path,
                                'whatever')
     cdist_object.create()
     self.explorer.run_type_explorers(cdist_object)
     self.assertEqual(cdist_object.explorers, {'world': 'hello'})
Ejemplo n.º 4
0
 def test_run_type_explorer(self):
     cdist_type = core.Type(self.local.type_path, '__test_type')
     cdist_object = core.Object(cdist_type, self.local.object_path,
                                'whatever')
     self.explorer.transfer_type_explorers(cdist_type)
     output = self.explorer.run_type_explorer('world', cdist_object)
     self.assertEqual(output, 'hello\n')
Ejemplo n.º 5
0
 def test_transfer_type_explorers(self):
     cdist_type = core.Type(self.local.type_path, '__test_type')
     self.explorer.transfer_type_explorers(cdist_type)
     source = os.path.join(self.local.type_path, cdist_type.explorer_path)
     destination = os.path.join(self.remote.type_path,
                                cdist_type.explorer_path)
     self.assertEqual(os.listdir(source), os.listdir(destination))
Ejemplo n.º 6
0
 def test_autorequire(self):
     initial_manifest = os.path.join(self.local.manifest_path, "init")
     self.manifest.run_initial_manifest(initial_manifest)
     cdist_type = core.Type(self.local.type_path, '__saturn')
     cdist_object = core.Object(cdist_type, self.local.object_path, 'singleton')
     self.manifest.run_type_manifest(cdist_object)
     expected = ['__planet/Saturn', '__moon/Prometheus']
     self.assertEqual(sorted(cdist_object.requirements), sorted(expected))
Ejemplo n.º 7
0
    def test_arguments_with_dashes(self):
        argv = ['__arguments_with_dashes', 'some-id', '--with-dash', 'some value']
        os.environ.update(self.env)
        emu = emulator.Emulator(argv)
        emu.run()

        cdist_type = core.Type(self.local.type_path, '__arguments_with_dashes')
        cdist_object = core.Object(cdist_type, self.local.object_path, 'some-id')
        self.assertTrue('with-dash' in cdist_object.parameters)
Ejemplo n.º 8
0
 def test_transfer_type_explorers_only_once(self):
     cdist_type = core.Type(self.local.type_path, '__test_type')
     # first transfer
     self.explorer.transfer_type_explorers(cdist_type)
     source = os.path.join(self.local.type_path, cdist_type.explorer_path)
     destination = os.path.join(self.remote.type_path,
                                cdist_type.explorer_path)
     self.assertEqual(os.listdir(source), os.listdir(destination))
     # nuke destination folder content, but recreate directory
     shutil.rmtree(destination)
     os.makedirs(destination)
     # second transfer, should not happen
     self.explorer.transfer_type_explorers(cdist_type)
     self.assertFalse(os.listdir(destination))
Ejemplo n.º 9
0
    def __init__(self, argv):
        self.argv = argv
        self.object_id = False

        self.global_path = os.environ['__global']
        self.object_source = os.environ['__cdist_manifest']
        self.target_host = os.environ['__target_host']
        self.type_base_path = os.environ['__cdist_type_base_path']

        self.object_base_path = os.path.join(self.global_path, "object")

        self.type_name = os.path.basename(argv[0])
        self.cdist_type = core.Type(self.type_base_path, self.type_name)

        self.__init_log()
Ejemplo n.º 10
0
 def test_transfer_object_parameters(self):
     cdist_type = core.Type(self.local.type_path, '__test_type')
     cdist_object = core.Object(cdist_type, self.local.object_path,
                                'whatever')
     cdist_object.create()
     cdist_object.parameters = {
         'first': 'first value',
         'second': 'second value'
     }
     self.explorer.transfer_object_parameters(cdist_object)
     source = os.path.join(self.local.object_path,
                           cdist_object.parameter_path)
     destination = os.path.join(self.remote.object_path,
                                cdist_object.parameter_path)
     self.assertEqual(sorted(os.listdir(source)),
                      sorted(os.listdir(destination)))
Ejemplo n.º 11
0
    def record_requirements(self):
        """record requirements"""

        if "require" in os.environ:
            requirements = os.environ['require']
            self.log.debug("reqs = " + requirements)
            for requirement in requirements.split(" "):
                # Ignore empty fields - probably the only field anyway
                if len(requirement) == 0:
                    continue

                self.log.debug("Recording requirement: " + requirement)
                requirement_parts = requirement.split(os.sep, 1)
                requirement_type_name = requirement_parts[0]
                try:
                    requirement_object_id = requirement_parts[1]
                except IndexError:
                    # no object id, assume singleton
                    requirement_object_id = 'singleton'

                # Remove leading / from object id
                requirement_object_id = requirement_object_id.lstrip('/')

                # Instantiate type which fails if type does not exist
                requirement_type = core.Type(self.type_base_path,
                                             requirement_type_name)

                if requirement_object_id == 'singleton' \
                    and not requirement_type.is_singleton:
                    raise IllegalRequirementError(
                        requirement,
                        "Missing object_id and type is not a singleton.")

                # Instantiate object which fails if the object_id is illegal
                requirement_object = core.Object(requirement_type,
                                                 self.object_base_path,
                                                 requirement_object_id)

                # Construct cleaned up requirement with only one / :-)
                requirement = requirement_type_name + '/' + requirement_object_id
                self.cdist_object.requirements.append(requirement)

        # Record / Append source
        self.cdist_object.source.append(self.object_source)
Ejemplo n.º 12
0
    def test_type_manifest_environment(self):
        cdist_type = core.Type(self.local.type_path, '__dump_environment')
        cdist_object = core.Object(cdist_type, self.local.object_path, 'whatever')
        handle, output_file = self.mkstemp(dir=self.temp_dir)
        os.close(handle)
        os.environ['__cdist_test_out'] = output_file
        self.manifest.run_type_manifest(cdist_object)

        with open(output_file, 'r') as fd:
            output_string = fd.read()
        output_dict = {}
        for line in output_string.split('\n'):
            if line:
                key,value = line.split(': ')
                output_dict[key] = value
        self.assertTrue(output_dict['PATH'].startswith(self.local.bin_path))
        self.assertEqual(output_dict['__target_host'], self.local.target_host)
        self.assertEqual(output_dict['__global'], self.local.out_path)
        self.assertEqual(output_dict['__cdist_type_base_path'], self.local.type_path)
        self.assertEqual(output_dict['__type'], cdist_type.absolute_path)
        self.assertEqual(output_dict['__object'], cdist_object.absolute_path)
        self.assertEqual(output_dict['__object_id'], cdist_object.object_id)
        self.assertEqual(output_dict['__object_name'], cdist_object.name)
        self.assertEqual(output_dict['__self'], cdist_object.name)
Ejemplo n.º 13
0
    def setUp(self):
        self.target_host = 'localhost'

        self.local_base_path = local_base_path
        self.out_path = self.mkdtemp()
        self.local = local.Local(self.target_host, self.local_base_path,
                                 self.out_path)
        self.local.create_directories()

        self.remote_base_path = self.mkdtemp()
        self.user = getpass.getuser()
        remote_exec = "ssh -o User=%s -q" % self.user
        remote_copy = "scp -o User=%s -q" % self.user
        self.remote = remote.Remote(self.target_host, self.remote_base_path,
                                    remote_exec, remote_copy)

        self.code = code.Code(self.target_host, self.local, self.remote)

        self.cdist_type = core.Type(self.local.type_path, '__dump_environment')
        self.cdist_object = core.Object(self.cdist_type,
                                        self.local.object_path, 'whatever')
        self.cdist_object.create()

        self.log = logging.getLogger("cdist")
Ejemplo n.º 14
0
 def test_base_path(self):
     base_path = fixtures
     cdist_type = core.Type(base_path, '__name_path')
     self.assertEqual(cdist_type.base_path, base_path)
Ejemplo n.º 15
0
 def test_only_one_instance(self):
     base_path = fixtures
     cdist_type1 = core.Type(base_path, '__name_path')
     cdist_type2 = core.Type(base_path, '__name_path')
     self.assertEqual(id(cdist_type1), id(cdist_type2))
Ejemplo n.º 16
0
 def test_without_optional_parameters(self):
     base_path = fixtures
     cdist_type = core.Type(base_path, '__without_optional_parameters')
     self.assertEqual(cdist_type.optional_parameters, [])
Ejemplo n.º 17
0
 def test_with_required_parameters(self):
     base_path = fixtures
     cdist_type = core.Type(base_path, '__with_required_parameters')
     self.assertEqual(cdist_type.required_parameters,
                      ['required1', 'required2'])
Ejemplo n.º 18
0
 def test_without_explorers(self):
     base_path = fixtures
     cdist_type = core.Type(base_path, '__without_explorers')
     self.assertEqual(cdist_type.explorers, [])
Ejemplo n.º 19
0
 def test_not_install_is_install(self):
     base_path = fixtures
     cdist_type = core.Type(base_path, '__not_install')
     self.assertFalse(cdist_type.is_install)
Ejemplo n.º 20
0
 def test_not_singleton_is_singleton(self):
     base_path = fixtures
     cdist_type = core.Type(base_path, '__not_singleton')
     self.assertFalse(cdist_type.is_singleton)
Ejemplo n.º 21
0
 def setUp(self):
     self.cdist_type = core.Type(type_base_path, '__third')
     self.cdist_object = core.Object(self.cdist_type, object_base_path,
                                     'moon')
Ejemplo n.º 22
0
 def test_object_id_contains_object_marker_string(self):
     cdist_type = core.Type(type_base_path, '__third')
     illegal_object_id = 'object_id/may/contain_%s_in_filename' % core.OBJECT_MARKER
     core.Object(cdist_type, object_base_path, illegal_object_id)
Ejemplo n.º 23
0
 def test_list_type_explorer_names(self):
     cdist_type = core.Type(self.local.type_path, '__test_type')
     expected = cdist_type.explorers
     self.assertEqual(self.explorer.list_type_explorer_names(cdist_type),
                      expected)
Ejemplo n.º 24
0
 def test_explorer_path(self):
     base_path = fixtures
     cdist_type = core.Type(base_path, '__name_path')
     self.assertEqual(cdist_type.explorer_path,
                      os.path.join('__name_path', 'explorer'))
Ejemplo n.º 25
0
 def test_absolute_path(self):
     base_path = fixtures
     cdist_type = core.Type(base_path, '__name_path')
     self.assertEqual(cdist_type.absolute_path,
                      os.path.join(base_path, '__name_path'))
Ejemplo n.º 26
0
 def test_manifest_path(self):
     base_path = fixtures
     cdist_type = core.Type(base_path, '__name_path')
     self.assertEqual(cdist_type.manifest_path,
                      os.path.join('__name_path', 'manifest'))
Ejemplo n.º 27
0
 def test_object_id_starts_with_slash(self):
     cdist_type = core.Type(type_base_path, '__third')
     illegal_object_id = '/object_id/may/not/start/with/slash'
     with self.assertRaises(core.IllegalObjectIdError):
         core.Object(cdist_type, object_base_path, illegal_object_id)
Ejemplo n.º 28
0
 def test_gencode_remote_path(self):
     base_path = fixtures
     cdist_type = core.Type(base_path, '__name_path')
     self.assertEqual(cdist_type.gencode_remote_path,
                      os.path.join('__name_path', 'gencode-remote'))
Ejemplo n.º 29
0
 def test_object_id_contains_object_marker(self):
     cdist_type = core.Type(type_base_path, '__third')
     illegal_object_id = 'object_id/may/not/contain/%s/anywhere' % core.OBJECT_MARKER
     with self.assertRaises(core.IllegalObjectIdError):
         core.Object(cdist_type, object_base_path, illegal_object_id)