def test_requirement_via_order_dependency(self): self.env['CDIST_ORDER_DEPENDENCY'] = 'on' argv = ['__planet', 'erde'] emu = emulator.Emulator(argv, env=self.env) emu.run() argv = ['__planet', 'mars'] emu = emulator.Emulator(argv, env=self.env) emu.run() # In real world, this is not shared over instances del self.env['require'] argv = ['__file', '/tmp/cdisttest'] emu = emulator.Emulator(argv, env=self.env) emu.run() # now load the objects and verify the require parameter of the objects cdist_type = core.CdistType(self.local.type_path, '__planet') erde_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, 'erde') mars_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, 'mars') cdist_type = core.CdistType(self.local.type_path, '__file') file_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, '/tmp/cdisttest') # now test the recorded requirements self.assertTrue(len(erde_object.requirements) == 0) self.assertEqual(list(mars_object.requirements), ['__planet/erde']) self.assertEqual(list(file_object.requirements), ['__planet/mars'])
def test_list_types(self): base_path = op.join(fixtures, 'list_types') types = list(core.CdistType.list_types(base_path)) types_expected = [ core.CdistType(base_path, '__first'), core.CdistType(base_path, '__second'), core.CdistType(base_path, '__third'), ] self.assertEqual(types, types_expected)
def test_list_objects(self): objects = list( core.CdistObject.list_objects(object_base_path, type_base_path)) objects_expected = [ core.CdistObject(core.CdistType(type_base_path, '__first'), object_base_path, 'man'), core.CdistObject(core.CdistType(type_base_path, '__second'), object_base_path, 'on-the'), core.CdistObject(core.CdistType(type_base_path, '__third'), object_base_path, 'moon'), ] self.assertEqual(objects, objects_expected)
def setUp(self): self.local_dir = self.mkdtemp() self.hostdir = cdist.str_hash(self.target_host[0]) self.host_base_path = os.path.join(self.local_dir, self.hostdir) self.local = local.Local(target_host=self.target_host, target_host_tags=self.target_host_tags, base_root_path=self.host_base_path, host_dir_name=self.hostdir, exec_path=cdist.test.cdist_exec_path, add_conf_dirs=[conf_dir]) self.local.create_files_dirs() self.remote_dir = self.mkdtemp() remote_exec = self.remote_exec remote_copy = self.remote_copy self.remote = remote.Remote(target_host=self.target_host, remote_exec=remote_exec, remote_copy=remote_copy, base_path=self.remote_dir) self.remote.create_files_dirs() self.code = code.Code(self.target_host, self.local, self.remote) self.cdist_type = core.CdistType(self.local.type_path, '__dump_environment') self.cdist_object = core.CdistObject(self.cdist_type, self.local.object_path, 'whatever', self.local.object_marker_name) self.cdist_object.create()
def test_transfer_type_explorers(self): """Test if transferring type explorers works""" cdist_type = core.CdistType(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_object_different_requirements(self): argv = ['__directory', 'spam'] emu = emulator.Emulator(argv, env=self.env) emu.run() argv = ['__directory', 'spameggs'] emu = emulator.Emulator(argv, env=self.env) emu.run() argv = ['__file', 'eggs'] if 'require' in self.env: del self.env['require'] self.env['require'] = '__directory/spam' emu = emulator.Emulator(argv, env=self.env) emu.run() argv = ['__file', 'eggs'] self.env['require'] = '__directory/spameggs' emu = emulator.Emulator(argv, env=self.env) emu.run() cdist_type = core.CdistType(self.local.type_path, '__file') cdist_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, 'eggs') reqs = set(( '__directory/spam', '__directory/spameggs', )) self.assertEqual(reqs, set(cdist_object.requirements))
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.CdistType(self.local.type_path, '__dump_environment') self.cdist_object = core.CdistObject(self.cdist_type, self.local.object_path, 'whatever') self.cdist_object.create() self.log = logging.getLogger("cdist")
def test_type_manifest_environment(self): cdist_type = core.CdistType(self.local.type_path, '__dump_environment') cdist_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, '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[0]) self.assertEqual(output_dict['__target_hostname'], self.local.target_host[1]) self.assertEqual(output_dict['__target_fqdn'], self.local.target_host[2]) self.assertEqual(output_dict['__global'], self.local.base_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['__files'], self.local.files_path)
def test_explorer_environment(self): cdist_type = core.CdistType(self.local.type_path, '__dump_env') cdist_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, 'whatever') self.explorer.transfer_type_explorers(cdist_type) output = self.explorer.run_type_explorer('dump', cdist_object) output_dict = {} for line in output.split('\n'): if line: key, value = line.split(': ') output_dict[key] = value self.assertEqual(output_dict['__target_host'], self.local.target_host[0]) self.assertEqual(output_dict['__target_hostname'], self.local.target_host[1]) self.assertEqual(output_dict['__target_fqdn'], self.local.target_host[2]) self.assertEqual(output_dict['__explorer'], self.remote.global_explorer_path) self.assertEqual(output_dict['__target_host_tags'], self.local.target_host_tags) self.assertEqual(output_dict['__cdist_log_level'], str(logging.WARNING)) self.assertEqual(output_dict['__cdist_log_level_name'], 'WARNING')
def test_directory_in_default(self): base_path = fixtures cdist_type = core.CdistType(base_path, '__directory_in_default') self.assertEqual( list(sorted(cdist_type.parameter_defaults.keys())), ['bar', 'foo'] )
def __init__(self, argv, stdin=sys.stdin.buffer, env=os.environ): self.argv = argv self.stdin = stdin self.env = env self.object_id = '' try: self.global_path = self.env['__global'] self.target_host = self.env['__target_host'] # Internal variables self.object_source = self.env['__cdist_manifest'] self.type_base_path = self.env['__cdist_type_base_path'] self.object_marker = self.env['__cdist_object_marker'] except KeyError as e: raise MissingRequiredEnvironmentVariableError(e.args[0]) self.object_base_path = os.path.join(self.global_path, "object") self.typeorder_path = os.path.join(self.global_path, "typeorder") self.type_name = os.path.basename(argv[0]) self.cdist_type = core.CdistType(self.type_base_path, self.type_name) self.__init_log()
def __init__(self, argv, stdin=sys.stdin.buffer, env=os.environ): self.argv = argv self.stdin = stdin self.env = env self.object_id = '' try: self.global_path = self.env['__global'] self.target_host = (self.env['__target_host'], self.env['__target_hostname'], self.env['__target_fqdn']) # Internal variables self.object_source = self.env['__cdist_manifest'] self.type_base_path = self.env['__cdist_type_base_path'] self.object_marker = self.env['__cdist_object_marker'] except KeyError as e: raise MissingRequiredEnvironmentVariableError(e.args[0]) self.object_base_path = os.path.join(self.global_path, "object") self.typeorder_path = os.path.join(self.global_path, "typeorder") self.type_name = os.path.basename(argv[0]) self.cdist_type = core.CdistType(self.type_base_path, self.type_name) # If set then object alreay exists and this var holds existing # requirements. self._existing_reqs = None self.__init_log()
def test_object_id_contains_object_marker(self): cdist_type = core.CdistType(type_base_path, '__third') illegal_object_id = ('object_id/may/not/contain/%s/anywhere' % OBJECT_MARKER_NAME) with self.assertRaises(core.IllegalObjectIdError): core.CdistObject(cdist_type, self.object_base_path, OBJECT_MARKER_NAME, illegal_object_id)
def test_run_type_explorers(self): cdist_type = core.CdistType(self.local.type_path, '__test_type') cdist_object = core.CdistObject(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.CdistType(self.local.type_path, '__test_type') cdist_object = core.CdistObject(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 setUp(self): self.local_dir = self.mkdtemp() self.local = local.Local(target_host=self.target_host, base_path=self.local_dir, exec_path=cdist.test.cdist_exec_path, add_conf_dirs=[conf_dir]) self.local.create_files_dirs() self.remote_dir = self.mkdtemp() remote_exec = self.remote_exec remote_copy = self.remote_copy self.remote = remote.Remote(target_host=self.target_host, remote_exec=remote_exec, remote_copy=remote_copy, base_path=self.remote_dir) self.remote.create_files_dirs() self.code = code.Code(self.target_host, self.local, self.remote) self.cdist_type = core.CdistType(self.local.type_path, '__dump_environment') self.cdist_object = core.CdistObject(self.cdist_type, self.local.object_path, 'whatever') self.cdist_object.create()
def test_with_deprecated_parameters(self): base_path = fixtures cdist_type = core.CdistType(base_path, '__with_deprecated_parameters') self.assertTrue('eggs' in cdist_type.deprecated_parameters) self.assertTrue('spam' in cdist_type.deprecated_parameters) self.assertEqual(cdist_type.deprecated_parameters['eggs'], 'Deprecated') self.assertEqual(cdist_type.deprecated_parameters['spam'], '')
def test_autorequire(self): initial_manifest = os.path.join(self.local.manifest_path, "init") self.manifest.run_initial_manifest(initial_manifest) cdist_type = core.CdistType(self.local.type_path, '__saturn') cdist_object = core.CdistObject(cdist_type, self.local.object_path) self.manifest.run_type_manifest(cdist_object) expected = ['__planet/Saturn', '__moon/Prometheus'] self.assertEqual(sorted(cdist_object.autorequire), 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.CdistType(self.local.type_path, '__arguments_with_dashes') cdist_object = core.CdistObject(cdist_type, self.local.object_path, 'some-id') self.assertTrue('with-dash' in cdist_object.parameters)
def test_transfer_object_parameters(self): cdist_type = core.CdistType(self.local.type_path, '__test_type') cdist_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, '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 setUp(self): self.objects = list( core.CdistObject.list_objects(object_base_path, type_base_path)) self.object_index = dict((o.name, o) for o in self.objects) self.object_names = [o.name for o in self.objects] print(self.objects) self.cdist_type = core.CdistType(type_base_path, '__third') self.cdist_object = core.CdistObject(self.cdist_type, object_base_path, 'moon')
def setUp(self): # logging.root.setLevel(logging.TRACE) save_output_streams = False self.temp_dir = self.mkdtemp() self.local_dir = os.path.join(self.temp_dir, "local") self.hostdir = cdist.str_hash(self.target_host[0]) self.host_base_path = os.path.join(self.local_dir, self.hostdir) os.makedirs(self.host_base_path) self.local = local.Local(target_host=self.target_host, target_host_tags=None, base_root_path=self.host_base_path, host_dir_name=self.hostdir, exec_path=cdist.test.cdist_exec_path, add_conf_dirs=[conf_dir], save_output_streams=save_output_streams) self.local.create_files_dirs() self.remote_dir = self.mkdtemp() remote_exec = self.remote_exec remote_copy = self.remote_copy self.remote = remote.Remote( target_host=self.target_host, remote_exec=remote_exec, remote_copy=remote_copy, base_path=self.remote_dir, stdout_base_path=self.local.stdout_base_path, stderr_base_path=self.local.stderr_base_path, save_output_streams=save_output_streams) self.remote.create_files_dirs() self.code = code.Code(self.target_host, self.local, self.remote) self.manifest = manifest.Manifest(self.target_host, self.local) self.cdist_type = core.CdistType(self.local.type_path, '__write_to_stdout_and_stderr') self.cdist_object = core.CdistObject(self.cdist_type, self.local.object_path, self.local.object_marker_name, '') self.cdist_object.create() self.output_dirs = { 'object': { 'stdout': os.path.join(self.cdist_object.absolute_path, 'stdout'), 'stderr': os.path.join(self.cdist_object.absolute_path, 'stderr'), }, 'init': { 'stdout': os.path.join(self.local.base_path, 'stdout'), 'stderr': os.path.join(self.local.base_path, 'stderr'), }, }
def setUp(self): self.tempdir = tempfile.mkdtemp(prefix="cdist.test") self.object_base_path = self.tempdir self.cdist_type = core.CdistType(type_base_path, '__third') self.cdist_object = core.CdistObject(self.cdist_type, self.object_base_path, OBJECT_MARKER_NAME, 'moon') self.cdist_object.create() self.cdist_object.parameters['planet'] = 'Saturn' self.cdist_object.parameters['name'] = 'Prometheus'
def setUp(self): self.tempdir = tempfile.mkdtemp(prefix="cdist.test") self.object_base_path = self.tempdir self.expected_objects = [] for cdist_object_name in expected_object_names: cdist_type, cdist_object_id = cdist_object_name.split("/", 1) cdist_object = core.CdistObject( core.CdistType(type_base_path, cdist_type), self.object_base_path, OBJECT_MARKER_NAME, cdist_object_id) cdist_object.create() self.expected_objects.append(cdist_object)
def test_transfer_type_explorers_only_once(self): cdist_type = core.CdistType(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 setUp(self): self.expected_object_names = sorted([ '__first/child', '__first/dog', '__first/man', '__first/woman', '__second/on-the', '__second/under-the', '__third/moon' ]) self.expected_objects = [] for cdist_object_name in self.expected_object_names: cdist_type, cdist_object_id = cdist_object_name.split("/", maxsplit=1) cdist_object = core.CdistObject( core.CdistType(type_base_path, cdist_type), object_base_path, cdist_object_id) self.expected_objects.append(cdist_object)
def test_boolean(self): type_name = '__arguments_boolean' object_id = 'some-id' argv = [type_name, object_id, '--boolean1'] os.environ.update(self.env) emu = emulator.Emulator(argv) emu.run() cdist_type = core.CdistType(self.local.type_path, type_name) cdist_object = core.CdistObject(cdist_type, self.local.object_path, object_id) self.assertTrue('boolean1' in cdist_object.parameters) self.assertFalse('boolean2' in cdist_object.parameters) # empty file -> True self.assertTrue(cdist_object.parameters['boolean1'] == '')
def test_argument_defaults(self): type_name = '__argument_defaults' object_id = 'some-id' value = 'value1' argv = [type_name, object_id] os.environ.update(self.env) emu = emulator.Emulator(argv) emu.run() cdist_type = core.CdistType(self.local.type_path, type_name) cdist_object = core.CdistObject(cdist_type, self.local.object_path, object_id) self.assertTrue('optional1' in cdist_object.parameters) self.assertFalse('optional2' in cdist_object.parameters) self.assertEqual(cdist_object.parameters['optional1'], value)
def test_order_dependency_context(self): test_seq = ( 'A', True, 'B', 'C', 'D', False, 'E', 'F', True, 'G', 'H', False, 'I', ) expected_requirements = { 'C': set(('__planet/B', )), 'D': set(('__planet/C', )), 'H': set(('__planet/G', )), } # Ensure env var is not in env if 'CDIST_ORDER_DEPENDENCY' in self.env: del self.env['CDIST_ORDER_DEPENDENCY'] for x in test_seq: if isinstance(x, str): # Clear because of order dep injection # In real world, this is not shared over instances if 'require' in self.env: del self.env['require'] argv = ['__planet', x] emu = emulator.Emulator(argv, env=self.env) emu.run() elif isinstance(x, bool): if x: self.env['CDIST_ORDER_DEPENDENCY'] = 'on' elif 'CDIST_ORDER_DEPENDENCY' in self.env: del self.env['CDIST_ORDER_DEPENDENCY'] cdist_type = core.CdistType(self.local.type_path, '__planet') for x in test_seq: if isinstance(x, str): obj = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, x) reqs = set(obj.requirements) if x in expected_requirements: self.assertEqual(reqs, expected_requirements[x]) else: self.assertTrue(len(reqs) == 0)
def setUp(self): # Change env for context self.orig_environ = os.environ os.environ = os.environ.copy() self.temp_dir = self.mkdtemp() self.local_dir = os.path.join(self.temp_dir, "local") self.hostdir = cdist.str_hash(self.target_host[0]) self.host_base_path = os.path.join(self.local_dir, self.hostdir) os.makedirs(self.host_base_path) self.local = cdist.exec.local.Local( target_host=self.target_host, target_host_tags=self.target_host_tags, base_root_path=self.host_base_path, host_dir_name=self.hostdir) # Setup test objects self.object_base_path = op.join(self.temp_dir, 'object') self.objects = [] for cdist_object_name in expected_object_names: cdist_type, cdist_object_id = cdist_object_name.split("/", 1) cdist_object = core.CdistObject( core.CdistType(type_base_path, cdist_type), self.object_base_path, self.local.object_marker_name, cdist_object_id) cdist_object.create() self.objects.append(cdist_object) self.object_index = dict((o.name, o) for o in self.objects) self.object_names = [o.name for o in self.objects] self.remote_dir = os.path.join(self.temp_dir, "remote") os.mkdir(self.remote_dir) self.remote = cdist.exec.remote.Remote( target_host=self.target_host, remote_copy=self.remote_copy, remote_exec=self.remote_exec, base_path=self.remote_dir, stdout_base_path=self.local.stdout_base_path, stderr_base_path=self.local.stderr_base_path) self.local.object_path = self.object_base_path self.local.type_path = type_base_path self.config = cdist.config.Config(self.local, self.remote)