1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4
# not use this file except in compliance with the License. You may obtain
5
# a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
# License for the specific language governing permissions and limitations
16
from heat.engine import environment
17
from heat.tests.v1_1 import fakes
18
from heat.engine.resources import instance as instances
19
from heat.engine.resources import network_interface as network_interfaces
20
from heat.common import template_format
21
from heat.engine import parser
22
from heat.engine import scheduler
23
from heat.openstack.common import uuidutils
24
from heat.tests.common import HeatTestCase
25
from heat.tests import utils
26
from heat.tests.utils import setup_dummy_db
31
"AWSTemplateFormatVersion" : "2010-09-09",
32
"Description" : "WordPress",
35
"Description" : "KeyName",
41
"Description": "EC2 instance type",
42
"Default": "m1.small",
43
"AllowedValues": [ "m1.small", "m1.large" ]
47
"Description" : "SubnetId of an existing subnet in your VPC"
52
"Type": "AWS::EC2::Instance",
54
"ImageId" : "F17-x86_64-gold",
55
"InstanceType" : { "Ref" : "InstanceType" },
56
"SubnetId" : { "Ref" : "SubnetId" },
57
"KeyName" : { "Ref" : "KeyName" },
58
"UserData" : "wordpress"
66
wp_template_with_nic = '''
68
"AWSTemplateFormatVersion" : "2010-09-09",
69
"Description" : "WordPress",
72
"Description" : "KeyName",
78
"Description": "EC2 instance type",
79
"Default": "m1.small",
80
"AllowedValues": [ "m1.small", "m1.large" ]
84
"Description" : "SubnetId of an existing subnet in your VPC"
90
"Type": "AWS::EC2::NetworkInterface",
92
"SubnetId": { "Ref": "SubnetId" }
97
"Type": "AWS::EC2::Instance",
99
"ImageId" : "F17-x86_64-gold",
100
"InstanceType" : { "Ref" : "InstanceType" },
101
"NetworkInterfaces": [ { "NetworkInterfaceId" : {"Ref": "nic1"},
102
"DeviceIndex" : "0" } ],
103
"KeyName" : { "Ref" : "KeyName" },
104
"UserData" : "wordpress"
112
class FakeQuantum(object):
114
def show_subnet(self, subnet, **_params):
118
'network_id': 'fc68ea2c-b60b-4b4f-bd82-94ec81110766',
119
'tenant_id': 'c1210485b2424d48804aad5d39c61b8f',
120
'allocation_pools': [{'start': '10.10.0.2',
121
'end': '10.10.0.254'}],
122
'gateway_ip': '10.10.0.1',
124
'cidr': '10.10.0.0/24',
125
'id': '4156c7a5-e8c4-4aff-a6e1-8f3c7bc83861',
126
'enable_dhcp': False,
129
def create_port(self, body=None):
132
'admin_state_up': True,
136
'ip_address': '10.0.3.3',
137
'subnet_id': '4156c7a5-e8c4-4aff-a6e1-8f3c7bc83861'}],
138
'id': '64d913c1-bcb1-42d2-8f0a-9593dbcaf251',
139
'mac_address': 'fa:16:3e:25:32:5d',
141
'network_id': 'fc68ea2c-b60b-4b4f-bd82-94ec81110766',
143
'tenant_id': 'c1210485b2424d48804aad5d39c61b8f'
147
class instancesTest(HeatTestCase):
149
super(instancesTest, self).setUp()
150
self.fc = fakes.FakeClient()
153
def _create_test_instance(self, return_server, name):
154
stack_name = '%s_stack' % name
155
t = template_format.parse(wp_template)
156
template = parser.Template(t)
157
kwargs = {'KeyName': 'test',
158
'InstanceType': 'm1.large',
159
'SubnetId': '4156c7a5-e8c4-4aff-a6e1-8f3c7bc83861'}
160
stack = parser.Stack(None, stack_name, template,
161
environment.Environment(kwargs),
162
stack_id=uuidutils.generate_uuid())
164
t['Resources']['WebServer']['Properties']['ImageId'] = 'CentOS 5.2'
165
instance = instances.Instance('%s_name' % name,
166
t['Resources']['WebServer'], stack)
168
self.m.StubOutWithMock(instance, 'nova')
169
instance.nova().MultipleTimes().AndReturn(self.fc)
171
self.m.StubOutWithMock(instance, 'quantum')
172
instance.quantum().MultipleTimes().AndReturn(FakeQuantum())
174
instance.t = instance.stack.resolve_runtime_data(instance.t)
176
# need to resolve the template functions
177
server_userdata = instance._build_userdata(
178
instance.t['Properties']['UserData'])
180
self.m.StubOutWithMock(self.fc.servers, 'create')
181
self.fc.servers.create(
182
image=1, flavor=3, key_name='test',
183
name=utils.PhysName(stack_name, instance.name),
184
security_groups=None,
185
userdata=server_userdata, scheduler_hints=None, meta=None,
186
nics=[{'port-id': '64d913c1-bcb1-42d2-8f0a-9593dbcaf251'}],
187
availability_zone=None).AndReturn(
191
scheduler.TaskRunner(instance.create)()
194
def _create_test_instance_with_nic(self, return_server, name):
195
stack_name = '%s_stack' % name
196
t = template_format.parse(wp_template_with_nic)
197
template = parser.Template(t)
198
kwargs = {'KeyName': 'test',
199
'InstanceType': 'm1.large',
200
'SubnetId': '4156c7a5-e8c4-4aff-a6e1-8f3c7bc83861'}
201
stack = parser.Stack(None, stack_name, template,
202
environment.Environment(kwargs),
203
stack_id=uuidutils.generate_uuid())
205
t['Resources']['WebServer']['Properties']['ImageId'] = 'CentOS 5.2'
207
nic = network_interfaces.NetworkInterface('%s_nic' % name,
208
t['Resources']['nic1'],
211
instance = instances.Instance('%s_name' % name,
212
t['Resources']['WebServer'], stack)
214
self.m.StubOutWithMock(nic, 'quantum')
215
nic.quantum().MultipleTimes().AndReturn(FakeQuantum())
217
self.m.StubOutWithMock(instance, 'nova')
218
instance.nova().MultipleTimes().AndReturn(self.fc)
220
nic.t = nic.stack.resolve_runtime_data(nic.t)
221
instance.t = instance.stack.resolve_runtime_data(instance.t)
223
# need to resolve the template functions
224
server_userdata = instance._build_userdata(
225
instance.t['Properties']['UserData'])
226
self.m.StubOutWithMock(self.fc.servers, 'create')
227
self.fc.servers.create(
228
image=1, flavor=3, key_name='test',
229
name=utils.PhysName(stack_name, instance.name),
230
security_groups=None,
231
userdata=server_userdata, scheduler_hints=None, meta=None,
232
nics=[{'port-id': '64d913c1-bcb1-42d2-8f0a-9593dbcaf251'}],
233
availability_zone=None).AndReturn(
237
# create network interface
238
scheduler.TaskRunner(nic.create)()
239
stack.resources["nic1"] = nic
241
scheduler.TaskRunner(instance.create)()
244
def test_instance_create(self):
245
return_server = self.fc.servers.list()[1]
246
instance = self._create_test_instance(return_server,
247
'test_instance_create')
248
# this makes sure the auto increment worked on instance creation
249
self.assertTrue(instance.id > 0)
251
expected_ip = return_server.networks['public'][0]
252
self.assertEqual(instance.FnGetAtt('PublicIp'), expected_ip)
253
self.assertEqual(instance.FnGetAtt('PrivateIp'), expected_ip)
254
self.assertEqual(instance.FnGetAtt('PrivateDnsName'), expected_ip)
255
self.assertEqual(instance.FnGetAtt('PrivateDnsName'), expected_ip)
259
def test_instance_create_with_nic(self):
260
return_server = self.fc.servers.list()[1]
261
instance = self._create_test_instance_with_nic(
262
return_server, 'test_instance_create_with_network_interface')
264
# this makes sure the auto increment worked on instance creation
265
self.assertTrue(instance.id > 0)
267
expected_ip = return_server.networks['public'][0]
268
self.assertEqual(instance.FnGetAtt('PublicIp'), expected_ip)
269
self.assertEqual(instance.FnGetAtt('PrivateIp'), expected_ip)
270
self.assertEqual(instance.FnGetAtt('PrivateDnsName'), expected_ip)
271
self.assertEqual(instance.FnGetAtt('PrivateDnsName'), expected_ip)