def test_deferred(self): """Test deferred variables.""" data = { 'var1': 'val1', 'var3': {'subvar1': 'subval1', 'subvar2': 'subval2'}, } sys_data = { 'var1': variables.DeferredVariable(), } slurm_data = { 'num_nodes': '45' } var_man = variables.VariableSetManager() var_man.add_var_set('var', data) var_man.add_var_set('sys', sys_data) var_man.add_var_set('sched', slurm_data) with self.assertRaises(ValueError): var_man.len('sys', 'var1') for key in ( 'sys.var1', 'sys.var1.3', 'sys.var1.1.subvar1', 'sys.var1.noexist'): with self.assertRaises(KeyError): _ = var_man[key]
def get(self, defer): """Get the value for this system variable. :params bool defer: If the variable is deferable, return a DeferredVariable object instead. """ if defer and self.is_deferable: return variables.DeferredVariable() try: values = self._get() except Exception as err: raise SystemPluginError( "Error getting value for system plugin {s.name}: {err}".format( s=self, err=err)) chk_vals = values if not isinstance(chk_vals, list): chk_vals = [chk_vals] for i in range(len(chk_vals)): if not isinstance(chk_vals[i], dict): chk_vals[i] = {None: chk_vals[i]} for vals in chk_vals: for key, val in vals.items(): if not isinstance(val, str): raise SystemPluginError( "System variable plugin {s.path} called {s.name} " "returned non-string value '{val}' in '{values}'". format(s=self, val=val, values=values)) return values
def test_deferred_errors(self): """Using deferred variables in inappropriate places should raise errors.""" test = { 'permute_on': ['sys.def'], 'variables': {}, } var_man = variables.VariableSetManager() var_man.add_var_set('sys', {'def': variables.DeferredVariable()}) with self.assertRaises(TestConfigError): self.resolver.resolve_permutations(test, var_man) test = {'permute_on': ['foo.1'], 'variables': {'foo': 'bleh'}} with self.assertRaises(TestConfigError): self.resolver.resolve_permutations(test, var_man) test = { 'permute_on': ['no_exist'], 'variables': {}, } with self.assertRaises(TestConfigError): self.resolver.resolve_permutations(test, var_man)
def test_deferred(self): """Test deferred variables.""" data = { 'var1': 'val1', 'var3': {'subvar1': 'subval1', 'subvar2': 'subval2'}, } sys_data = { 'var1': variables.DeferredVariable('var1'), 'var3': variables.DeferredVariable('var3', sub_keys=['subvar1', 'subvar2']), } with self.assertRaises(ValueError): variables.DeferredVariable('test', var_set='var') slurm_data = { 'num_nodes': '45' } vsetm = variables.VariableSetManager() vsetm.add_var_set('var', data) vsetm.add_var_set('sys', sys_data) vsetm.add_var_set('sched', slurm_data) self.assertEqual(vsetm.len('sys', 'var1'), 1) self.assertEqual(vsetm['sys.var1'], '[\x1esys.var1\x1e]') self.assertEqual(vsetm['sys.var3.subvar1'], '[\x1esys.var3.subvar1\x1e]') for key in ( 'sys.var1.3', 'sys.var1.1.subvar1', 'sys.var3.noexist', 'sys.var1.noexist', 'sys.var3'): with self.assertRaises(KeyError): _ = vsetm[key]
def get(self, defer): if defer and self.is_deferable: return variables.DeferredVariable(self.name, var_set='sys', sub_keys=self.sub_keys) if self.values is None: try: self.values = self._get() except Exception as err: raise SystemPluginError( "Error getting value for system plugin {s.name}: {err}". format(s=self, err=err)) return self.values
def get(self, defer): if defer and self.is_deferable: return variables.DeferredVariable(self.name, var_set='sys', sub_keys=self.sub_keys) elif defer and not self.is_deferable: raise SystemPluginError( "Deferred variable '{}' was requested but is not deferrable." .format(self.name) ) elif self.values is None: self.values = {} if len(self.sub_keys) == 0: self.sub_keys = [None] for key in self.sub_keys: self.values[key] = None self._get() if list(self.values.keys()) == [None]: self.values = self.values[None] return self.values
def test_deferred_errors(self): test = { 'permute_on': ['sys.def'], 'variables': {}, } with self.assertRaises(TestConfigError): resolve_permutations(test, {}, {'def': variables.DeferredVariable()}) test = {'permute_on': ['foo.1'], 'variables': {'foo': 'bleh'}} with self.assertRaises(TestConfigError): resolve_permutations(test, {}, {}) test = { 'permute_on': ['no_exist'], 'variables': {}, } with self.assertRaises(TestConfigError): resolve_permutations(test, {}, {})
def test_resolve_all_vars(self): # Most of the variable resolution stuff is tested elsewhere, # but it's good to have it all put together in one final test. test = { 'build': { 'cmds': [ "echo {{foo}} {{bar.p}}", "echo {{var.foo}}", "echo {{bar.q}}" ], 'env': [{ 'baz': '{{baz}}' }, { 'oof': '{{var.blarg.l}}-{{var.blarg.r}}' }, { 'pav': '{{pav.nope}}' }, { 'sys': '{{nope}}' }] }, 'variables': { 'baz': ['6'], 'blarg': [{ 'l': '7', 'r': '8' }], 'foo': ['1', '2'], 'bar': [ { 'p': '4', 'q': '4a' }, ], }, 'permute_on': ['foo', 'bar'] } answer1 = { 'build': { 'cmds': ["echo 1 4", "echo 1", "echo 4a"], 'env': [{ 'baz': '6' }, { 'oof': '7-8' }, { 'pav': '9' }, { 'sys': '10' }] } } # This is all that changes between the two. import copy answer2 = copy.deepcopy(answer1) answer2['build']['cmds'] = ["echo 2 4", "echo 2", "echo 4a"] answers = [answer1, answer2] test, permuted = resolve_permutations( test, # Pav vars {'nope': '9'}, # sys vars {'nope': '10'}) self.assertEqual(len(permuted), 2) # Make sure each of our permuted results is in the list of answers. for var_man in permuted: out_test = resolve_config(test, var_man, ['build']) self.assertIn(out_test, answers) # Make sure we can successfully disallow deferred variables in a # section. test = { 'build': { 'cmds': ['echo {{foo}}'] }, 'permute_on': [], 'variables': {} } dvar = variables.DeferredVariable() test, permuted = resolve_permutations(test, {}, {'foo': dvar}) with self.assertRaises(string_parser.ResolveError): # No deferred variables in the build section. resolve_config(test, permuted[0], ['build'])
def test_resolve_all_vars(self): """Most of the variable resolution stuff is tested elsewhere, but it's good to have it all put together in one final test.""" test = { 'build': { 'cmds': [ "echo {{foo}} {{bar.p}}", "echo {{var.foo}}", "echo {{bar.q}}" ], 'env': [{ 'baz': '{{baz}}' }, { 'oof': '{{var.blarg.l}}-{{var.blarg.r}}' }, { 'pav': '{{pav.nope}}' }, { 'sys': '{{nope}}' }] }, 'variables': { 'baz': ['6'], 'blarg': [{ 'l': '7', 'r': '8' }], 'foo': ['1', '2'], 'bar': [ { 'p': '4', 'q': '4a' }, ], }, 'permute_on': ['foo', 'bar'], 'subtitle': None, } answer1 = { 'permute_on': ['foo', 'bar'], 'subtitle': '1-_bar_', 'build': { 'cmds': ["echo 1 4", "echo 1", "echo 4a"], 'env': [{ 'baz': '6' }, { 'oof': '7-8' }, { 'pav': '9' }, { 'sys': '10' }] } } # This is all that changes between the two. answer2 = copy.deepcopy(answer1) answer2['build']['cmds'] = ["echo 2 4", "echo 2", "echo 4a"] answer2['subtitle'] = '2-_bar_' answers = [answer1, answer2] var_man = variables.VariableSetManager() var_man.add_var_set('pav', {'nope': '9'}) var_man.add_var_set('sys', {'nope': '10'}) var_man.add_var_set('var', test['variables']) del test['variables'] test, permuted = self.resolver.resolve_permutations(test, var_man) self.assertEqual(len(permuted), 2) # Make sure each of our permuted results is in the list of answers. for var_man in permuted: out_test = self.resolver.resolve_test_vars(test, var_man) self.assertIn(out_test, answers) # Make sure we can successfully disallow deferred variables in a # section. test = { 'build': { 'cmds': ['echo {{foo}}'] }, 'permute_on': [], 'variables': {} } var_man = variables.VariableSetManager() var_man.add_var_set('sys', {'foo': variables.DeferredVariable()}) var_man.add_var_set('var', {}) test, permuted = self.resolver.resolve_permutations(test, var_man) with self.assertRaises(resolver.TestConfigError): # No deferred variables in the build section. self.resolver.resolve_test_vars(test, permuted[0])