diff options
Diffstat (limited to 'heat/tests')
-rw-r--r-- | heat/tests/test_nested_stack.py | 65 | ||||
-rw-r--r-- | heat/tests/test_provider_template.py | 67 | ||||
-rw-r--r-- | heat/tests/test_stack_resource.py | 141 |
3 files changed, 132 insertions, 141 deletions
diff --git a/heat/tests/test_nested_stack.py b/heat/tests/test_nested_stack.py index 2251edba5..c6bee8b4d 100644 --- a/heat/tests/test_nested_stack.py +++ b/heat/tests/test_nested_stack.py @@ -15,6 +15,7 @@ import copy import json import six +import yaml import mock from oslo.config import cfg @@ -26,6 +27,7 @@ from heat.common import urlfetch from heat.db import api as db_api from heat.engine import parser from heat.engine import resource +from heat.engine.resources import stack as stack_res from heat.engine import rsrc_defn from heat.engine import scheduler from heat.tests import common @@ -671,3 +673,66 @@ Outputs: self.assertEqual((stack.DELETE, stack.COMPLETE), stack.state) self.assertRaises(exception.NotFound, db_api.resource_data_get, res, 'test') + + +class NestedStackCrudTest(common.HeatTestCase): + nested_template = ''' +HeatTemplateFormatVersion: '2012-12-12' +Parameters: + KeyName: + Type: String +Outputs: + Foo: + Value: bar +''' + + def setUp(self): + super(NestedStackCrudTest, self).setUp() + + ctx = utils.dummy_context('test_username', 'aaaa', 'password') + empty_template = {"HeatTemplateFormatVersion": "2012-12-12"} + stack = parser.Stack(ctx, 'test', parser.Template(empty_template)) + stack.store() + + self.patchobject(urlfetch, 'get', return_value=self.nested_template) + self.nested_parsed = yaml.load(self.nested_template) + self.nested_params = {"KeyName": "foo"} + self.defn = rsrc_defn.ResourceDefinition( + 'test_t_res', + 'AWS::CloudFormation::Stack', + {"TemplateURL": "https://server.test/the.template", + "Parameters": self.nested_params}) + self.res = stack_res.NestedStack('test_t_res', + self.defn, stack) + self.assertIsNone(self.res.validate()) + + def test_handle_create(self): + self.res.create_with_template = mock.Mock(return_value=None) + + self.res.handle_create() + + self.res.create_with_template.assert_called_once_with( + self.nested_parsed, self.nested_params, None, adopt_data=None) + + def test_handle_adopt(self): + self.res.create_with_template = mock.Mock(return_value=None) + + self.res.handle_adopt(resource_data={'resource_id': 'fred'}) + + self.res.create_with_template.assert_called_once_with( + self.nested_parsed, self.nested_params, None, + adopt_data={'resource_id': 'fred'}) + + def test_handle_update(self): + self.res.update_with_template = mock.Mock(return_value=None) + + self.res.handle_update(self.defn, None, None) + + self.res.update_with_template.assert_called_once_with( + self.nested_parsed, self.nested_params, None) + + def test_handle_delete(self): + self.res.nested = mock.MagicMock() + self.res.nested.return_value.delete.return_value = None + self.res.handle_delete() + self.res.nested.return_value.delete.assert_called_once_with() diff --git a/heat/tests/test_provider_template.py b/heat/tests/test_provider_template.py index facfa3a2b..320fa5296 100644 --- a/heat/tests/test_provider_template.py +++ b/heat/tests/test_provider_template.py @@ -1108,3 +1108,70 @@ Outputs: stack.output('value')) self.m.VerifyAll() + + +class TemplateResourceCrudTest(common.HeatTestCase): + provider = { + 'HeatTemplateFormatVersion': '2012-12-12', + 'Parameters': { + 'Foo': {'Type': 'String'}, + 'Blarg': {'Type': 'String', 'Default': 'wibble'}, + }, + } + + def setUp(self): + super(TemplateResourceCrudTest, self).setUp() + files = {'test_resource.template': json.dumps(self.provider)} + + class DummyResource(object): + support_status = support.SupportStatus() + properties_schema = {"Foo": + properties.Schema(properties.Schema.STRING, + required=True)} + attributes_schema = {} + + env = environment.Environment() + resource._register_class('DummyResource', DummyResource) + env.load({'resource_registry': + {'DummyResource': 'test_resource.template'}}) + stack = parser.Stack(utils.dummy_context(), 'test_stack', + parser.Template(empty_template, files=files), + env=env, + stack_id=str(uuid.uuid4())) + + self.defn = rsrc_defn.ResourceDefinition('test_t_res', + "DummyResource", + {"Foo": "bar"}) + self.res = template_resource.TemplateResource('test_t_res', + self.defn, stack) + self.assertIsNone(self.res.validate()) + + def test_handle_create(self): + self.res.create_with_template = mock.Mock(return_value=None) + + self.res.handle_create() + + self.res.create_with_template.assert_called_once_with( + self.provider, {'Foo': 'bar'}) + + def test_handle_adopt(self): + self.res.create_with_template = mock.Mock(return_value=None) + + self.res.handle_adopt(resource_data={'resource_id': 'fred'}) + + self.res.create_with_template.assert_called_once_with( + self.provider, {'Foo': 'bar'}, adopt_data={'resource_id': 'fred'}) + + def test_handle_update(self): + self.res.update_with_template = mock.Mock(return_value=None) + + self.res.handle_update(self.defn, None, None) + + self.res.update_with_template.assert_called_once_with( + self.provider, {'Foo': 'bar'}) + + def test_handle_delete(self): + self.res.nested = mock.MagicMock() + self.res.nested.return_value.delete.return_value = None + self.res.handle_delete() + self.res.nested.return_value.delete.assert_called_once_with() diff --git a/heat/tests/test_stack_resource.py b/heat/tests/test_stack_resource.py index 1f8d858a6..6b04c973a 100644 --- a/heat/tests/test_stack_resource.py +++ b/heat/tests/test_stack_resource.py @@ -11,7 +11,6 @@ # License for the specific language governing permissions and limitations # under the License. -import datetime import six import uuid @@ -414,71 +413,6 @@ class StackResourceTest(common.HeatTestCase): self.parent_resource._validate_nested_resources, template) - def test_create_with_template_ok(self): - self.parent_resource.create_with_template(self.templ, - {"KeyName": "key"}) - self.stack = self.parent_resource.nested() - - self.assertEqual(self.parent_resource, self.stack.parent_resource) - self.assertEqual("cb2f2b28-a663-4683-802c-4b40c916e1ff", - self.stack.name) - self.assertEqual(self.templ, self.stack.t.t) - self.assertEqual(self.stack.id, self.parent_resource.resource_id) - self.assertEqual(1, self.stack.nested_depth) - self.assertIsNone(self.stack.timeout_mins) - self.assertEqual('aprojectid', self.stack.stack_user_project_id) - - def test_create_with_template_timeout_mins(self): - self.assertIsNone(self.parent_stack.timeout_mins) - self.m.StubOutWithMock(self.parent_stack, 'timeout_mins') - self.parent_stack.timeout_mins = 100 - self.m.ReplayAll() - self.parent_resource.create_with_template(self.templ, - {"KeyName": "key"}) - self.stack = self.parent_resource.nested() - self.assertEqual(100, self.stack.timeout_mins) - self.m.VerifyAll() - - def test_adopt_with_template_ok(self): - adopt_data = { - "resources": { - "WebServer": { - "resource_id": "test-res-id" - } - } - } - self.parent_resource.create_with_template(self.templ, - {"KeyName": "key"}, - adopt_data=adopt_data) - self.stack = self.parent_resource.nested() - - self.assertEqual(self.stack.ADOPT, self.stack.action) - self.assertEqual('test-res-id', - self.stack.resources['WebServer'].resource_id) - self.assertEqual(self.parent_resource, self.stack.parent_resource) - self.assertEqual("cb2f2b28-a663-4683-802c-4b40c916e1ff", - self.stack.name) - self.assertEqual(self.templ, self.stack.t.t) - self.assertEqual(self.stack.id, self.parent_resource.resource_id) - - def test_prepare_abandon(self): - self.parent_resource.create_with_template(self.templ, - {"KeyName": "key"}) - ret = self.parent_resource.prepare_abandon() - # check abandoned data contains all the necessary information. - # (no need to check stack/resource IDs, because they are - # randomly generated uuids) - self.assertEqual(9, len(ret)) - self.assertEqual('CREATE', ret['action']) - self.assertIn('name', ret) - self.assertIn('id', ret) - self.assertIn('resources', ret) - self.assertEqual(template_format.parse(param_template), - ret['template']) - self.assertIn('stack_user_project_id', ret) - self.assertIn('project_id', ret) - self.assertIn('environment', ret) - def test_create_with_template_validates(self): """ Creating a stack with a template validates the created stack, so that @@ -507,81 +441,6 @@ class StackResourceTest(common.HeatTestCase): self.parent_resource.update_with_template, template, {'WebServer': 'foo'}) - def test_update_with_template_ok(self): - """ - The update_with_template method updates the nested stack with the - given template and user parameters. - """ - create_result = self.parent_resource.create_with_template( - self.simple_template, {}) - while not create_result.step(): - pass - self.stack = self.parent_resource.nested() - - new_templ = self.simple_template.copy() - inst_snippet = new_templ["Resources"]["WebServer"].copy() - new_templ["Resources"]["WebServer2"] = inst_snippet - self.parent_resource.updated_time = \ - datetime.datetime(2014, 10, 24, 15, 40) - - updater = self.parent_resource.update_with_template( - new_templ, {}) - updater.run_to_completion() - self.assertIs(True, - self.parent_resource.check_update_complete(updater)) - self.assertEqual(('UPDATE', 'COMPLETE'), self.stack.state) - self.assertEqual(set(["WebServer", "WebServer2"]), - set(self.stack.keys())) - self.assertIsNone(self.stack.timeout_mins) - self.assertIsNotNone(self.stack.updated_time) - - # The stack's owner_id is maintained. - saved_stack = parser.Stack.load( - self.parent_stack.context, self.stack.id) - self.assertEqual(self.parent_stack.id, saved_stack.owner_id) - # Check the stack's updated_time is saved and same as the resource - self.assertEqual(self.parent_resource.updated_time, - saved_stack.updated_time) - - def test_update_with_template_timeout_mins(self): - self.assertIsNone(self.parent_stack.timeout_mins) - self.m.StubOutWithMock(self.parent_stack, 'timeout_mins') - self.parent_stack.timeout_mins = 100 - self.m.ReplayAll() - - create_result = self.parent_resource.create_with_template( - self.simple_template, {}) - while not create_result.step(): - pass - self.stack = self.parent_resource.nested() - self.assertEqual(100, self.stack.timeout_mins) - - self.parent_stack.timeout_mins = 200 - self.m.ReplayAll() - - updater = self.parent_resource.update_with_template( - self.simple_template, {}) - updater.run_to_completion() - self.assertEqual(200, self.stack.timeout_mins) - self.m.VerifyAll() - - def test_update_with_template_files(self): - create_result = self.parent_resource.create_with_template( - self.simple_template, {}) - while not create_result.step(): - pass - self.stack = self.parent_resource.nested() - - new_templ = self.simple_template.copy() - inst_snippet = new_templ["Resources"]["WebServer"].copy() - new_templ["Resources"]["WebServer2"] = inst_snippet - self.parent_stack.t.files["foo"] = "bar" - updater = self.parent_resource.update_with_template( - new_templ, {}) - updater.run_to_completion() - - self.assertEqual({"foo": "bar"}, self.stack.t.files) - def test_update_with_template_state_err(self): """ update_with_template_state_err method should raise error when update |