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
15
from testtools import skipIf
17
from heat.common import context
18
from heat.common import exception
19
from heat.common import template_format
20
from heat.engine import parser
21
from heat.engine import resource
22
from heat.tests.common import HeatTestCase
23
from heat.tests import utils
24
from heat.tests.utils import setup_dummy_db
27
from quantumclient.common.exceptions import QuantumClientException
28
from quantumclient.v2_0 import client as quantumclient
33
class VPCTestBase(HeatTestCase):
35
@skipIf(quantumclient is None, 'quantumclient unavaialble')
37
super(VPCTestBase, self).setUp()
39
self.m.StubOutWithMock(quantumclient.Client, 'add_interface_router')
40
self.m.StubOutWithMock(quantumclient.Client, 'add_gateway_router')
41
self.m.StubOutWithMock(quantumclient.Client, 'create_network')
42
self.m.StubOutWithMock(quantumclient.Client, 'create_port')
43
self.m.StubOutWithMock(quantumclient.Client, 'create_router')
44
self.m.StubOutWithMock(quantumclient.Client, 'create_subnet')
45
self.m.StubOutWithMock(quantumclient.Client, 'delete_network')
46
self.m.StubOutWithMock(quantumclient.Client, 'delete_port')
47
self.m.StubOutWithMock(quantumclient.Client, 'delete_router')
48
self.m.StubOutWithMock(quantumclient.Client, 'delete_subnet')
49
self.m.StubOutWithMock(quantumclient.Client, 'list_networks')
50
self.m.StubOutWithMock(quantumclient.Client, 'list_routers')
51
self.m.StubOutWithMock(quantumclient.Client, 'remove_gateway_router')
52
self.m.StubOutWithMock(quantumclient.Client, 'remove_interface_router')
53
self.m.StubOutWithMock(quantumclient.Client, 'show_subnet')
54
self.m.StubOutWithMock(quantumclient.Client, 'show_network')
55
self.m.StubOutWithMock(quantumclient.Client, 'show_router')
56
self.m.StubOutWithMock(quantumclient.Client, 'create_security_group')
57
self.m.StubOutWithMock(quantumclient.Client, 'show_security_group')
58
self.m.StubOutWithMock(quantumclient.Client, 'delete_security_group')
59
self.m.StubOutWithMock(
60
quantumclient.Client, 'create_security_group_rule')
61
self.m.StubOutWithMock(
62
quantumclient.Client, 'delete_security_group_rule')
64
def create_stack(self, template):
65
t = template_format.parse(template)
66
stack = self.parse_stack(t)
67
self.assertEqual(None, stack.create())
70
def parse_stack(self, t):
71
ctx = context.RequestContext.from_dict({
72
'tenant': 'test_tenant',
73
'username': 'test_username',
74
'password': 'password',
75
'auth_url': 'http://localhost:5000/v2.0'})
76
stack_name = 'test_stack'
77
tmpl = parser.Template(t)
78
stack = parser.Stack(ctx, stack_name, tmpl)
82
def mock_create_network(self):
83
self.vpc_name = utils.PhysName('test_stack', 'the_vpc')
84
quantumclient.Client.create_network(
86
'network': {'name': self.vpc_name}
87
}).AndReturn({'network': {
91
'admin_state_up': True,
93
'tenant_id': 'c1210485b2424d48804aad5d39c61b8f',
96
quantumclient.Client.show_network(
98
).AndReturn({"network": {
101
"name": self.vpc_name,
102
"admin_state_up": False,
104
"tenant_id": "c1210485b2424d48804aad5d39c61b8f",
108
quantumclient.Client.show_network(
110
).MultipleTimes().AndReturn({"network": {
113
"name": self.vpc_name,
114
"admin_state_up": False,
116
"tenant_id": "c1210485b2424d48804aad5d39c61b8f",
119
quantumclient.Client.create_router(
120
{'router': {'name': self.vpc_name}}).AndReturn({
123
'name': self.vpc_name,
124
'admin_state_up': True,
125
'tenant_id': 'c1210485b2424d48804aad5d39c61b8f',
128
quantumclient.Client.list_routers(name=self.vpc_name).AndReturn({
131
"external_gateway_info": None,
132
"name": self.vpc_name,
133
"admin_state_up": True,
134
"tenant_id": "3e21026f2dc94372b105808c0e721661",
139
self.mock_router_for_vpc()
141
def mock_create_subnet(self):
142
self.subnet_name = utils.PhysName('test_stack', 'the_subnet')
143
quantumclient.Client.create_subnet(
145
'network_id': u'aaaa',
146
'cidr': u'10.0.0.0/24',
148
'name': self.subnet_name}}).AndReturn({
151
'name': self.subnet_name,
152
'admin_state_up': True,
153
'tenant_id': 'c1210485b2424d48804aad5d39c61b8f',
155
self.mock_router_for_vpc()
156
quantumclient.Client.add_interface_router(
158
{'subnet_id': 'cccc'}).AndReturn(None)
160
def mock_show_subnet(self):
161
quantumclient.Client.show_subnet('cccc').AndReturn({
163
'name': self.subnet_name,
164
'network_id': 'aaaa',
165
'tenant_id': 'c1210485b2424d48804aad5d39c61b8f',
166
'allocation_pools': [{'start': '10.0.0.2',
167
'end': '10.0.0.254'}],
168
'gateway_ip': '10.0.0.1',
170
'cidr': '10.0.0.0/24',
172
'enable_dhcp': False,
175
def mock_create_security_group(self):
176
self.sg_name = utils.PhysName('test_stack', 'the_sg')
177
quantumclient.Client.create_security_group({
179
'name': self.sg_name,
180
'description': 'SSH access'
184
'tenant_id': 'c1210485b2424d48804aad5d39c61b8f',
185
'name': self.sg_name,
186
'description': 'SSH access',
187
'security_group_rules': [],
192
quantumclient.Client.create_security_group_rule({
193
'security_group_rule': {
194
'direction': 'ingress',
195
'remote_ip_prefix': '0.0.0.0/0',
196
'port_range_min': 22,
198
'port_range_max': 22,
200
'security_group_id': 'eeee'
203
'security_group_rule': {
204
'direction': 'ingress',
205
'remote_ip_prefix': '0.0.0.0/0',
206
'port_range_min': 22,
208
'port_range_max': 22,
210
'security_group_id': 'eeee',
215
def mock_delete_security_group(self):
216
sg_name = utils.PhysName('test_stack', 'the_sg')
217
quantumclient.Client.show_security_group('eeee').AndReturn({
219
'tenant_id': 'c1210485b2424d48804aad5d39c61b8f',
222
'security_group_rules': [{
223
'direction': 'ingress',
225
'port_range_max': 22,
228
'security_group_id': 'eeee',
229
'remote_ip_prefix': '0.0.0.0/0',
230
'tenant_id': 'c1210485b2424d48804aad5d39c61b8f',
234
quantumclient.Client.delete_security_group_rule('bbbb').AndReturn(None)
235
quantumclient.Client.delete_security_group('eeee').AndReturn(None)
237
def mock_router_for_vpc(self):
238
quantumclient.Client.list_routers(name=self.vpc_name).AndReturn({
241
"external_gateway_info": {
242
"network_id": "zzzz",
243
"enable_snat": True},
244
"name": self.vpc_name,
245
"admin_state_up": True,
246
"tenant_id": "3e21026f2dc94372b105808c0e721661",
252
def mock_delete_network(self):
253
self.mock_router_for_vpc()
254
quantumclient.Client.delete_router('bbbb').AndReturn(None)
255
quantumclient.Client.delete_network('aaaa').AndReturn(None)
257
def mock_delete_subnet(self):
258
self.mock_router_for_vpc()
259
quantumclient.Client.remove_interface_router(
261
{'subnet_id': 'cccc'}).AndReturn(None)
262
quantumclient.Client.delete_subnet('cccc').AndReturn(None)
264
def mock_create_route_table(self):
265
self.rt_name = utils.PhysName('test_stack', 'the_route_table')
266
quantumclient.Client.create_router({
267
'router': {'name': self.rt_name}}).AndReturn({
270
'name': self.rt_name,
271
'admin_state_up': True,
272
'tenant_id': 'c1210485b2424d48804aad5d39c61b8f',
276
quantumclient.Client.show_router('ffff').AndReturn({
279
'name': self.rt_name,
280
'admin_state_up': True,
281
'tenant_id': 'c1210485b2424d48804aad5d39c61b8f',
285
quantumclient.Client.show_router('ffff').AndReturn({
288
'name': self.rt_name,
289
'admin_state_up': True,
290
'tenant_id': 'c1210485b2424d48804aad5d39c61b8f',
294
self.mock_router_for_vpc()
295
quantumclient.Client.add_gateway_router(
296
'ffff', {'network_id': 'zzzz'}).AndReturn(None)
298
def mock_create_association(self):
299
self.mock_show_subnet()
300
self.mock_router_for_vpc()
301
quantumclient.Client.remove_interface_router(
303
{'subnet_id': u'cccc'}).AndReturn(None)
304
quantumclient.Client.add_interface_router(
306
{'subnet_id': 'cccc'}).AndReturn(None)
308
def mock_delete_association(self):
309
self.mock_show_subnet()
310
self.mock_router_for_vpc()
311
quantumclient.Client.remove_interface_router(
313
{'subnet_id': u'cccc'}).AndReturn(None)
314
quantumclient.Client.add_interface_router(
316
{'subnet_id': 'cccc'}).AndReturn(None)
318
def mock_delete_route_table(self):
319
quantumclient.Client.delete_router('ffff').AndReturn(None)
320
quantumclient.Client.remove_gateway_router('ffff').AndReturn(None)
322
def assertResourceState(self, resource, ref_id):
323
self.assertEqual(None, resource.validate())
324
self.assertEqual((resource.CREATE, resource.COMPLETE), resource.state)
325
self.assertEqual(ref_id, resource.FnGetRefId())
328
class VPCTest(VPCTestBase):
331
HeatTemplateFormatVersion: '2012-12-12'
335
Properties: {CidrBlock: '10.0.0.0/16'}
339
self.mock_create_network()
340
self.mock_delete_network()
343
stack = self.create_stack(self.test_template)
344
vpc = stack['the_vpc']
345
self.assertResourceState(vpc, 'aaaa')
346
self.assertRaises(resource.UpdateReplace,
347
vpc.handle_update, {}, {}, {})
349
self.assertEqual(None, vpc.delete())
353
class SubnetTest(VPCTestBase):
356
HeatTemplateFormatVersion: '2012-12-12'
360
Properties: {CidrBlock: '10.0.0.0/16'}
362
Type: AWS::EC2::Subnet
364
CidrBlock: 10.0.0.0/24
365
VpcId: {Ref: the_vpc}
366
AvailabilityZone: moon
369
def test_subnet(self):
370
self.mock_create_network()
371
self.mock_create_subnet()
372
self.mock_delete_subnet()
373
self.mock_delete_network()
375
# mock delete subnet which is already deleted
376
self.mock_router_for_vpc()
377
quantumclient.Client.remove_interface_router(
379
{'subnet_id': 'cccc'}).AndRaise(
380
QuantumClientException(status_code=404))
381
quantumclient.Client.delete_subnet('cccc').AndRaise(
382
QuantumClientException(status_code=404))
385
stack = self.create_stack(self.test_template)
387
subnet = stack['the_subnet']
388
self.assertResourceState(subnet, 'cccc')
390
self.assertRaises(resource.UpdateReplace,
391
subnet.handle_update, {}, {}, {})
393
exception.InvalidTemplateAttribute,
397
self.assertEqual('moon', subnet.FnGetAtt('AvailabilityZone'))
399
self.assertEqual(None, subnet.delete())
400
subnet.state_set(subnet.CREATE, subnet.COMPLETE, 'to delete again')
401
self.assertEqual(None, subnet.delete())
402
self.assertEqual(None, stack['the_vpc'].delete())
406
class NetworkInterfaceTest(VPCTestBase):
409
HeatTemplateFormatVersion: '2012-12-12'
412
Type: AWS::EC2::SecurityGroup
414
VpcId: {Ref: the_vpc}
415
GroupDescription: SSH access
416
SecurityGroupIngress:
423
Properties: {CidrBlock: '10.0.0.0/16'}
425
Type: AWS::EC2::Subnet
427
CidrBlock: 10.0.0.0/24
428
VpcId: {Ref: the_vpc}
429
AvailabilityZone: moon
431
Type: AWS::EC2::NetworkInterface
433
PrivateIpAddress: 10.0.0.100
434
SubnetId: {Ref: the_subnet}
439
test_template_no_groupset = '''
440
HeatTemplateFormatVersion: '2012-12-12'
444
Properties: {CidrBlock: '10.0.0.0/16'}
446
Type: AWS::EC2::Subnet
448
CidrBlock: 10.0.0.0/24
449
VpcId: {Ref: the_vpc}
450
AvailabilityZone: moon
452
Type: AWS::EC2::NetworkInterface
454
PrivateIpAddress: 10.0.0.100
455
SubnetId: {Ref: the_subnet}
458
test_template_error = '''
459
HeatTemplateFormatVersion: '2012-12-12'
462
Type: AWS::EC2::SecurityGroup
464
VpcId: {Ref: the_vpc}
465
GroupDescription: SSH access
466
SecurityGroupIngress:
473
Properties: {CidrBlock: '10.0.0.0/16'}
475
Type: AWS::EC2::Subnet
477
CidrBlock: 10.0.0.0/24
478
VpcId: {Ref: the_vpc}
479
AvailabilityZone: moon
481
Type: AWS::EC2::NetworkInterface
483
PrivateIpAddress: 10.0.0.100
484
SubnetId: {Ref: the_subnet}
486
- Ref: INVALID-REF-IN-TEMPLATE
489
test_template_error_no_ref = '''
490
HeatTemplateFormatVersion: '2012-12-12'
494
Properties: {CidrBlock: '10.0.0.0/16'}
496
Type: AWS::EC2::Subnet
498
CidrBlock: 10.0.0.0/24
499
VpcId: {Ref: the_vpc}
500
AvailabilityZone: moon
502
Type: AWS::EC2::NetworkInterface
504
PrivateIpAddress: 10.0.0.100
505
SubnetId: {Ref: the_subnet}
510
def mock_create_network_interface(self, security_groups=['eeee']):
511
self.nic_name = utils.PhysName('test_stack', 'the_nic')
512
port = {'network_id': 'aaaa',
514
'subnet_id': u'cccc',
515
'ip_address': u'10.0.0.100'
517
'name': self.nic_name,
518
'admin_state_up': True}
520
port['security_groups'] = security_groups
522
quantumclient.Client.create_port({'port': port}).AndReturn({
524
'admin_state_up': True,
529
'ip_address': '10.0.0.100',
534
'mac_address': 'fa:16:3e:25:32:5d',
535
'name': self.nic_name,
536
'network_id': 'aaaa',
538
'tenant_id': 'c1210485b2424d48804aad5d39c61b8f'
542
def mock_delete_network_interface(self):
543
quantumclient.Client.delete_port('dddd').AndReturn(None)
545
def test_network_interface(self):
546
self.mock_create_security_group()
547
self.mock_create_network()
548
self.mock_create_subnet()
549
self.mock_show_subnet()
550
self.mock_create_network_interface()
551
self.mock_delete_network_interface()
552
self.mock_delete_subnet()
553
self.mock_delete_network()
554
self.mock_delete_security_group()
558
stack = self.create_stack(self.test_template)
560
self.assertEqual((stack.CREATE, stack.COMPLETE), stack.state)
561
rsrc = stack['the_nic']
562
self.assertResourceState(rsrc, 'dddd')
564
self.assertRaises(resource.UpdateReplace,
565
rsrc.handle_update, {}, {}, {})
572
def test_network_interface_no_groupset(self):
573
self.mock_create_network()
574
self.mock_create_subnet()
575
self.mock_show_subnet()
576
self.mock_create_network_interface(security_groups=None)
577
self.mock_delete_network_interface()
578
self.mock_delete_subnet()
579
self.mock_delete_network()
583
stack = self.create_stack(self.test_template_no_groupset)
588
def test_network_interface_error(self):
589
real_exception = self.assertRaises(
590
exception.InvalidTemplateReference,
592
self.test_template_error)
593
expected_exception = exception.InvalidTemplateReference(
594
resource='INVALID-REF-IN-TEMPLATE',
597
self.assertEquals(str(expected_exception), str(real_exception))
599
def test_network_interface_error_no_ref(self):
600
self.mock_create_network()
601
self.mock_create_subnet()
602
self.mock_show_subnet()
603
self.mock_delete_subnet()
604
self.mock_delete_network()
608
stack = self.create_stack(self.test_template_error_no_ref)
610
self.assertEqual((stack.CREATE, stack.FAILED), stack.state)
611
rsrc = stack['the_nic']
612
self.assertEqual((rsrc.CREATE, rsrc.FAILED), rsrc.state)
613
reason = rsrc.status_reason
614
self.assertTrue(reason.startswith('InvalidTemplateAttribute:'))
621
class InternetGatewayTest(VPCTestBase):
624
HeatTemplateFormatVersion: '2012-12-12'
627
Type: AWS::EC2::InternetGateway
631
CidrBlock: '10.0.0.0/16'
633
Type: AWS::EC2::Subnet
635
CidrBlock: 10.0.0.0/24
636
VpcId: {Ref: the_vpc}
637
AvailabilityZone: moon
639
Type: AWS::EC2::VPCGatewayAttachment
641
VpcId: {Ref: the_vpc}
642
InternetGatewayId: {Ref: the_gateway}
644
Type: AWS::EC2::RouteTable
646
VpcId: {Ref: the_vpc}
648
Type: AWS::EC2::SubnetRouteTableAssocation
650
RouteTableId: {Ref: the_route_table}
651
SubnetId: {Ref: the_subnet}
654
def mock_create_internet_gateway(self):
655
quantumclient.Client.list_networks(
656
**{'router:external': True}).AndReturn({'networks': [{
660
'router:external': True,
661
'tenant_id': 'c1210485b2424d48804aad5d39c61b8f',
662
'admin_state_up': True,
667
def mock_create_gateway_attachment(self):
668
quantumclient.Client.add_gateway_router(
669
'ffff', {'network_id': 'eeee'}).AndReturn(None)
671
def mock_delete_gateway_attachment(self):
672
quantumclient.Client.remove_gateway_router('ffff').AndReturn(None)
674
def test_internet_gateway(self):
675
self.mock_create_internet_gateway()
676
self.mock_create_network()
677
self.mock_create_subnet()
678
self.mock_create_route_table()
679
self.mock_create_association()
680
self.mock_create_gateway_attachment()
681
self.mock_delete_gateway_attachment()
682
self.mock_delete_association()
683
self.mock_delete_route_table()
684
self.mock_delete_subnet()
685
self.mock_delete_network()
689
stack = self.create_stack(self.test_template)
691
gateway = stack['the_gateway']
692
self.assertResourceState(gateway, gateway.physical_resource_name())
693
self.assertRaises(resource.UpdateReplace, gateway.handle_update,
696
attachment = stack['the_attachment']
697
self.assertResourceState(attachment, 'the_attachment')
698
self.assertRaises(resource.UpdateReplace,
699
attachment.handle_update, {}, {}, {})
701
route_table = stack['the_route_table']
702
self.assertEqual([route_table], list(attachment._vpc_route_tables()))
708
class RouteTableTest(VPCTestBase):
711
HeatTemplateFormatVersion: '2012-12-12'
716
CidrBlock: '10.0.0.0/16'
718
Type: AWS::EC2::Subnet
720
CidrBlock: 10.0.0.0/24
721
VpcId: {Ref: the_vpc}
722
AvailabilityZone: moon
724
Type: AWS::EC2::RouteTable
726
VpcId: {Ref: the_vpc}
728
Type: AWS::EC2::SubnetRouteTableAssocation
730
RouteTableId: {Ref: the_route_table}
731
SubnetId: {Ref: the_subnet}
734
def test_route_table(self):
735
self.mock_create_network()
736
self.mock_create_subnet()
737
self.mock_create_route_table()
738
self.mock_create_association()
739
self.mock_delete_association()
740
self.mock_delete_route_table()
741
self.mock_delete_subnet()
742
self.mock_delete_network()
746
stack = self.create_stack(self.test_template)
748
route_table = stack['the_route_table']
749
self.assertResourceState(route_table, 'ffff')
751
resource.UpdateReplace,
752
route_table.handle_update, {}, {}, {})
754
association = stack['the_association']
755
self.assertResourceState(association, 'the_association')
757
resource.UpdateReplace,
758
association.handle_update, {}, {}, {})