def test_builtin_env(self): test_yaml = { 'key1': { 'func': 'claw.patcher:env', 'args': ['KEY1'] }, 'key2': { 'func': 'claw.patcher:env', 'args': ['KEY2', 'default'] }, 'key3': { 'func': 'claw.patcher:env', 'args': ['KEY3', 'default'] } } self.write_yaml(test_yaml) with mock.patch.dict(os.environ, {'KEY1': 'VALUE1', 'KEY2': 'VALUE2'}): with patcher.YamlPatcher(self.yaml_path) as patch: for key in ['key1', 'key2', 'key3']: func = patch.obj[key] patch.set_value(key, func) with self.assertRaises(KeyError) as c: with patcher.YamlPatcher(self.yaml_path) as patch: patch.set_value('key4', { 'func': 'claw.patcher:env', 'args': ['I_DO_NOT_EXIST'] }) self.assertIn('I_DO_NOT_EXIST', str(c.exception)) self.assertEqual(self.read_yaml(), { 'key1': 'VALUE1', 'key2': 'VALUE2', 'key3': 'default' })
def setUp(self): super(TestCompletion, self).setUp() self.scripts_dir = self.workdir / 'scripts' self.scripts_dir2 = self.workdir / 'scripts2' self.scripts = ['example-script'] for scripts_dir in [self.scripts_dir, self.scripts_dir2]: scripts_dir.mkdir_p() for _ in range(3): (scripts_dir / 'script{0}.py'.format(uuid.uuid4())).touch() (scripts_dir / 'script{0}.pyy'.format(uuid.uuid4())).touch() self.scripts += [ f.basename()[:-3] if f.basename().endswith('.py') else f.basename() for f in scripts_dir.files()] self.init() with patcher.YamlPatcher(self.settings.settings_path) as patch: patch.obj['scripts'] = [str(self.scripts_dir), str(self.scripts_dir2)] with patcher.YamlPatcher(self.settings.user_suites_yaml) as patch: obj = patch.obj stub = obj['handler_configurations'][tests.STUB_CONFIGURATION] configs = {'conf{0}'.format(i): stub for i in range(3)} obj['handler_configurations'] = configs self.configurations = configs.keys() self.inputs_templates = obj['inputs_override_templates'].keys() self.manager_blueprint_templates = obj[ 'manager_blueprint_override_templates'].keys() with patcher.YamlPatcher(self.settings.blueprints_yaml) as patch: obj = patch.obj stub = obj['blueprints'][tests.STUB_BLUEPRINT] blueprints = {'blue{0}'.format(i): stub for i in range(3)} obj['blueprints'] = blueprints self.blueprints = blueprints.keys() self.help_args = ['-h', '--help'] self.existing_configurations = None
def test_builtin_filter_list_dict_list(self): test_yaml = { 'a': [{ 'key1': 'string value11' }, { 'key1': 'string value12', 'key2': 'string value22' }, { 'key1': 'string value13', 'key2': 'string value23' }] } self.write_yaml(test_yaml) with patcher.YamlPatcher(self.yaml_path) as patch: patch.set_value( 'a', { 'func': 'claw.patcher:filter_list', 'kwargs': { 'include': [{ 'key1': 'value12', 'key2': 'value22' }, { 'key1': 'value11' }] } }) self.assertEqual(self.read_yaml()['a'], [{ 'key1': 'string value11' }, { 'key1': 'string value12', 'key2': 'string value22' }])
def test_set_value_no_current_value(self): self.write_yaml({}) with patcher.YamlPatcher(self.yaml_path) as patch: patch.set_value( 'b.c', {'func': '{0}:func_no_current_value'.format(__name__)}) self.assertEqual(self.read_yaml()['b']['c'], func_no_current_value(None))
def _generate_configuration(cmd_inputs_override, cmd_blueprint_override, conf_obj, conf_key, conf_name, conf_additional, conf_blueprint_key, blueprint_dir_name, blueprint_override_key, blueprint_override_template_key, blueprint_path, reset, properties, user_yaml): if conf_name not in user_yaml[conf_key]: raise NO_SUCH_CONFIGURATION conf = user_yaml[conf_key][conf_name] cmd_inputs_override = [user_yaml['inputs_override_templates'][key] for key in (cmd_inputs_override or [])] cmd_blueprint_override = [user_yaml[blueprint_override_template_key][key] for key in (cmd_blueprint_override or [])] original_inputs_path = os.path.expanduser(conf.get('inputs', '')) original_blueprint_path = os.path.expanduser(conf[conf_blueprint_key]) if conf_obj.dir.exists(): if reset: if conf_obj.dir == os.getcwd(): os.chdir(conf_obj.dir.dirname()) shutil.rmtree(conf_obj.dir) else: raise ALREADY_INITIALIZED conf_obj.dir.makedirs() if not original_inputs_path: fd, original_inputs_path = tempfile.mkstemp() os.close(fd) with open(original_inputs_path, 'w') as f: f.write('{}') _, tmp_blueprint_path = util.generate_unique_configurations( workdir=conf_obj.dir, original_inputs_path=original_inputs_path, original_manager_blueprint_path=original_blueprint_path, manager_blueprint_dir_name=blueprint_dir_name) shutil.move(tmp_blueprint_path, blueprint_path) conf['inputs'] = str(conf_obj.inputs_path) conf[conf_blueprint_key] = str(blueprint_path) conf.update(conf_additional or {}) user_yaml['variables'] = user_yaml.get('variables', {}) user_yaml['variables']['properties'] = properties or {} overrides = [ (conf_obj.inputs_path, 'inputs_override', cmd_inputs_override), (blueprint_path, blueprint_override_key, cmd_blueprint_override) ] for yaml_path, prop, additional_overrides in overrides: unprocessed = conf.pop(prop, {}) for additional in additional_overrides: unprocessed.update(additional) override = util.process_variables(user_yaml, unprocessed) with patcher.YamlPatcher(yaml_path, default_flow_style=False) as patch: for k, v in override.items(): patch.set_value(k, v) return conf
def test_set_value_current_value(self): current_value = 'A' self.write_yaml({'a': current_value}) with patcher.YamlPatcher(self.yaml_path) as patch: patch.set_value( 'a', {'func': '{0}:func_current_value'.format(__name__)}) self.assertEqual(self.read_yaml()['a'], func_current_value(current_value))
def setUp(self): super(CleanupTest, self).setUp() with patcher.YamlPatcher(self.settings.user_suites_yaml) as patch: obj = patch.obj conf = obj['handler_configurations'][tests.STUB_CONFIGURATION] conf['handler'] = 'stub_handler' self.configuration = configuration.Configuration( tests.STUB_CONFIGURATION)
def test_builtin_filter_dict(self): test_yaml = {'a': {'a1': 1, 'a2': 2, 'a3': 3}} self.write_yaml(test_yaml) with patcher.YamlPatcher(self.yaml_path) as patch: patch.set_value('a', { 'func': 'claw.patcher:filter_dict', 'kwargs': { 'exclude': ['a3'] } }) self.assertEqual(self.read_yaml()['a'], {'a1': 1, 'a2': 2})
def test_builtin_filter_list_string_list(self): test_yaml = {'a': ['string one', 'string two', 'string three']} self.write_yaml(test_yaml) with patcher.YamlPatcher(self.yaml_path) as patch: patch.set_value( 'a', { 'func': 'claw.patcher:filter_list', 'kwargs': { 'include': ['one', 'two'] } }) self.assertEqual(self.read_yaml()['a'], ['string one', 'string two'])
def test_set_value_args_and_kwargs(self): current_value = 'A' args = [1, 2, 3] kwargs = {'one': 'ONE', 'two': 'TWO'} self.write_yaml({'a': 'A'}) with patcher.YamlPatcher(self.yaml_path) as patch: patch.set_value( 'a', { 'func': '{0}:func_args_and_kwargs'.format(__name__), 'args': args, 'kwargs': kwargs }) self.assertEqual(self.read_yaml()['a'], [current_value, args, kwargs])
def test_script_in_script_dirs(self): scripts_dir1 = self.settings.default_scripts_dir scripts_dir2 = self.workdir / 'scripts2' scripts_dir2.mkdir() with patcher.YamlPatcher(self.settings.settings_path) as patch: patch.obj['scripts'] += [str(scripts_dir2)] for scripts_dir in [scripts_dir1, scripts_dir2]: gen_id = uuid.uuid4() value = 'VALUE-{0}'.format(gen_id) script = "def script(): print '{}'".format(value) script_name = 'my-script-{0}.py'.format(gen_id) partial_script_name = script_name[:-3] for script_arg in [script_name, partial_script_name]: self._test(script=script, expected_output=value, script_dir=scripts_dir, script_name=script_name, script_arg=script_arg)
def test_derived_set_value(self): test_yaml = {'a': 'A'} self.write_yaml(test_yaml) with patcher.YamlPatcher(self.yaml_path) as patch: patch.set_value('b', 'B') self.assertEqual(self.read_yaml(), {'a': 'A', 'b': 'B'})
def __getattr__(self, item): path = getattr(self.obj, '{0}_path'.format(item)) with patcher.YamlPatcher(path, default_flow_style=False) as patch: yield patch