def tearDown(self): super(TestInput, self).tearDown() for entity in six.itervalues(self._test_entities): try: self.service.inputs.delete(kind=entity.kind, name=entity.name) except KeyError: pass
def test_update(self): inputs = self.service.inputs for entity in six.itervalues(self._test_entities): kind, name = entity.kind, entity.name kwargs = {'host': 'foo'} entity.update(**kwargs) entity.refresh() self.assertEqual(entity.host, kwargs['host'])
def test_update(self): inputs = self.service.inputs for entity in six.itervalues(self._test_entities): kind, name = entity.kind, entity.name kwargs = {'host': 'foo'} entity.update(**kwargs) entity.refresh() self.assertEqual(entity.host, kwargs['host'])
def test_read(self): inputs = self.service.inputs for this_entity in six.itervalues(self._test_entities): kind, name = this_entity.kind, this_entity.name read_entity = inputs[name, kind] self.assertEqual(this_entity.kind, read_entity.kind) self.assertEqual(this_entity.name, read_entity.name) self.assertEqual(this_entity.host, read_entity.host)
def test_read(self): inputs = self.service.inputs for this_entity in six.itervalues(self._test_entities): kind, name = this_entity.kind, this_entity.name read_entity = inputs[name, kind] self.assertEqual(this_entity.kind, read_entity.kind) self.assertEqual(this_entity.name, read_entity.name) self.assertEqual(this_entity.host, read_entity.host)
def tearDown(self): super(TestInput, self).tearDown() for entity in six.itervalues(self._test_entities): try: self.service.inputs.delete( kind=entity.kind, name=entity.name) except KeyError: pass
def test_delete(self): inputs = self.service.inputs remaining = len(self._test_entities) - 1 for input_entity in six.itervalues(self._test_entities): name = input_entity.name kind = input_entity.kind self.assertTrue(name in inputs) self.assertTrue((name, kind) in inputs) if remaining == 0: inputs.delete(name) self.assertFalse(name in inputs) else: if not name.startswith('boris'): self.assertRaises(client.AmbiguousReferenceException, inputs.delete, name) self.service.inputs.delete(name, kind) self.assertFalse((name, kind) in inputs) self.assertRaises(client.HTTPError, input_entity.refresh) remaining -= 1
def test_delete(self): inputs = self.service.inputs remaining = len(self._test_entities)-1 for input_entity in six.itervalues(self._test_entities): name = input_entity.name kind = input_entity.kind self.assertTrue(name in inputs) self.assertTrue((name,kind) in inputs) if remaining == 0: inputs.delete(name) self.assertFalse(name in inputs) else: if not name.startswith('boris'): self.assertRaises(client.AmbiguousReferenceException, inputs.delete, name) self.service.inputs.delete(name, kind) self.assertFalse((name, kind) in inputs) self.assertRaises(client.HTTPError, input_entity.refresh) remaining -= 1
def test_command_line_parser(self): @Configuration() class TestCommandLineParserCommand(SearchCommand): required_option = Option(validate=Boolean(), require=True) unnecessary_option = Option(validate=Boolean(), default=True, require=False) class ConfigurationSettings(SearchCommand.ConfigurationSettings): @classmethod def fix_up(cls, command_class): pass # Command line without fieldnames options = ['required_option=true', 'unnecessary_option=false'] command = TestCommandLineParserCommand() CommandLineParser.parse(command, options) for option in six.itervalues(command.options): if option.name in ['logging_configuration', 'logging_level', 'record', 'show_configuration']: self.assertFalse(option.is_set) continue self.assertTrue(option.is_set) expected = 'testcommandlineparser required_option="t" unnecessary_option="f"' self.assertEqual(expected, str(command)) self.assertEqual(command.fieldnames, []) # Command line with fieldnames fieldnames = ['field_1', 'field_2', 'field_3'] command = TestCommandLineParserCommand() CommandLineParser.parse(command, options + fieldnames) for option in six.itervalues(command.options): if option.name in ['logging_configuration', 'logging_level', 'record', 'show_configuration']: self.assertFalse(option.is_set) continue self.assertTrue(option.is_set) expected = 'testcommandlineparser required_option="t" unnecessary_option="f" field_1 field_2 field_3' self.assertEqual(expected, str(command)) self.assertEquals(command.fieldnames, fieldnames) # Command line without any unnecessary options command = TestCommandLineParserCommand() CommandLineParser.parse(command, ['required_option=true'] + fieldnames) for option in six.itervalues(command.options): if option.name in ['unnecessary_option', 'logging_configuration', 'logging_level', 'record', 'show_configuration']: self.assertFalse(option.is_set) continue self.assertTrue(option.is_set) expected = 'testcommandlineparser required_option="t" field_1 field_2 field_3' self.assertEqual(expected, str(command)) self.assertEquals(command.fieldnames, fieldnames) # Command line with missing required options, with or without fieldnames or unnecessary options options = ['unnecessary_option=true'] self.assertRaises(ValueError, CommandLineParser.parse, command, options + fieldnames) self.assertRaises(ValueError, CommandLineParser.parse, command, options) self.assertRaises(ValueError, CommandLineParser.parse, command, []) # Command line with unrecognized options self.assertRaises(ValueError, CommandLineParser.parse, command, ['unrecognized_option_1=foo', 'unrecognized_option_2=bar']) # Command line with a variety of quoted/escaped text options @Configuration() class TestCommandLineParserCommand(SearchCommand): text = Option() class ConfigurationSettings(SearchCommand.ConfigurationSettings): @classmethod def fix_up(cls, command_class): pass strings = [ r'"foo bar"', r'"foo/bar"', r'"foo\\bar"', r'"""foo bar"""', r'"\"foo bar\""', r'Hello\ World!', r'\"Hello\ World!\"'] expected_values = [ r'foo bar', r'foo/bar', r'foo\bar', r'"foo bar"', r'"foo bar"', r'Hello World!', r'"Hello World!"' ] for string, expected_value in izip(strings, expected_values): command = TestCommandLineParserCommand() argv = ['text', '=', string] CommandLineParser.parse(command, argv) self.assertEqual(command.text, expected_value) for string, expected_value in izip(strings, expected_values): command = TestCommandLineParserCommand() argv = [string] CommandLineParser.parse(command, argv) self.assertEqual(command.fieldnames[0], expected_value) for string, expected_value in izip(strings, expected_values): command = TestCommandLineParserCommand() argv = ['text', '=', string] + strings CommandLineParser.parse(command, argv) self.assertEqual(command.text, expected_value) self.assertEqual(command.fieldnames, expected_values) strings = [ 'some\\ string\\', r'some\ string"', r'"some string', r'some"string' ] for string in strings: command = TestCommandLineParserCommand() argv = [string] self.assertRaises(SyntaxError, CommandLineParser.parse, command, argv) return
def test_option(self): rebase_environment('app_with_logging_configuration') presets = [ 'logging_configuration=' + json_encode_string(environment.logging_configuration), 'logging_level="WARNING"', 'record="f"', 'show_configuration="f"'] command = TestSearchCommand() options = command.options itervalues = lambda: six.itervalues(options) options.reset() missing = options.get_missing() self.assertListEqual(missing, [option.name for option in itervalues() if option.is_required]) self.assertListEqual(presets, [six.text_type(option) for option in itervalues() if option.value is not None]) self.assertListEqual(presets, [six.text_type(option) for option in itervalues() if six.text_type(option) != option.name + '=None']) test_option_values = { validators.Boolean: ('0', 'non-boolean value'), validators.Code: ('foo == "bar"', 'bad code'), validators.Duration: ('24:59:59', 'non-duration value'), validators.Fieldname: ('some.field_name', 'non-fieldname value'), validators.File: (__file__, 'non-existent file'), validators.Integer: ('100', 'non-integer value'), validators.List: ('a,b,c', '"non-list value'), validators.Map: ('foo', 'non-existent map entry'), validators.Match: ('123-45-6789', 'not a social security number'), validators.OptionName: ('some_option_name', 'non-option name value'), validators.RegularExpression: ('\\s+', '(poorly formed regular expression'), validators.Set: ('bar', 'non-existent set entry')} for option in itervalues(): validator = option.validator if validator is None: self.assertIn(option.name, ['logging_configuration', 'logging_level']) continue legal_value, illegal_value = test_option_values[type(validator)] option.value = legal_value self.assertEqual( validator.format(option.value), validator.format(validator.__call__(legal_value)), "{}={}".format(option.name, legal_value)) try: option.value = illegal_value except ValueError: pass except BaseException as error: self.assertFalse('Expected ValueError for {}={}, not this {}: {}'.format( option.name, illegal_value, type(error).__name__, error)) else: self.assertFalse('Expected ValueError for {}={}, not a pass.'.format(option.name, illegal_value)) expected = { u'foo': False, 'boolean': False, 'code': u'foo == \"bar\"', 'duration': 89999, 'fieldname': u'some.field_name', 'file': six.text_type(repr(__file__)), 'integer': 100, 'logging_configuration': environment.logging_configuration, 'logging_level': u'WARNING', 'map': 'foo', 'match': u'123-45-6789', 'optionname': u'some_option_name', 'record': False, 'regularexpression': u'\\s+', 'required_boolean': False, 'required_code': u'foo == \"bar\"', 'required_duration': 89999, 'required_fieldname': u'some.field_name', 'required_file': six.text_type(repr(__file__)), 'required_integer': 100, 'required_map': 'foo', 'required_match': u'123-45-6789', 'required_optionname': u'some_option_name', 'required_regularexpression': u'\\s+', 'required_set': u'bar', 'set': u'bar', 'show_configuration': False, } self.maxDiff = None tuplewrap = lambda x: x if isinstance(x, tuple) else (x,) invert = lambda x: {v: k for k, v in six.iteritems(x)} for x in six.itervalues(command.options): # isinstance doesn't work for some reason if type(x.value).__name__ == 'Code': self.assertEqual(expected[x.name], x.value.source) elif type(x.validator).__name__ == 'Map': self.assertEqual(expected[x.name], invert(x.validator.membership)[x.value]) elif type(x.validator).__name__ == 'RegularExpression': self.assertEqual(expected[x.name], x.value.pattern) elif isinstance(x.value, TextIOWrapper): self.assertEqual(expected[x.name], "'%s'" % x.value.name) elif not isinstance(x.value, (bool,) + (six.text_type,) + (six.binary_type,) + tuplewrap(six.integer_types)): self.assertEqual(expected[x.name], repr(x.value)) else: self.assertEqual(expected[x.name], x.value) expected = ( 'foo="f" boolean="f" code="foo == \\"bar\\"" duration="24:59:59" fieldname="some.field_name" ' 'file=' + json_encode_string(__file__) + ' integer="100" map="foo" match="123-45-6789" ' 'optionname="some_option_name" record="f" regularexpression="\\\\s+" required_boolean="f" ' 'required_code="foo == \\"bar\\"" required_duration="24:59:59" required_fieldname="some.field_name" ' 'required_file=' + json_encode_string(__file__) + ' required_integer="100" required_map="foo" ' 'required_match="123-45-6789" required_optionname="some_option_name" required_regularexpression="\\\\s+" ' 'required_set="bar" set="bar" show_configuration="f"') observed = six.text_type(command.options) self.assertEqual(observed, expected) return
def reset(self): for value in six.itervalues(self): value.reset()
def get_missing(self): missing = [item.name for item in six.itervalues(self) if item.is_required and not item.is_set] return missing if len(missing) > 0 else None
def __str__(self): text = ' '.join([str(item) for item in six.itervalues(self) if item.is_set]) return text
def __repr__(self): text = 'Option.View([' + ','.join(imap(lambda item: repr(item), six.itervalues(self))) + '])' return text
def test_create(self): inputs = self.service.inputs for entity in six.itervalues(self._test_entities): self.check_entity(entity) self.assertTrue(isinstance(entity, client.Input))
def __repr__(self): text = 'Option.View([' + ','.join(imap(lambda item: repr(item), six.itervalues(self))) + '])' return text
def test_create(self): inputs = self.service.inputs for entity in six.itervalues(self._test_entities): self.check_entity(entity) self.assertTrue(isinstance(entity, client.Input))
def test_command_line_parser(self): @Configuration() class TestCommandLineParserCommand(SearchCommand): required_option = Option(validate=Boolean(), require=True) unnecessary_option = Option(validate=Boolean(), default=True, require=False) class ConfigurationSettings(SearchCommand.ConfigurationSettings): @classmethod def fix_up(cls, command_class): pass # Command line without fieldnames options = ['required_option=true', 'unnecessary_option=false'] command = TestCommandLineParserCommand() CommandLineParser.parse(command, options) for option in six.itervalues(command.options): if option.name in [ 'logging_configuration', 'logging_level', 'record', 'show_configuration' ]: self.assertFalse(option.is_set) continue self.assertTrue(option.is_set) expected = 'testcommandlineparser required_option="t" unnecessary_option="f"' self.assertEqual(expected, str(command)) self.assertEqual(command.fieldnames, []) # Command line with fieldnames fieldnames = ['field_1', 'field_2', 'field_3'] command = TestCommandLineParserCommand() CommandLineParser.parse(command, options + fieldnames) for option in six.itervalues(command.options): if option.name in [ 'logging_configuration', 'logging_level', 'record', 'show_configuration' ]: self.assertFalse(option.is_set) continue self.assertTrue(option.is_set) expected = 'testcommandlineparser required_option="t" unnecessary_option="f" field_1 field_2 field_3' self.assertEqual(expected, str(command)) self.assertEquals(command.fieldnames, fieldnames) # Command line without any unnecessary options command = TestCommandLineParserCommand() CommandLineParser.parse(command, ['required_option=true'] + fieldnames) for option in six.itervalues(command.options): if option.name in [ 'unnecessary_option', 'logging_configuration', 'logging_level', 'record', 'show_configuration' ]: self.assertFalse(option.is_set) continue self.assertTrue(option.is_set) expected = 'testcommandlineparser required_option="t" field_1 field_2 field_3' self.assertEqual(expected, str(command)) self.assertEquals(command.fieldnames, fieldnames) # Command line with missing required options, with or without fieldnames or unnecessary options options = ['unnecessary_option=true'] self.assertRaises(ValueError, CommandLineParser.parse, command, options + fieldnames) self.assertRaises(ValueError, CommandLineParser.parse, command, options) self.assertRaises(ValueError, CommandLineParser.parse, command, []) # Command line with unrecognized options self.assertRaises( ValueError, CommandLineParser.parse, command, ['unrecognized_option_1=foo', 'unrecognized_option_2=bar']) # Command line with a variety of quoted/escaped text options @Configuration() class TestCommandLineParserCommand(SearchCommand): text = Option() class ConfigurationSettings(SearchCommand.ConfigurationSettings): @classmethod def fix_up(cls, command_class): pass strings = [ r'"foo bar"', r'"foo/bar"', r'"foo\\bar"', r'"""foo bar"""', r'"\"foo bar\""', r'Hello\ World!', r'\"Hello\ World!\"' ] expected_values = [ r'foo bar', r'foo/bar', r'foo\bar', r'"foo bar"', r'"foo bar"', r'Hello World!', r'"Hello World!"' ] for string, expected_value in izip(strings, expected_values): command = TestCommandLineParserCommand() argv = ['text', '=', string] CommandLineParser.parse(command, argv) self.assertEqual(command.text, expected_value) for string, expected_value in izip(strings, expected_values): command = TestCommandLineParserCommand() argv = [string] CommandLineParser.parse(command, argv) self.assertEqual(command.fieldnames[0], expected_value) for string, expected_value in izip(strings, expected_values): command = TestCommandLineParserCommand() argv = ['text', '=', string] + strings CommandLineParser.parse(command, argv) self.assertEqual(command.text, expected_value) self.assertEqual(command.fieldnames, expected_values) strings = [ 'some\\ string\\', r'some\ string"', r'"some string', r'some"string' ] for string in strings: command = TestCommandLineParserCommand() argv = [string] self.assertRaises(SyntaxError, CommandLineParser.parse, command, argv) return
def reset(self): for value in six.itervalues(self): value.reset()
def get_missing(self): missing = [item.name for item in six.itervalues(self) if item.is_required and not item.is_set] return missing if len(missing) > 0 else None
def __str__(self): text = ' '.join([str(item) for item in six.itervalues(self) if item.is_set]) return text