def parse_configuration(node, base_config=None): """ Parse input config to configuration information """ test_config = base_config if not test_config: test_config = TestConfig() node = lowercase_keys(flatten_dictionaries(node)) # Make it usable for key, value in node.items(): if key == u'timeout': test_config.timeout = int(value) elif key == u'print_bodies': test_config.print_bodies = safe_to_bool(value) elif key == u'retries': test_config.retries = int(value) elif key == u'variable_binds': if not test_config.variable_binds: test_config.variable_binds = dict() test_config.variable_binds.update(flatten_dictionaries(value)) elif key == u'generators': flat = flatten_dictionaries(value) gen_map = dict() for generator_name, generator_config in flat.items(): gen = parse_generator(generator_config) gen_map[str(generator_name)] = gen test_config.generators = gen_map return test_config
def parse(config): """ Create a validator that does an extract from body and applies a comparator, Then does comparison vs expected value Syntax sample: { jsonpath_mini: 'node.child', operator: 'eq', expected: 'myValue' } """ output = ComparatorValidator() config = parsing.lowercase_keys(parsing.flatten_dictionaries(config)) output.config = config # Extract functions are called by using defined extractor names output.extractor = _get_extractor(config) if output.extractor is None: raise ValueError( "Extract function for comparison is not valid or not found!") if 'comparator' not in config: # Equals comparator if unspecified output.comparator_name = 'eq' else: output.comparator_name = config['comparator'].lower() output.comparator = COMPARATORS[output.comparator_name] if not output.comparator: raise ValueError("Invalid comparator given!") try: expected = config['expected'] except KeyError: raise ValueError( "No expected value found in comparator validator config, one must be!" ) # Expected value can be another extractor query, or a single value, or # a templated value if isinstance(expected, basestring) or isinstance( expected, (int, long, float, complex)): output.expected = expected elif isinstance(expected, dict): expected = parsing.lowercase_keys(expected) template = expected.get('template') if template: # Templated string if not isinstance(template, basestring): raise ValueError( "Can't template a comparator-validator unless template value is a string" ) output.isTemplateExpected = True output.expected = template else: # Extractor to compare against output.expected = _get_extractor(expected) if not output.expected: raise ValueError( "Can't supply a non-template, non-extract dictionary to comparator-validator" ) return output
def parse(config): """ Create a validator that does an extract from body and applies a comparator, Then does comparison vs expected value Syntax sample: { jsonpath_mini: 'node.child', operator: 'eq', expected: 'myValue' } """ output = ComparatorValidator() config = parsing.lowercase_keys(parsing.flatten_dictionaries(config)) output.config = config # Extract functions are called by using defined extractor names output.extractor = _get_extractor(config) if output.extractor is None: raise ValueErrors( "Extract function for comparison is not valid or not found!") if 'comparator' not in config: # Equals comparator if unspecified output.comparator_name = 'eq' else: output.comparator_name = config['comparator'].lower() output.comparator = COMPARATORS[output.comparator_name] if not output.comparator: raise ValueError("Invalid comparator given!") try: expected = config['expected'] except KeyError: raise ValueError( "No expected value found in comparator validator config, one must be!") # Expected value can be another extractor query, or a single value, or # a templated value if isinstance(expected, basestring) or isinstance(expected, (int, long, float, complex)): output.expected = expected elif isinstance(expected, dict): expected = parsing.lowercase_keys(expected) template = expected.get('template') if template: # Templated string if not isinstance(template, basestring): raise ValueError( "Can't template a comparator-validator unless template value is a string") output.isTemplateExpected = True output.expected = template else: # Extractor to compare against output.expected = _get_extractor(expected) if not output.expected: raise ValueError( "Can't supply a non-template, non-extract dictionary to comparator-validator") return output
def test_flatten(self): """ Test flattening of lists of dictionaries to single dictionaries """ # Test happy path: list of single-item dictionaries in array = [{"url": "/cheese"}, {"method": "POST"}] expected = {"url": "/cheese", "method": "POST"} output = flatten_dictionaries(array) self.assertTrue(isinstance(output, dict)) # Test that expected output matches actual self.assertFalse(len(set(output.items()) ^ set(expected.items()))) # Test dictionary input array = {"url": "/cheese", "method": "POST"} expected = {"url": "/cheese", "method": "POST"} output = flatten_dictionaries(array) self.assertTrue(isinstance(output, dict)) # Test that expected output matches actual self.assertTrue(len(set(output.items()) ^ set(expected.items())) == 0) # Test empty list input array = [] expected = {} output = flatten_dictionaries(array) self.assertTrue(isinstance(output, dict)) # Test that expected output matches actual self.assertFalse(len(set(output.items()) ^ set(expected.items()))) # Test empty dictionary input array = {} expected = {} output = flatten_dictionaries(array) self.assertTrue(isinstance(output, dict)) # Test that expected output matches actual self.assertFalse(len(set(output.items()) ^ set(expected.items()))) # Test mixed-size input dictionaries array = [{"url": "/cheese"}, {"method": "POST", "foo": "bar"}] expected = {"url": "/cheese", "method": "POST", "foo": "bar"} output = flatten_dictionaries(array) self.assertTrue(isinstance(output, dict)) # Test that expected output matches actual self.assertFalse(len(set(output.items()) ^ set(expected.items())))
def parse(config): output = ExtractTestValidator() config = parsing.lowercase_keys(parsing.flatten_dictionaries(config)) output.config = config extractor = _get_extractor(config) output.extractor = extractor test_name = config['test'] output.test_name = test_name test_fn = VALIDATOR_TESTS[test_name] output.test_fn = test_fn return output
def parse_generator(configuration): """ Parses a configuration built from yaml and returns a generator Configuration should be a map """ configuration = lowercase_keys(flatten_dictionaries(configuration)) gen_type = str(configuration.get(u'type')).lower() if gen_type not in GENERATOR_TYPES: raise ValueError( 'Generator type given {0} is not valid '.format(gen_type)) # Do the easy parsing, delegate more complex logic to parsing functions if gen_type == u'env_variable': return factory_env_variable(configuration[u'variable_name'])() elif gen_type == u'env_string': return factory_env_string(configuration[u'string'])() elif gen_type == u'number_sequence': start = configuration.get('start') increment = configuration.get('increment') if not start: start = 1 else: start = int(start) if not increment: increment = 1 else: increment = int(increment) return factory_generate_ids(start, increment)() elif gen_type == u'random_int': return generator_random_int32() elif gen_type == u'random_text': return parse_random_text_generator(configuration) elif gen_type in GENERATOR_TYPES: return GENERATOR_PARSING[gen_type](configuration) else: raise Exception("Unknown generator type: {0}".format('gen_type'))