Beispiel #1
0
    def test_parse_text_generator(self):
        """ Test the text generator parsing """
        config = dict()
        config['type'] = 'random_text'
        config['character_set'] = 'reallyINVALID'

        try:
            gen = generators.parse_generator(config)
            self.fail(
                "Should never parse an invalid character_set successfully, but did!"
            )
        except ValueError:
            pass

        # Test for character set handling
        for charset in generators.CHARACTER_SETS:
            try:
                config['character_set'] = charset
                gen = generators.parse_generator(config)
                myset = set(generators.CHARACTER_SETS[charset])
                for x in range(0, 50):
                    val = next(gen)
                    self.assertTrue(set(val).issubset(set(myset)))
            except Exception as e:
                print('Exception occurred with charset: ' + charset)
                raise e

        my_min = 1
        my_max = 10

        # Test for explicit character setting
        del config['character_set']
        temp_chars = 'ay78%&'
        config['characters'] = temp_chars
        gen = generators.parse_generator(config)
        self.generator_basic_test(
            gen,
            value_test_function=lambda x: set(x).issubset(set(temp_chars)))

        # Test for length setting
        config['length'] = '3'
        gen = generators.parse_generator(config)
        self.generator_basic_test(gen,
                                  value_test_function=lambda x: len(x) == 3)
        del config['length']

        # Test for explicit min/max length
        config['min_length'] = '9'
        config['max_length'] = 12
        gen = generators.parse_generator(config)
        self.generator_basic_test(
            gen, value_test_function=lambda x: len(x) >= 9 and len(x) <= 12)
 def test_parse_fixed_sequence(self):
     vals = ['moobie', 'moby', 'moo']
     config = {'type': 'fixed_sequence',
               'values': vals}
     gen = generators.parse_generator(config)
     self.generator_basic_test(gen, lambda x: x in vals)
     self.assertRaises(ValueError, generators.parse_generator, {'type': 'fixed_sequence', 'values': []})
     self.assertRaises(ValueError, generators.parse_generator, {'type': 'fixed_sequence', 'values': {'x': 1}})
Beispiel #3
0
    def test_parse_basic(self):
        """ Test basic parsing, simple cases that should succeed or throw known errors """
        config = {'type': 'unsupported'}

        try:
            gen = generators.parse_generator(config)
            self.fail(
                "Expected failure due to invalid generator type, did not emit it"
            )
        except ValueError:
            pass

        # Try creating a random_int generator
        config['type'] = 'random_int'
        gen = generators.parse_generator(config)
        self.generator_basic_test(
            gen, value_test_function=lambda x: isinstance(x, int))
        self.generator_repeat_test(gen)

        # Sample variable
        os.environ['SAMPLEVAR'] = 'goober'

        config['type'] = 'env_variable'
        config['variable_name'] = 'SAMPLEVAR'
        gen = generators.parse_generator(config)
        self.generator_basic_test(gen)
        del config['variable_name']

        config['type'] = 'env_string'
        config['string'] = '$SAMPLEVAR'
        gen = generators.parse_generator(config)
        self.generator_basic_test(gen)
        del config['string']

        config['type'] = 'number_sequence'
        config['start'] = '1'
        config['increment'] = '10'
        gen = generators.parse_generator(config)
        self.assertEqual(1, next(gen))
        self.assertEqual(11, next(gen))
        self.generator_basic_test(gen)
        del config['type']
Beispiel #4
0
    def parse(self, config_node):
        node = Parser.flatten_lowercase_keys_dict(config_node)

        for key, value in node.items():
            if key == 'timeout':
                self.timeout = int(value)
            elif key == u'print_bodies':
                self.print_bodies = Parser.safe_to_bool(value)
            elif key == 'retries':
                self.retries = int(value)
            elif key == 'variable_binds':
                self.variable_binds = value
            elif key == u'generators':
                if not isinstance(value, list):
                    raise TypeError(
                        "generators in config should defined as list(array).")
                flat = Parser.flatten_dictionaries(value)
                gen_dict = {}
                for generator_name, generator_config in flat.items():
                    gen = parse_generator(generator_config)
                    gen_dict[str(generator_name)] = gen
                self.generators = gen_dict
 def test_parse_choice_generatpr(self):
     vals = ['moobie', 'moby', 'moo']
     config = {'type': 'choice',
               'values': vals}
     gen = generators.parse_generator(config)
     self.generator_basic_test(gen, lambda x: x in vals)
Beispiel #6
0
 def test_parse_fixed_sequence(self):
     vals = ['moobie', 'moby', 'moo']
     config = {'type': 'fixed_sequence', 'values': vals}
     gen = generators.parse_generator(config)
     self.generator_basic_test(gen, lambda x: x in vals)