def body(self, value): if value: if isinstance(value, bytes): self.__body = ContentHandler.parse_content(value.decode()) elif isinstance(value, str): self.__body = ContentHandler.parse_content(value) else: self.__body = value else: self.__body = value
def test_parse_content_template_unicode(self): """ Unicode parsing tests """ node = {'template': u'myval 😽 $var'} handler = ContentHandler.parse_content(node) context = Context() context.bind_variable('var', 'cheese') self.assertEqual(u'myval 😽 cheese', handler.get_content(context))
def test_parse_content_double_templated_file(self): """ Test parsing of file with path and content templated """ node = {'template': {'file': {'template': '/$var-path.yaml'}}} handler = ContentHandler.parse_content(node) self.assertEqual('/$var-path.yaml', handler.content) self.assertTrue(handler.is_dynamic()) self.assertTrue(handler.is_file) self.assertTrue(handler.is_template_path) self.assertTrue(handler.is_template_content)
def test_parse_content_templated_file_content(self): """ Test parsing of templated file content """ node = {'template': {'file': '/path.yaml'}} handler = ContentHandler.parse_content(node) self.assertEqual('/path.yaml', handler.content) self.assertTrue(handler.is_dynamic()) self.assertTrue(handler.is_file) self.assertFalse(handler.is_template_path) self.assertTrue(handler.is_template_content)
def test_parse_content_file(self): """ Test parsing of file content """ node = {'file': '/myval'} handler = ContentHandler.parse_content(node) self.assertEqual(node['file'], handler.content) self.assertFalse(handler.is_dynamic()) self.assertTrue(handler.is_file) self.assertFalse(handler.is_template_path) self.assertFalse(handler.is_template_content)
def parse(cls, config): validator = JsonSchemaValidator() config = Parser.lowercase_keys(config) if 'schema' not in config: raise ValueError( "Cannot create schema validator without a 'schema' configuration element!" ) validator.schema_context = ContentHandler.parse_content( config['schema']) return validator
def test_parse_content_templated(self): """ Test parsing of templated content """ node = {'template': 'myval $var'} handler = ContentHandler.parse_content(node) context = Context() context.bind_variable('var', 'cheese') self.assertEqual(node['template'], handler.content) self.assertEqual('myval cheese', handler.get_content(context)) self.assertTrue(handler.is_dynamic()) self.assertFalse(handler.is_file) self.assertFalse(handler.is_template_path) self.assertTrue(handler.is_template_content)
def test_parse_content_simple(self): """ Test parsing of simple content """ node = "myval" handler = ContentHandler.parse_content(node) self.assertEqual(node, handler.content) self.assertEqual(node, handler.get_content()) self.assertFalse(handler.is_dynamic()) self.assertFalse(handler.is_file) self.assertFalse(handler.is_template_path) self.assertFalse(handler.is_template_content) node = 2 self.assertRaises(TypeError, ContentHandler.parse_content, node)
def test_parse_content_breaks(self): """ Test for handling parsing of some bad input cases """ failing_configs = list() failing_configs.append({'template': None}) failing_configs.append({'file': None}) failing_configs.append({'file': {'template': None}}) failing_configs.append({'file': {'template': 1}}) failing_configs.append({'file': {'template': 1}}) failing_configs.append({'fil': {'template': 'pathname.yaml'}}) for config in failing_configs: try: handler = ContentHandler.parse_content(config) self.fail("Should raise an exception on invalid parse, config: " + json.dumps(config, default=lambda o: o.__dict__)) except Exception: pass