blob: 5f7674767409d40c1cad00c42ed1119a5a047a7e [file] [log] [blame]
Angus Salkeldebf15d72014-12-10 17:03:15 +10001# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13import logging
14
15from heat_integrationtests.common import test
16
17
18LOG = logging.getLogger(__name__)
19
20
21class InstanceGroupTest(test.HeatIntegrationTest):
22
23 template = '''
24{
25 "AWSTemplateFormatVersion" : "2010-09-09",
26 "Description" : "Template to create multiple instances.",
27 "Parameters" : {"size": {"Type": "String", "Default": "1"},
28 "AZ": {"Type": "String", "Default": "nova"},
29 "image": {"Type": "String"},
30 "flavor": {"Type": "String"},
31 "keyname": {"Type": "String"}},
32 "Resources": {
33 "JobServerGroup": {
34 "Type": "OS::Heat::InstanceGroup",
35 "Properties": {
36 "LaunchConfigurationName" : {"Ref": "JobServerConfig"},
37 "Size" : {"Ref": "size"},
38 "AvailabilityZones" : [{"Ref": "AZ"}]
39 }
40 },
41
42 "JobServerConfig" : {
43 "Type" : "AWS::AutoScaling::LaunchConfiguration",
44 "Metadata": {"foo": "bar"},
45 "Properties": {
46 "ImageId" : {"Ref": "image"},
47 "InstanceType" : {"Ref": "flavor"},
48 "KeyName" : {"Ref": "keyname"},
49 "SecurityGroups" : [ "sg-1" ],
50 "UserData" : "jsconfig data",
51 }
52 }
53 },
54 "Outputs": {
55 "InstanceList": {"Value": {
56 "Fn::GetAtt": ["JobServerGroup", "InstanceList"]}}
57 }
58}
59'''
60
61 instance_template = '''
62heat_template_version: 2013-05-23
63parameters:
64 ImageId: {type: string}
65 InstanceType: {type: string}
66 KeyName: {type: string}
67 SecurityGroups: {type: comma_delimited_list}
68 UserData: {type: string}
69 Tags: {type: comma_delimited_list}
70
71resources:
72 random1:
73 type: OS::Heat::RandomString
74
75outputs:
76 PublicIp:
77 value: {get_attr: [random1, value]}
78'''
79
80 def setUp(self):
81 super(InstanceGroupTest, self).setUp()
82 self.client = self.orchestration_client
83 if not self.conf.image_ref:
84 raise self.skipException("No image configured to test")
85 if not self.conf.keypair_name:
86 raise self.skipException("No keyname configured to test")
87 if not self.conf.instance_type:
88 raise self.skipException("No flavor configured to test")
89
Angus Salkeldbfc7e932014-12-15 11:15:45 +100090 def assert_instance_count(self, stack, expected_count):
91 inst_list = self._stack_output(stack, 'InstanceList')
92 self.assertEqual(expected_count, len(inst_list.split(',')))
93
Angus Salkeldebf15d72014-12-10 17:03:15 +100094 def test_basic_create_works(self):
95 """Make sure the working case is good.
96 Note this combines test_override_aws_ec2_instance into this test as
97 well, which is:
98 If AWS::EC2::Instance is overridden, InstanceGroup will automatically
99 use that overridden resource type.
100 """
101
Angus Salkeldebf15d72014-12-10 17:03:15 +1000102 files = {'provider.yaml': self.instance_template}
103 env = {'resource_registry': {'AWS::EC2::Instance': 'provider.yaml'},
104 'parameters': {'size': 4,
105 'image': self.conf.image_ref,
106 'keyname': self.conf.keypair_name,
107 'flavor': self.conf.instance_type}}
Angus Salkeldbfc7e932014-12-15 11:15:45 +1000108 stack_identifier = self.stack_create(template=self.template,
109 files=files, environment=env)
Angus Salkeldebf15d72014-12-10 17:03:15 +1000110 initial_resources = {
111 'JobServerConfig': 'AWS::AutoScaling::LaunchConfiguration',
112 'JobServerGroup': 'OS::Heat::InstanceGroup'}
113 self.assertEqual(initial_resources,
114 self.list_resources(stack_identifier))
115
Angus Salkeldbfc7e932014-12-15 11:15:45 +1000116 stack = self.client.stacks.get(stack_identifier)
117 self.assert_instance_count(stack, 4)
118
119 def test_size_updates_work(self):
120 files = {'provider.yaml': self.instance_template}
121 env = {'resource_registry': {'AWS::EC2::Instance': 'provider.yaml'},
122 'parameters': {'size': 2,
123 'image': self.conf.image_ref,
124 'keyname': self.conf.keypair_name,
125 'flavor': self.conf.instance_type}}
126
127 stack_identifier = self.stack_create(template=self.template,
128 files=files,
129 environment=env)
130 stack = self.client.stacks.get(stack_identifier)
131 self.assert_instance_count(stack, 2)
132
133 # Increase min size to 5
134 env2 = {'resource_registry': {'AWS::EC2::Instance': 'provider.yaml'},
135 'parameters': {'size': 5,
136 'image': self.conf.image_ref,
137 'keyname': self.conf.keypair_name,
138 'flavor': self.conf.instance_type}}
139 self.update_stack(stack_identifier, self.template,
140 environment=env2, files=files)
141 self._wait_for_stack_status(stack_identifier, 'UPDATE_COMPLETE')
142 stack = self.client.stacks.get(stack_identifier)
143 self.assert_instance_count(stack, 5)