From: Steven Hardy Date: Fri, 3 May 2013 10:43:54 +0000 (+0100) Subject: heat tests : convert most tests to inline templates X-Git-Tag: 2014.1~647^2 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=6e9a0e468317bd5fc50517a58448408ef5fa24ce;p=openstack-build%2Fheat-build.git heat tests : convert most tests to inline templates Convert to inline minimal templates, to remove dependency on in-tree templates which we plan to remove. Note there are still some more tests remaining which need similar changes (shardy todo) Change-Id: I0864d1f15ea3b815d449277387440157f104bbf3 --- diff --git a/heat/tests/test_autoscaling.py b/heat/tests/test_autoscaling.py index 30c9a4fd..236e8dc3 100644 --- a/heat/tests/test_autoscaling.py +++ b/heat/tests/test_autoscaling.py @@ -12,28 +12,98 @@ # License for the specific language governing permissions and limitations # under the License. - -import os import datetime import copy import eventlet import mox -from heat.common import context from heat.common import template_format from heat.engine.resources import autoscaling as asc from heat.engine.resources import loadbalancer from heat.engine.resources import instance from heat.engine.resources import cloud_watch from heat.engine import clients -from heat.engine import parser from heat.engine import scheduler from heat.engine.resource import Metadata from heat.openstack.common import timeutils from heat.tests.v1_1 import fakes from heat.tests.common import HeatTestCase from heat.tests.utils import setup_dummy_db +from heat.tests.utils import parse_stack + +as_template = ''' +{ + "AWSTemplateFormatVersion" : "2010-09-09", + "Description" : "AutoScaling Test", + "Parameters" : {}, + "Resources" : { + "WebServerGroup" : { + "Type" : "AWS::AutoScaling::AutoScalingGroup", + "Properties" : { + "AvailabilityZones" : ["nova"], + "LaunchConfigurationName" : { "Ref" : "LaunchConfig" }, + "MinSize" : "1", + "MaxSize" : "3", + "LoadBalancerNames" : [ { "Ref" : "ElasticLoadBalancer" } ] + } + }, + "WebServerScaleUpPolicy" : { + "Type" : "AWS::AutoScaling::ScalingPolicy", + "Properties" : { + "AdjustmentType" : "ChangeInCapacity", + "AutoScalingGroupName" : { "Ref" : "WebServerGroup" }, + "Cooldown" : "60", + "ScalingAdjustment" : "1" + } + }, + "WebServerScaleDownPolicy" : { + "Type" : "AWS::AutoScaling::ScalingPolicy", + "Properties" : { + "AdjustmentType" : "ChangeInCapacity", + "AutoScalingGroupName" : { "Ref" : "WebServerGroup" }, + "Cooldown" : "60", + "ScalingAdjustment" : "-1" + } + }, + "ElasticLoadBalancer" : { + "Type" : "AWS::ElasticLoadBalancing::LoadBalancer", + }, + "LaunchConfig" : { + "Type" : "AWS::AutoScaling::LaunchConfiguration", + "Properties": { + "ImageId" : "foo", + "InstanceType" : "bar", + } + } + } +} +''' + +alarm_template = ''' +{ + "AWSTemplateFormatVersion" : "2010-09-09", + "Description" : "Alarm Test", + "Parameters" : {}, + "Resources" : { + "MEMAlarmHigh": { + "Type": "AWS::CloudWatch::Alarm", + "Properties": { + "AlarmDescription": "Scale-up if MEM > 50% for 1 minute", + "MetricName": "MemoryUtilization", + "Namespace": "system/linux", + "Statistic": "Average", + "Period": "60", + "EvaluationPeriods": "1", + "Threshold": "50", + "AlarmActions": [], + "Dimensions": [], + "ComparisonOperator": "GreaterThanThreshold" + } + } + } +} +''' class AutoScalingTest(HeatTestCase): @@ -42,28 +112,6 @@ class AutoScalingTest(HeatTestCase): setup_dummy_db() self.fc = fakes.FakeClient() self.m.StubOutWithMock(clients.OpenStackClients, 'nova') - clients.OpenStackClients.nova().MultipleTimes().AndReturn(self.fc) - - def load_template(self): - self.path = os.path.dirname(os.path.realpath(__file__)).\ - replace('heat/tests', 'templates') - f = open("%s/AutoScalingMultiAZSample.template" % self.path) - t = template_format.parse(f.read()) - f.close() - return t - - def parse_stack(self, t): - self.m.ReplayAll() - ctx = context.RequestContext.from_dict({ - 'tenant': 'test_tenant', - 'username': 'test_username', - 'password': 'password', - 'auth_url': 'http://localhost:5000/v2.0'}) - template = parser.Template(t) - params = parser.Parameters('test_stack', template, {'KeyName': 'test'}) - stack = parser.Stack(ctx, 'test_stack', template, params) - - return stack def create_scaling_group(self, t, stack, resource_name): resource = asc.AutoScalingGroup(resource_name, @@ -129,8 +177,8 @@ class AutoScalingTest(HeatTestCase): Metadata.__set__(mox.IgnoreArg(), expected).AndReturn(None) def test_scaling_group_update(self): - t = self.load_template() - stack = self.parse_stack(t) + t = template_format.parse(as_template) + stack = parse_stack(t) self._stub_lb_reload(['WebServerGroup-0']) now = timeutils.utcnow() @@ -148,11 +196,11 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() def test_scaling_group_update_ok_maxsize(self): - t = self.load_template() + t = template_format.parse(as_template) properties = t['Resources']['WebServerGroup']['Properties'] properties['MinSize'] = '1' properties['MaxSize'] = '3' - stack = self.parse_stack(t) + stack = parse_stack(t) self._stub_lb_reload(['WebServerGroup-0']) now = timeutils.utcnow() @@ -173,11 +221,11 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() def test_scaling_group_update_ok_minsize(self): - t = self.load_template() + t = template_format.parse(as_template) properties = t['Resources']['WebServerGroup']['Properties'] properties['MinSize'] = '1' properties['MaxSize'] = '3' - stack = self.parse_stack(t) + stack = parse_stack(t) self._stub_lb_reload(['WebServerGroup-0']) now = timeutils.utcnow() @@ -204,11 +252,11 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() def test_scaling_group_update_ok_desired(self): - t = self.load_template() + t = template_format.parse(as_template) properties = t['Resources']['WebServerGroup']['Properties'] properties['MinSize'] = '1' properties['MaxSize'] = '3' - stack = self.parse_stack(t) + stack = parse_stack(t) self._stub_lb_reload(['WebServerGroup-0']) now = timeutils.utcnow() @@ -235,10 +283,10 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() def test_scaling_group_update_ok_desired_remove(self): - t = self.load_template() + t = template_format.parse(as_template) properties = t['Resources']['WebServerGroup']['Properties'] properties['DesiredCapacity'] = '2' - stack = self.parse_stack(t) + stack = parse_stack(t) self._stub_lb_reload(['WebServerGroup-0', 'WebServerGroup-1']) now = timeutils.utcnow() @@ -262,10 +310,10 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() def test_scaling_group_update_ok_cooldown(self): - t = self.load_template() + t = template_format.parse(as_template) properties = t['Resources']['WebServerGroup']['Properties'] properties['Cooldown'] = '60' - stack = self.parse_stack(t) + stack = parse_stack(t) self._stub_lb_reload(['WebServerGroup-0']) now = timeutils.utcnow() @@ -289,14 +337,14 @@ class AutoScalingTest(HeatTestCase): Make sure that we can change the update-able properties without replacing the Alarm resource. ''' - t = self.load_template() + t = template_format.parse(alarm_template) #short circuit the alarm's references properties = t['Resources']['MEMAlarmHigh']['Properties'] properties['AlarmActions'] = ['a'] properties['Dimensions'] = [{'a': 'v'}] - stack = self.parse_stack(t) + stack = parse_stack(t) # the watch rule needs a valid stack_id stack.store() @@ -321,14 +369,14 @@ class AutoScalingTest(HeatTestCase): Make sure that the Alarm resource IS replaced when non-update-able properties are changed. ''' - t = self.load_template() + t = template_format.parse(alarm_template) #short circuit the alarm's references properties = t['Resources']['MEMAlarmHigh']['Properties'] properties['AlarmActions'] = ['a'] properties['Dimensions'] = [{'a': 'v'}] - stack = self.parse_stack(t) + stack = parse_stack(t) # the watch rule needs a valid stack_id stack.store() @@ -344,8 +392,8 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() def test_scaling_group_adjust(self): - t = self.load_template() - stack = self.parse_stack(t) + t = template_format.parse(as_template) + stack = parse_stack(t) # start with 3 properties = t['Resources']['WebServerGroup']['Properties'] @@ -387,8 +435,8 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() def test_scaling_group_nochange(self): - t = self.load_template() - stack = self.parse_stack(t) + t = template_format.parse(as_template) + stack = parse_stack(t) # Create initial group, 2 instances properties = t['Resources']['WebServerGroup']['Properties'] @@ -421,8 +469,8 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() def test_scaling_group_percent(self): - t = self.load_template() - stack = self.parse_stack(t) + t = template_format.parse(as_template) + stack = parse_stack(t) # Create initial group, 2 instances properties = t['Resources']['WebServerGroup']['Properties'] @@ -458,8 +506,8 @@ class AutoScalingTest(HeatTestCase): resource.delete() def test_scaling_group_cooldown_toosoon(self): - t = self.load_template() - stack = self.parse_stack(t) + t = template_format.parse(as_template) + stack = parse_stack(t) # Create initial group, 2 instances, Cooldown 60s properties = t['Resources']['WebServerGroup']['Properties'] @@ -511,8 +559,8 @@ class AutoScalingTest(HeatTestCase): resource.delete() def test_scaling_group_cooldown_ok(self): - t = self.load_template() - stack = self.parse_stack(t) + t = template_format.parse(as_template) + stack = parse_stack(t) # Create initial group, 2 instances, Cooldown 60s properties = t['Resources']['WebServerGroup']['Properties'] @@ -564,8 +612,8 @@ class AutoScalingTest(HeatTestCase): resource.delete() def test_scaling_group_cooldown_zero(self): - t = self.load_template() - stack = self.parse_stack(t) + t = template_format.parse(as_template) + stack = parse_stack(t) # Create initial group, 2 instances, Cooldown 0 properties = t['Resources']['WebServerGroup']['Properties'] @@ -614,8 +662,8 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() def test_scaling_policy_up(self): - t = self.load_template() - stack = self.parse_stack(t) + t = template_format.parse(as_template) + stack = parse_stack(t) # Create initial group self._stub_lb_reload(['WebServerGroup-0']) @@ -642,8 +690,8 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() def test_scaling_policy_down(self): - t = self.load_template() - stack = self.parse_stack(t) + t = template_format.parse(as_template) + stack = parse_stack(t) # Create initial group, 2 instances properties = t['Resources']['WebServerGroup']['Properties'] @@ -671,8 +719,8 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() def test_scaling_policy_cooldown_toosoon(self): - t = self.load_template() - stack = self.parse_stack(t) + t = template_format.parse(as_template) + stack = parse_stack(t) # Create initial group self._stub_lb_reload(['WebServerGroup-0']) @@ -722,8 +770,8 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() def test_scaling_policy_cooldown_ok(self): - t = self.load_template() - stack = self.parse_stack(t) + t = template_format.parse(as_template) + stack = parse_stack(t) # Create initial group self._stub_lb_reload(['WebServerGroup-0']) @@ -773,8 +821,8 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() def test_scaling_policy_cooldown_zero(self): - t = self.load_template() - stack = self.parse_stack(t) + t = template_format.parse(as_template) + stack = parse_stack(t) # Create initial group self._stub_lb_reload(['WebServerGroup-0']) @@ -824,8 +872,8 @@ class AutoScalingTest(HeatTestCase): self.m.VerifyAll() def test_scaling_policy_cooldown_none(self): - t = self.load_template() - stack = self.parse_stack(t) + t = template_format.parse(as_template) + stack = parse_stack(t) # Create initial group self._stub_lb_reload(['WebServerGroup-0']) diff --git a/heat/tests/test_dbinstance.py b/heat/tests/test_dbinstance.py index 3b5141bb..0a5728d9 100644 --- a/heat/tests/test_dbinstance.py +++ b/heat/tests/test_dbinstance.py @@ -13,18 +13,44 @@ # under the License. -import os - import mox -from heat.common import context from heat.common import exception from heat.common import template_format -from heat.engine import parser from heat.engine import scheduler from heat.engine.resources import dbinstance as dbi from heat.tests.common import HeatTestCase from heat.tests.utils import setup_dummy_db +from heat.tests.utils import parse_stack + + +rds_template = ''' +{ + "AWSTemplateFormatVersion" : "2010-09-09", + "Description" : "RDS Test", + "Parameters" : { + "KeyName" : { + "Description" : "KeyName", + "Type" : "String", + "Default" : "test" + } + }, + "Resources" : { + "DatabaseServer": { + "Type": "AWS::RDS::DBInstance", + "Properties": { + "DBName" : "wordpress", + "Engine" : "MySQL", + "MasterUsername" : "admin", + "DBInstanceClass" : "db.m1.small", + "DBSecurityGroups" : [], + "AllocatedStorage" : "5", + "MasterUserPassword": "admin" + } + } + } +} +''' class DBInstanceTest(HeatTestCase): @@ -34,27 +60,6 @@ class DBInstanceTest(HeatTestCase): self.m.StubOutWithMock(dbi.DBInstance, 'create_with_template') self.m.StubOutWithMock(dbi.DBInstance, 'nested') - def load_template(self): - self.path = os.path.dirname(os.path.realpath(__file__)).\ - replace('heat/tests', 'templates') - f = open("%s/WordPress_With_RDS.template" % self.path) - t = template_format.parse(f.read()) - f.close() - return t - - def parse_stack(self, t): - ctx = context.RequestContext.from_dict({ - 'tenant': 'test_tenant', - 'tenant_id': '1234abcd', - 'username': 'test_username', - 'password': 'password', - 'auth_url': 'http://localhost:5000/v2.0'}) - template = parser.Template(t) - params = parser.Parameters('test_stack', template, {'KeyName': 'test'}) - stack = parser.Stack(ctx, 'test_stack', template, params) - - return stack - def create_dbinstance(self, t, stack, resource_name): resource = dbi.DBInstance(resource_name, t['Resources'][resource_name], @@ -73,16 +78,14 @@ class DBInstanceTest(HeatTestCase): class FakeNested: resources = {'DatabaseInstance': FakeDatabaseInstance()} - params = { - 'AllocatedStorage': u'5', - 'DBInstanceClass': u'db.m1.small', - 'DBName': u'wordpress', - 'DBSecurityGroups': '', - 'KeyName': 'test', - 'MasterUserPassword': u'admin', - 'MasterUsername': u'admin', - 'Port': '3306' - } + params = {'DBSecurityGroups': '', + 'MasterUsername': u'admin', + 'MasterUserPassword': u'admin', + 'DBName': u'wordpress', + 'KeyName': u'test', + 'AllocatedStorage': u'5', + 'DBInstanceClass': u'db.m1.small', + 'Port': '3306'} dbi.DBInstance.create_with_template(mox.IgnoreArg(), params).AndReturn(None) @@ -93,8 +96,8 @@ class DBInstanceTest(HeatTestCase): dbi.DBInstance.nested().MultipleTimes().AndReturn(fn) self.m.ReplayAll() - t = self.load_template() - s = self.parse_stack(t) + t = template_format.parse(rds_template) + s = parse_stack(t) resource = self.create_dbinstance(t, s, 'DatabaseServer') self.assertEqual('0.0.0.0', resource.FnGetAtt('Endpoint.Address')) diff --git a/heat/tests/test_eip.py b/heat/tests/test_eip.py index 5aa01d20..169f04e1 100644 --- a/heat/tests/test_eip.py +++ b/heat/tests/test_eip.py @@ -13,16 +13,37 @@ # under the License. -import os - -from heat.common import context from heat.common import template_format from heat.engine.resources import eip -from heat.engine import parser from heat.engine import scheduler from heat.tests.common import HeatTestCase from heat.tests.v1_1 import fakes from heat.tests.utils import setup_dummy_db +from heat.tests.utils import parse_stack + + +eip_template = ''' +{ + "AWSTemplateFormatVersion" : "2010-09-09", + "Description" : "EIP Test", + "Parameters" : {}, + "Resources" : { + "IPAddress" : { + "Type" : "AWS::EC2::EIP" + }, + "IPAssoc" : { + "Type" : "AWS::EC2::EIPAssociation", + "Properties" : { + "InstanceId" : { "Ref" : "WebServer" }, + "EIP" : { "Ref" : "IPAddress" } + } + }, + "WebServer": { + "Type": "AWS::EC2::Instance", + } + } +} +''' class EIPTest(HeatTestCase): @@ -34,26 +55,6 @@ class EIPTest(HeatTestCase): self.m.StubOutWithMock(self.fc.servers, 'get') setup_dummy_db() - def load_template(self): - self.path = os.path.dirname(os.path.realpath(__file__)).\ - replace('heat/tests', 'templates') - f = open("%s/WordPress_Single_Instance_With_EIP.template" % self.path) - t = template_format.parse(f.read()) - f.close() - return t - - def parse_stack(self, t): - ctx = context.RequestContext.from_dict({ - 'tenant': 'test_tenant', - 'username': 'test_username', - 'password': 'password', - 'auth_url': 'http://localhost:5000/v2.0'}) - template = parser.Template(t) - params = parser.Parameters('test_stack', template, {'KeyName': 'test'}) - stack = parser.Stack(ctx, 'test_stack', template, params) - - return stack - def create_eip(self, t, stack, resource_name): resource = eip.ElasticIp(resource_name, t['Resources'][resource_name], @@ -79,8 +80,9 @@ class EIPTest(HeatTestCase): self.m.ReplayAll() - t = self.load_template() - stack = self.parse_stack(t) + t = template_format.parse(eip_template) + stack = parse_stack(t) + resource = self.create_eip(t, stack, 'IPAddress') try: @@ -111,8 +113,8 @@ class EIPTest(HeatTestCase): self.m.ReplayAll() - t = self.load_template() - stack = self.parse_stack(t) + t = template_format.parse(eip_template) + stack = parse_stack(t) resource = self.create_eip(t, stack, 'IPAddress') association = self.create_association(t, stack, 'IPAssoc') diff --git a/heat/tests/test_engine_service.py b/heat/tests/test_engine_service.py index de265557..4b657a7e 100644 --- a/heat/tests/test_engine_service.py +++ b/heat/tests/test_engine_service.py @@ -13,7 +13,6 @@ # under the License. -import os import json import mox @@ -36,10 +35,30 @@ from heat.tests.common import HeatTestCase from heat.tests.utils import setup_dummy_db -tests_dir = os.path.dirname(os.path.realpath(__file__)) -templates_dir = os.path.normpath(os.path.join(tests_dir, - os.path.pardir, os.path.pardir, - 'templates')) +wp_template = ''' +{ + "AWSTemplateFormatVersion" : "2010-09-09", + "Description" : "WordPress", + "Parameters" : { + "KeyName" : { + "Description" : "KeyName", + "Type" : "String", + "Default" : "test" + } + }, + "Resources" : { + "WebServer": { + "Type": "AWS::EC2::Instance", + "Properties": { + "ImageId" : "F17-x86_64-gold", + "InstanceType" : "m1.large", + "KeyName" : "test", + "UserData" : "wordpress" + } + } + } +} +''' def create_context(mocks, user='stacks_test_user', @@ -53,17 +72,12 @@ def create_context(mocks, user='stacks_test_user', def get_wordpress_stack(stack_name, ctx): - tmpl_path = os.path.join(templates_dir, - 'WordPress_Single_Instance_gold.template') - with open(tmpl_path) as f: - t = template_format.parse(f.read()) - + t = template_format.parse(wp_template) template = parser.Template(t) parameters = parser.Parameters(stack_name, template, {'KeyName': 'test'}) stack = parser.Stack(ctx, stack_name, template, parameters) - return stack diff --git a/heat/tests/test_instance.py b/heat/tests/test_instance.py index 1069a0e6..4c4ba02e 100644 --- a/heat/tests/test_instance.py +++ b/heat/tests/test_instance.py @@ -13,7 +13,6 @@ # under the License. -import os import copy import mox @@ -28,20 +27,41 @@ from heat.tests.common import HeatTestCase from heat.tests.utils import setup_dummy_db +wp_template = ''' +{ + "AWSTemplateFormatVersion" : "2010-09-09", + "Description" : "WordPress", + "Parameters" : { + "KeyName" : { + "Description" : "KeyName", + "Type" : "String", + "Default" : "test" + } + }, + "Resources" : { + "WebServer": { + "Type": "AWS::EC2::Instance", + "Properties": { + "ImageId" : "F17-x86_64-gold", + "InstanceType" : "m1.large", + "KeyName" : "test", + "UserData" : "wordpress" + } + } + } +} +''' + + class instancesTest(HeatTestCase): def setUp(self): super(instancesTest, self).setUp() self.fc = fakes.FakeClient() - self.path = os.path.dirname(os.path.realpath(__file__)).\ - replace('heat/tests', 'templates') setup_dummy_db() def _create_test_instance(self, return_server, name): - f = open("%s/WordPress_Single_Instance_gold.template" % self.path) - t = template_format.parse(f.read()) - f.close() - stack_name = '%s_stack' % name + t = template_format.parse(wp_template) template = parser.Template(t) params = parser.Parameters(stack_name, template, {'KeyName': 'test'}) stack = parser.Stack(None, stack_name, template, params, diff --git a/heat/tests/test_instance_group.py b/heat/tests/test_instance_group.py index c6bc46b6..b7cb51d2 100644 --- a/heat/tests/test_instance_group.py +++ b/heat/tests/test_instance_group.py @@ -13,54 +13,52 @@ # under the License. import copy -import os import eventlet import mox -from heat.tests.v1_1 import fakes -from heat.common import context from heat.common import exception from heat.common import template_format from heat.engine.resources import autoscaling as asc from heat.engine.resources import instance -from heat.engine.resources import loadbalancer -from heat.engine import clients -from heat.engine import parser from heat.engine import scheduler from heat.tests.common import HeatTestCase from heat.tests.utils import setup_dummy_db +from heat.tests.utils import parse_stack + +ig_template = ''' +{ + "AWSTemplateFormatVersion" : "2010-09-09", + "Description" : "Template to create multiple instances.", + "Parameters" : {}, + "Resources" : { + "JobServerGroup" : { + "Type" : "OS::Heat::InstanceGroup", + "Properties" : { + "LaunchConfigurationName" : { "Ref" : "JobServerConfig" }, + "Size" : "1", + "AvailabilityZones" : ["nova"] + } + }, + + "JobServerConfig" : { + "Type" : "AWS::AutoScaling::LaunchConfiguration", + "Properties": { + "ImageId" : "foo", + "InstanceType" : "m1.large", + "KeyName" : "test", + "UserData" : "jsconfig data" + } + } + } +} +''' class InstanceGroupTest(HeatTestCase): def setUp(self): super(InstanceGroupTest, self).setUp() - self.fc = fakes.FakeClient() - self.m.StubOutWithMock(loadbalancer.LoadBalancer, 'reload') setup_dummy_db() - self.m.StubOutWithMock(clients.OpenStackClients, 'nova') - clients.OpenStackClients.nova().MultipleTimes().AndReturn(self.fc) - - def load_template(self): - self.path = os.path.dirname(os.path.realpath(__file__)).\ - replace('heat/tests', 'templates') - f = open("%s/InstanceGroup.template" % self.path) - t = template_format.parse(f.read()) - f.close() - return t - - def parse_stack(self, t): - self.m.ReplayAll() - ctx = context.RequestContext.from_dict({ - 'tenant': 'test_tenant', - 'username': 'test_username', - 'password': 'password', - 'auth_url': 'http://localhost:5000/v2.0'}) - template = parser.Template(t) - params = parser.Parameters('test_stack', template, {'KeyName': 'test'}) - stack = parser.Stack(ctx, 'test_stack', template, params) - - return stack def _stub_create(self, num): self.m.StubOutWithMock(eventlet, 'sleep') @@ -85,8 +83,8 @@ class InstanceGroupTest(HeatTestCase): def test_instance_group(self): - t = self.load_template() - stack = self.parse_stack(t) + t = template_format.parse(ig_template) + stack = parse_stack(t) # start with min then delete self._stub_create(1) @@ -107,8 +105,8 @@ class InstanceGroupTest(HeatTestCase): def test_missing_image(self): - t = self.load_template() - stack = self.parse_stack(t) + t = template_format.parse(ig_template) + stack = parse_stack(t) resource = asc.InstanceGroup('JobServerGroup', t['Resources']['JobServerGroup'], @@ -127,10 +125,10 @@ class InstanceGroupTest(HeatTestCase): self.m.VerifyAll() def test_update_size(self): - t = self.load_template() + t = template_format.parse(ig_template) properties = t['Resources']['JobServerGroup']['Properties'] properties['Size'] = '2' - stack = self.parse_stack(t) + stack = parse_stack(t) self._stub_create(2) self.m.ReplayAll() diff --git a/heat/tests/test_loadbalancer.py b/heat/tests/test_loadbalancer.py index 35f9782f..644b61e8 100644 --- a/heat/tests/test_loadbalancer.py +++ b/heat/tests/test_loadbalancer.py @@ -15,15 +15,12 @@ import mox import re -import os from oslo.config import cfg from heat.common import exception from heat.common import config -from heat.common import context from heat.common import template_format from heat.engine import clients -from heat.engine import parser from heat.engine import scheduler from heat.engine.resources import instance from heat.engine.resources import user @@ -32,18 +29,47 @@ from heat.engine.resources import wait_condition as wc from heat.engine.resource import Metadata from heat.tests.common import HeatTestCase from heat.tests.utils import setup_dummy_db +from heat.tests.utils import parse_stack from heat.tests.v1_1 import fakes from heat.tests import fakes as test_fakes -def create_context(mocks, user='lb_test_user', - tenant='test_tenant', ctx=None): - ctx = ctx or context.get_admin_context() - mocks.StubOutWithMock(ctx, 'username') - mocks.StubOutWithMock(ctx, 'tenant_id') - ctx.username = user - ctx.tenant_id = tenant - return ctx +lb_template = ''' +{ + "AWSTemplateFormatVersion": "2010-09-09", + "Description": "LB Template", + "Parameters" : { + "KeyName" : { + "Description" : "KeyName", + "Type" : "String", + "Default" : "test" + } + }, + "Resources": { + "WikiServerOne": { + "Type": "AWS::EC2::Instance", + "Properties": { + "ImageId": "F17-x86_64-gold", + "InstanceType" : "m1.large", + "KeyName" : "test", + "UserData" : "some data" + } + }, + "LoadBalancer" : { + "Type" : "AWS::ElasticLoadBalancing::LoadBalancer", + "Properties" : { + "AvailabilityZones" : ["nova"], + "Instances" : [{"Ref": "WikiServerOne"}], + "Listeners" : [ { + "LoadBalancerPort" : "80", + "InstancePort" : "80", + "Protocol" : "HTTP" + }] + } + } + } +} +''' class LoadBalancerTest(HeatTestCase): @@ -61,23 +87,6 @@ class LoadBalancerTest(HeatTestCase): 'http://127.0.0.1:8000/v1/waitcondition') setup_dummy_db() - def load_template(self): - self.path = os.path.dirname(os.path.realpath(__file__)).\ - replace('heat/tests', 'templates') - f = open("%s/WordPress_With_LB.template" % self.path) - t = template_format.parse(f.read()) - f.close() - return t - - def parse_stack(self, t): - template = parser.Template(t) - params = parser.Parameters('test_stack', template, {'KeyName': 'test'}) - stack = parser.Stack(create_context(self.m), 'test_stack', template, - params, stack_id=None, disable_rollback=True) - stack.store() - - return stack - def create_loadbalancer(self, t, stack, resource_name): resource = lb.LoadBalancer(resource_name, t['Resources'][resource_name], @@ -89,16 +98,15 @@ class LoadBalancerTest(HeatTestCase): def test_loadbalancer(self): self.m.StubOutWithMock(user.User, 'keystone') - user.User.keystone().MultipleTimes().AndReturn(self.fkc) + user.User.keystone().AndReturn(self.fkc) self.m.StubOutWithMock(user.AccessKey, 'keystone') - user.AccessKey.keystone().MultipleTimes().AndReturn(self.fkc) + user.AccessKey.keystone().AndReturn(self.fkc) self.m.StubOutWithMock(wc.WaitConditionHandle, 'keystone') wc.WaitConditionHandle.keystone().MultipleTimes().AndReturn(self.fkc) clients.OpenStackClients.nova( "compute").MultipleTimes().AndReturn(self.fc) - clients.OpenStackClients.nova().MultipleTimes().AndReturn(self.fc) self.fc.servers.create( flavor=2, image=745, key_name='test', meta=None, nics=None, name=u'test_stack.LoadBalancer.LB_instance', @@ -106,14 +114,15 @@ class LoadBalancerTest(HeatTestCase): security_groups=None, availability_zone=None).AndReturn( self.fc.servers.list()[1]) Metadata.__set__(mox.IgnoreArg(), - mox.IgnoreArg()).MultipleTimes().AndReturn(None) + mox.IgnoreArg()).AndReturn(None) self.m.StubOutWithMock(wc.WaitConditionHandle, 'get_status') wc.WaitConditionHandle.get_status().AndReturn(['SUCCESS']) self.m.ReplayAll() - t = self.load_template() - s = self.parse_stack(t) + t = template_format.parse(lb_template) + s = parse_stack(t) + s.store() resource = self.create_loadbalancer(t, s, 'LoadBalancer') diff --git a/heat/tests/test_user.py b/heat/tests/test_user.py index b9bee092..a9407a0f 100644 --- a/heat/tests/test_user.py +++ b/heat/tests/test_user.py @@ -13,23 +13,79 @@ # under the License. -import os - from oslo.config import cfg from heat.common import config -from heat.common import context from heat.common import exception from heat.common import template_format -from heat.engine import parser from heat.engine import scheduler from heat.engine.resources import user from heat.tests.common import HeatTestCase from heat.tests import fakes from heat.tests.utils import setup_dummy_db +from heat.tests.utils import parse_stack import keystoneclient.exceptions +user_template = ''' +{ + "AWSTemplateFormatVersion" : "2010-09-09", + "Description" : "Just a User", + "Parameters" : {}, + "Resources" : { + "CfnUser" : { + "Type" : "AWS::IAM::User" + } + } +} +''' + +user_accesskey_template = ''' +{ + "AWSTemplateFormatVersion" : "2010-09-09", + "Description" : "Just a User", + "Parameters" : {}, + "Resources" : { + "CfnUser" : { + "Type" : "AWS::IAM::User" + }, + + "HostKeys" : { + "Type" : "AWS::IAM::AccessKey", + "Properties" : { + "UserName" : {"Ref": "CfnUser"} + } + } + } +} +''' + + +user_policy_template = ''' +{ + "AWSTemplateFormatVersion" : "2010-09-09", + "Description" : "Just a User", + "Parameters" : {}, + "Resources" : { + "CfnUser" : { + "Type" : "AWS::IAM::User", + "Properties" : { + "Policies" : [ { "Ref": "WebServerAccessPolicy"} ] + } + }, + "WebServerAccessPolicy" : { + "Type" : "OS::Heat::AccessPolicy", + "Properties" : { + "AllowedResources" : [ "WikiDatabase" ] + } + }, + "WikiDatabase" : { + "Type" : "AWS::EC2::Instance", + } + } +} +''' + class UserPolicyTestCase(HeatTestCase): def setUp(self): @@ -42,31 +98,6 @@ class UserPolicyTestCase(HeatTestCase): class UserTest(UserPolicyTestCase): - def load_template(self, template_name='Rails_Single_Instance.template'): - self.path = os.path.dirname(os.path.realpath(__file__)).\ - replace('heat/tests', 'templates') - f = open("%s/%s" % (self.path, template_name)) - t = template_format.parse(f.read()) - f.close() - return t - - def parse_stack(self, t): - ctx = context.RequestContext.from_dict({ - 'tenant_id': 'test_tenant', - 'username': 'test_username', - 'password': 'password', - 'auth_url': 'http://localhost:5000/v2.0'}) - template = parser.Template(t) - params = parser.Parameters('test_stack', - template, - {'KeyName': 'test', - 'DBRootPassword': 'test', - 'DBUsername': 'test', - 'DBPassword': 'test'}) - stack = parser.Stack(ctx, 'test_stack', template, params) - - return stack - def create_user(self, t, stack, resource_name): resource = user.User(resource_name, t['Resources'][resource_name], @@ -83,8 +114,8 @@ class UserTest(UserPolicyTestCase): self.m.ReplayAll() - t = self.load_template() - stack = self.parse_stack(t) + t = template_format.parse(user_template) + stack = parse_stack(t) resource = self.create_user(t, stack, 'CfnUser') self.assertEqual(self.fc.user_id, resource.resource_id) @@ -119,9 +150,8 @@ class UserTest(UserPolicyTestCase): self.m.ReplayAll() - tmpl = 'WordPress_Single_Instance_With_HA_AccessPolicy.template' - t = self.load_template(template_name=tmpl) - stack = self.parse_stack(t) + t = template_format.parse(user_policy_template) + stack = parse_stack(t) resource = self.create_user(t, stack, 'CfnUser') self.assertEqual(self.fc.user_id, resource.resource_id) @@ -158,10 +188,9 @@ class UserTest(UserPolicyTestCase): def test_user_create_bad_policies(self): self.m.ReplayAll() - tmpl = 'WordPress_Single_Instance_With_HA_AccessPolicy.template' - t = self.load_template(template_name=tmpl) + t = template_format.parse(user_policy_template) t['Resources']['CfnUser']['Properties']['Policies'] = ['NoExistBad'] - stack = self.parse_stack(t) + stack = parse_stack(t) resource_name = 'CfnUser' resource = user.User(resource_name, t['Resources'][resource_name], @@ -181,9 +210,8 @@ class UserTest(UserPolicyTestCase): self.m.ReplayAll() - tmpl = 'WordPress_Single_Instance_With_HA_AccessPolicy.template' - t = self.load_template(template_name=tmpl) - stack = self.parse_stack(t) + t = template_format.parse(user_policy_template) + stack = parse_stack(t) resource = self.create_user(t, stack, 'CfnUser') self.assertEqual(self.fc.user_id, resource.resource_id) @@ -205,11 +233,10 @@ class UserTest(UserPolicyTestCase): self.m.ReplayAll() - tmpl = 'WordPress_Single_Instance_With_HA_AccessPolicy.template' - t = self.load_template(template_name=tmpl) + t = template_format.parse(user_policy_template) t['Resources']['CfnUser']['Properties']['Policies'] = [ 'WebServerAccessPolicy', {'an_ignored': 'policy'}] - stack = self.parse_stack(t) + stack = parse_stack(t) resource = self.create_user(t, stack, 'CfnUser') self.assertEqual(self.fc.user_id, resource.resource_id) @@ -223,31 +250,6 @@ class UserTest(UserPolicyTestCase): class AccessKeyTest(UserPolicyTestCase): - def load_template(self): - self.path = os.path.dirname(os.path.realpath(__file__)).\ - replace('heat/tests', 'templates') - f = open("%s/Rails_Single_Instance.template" % self.path) - t = template_format.parse(f.read()) - f.close() - return t - - def parse_stack(self, t): - ctx = context.RequestContext.from_dict({ - 'tenant_id': 'test_tenant', - 'username': 'test_username', - 'password': 'password', - 'auth_url': 'http://localhost:5000/v2.0'}) - template = parser.Template(t) - params = parser.Parameters('test_stack', - template, - {'KeyName': 'test', - 'DBRootPassword': 'test', - 'DBUsername': 'test', - 'DBPassword': 'test'}) - stack = parser.Stack(ctx, 'test_stack', template, params) - - return stack - def create_access_key(self, t, stack, resource_name): resource = user.AccessKey(resource_name, t['Resources'][resource_name], @@ -264,12 +266,12 @@ class AccessKeyTest(UserPolicyTestCase): self.m.ReplayAll() - t = self.load_template() + t = template_format.parse(user_accesskey_template) # Override the Ref for UserName with a hard-coded name, # so we don't need to create the User resource t['Resources']['HostKeys']['Properties']['UserName'] =\ 'test_stack.CfnUser' - stack = self.parse_stack(t) + stack = parse_stack(t) stack.resources['CfnUser'].resource_id = self.fc.user_id stack.resources['CfnUser'].state = 'CREATE_COMPLETE' @@ -309,11 +311,11 @@ class AccessKeyTest(UserPolicyTestCase): def test_access_key_no_user(self): self.m.ReplayAll() - t = self.load_template() + t = template_format.parse(user_accesskey_template) # Set the resource properties UserName to an unknown user t['Resources']['HostKeys']['Properties']['UserName'] =\ 'test_stack.NoExist' - stack = self.parse_stack(t) + stack = parse_stack(t) stack.resources['CfnUser'].resource_id = self.fc.user_id resource = user.AccessKey('HostKeys', @@ -332,36 +334,9 @@ class AccessKeyTest(UserPolicyTestCase): class AccessPolicyTest(UserPolicyTestCase): - def load_template(self): - template_name =\ - 'WordPress_Single_Instance_With_HA_AccessPolicy.template' - self.path = os.path.dirname(os.path.realpath(__file__)).\ - replace('heat/tests', 'templates') - f = open("%s/%s" % (self.path, template_name)) - t = template_format.parse(f.read()) - f.close() - return t - - def parse_stack(self, t): - ctx = context.RequestContext.from_dict({ - 'tenant_id': 'test_tenant', - 'username': 'test_username', - 'password': 'password', - 'auth_url': 'http://localhost:5000/v2.0'}) - template = parser.Template(t) - params = parser.Parameters('test_stack', - template, - {'KeyName': 'test', - 'DBRootPassword': 'test', - 'DBUsername': 'test', - 'DBPassword': 'test'}) - stack = parser.Stack(ctx, 'test_stack', template, params) - - return stack - def test_accesspolicy_create_ok(self): - t = self.load_template() - stack = self.parse_stack(t) + t = template_format.parse(user_policy_template) + stack = parse_stack(t) resource_name = 'WebServerAccessPolicy' resource = user.AccessPolicy(resource_name, @@ -371,10 +346,10 @@ class AccessPolicyTest(UserPolicyTestCase): self.assertEqual(user.User.CREATE_COMPLETE, resource.state) def test_accesspolicy_create_ok_empty(self): - t = self.load_template() + t = template_format.parse(user_policy_template) resource_name = 'WebServerAccessPolicy' t['Resources'][resource_name]['Properties']['AllowedResources'] = [] - stack = self.parse_stack(t) + stack = parse_stack(t) resource = user.AccessPolicy(resource_name, t['Resources'][resource_name], @@ -383,11 +358,11 @@ class AccessPolicyTest(UserPolicyTestCase): self.assertEqual(user.User.CREATE_COMPLETE, resource.state) def test_accesspolicy_create_err_notfound(self): - t = self.load_template() + t = template_format.parse(user_policy_template) resource_name = 'WebServerAccessPolicy' t['Resources'][resource_name]['Properties']['AllowedResources'] = [ 'NoExistResource'] - stack = self.parse_stack(t) + stack = parse_stack(t) resource = user.AccessPolicy(resource_name, t['Resources'][resource_name], @@ -395,9 +370,9 @@ class AccessPolicyTest(UserPolicyTestCase): self.assertRaises(exception.ResourceNotFound, resource.handle_create) def test_accesspolicy_update(self): - t = self.load_template() + t = template_format.parse(user_policy_template) resource_name = 'WebServerAccessPolicy' - stack = self.parse_stack(t) + stack = parse_stack(t) resource = user.AccessPolicy(resource_name, t['Resources'][resource_name], @@ -406,9 +381,9 @@ class AccessPolicyTest(UserPolicyTestCase): resource.handle_update({})) def test_accesspolicy_access_allowed(self): - t = self.load_template() + t = template_format.parse(user_policy_template) resource_name = 'WebServerAccessPolicy' - stack = self.parse_stack(t) + stack = parse_stack(t) resource = user.AccessPolicy(resource_name, t['Resources'][resource_name],