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_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_object(self): # Setup object_id - FIXME: unset / do not setup anymore! if self.cdist_type.is_singleton: self.object_id = "singleton" else: self.object_id = self.args.object_id[0] del self.args.object_id # Instantiate the cdist object we are defining self.cdist_object = core.CdistObject(self.cdist_type, self.object_base_path, self.object_id) # Create object with given parameters self.parameters = {} for key, value in vars(self.args).items(): if value is not None: self.parameters[key] = value if self.cdist_object.exists: if self.cdist_object.parameters != self.parameters: raise cdist.Error( "Object %s already exists with conflicting parameters:\n%s: %s\n%s: %s" % (self.cdist_object.name, " ".join( self.cdist_object.source), self.cdist_object.parameters, self.object_source, self.parameters)) else: self.cdist_object.create() self.cdist_object.parameters = self.parameters # Record / Append source self.cdist_object.source.append(self.object_source)
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.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_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 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 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 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_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_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_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.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 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.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)
def setup_object(self): # Setup object - and ensure it is not in args if self.cdist_type.is_singleton: self.object_id = '' else: self.object_id = self.args.object_id[0] del self.args.object_id # Instantiate the cdist object we are defining self.cdist_object = core.CdistObject(self.cdist_type, self.object_base_path, self.object_marker, self.object_id) # Create object with given parameters self.parameters = {} for key, value in vars(self.args).items(): if value is not None: self.parameters[key] = value if self.cdist_object.exists and 'CDIST_OVERRIDE' not in self.env: # Make existing requirements a set so that we can compare it # later with new requirements. self._existing_reqs = set(self.cdist_object.requirements) if self.cdist_object.parameters != self.parameters: errmsg = ("Object %s already exists with conflicting " "parameters:\n%s: %s\n%s: %s" % (self.cdist_object.name, " ".join( self.cdist_object.source), self.cdist_object.parameters, self.object_source, self.parameters)) self.log.error(errmsg) raise cdist.Error(errmsg) else: if self.cdist_object.exists: self.log.debug(('Object %s override forced with ' 'CDIST_OVERRIDE'), self.cdist_object.name) self.cdist_object.create(True) else: self.cdist_object.create() self.cdist_object.parameters = self.parameters # record the created object in typeorder file with open(self.typeorder_path, 'a') as typeorderfile: print(self.cdist_object.name, file=typeorderfile) # Record / Append source self.cdist_object.source.append(self.object_source)
def test_required_arguments(self): """check whether assigning required parameter works""" type_name = '__arguments_required' object_id = 'some-id' value = 'some value' argv = [type_name, object_id, '--required1', value, '--required2', value] # print(self.env) 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('required1' in cdist_object.parameters) self.assertTrue('required2' in cdist_object.parameters) self.assertEqual(cdist_object.parameters['required1'], value) self.assertEqual(cdist_object.parameters['required2'], value)
def init_object(self): # Initialize object - and ensure it is not in args if self.cdist_type.is_singleton: self.object_id = '' else: self.object_id = self.args.object_id[0] del self.args.object_id # Instantiate the cdist object we are defining self.cdist_object = core.CdistObject( self.cdist_type, self.object_base_path, self.object_marker, self.object_id) lockfname = ('.' + self.cdist_type.name + self.object_id + '_' + self.object_marker + '.lock') lockfname = lockfname.replace(os.sep, '_') self.flock_path = os.path.join(self.object_base_path, lockfname)
def test_optional_multiple(self): type_name = '__arguments_optional_multiple' object_id = 'some-id' value1 = 'value1' value2 = 'value2' argv = [type_name, object_id, '--optional1', value1, '--optional1', value2] 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, self.local.object_marker_name, object_id) self.assertTrue('optional1' in cdist_object.parameters) self.assertTrue(value1 in cdist_object.parameters['optional1']) self.assertTrue(value2 in cdist_object.parameters['optional1'])
def test_required_multiple_arguments(self): """check whether assigning required multiple parameter works""" type_name = '__arguments_required_multiple' object_id = 'some-id' value1 = 'value1' value2 = 'value2' argv = [type_name, object_id, '--required1', value1, '--required1', value2] 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, self.local.object_marker_name, object_id) self.assertTrue('required1' in cdist_object.parameters) self.assertTrue(value1 in cdist_object.parameters['required1']) self.assertTrue(value2 in cdist_object.parameters['required1'])
def test_file_from_stdin(self): """ Test whether reading from stdin works """ ###################################################################### # Create string with random content random_string = str(random.sample(range(1000), 800)) random_buffer = io.BytesIO(bytes(random_string, 'utf-8')) ###################################################################### # Prepare required args and environment for emulator type_name = '__file' object_id = "cdist-test-id" argv = [type_name, object_id] env = os.environ.copy() env['__cdist_manifest'] = "/cdist-test/path/that/does/not/exist" env['__cdist_object_marker'] = self.local.object_marker_name env['__cdist_type_base_path'] = self.local.type_path env['__global'] = self.local.base_path ###################################################################### # Create path where stdin should reside at cdist_type = core.CdistType(self.local.type_path, type_name) cdist_object = core.CdistObject(cdist_type, self.local.object_path, self.local.object_marker_name, object_id) stdin_out_path = os.path.join(cdist_object.absolute_path, 'stdin') ###################################################################### # Run emulator emu = emulator.Emulator(argv, stdin=random_buffer, env=env) emu.run() ###################################################################### # Read where emulator should have placed stdin with open(stdin_out_path, 'r') as fd: stdin_saved_by_emulator = fd.read() self.assertEqual(random_string, stdin_saved_by_emulator)