def test_validate_type_invalid(self): resolver = mock.Mock() # Test invalid string type attribute attr_schema = attributes.Schema("Test attribute", type=attributes.Schema.STRING) attr = attributes.Attribute("test1", attr_schema) attribs = attributes.Attributes('test resource', attr_schema, resolver) attribs._validate_type(attr, []) self.assertIn("Attribute test1 is not of type String", self.LOG.output) # Test invalid list type attribute attr_schema = attributes.Schema("Test attribute", type=attributes.Schema.LIST) attr = attributes.Attribute("test1", attr_schema) attribs = attributes.Attributes('test resource', attr_schema, resolver) attribs._validate_type(attr, 'invalid') self.assertIn("Attribute test1 is not of type List", self.LOG.output) # Test invalid map type attribute attr_schema = attributes.Schema("Test attribute", type=attributes.Schema.MAP) attr = attributes.Attribute("test1", attr_schema) attribs = attributes.Attributes('test resource', attr_schema, resolver) attribs._validate_type(attr, 'invalid') self.assertIn("Attribute test1 is not of type Map", self.LOG.output)
def test_get_attribute(self): """Test that we get the attribute values we expect.""" test_resolver = lambda x: "value1" self.m.ReplayAll() attribs = attributes.Attributes('test resource', self.attributes_schema, test_resolver) self.assertEqual("value1", attribs['test1'])
def __init__(self, name, definition, stack): if '/' in name: raise ValueError(_('Resource name may not contain "/"')) self.stack = stack self.context = stack.context self.name = name self.t = definition self.reparse() self.attributes = attributes.Attributes(self.name, self.attributes_schema, self._resolve_attribute) self.abandon_in_progress = False self.resource_id = None # if the stack is being deleted, assume we've already been deleted if stack.action == stack.DELETE: self.action = self.DELETE else: self.action = self.INIT self.status = self.COMPLETE self.status_reason = '' self.id = None self._data = {} self._rsrc_metadata = None self._stored_properties_data = None self.created_time = None self.updated_time = None resource = stack.db_resource_get(name) if resource: self._load_data(resource)
def test_get_attribute_nonexist(self): """Test that we get the attribute values we expect.""" test_resolver = lambda x: "value1" self.m.ReplayAll() attribs = attributes.Attributes('test resource', self.attributes_schema, test_resolver) self.assertRaises(KeyError, attribs.__getitem__, 'not there')
def test_resolve_attribute(self): res = self._get_some_neutron_resource() res.attributes_schema.update( {'attr2': attributes.Schema(type=attributes.Schema.STRING)}) res.attributes = attributes.Attributes(res.name, res.attributes_schema, res._resolve_all_attributes) side_effect = [{'attr1': 'val1', 'attr2': 'val2'}, {'attr1': 'val1', 'attr2': 'val2'}, {'attr1': 'val1', 'attr2': 'val2'}, qe.NotFound] self.patchobject(res, '_show_resource', side_effect=side_effect) res.resource_id = 'resource_id' self.assertEqual({'attr1': 'val1', 'attr2': 'val2'}, res.FnGetAtt('show')) self.assertEqual('val2', res.attributes['attr2']) self.assertRaises(KeyError, res._resolve_all_attributes, 'attr3') self.assertIsNone(res._resolve_all_attributes('attr1')) res.resource_id = None # use local cached object for non-show attribute self.assertEqual('val2', res.FnGetAtt('attr2')) # but the 'show' attribute is never cached self.assertIsNone(res.FnGetAtt('show')) # remove 'attr2' from res.attributes cache res.attributes.reset_resolved_values() # _resolve_attribute (in NeutronResource class) returns None # due to no resource_id self.assertIsNone(res.FnGetAtt('attr2'))
def test_get_attribute_nonexist(self): """Test that we get the attribute values we expect.""" self.resolver.return_value = "value1" attribs = attributes.Attributes('test resource', self.attributes_schema, self.resolver) self.assertRaises(KeyError, attribs.__getitem__, 'not there') self.assertFalse(self.resolver.called)
def test_get_attribute(self): """Test that we get the attribute values we expect.""" self.resolver.return_value = "value1" attribs = attributes.Attributes('test resource', self.attributes_schema, self.resolver) self.assertEqual("value1", attribs['test1']) self.resolver.assert_called_once_with('test1')
def _outputs_to_attribs(self, json_snippet): if not self.attributes and 'Outputs' in json_snippet: self.attributes_schema = ( attributes.Attributes .schema_from_outputs(json_snippet.get('Outputs'))) self.attributes = attributes.Attributes(self.name, self.attributes_schema, self._resolve_attribute)
def test_caching_none(self): self.resolver.side_effect = ["value3", "value3 changed"] attribs = attributes.Attributes('test resource', self.attributes_schema, self.resolver) self.assertEqual("value3", attribs['test3']) self.assertEqual("value3 changed", attribs['test3']) calls = [mock.call('test3'), mock.call('test3')] self.resolver.assert_has_calls(calls)
def test_caching_none(self): value = 'value3' test_resolver = lambda x: value self.m.ReplayAll() attribs = attributes.Attributes('test resource', self.attributes_schema, test_resolver) self.assertEqual("value3", attribs['test3']) value = 'value3 changed' self.assertEqual("value3 changed", attribs['test3'])
def test_attributes_representation(self): """Test that attributes are displayed correct.""" self.resolver.return_value = "value1" attribs = attributes.Attributes('test resource', self.attributes_schema, self.resolver) msg = 'Attributes for test resource:\n\tvalue1\n\tvalue1\n\tvalue1' self.assertEqual(msg, str(attribs)) calls = [mock.call('test1'), mock.call('test2'), mock.call('test3')] self.resolver.assert_has_calls(calls, any_order=True)
def _outputs_to_attribs(self, json_snippet): outputs = json_snippet.get('Outputs') if not self.attributes and outputs: self.attributes_schema = ( attributes.Attributes.schema_from_outputs(outputs)) # Note: it can be updated too and for show return dictionary # with all available outputs self.attributes = attributes.Attributes( self.name, self.attributes_schema, self._make_resolver(weakref.ref(self)))
def _outputs_to_attribs(self, parsed_template): outputs = parsed_template.outputs(None) if not self.attributes and outputs: self.attributes_schema = ( attributes.Attributes.schema_from_outputs(outputs)) # Note: it can be updated too and for show return dictionary # with all available outputs self.attributes = attributes.Attributes( self.name, self.attributes_schema, self._make_resolver(weakref.ref(self)))
def test_validate_type(self): resolver = mock.Mock() msg = 'Attribute test1 is not of type %s' % self.a_type attr_schema = attributes.Schema("Test attribute", type=self.a_type) attrs_schema = {'res1': attr_schema} attr = attributes.Attribute("test1", attr_schema) attribs = attributes.Attributes('test res1', attrs_schema, resolver) attribs._validate_type(attr, self.value) self.assertNotIn(msg, self.LOG.output) attribs._validate_type(attr, self.invalid_value) self.assertIn(msg, self.LOG.output)
def test_caching_local(self): self.resolver.side_effect = ["value1", "value1 changed"] attribs = attributes.Attributes('test resource', self.attributes_schema, self.resolver) self.assertEqual("value1", attribs['test1']) self.assertEqual("value1", attribs['test1']) attribs.reset_resolved_values() self.assertEqual("value1 changed", attribs['test1']) calls = [mock.call('test1'), mock.call('test1')] self.resolver.assert_has_calls(calls)
def test_caching_local(self): value = 'value1' test_resolver = lambda x: value self.m.ReplayAll() attribs = attributes.Attributes('test resource', self.attributes_schema, test_resolver) self.assertEqual("value1", attribs['test1']) value = 'value1 changed' self.assertEqual("value1", attribs['test1']) attribs.reset_resolved_values() self.assertEqual("value1 changed", attribs['test1'])
def test_schema_support_status(self): schema = { 'foo_sup': attributes.Schema('Description1'), 'bar_dep': attributes.Schema('Description2', support_status=support.SupportStatus( support.DEPRECATED, 'Do not use this ever')) } attrs = attributes.Attributes('test_rsrc', schema, lambda d: d) self.assertEqual(support.SUPPORTED, attrs._attributes['foo_sup'].support_status().status) self.assertEqual(support.DEPRECATED, attrs._attributes['bar_dep'].support_status().status) self.assertEqual('Do not use this ever', attrs._attributes['bar_dep'].support_status().message)
def _generate_schema(self): self._parsed_nested = None try: tmpl = template.Template(self.child_template()) except (exception.NotFound, ValueError) as download_error: self.validation_exception = download_error tmpl = template.Template( {"HeatTemplateFormatVersion": "2012-12-12"}) # re-generate the properties and attributes from the template. self.properties_schema, self.attributes_schema = self.get_schemas( tmpl, self.stack.env.param_defaults) self.attributes_schema.update(self.base_attributes_schema) self.attributes = attributes.Attributes(self.name, self.attributes_schema, self._resolve_all_attributes)
def __init__(self, name, definition, stack): def _validate_name(res_name): if '/' in res_name: message = _('Resource name may not contain "/"') raise exception.StackValidationFailed(message=message) _validate_name(name) self.stack = stack self.context = stack.context self.name = name self.t = definition self.reparse() self.attributes = attributes.Attributes(self.name, self.attributes_schema, self._resolve_attribute) self.abandon_in_progress = False self.resource_id = None # if the stack is being deleted, assume we've already been deleted if stack.action == stack.DELETE: self.action = self.DELETE else: self.action = self.INIT self.status = self.COMPLETE self.status_reason = '' self.id = None self.uuid = None self._data = {} self._rsrc_metadata = None self._stored_properties_data = None self.created_time = None self.updated_time = None self._rpc_client = None self.needed_by = [] self.requires = [] self.replaces = None self.replaced_by = None self.current_template_id = None if not stack.has_cache_data(): resource = stack.db_resource_get(name) if resource: self._load_data(resource)
def _generate_schema(self, props): self._parsed_nested = None try: tmpl = template.Template(self.child_template()) except ValueError as download_error: self.validation_exception = download_error tmpl = template.Template({}) # re-generate the properties and attributes from the template. self.properties_schema = (properties.Properties.schema_from_params( tmpl.param_schemata())) self.attributes_schema = (attributes.Attributes.schema_from_outputs( tmpl[tmpl.OUTPUTS])) self.properties = properties.Properties(self.properties_schema, props, self._resolve_runtime_data, self.name, self.context) self.attributes = attributes.Attributes(self.name, self.attributes_schema, self._resolve_attribute)
def _generate_schema(self, definition): self._parsed_nested = None try: tmpl = template.Template(self.child_template()) except ValueError as download_error: self.validation_exception = download_error tmpl = template.Template( {"HeatTemplateFormatVersion": "2012-12-12"}) # re-generate the properties and attributes from the template. self.properties_schema = (properties.Properties.schema_from_params( tmpl.param_schemata())) self.attributes_schema = (attributes.Attributes.schema_from_outputs( tmpl[tmpl.OUTPUTS])) self.properties = definition.properties(self.properties_schema, self.context) self.attributes = attributes.Attributes(self.name, self.attributes_schema, self._resolve_attribute)