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)
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)
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'})
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')
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))
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))
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)
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))
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()
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)))
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)
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)
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")
def test_base_path(self): base_path = fixtures cdist_type = core.Type(base_path, '__name_path') self.assertEqual(cdist_type.base_path, base_path)
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))
def test_without_optional_parameters(self): base_path = fixtures cdist_type = core.Type(base_path, '__without_optional_parameters') self.assertEqual(cdist_type.optional_parameters, [])
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'])
def test_without_explorers(self): base_path = fixtures cdist_type = core.Type(base_path, '__without_explorers') self.assertEqual(cdist_type.explorers, [])
def test_not_install_is_install(self): base_path = fixtures cdist_type = core.Type(base_path, '__not_install') self.assertFalse(cdist_type.is_install)
def test_not_singleton_is_singleton(self): base_path = fixtures cdist_type = core.Type(base_path, '__not_singleton') self.assertFalse(cdist_type.is_singleton)
def setUp(self): self.cdist_type = core.Type(type_base_path, '__third') self.cdist_object = core.Object(self.cdist_type, object_base_path, 'moon')
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)
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)
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'))
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'))
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'))
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)
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'))
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)