blob: 84790135d4c7a85c54b0f693d7aa2999073c08cc [file] [log] [blame]
Yair Friedf37dae32013-09-01 15:35:14 +03001# Copyright 2013 OpenStack Foundation
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
sridhargaddam4dbbc962014-05-14 02:10:56 +053016import six
17
Yair Friedd5479822013-10-14 15:33:32 +030018from tempest.api.network import base_security_groups as base
nayna-patel1c76bc92014-01-28 09:24:16 +000019from tempest.common.utils import data_utils
sridhargaddam510f8962014-09-08 23:37:16 +053020from tempest import config
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090021from tempest import test
Yair Friedf37dae32013-09-01 15:35:14 +030022
sridhargaddam510f8962014-09-08 23:37:16 +053023CONF = config.CONF
24
Yair Friedf37dae32013-09-01 15:35:14 +030025
Yair Friedd5479822013-10-14 15:33:32 +030026class SecGroupTest(base.BaseSecGroupTest):
Andrea Frittolic0978352015-02-06 15:57:40 +000027
sridhargaddam510f8962014-09-08 23:37:16 +053028 _tenant_network_cidr = CONF.network.tenant_network_cidr
Yair Friedf37dae32013-09-01 15:35:14 +030029
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090030 @classmethod
Andrea Frittolida4a2452014-09-15 13:12:08 +010031 def resource_setup(cls):
32 super(SecGroupTest, cls).resource_setup()
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090033 if not test.is_extension_enabled('security-group', 'network'):
34 msg = "security-group extension not enabled."
35 raise cls.skipException(msg)
36
sridhargaddam510f8962014-09-08 23:37:16 +053037 def _create_verify_security_group_rule(self, sg_id, direction,
38 ethertype, protocol,
39 port_range_min,
40 port_range_max,
41 remote_group_id=None,
42 remote_ip_prefix=None):
43 # Create Security Group rule with the input params and validate
44 # that SG rule is created with the same parameters.
David Kranz34e88122014-12-11 15:24:05 -050045 rule_create_body = self.client.create_security_group_rule(
sridhargaddam510f8962014-09-08 23:37:16 +053046 security_group_id=sg_id,
47 direction=direction,
48 ethertype=ethertype,
49 protocol=protocol,
50 port_range_min=port_range_min,
51 port_range_max=port_range_max,
52 remote_group_id=remote_group_id,
53 remote_ip_prefix=remote_ip_prefix
54 )
55
56 sec_group_rule = rule_create_body['security_group_rule']
57 self.addCleanup(self._delete_security_group_rule,
58 sec_group_rule['id'])
59
60 expected = {'direction': direction, 'protocol': protocol,
61 'ethertype': ethertype, 'port_range_min': port_range_min,
62 'port_range_max': port_range_max,
63 'remote_group_id': remote_group_id,
64 'remote_ip_prefix': remote_ip_prefix}
65 for key, value in six.iteritems(expected):
66 self.assertEqual(value, sec_group_rule[key],
67 "Field %s of the created security group "
68 "rule does not match with %s." %
69 (key, value))
70
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090071 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -080072 @test.idempotent_id('e30abd17-fef9-4739-8617-dc26da88e686')
Yair Friedf37dae32013-09-01 15:35:14 +030073 def test_list_security_groups(self):
74 # Verify the that security group belonging to tenant exist in list
David Kranz34e88122014-12-11 15:24:05 -050075 body = self.client.list_security_groups()
Yair Friedf37dae32013-09-01 15:35:14 +030076 security_groups = body['security_groups']
77 found = None
78 for n in security_groups:
79 if (n['name'] == 'default'):
80 found = n['id']
81 msg = "Security-group list doesn't contain default security-group"
82 self.assertIsNotNone(found, msg)
83
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090084 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -080085 @test.idempotent_id('bfd128e5-3c92-44b6-9d66-7fe29d22c802')
nayna-patel1c76bc92014-01-28 09:24:16 +000086 def test_create_list_update_show_delete_security_group(self):
Yair Friedd5479822013-10-14 15:33:32 +030087 group_create_body, name = self._create_security_group()
Yair Friedf37dae32013-09-01 15:35:14 +030088
Yair Friedf37dae32013-09-01 15:35:14 +030089 # List security groups and verify if created group is there in response
David Kranz34e88122014-12-11 15:24:05 -050090 list_body = self.client.list_security_groups()
Yair Friedf37dae32013-09-01 15:35:14 +030091 secgroup_list = list()
92 for secgroup in list_body['security_groups']:
93 secgroup_list.append(secgroup['id'])
94 self.assertIn(group_create_body['security_group']['id'], secgroup_list)
nayna-patel1c76bc92014-01-28 09:24:16 +000095 # Update the security group
96 new_name = data_utils.rand_name('security-')
97 new_description = data_utils.rand_name('security-description')
David Kranz34e88122014-12-11 15:24:05 -050098 update_body = self.client.update_security_group(
nayna-patel1c76bc92014-01-28 09:24:16 +000099 group_create_body['security_group']['id'],
100 name=new_name,
101 description=new_description)
102 # Verify if security group is updated
nayna-patel1c76bc92014-01-28 09:24:16 +0000103 self.assertEqual(update_body['security_group']['name'], new_name)
104 self.assertEqual(update_body['security_group']['description'],
105 new_description)
106 # Show details of the updated security group
David Kranz34e88122014-12-11 15:24:05 -0500107 show_body = self.client.show_security_group(
nayna-patel1c76bc92014-01-28 09:24:16 +0000108 group_create_body['security_group']['id'])
109 self.assertEqual(show_body['security_group']['name'], new_name)
110 self.assertEqual(show_body['security_group']['description'],
111 new_description)
Yair Friedbcdcb3b2013-10-11 09:08:15 +0300112
Yoshihiro Kaneko05670262014-01-18 19:22:44 +0900113 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800114 @test.idempotent_id('cfb99e0e-7410-4a3d-8a0c-959a63ee77e9')
Yair Friedbcdcb3b2013-10-11 09:08:15 +0300115 def test_create_show_delete_security_group_rule(self):
Yair Friedd5479822013-10-14 15:33:32 +0300116 group_create_body, _ = self._create_security_group()
Yair Friedbcdcb3b2013-10-11 09:08:15 +0300117
118 # Create rules for each protocol
119 protocols = ['tcp', 'udp', 'icmp']
120 for protocol in protocols:
David Kranz34e88122014-12-11 15:24:05 -0500121 rule_create_body = self.client.create_security_group_rule(
nayna-patel3e361372014-01-29 10:25:41 +0000122 security_group_id=group_create_body['security_group']['id'],
123 protocol=protocol,
sridhargaddam510f8962014-09-08 23:37:16 +0530124 direction='ingress',
125 ethertype=self.ethertype
Yair Friedbcdcb3b2013-10-11 09:08:15 +0300126 )
Yair Friedbcdcb3b2013-10-11 09:08:15 +0300127
sridhargaddam4dbbc962014-05-14 02:10:56 +0530128 # Show details of the created security rule
David Kranz34e88122014-12-11 15:24:05 -0500129 show_rule_body = self.client.show_security_group_rule(
sridhargaddam4dbbc962014-05-14 02:10:56 +0530130 rule_create_body['security_group_rule']['id']
131 )
sridhargaddam4dbbc962014-05-14 02:10:56 +0530132 create_dict = rule_create_body['security_group_rule']
133 for key, value in six.iteritems(create_dict):
134 self.assertEqual(value,
135 show_rule_body['security_group_rule'][key],
136 "%s does not match." % key)
Yair Friedf37dae32013-09-01 15:35:14 +0300137
sridhargaddam4dbbc962014-05-14 02:10:56 +0530138 # List rules and verify created rule is in response
David Kranz34e88122014-12-11 15:24:05 -0500139 rule_list_body = self.client.list_security_group_rules()
sridhargaddam4dbbc962014-05-14 02:10:56 +0530140 rule_list = [rule['id']
141 for rule in rule_list_body['security_group_rules']]
142 self.assertIn(rule_create_body['security_group_rule']['id'],
143 rule_list)
Yair Friedf37dae32013-09-01 15:35:14 +0300144
Yoshihiro Kaneko05670262014-01-18 19:22:44 +0900145 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800146 @test.idempotent_id('87dfbcf9-1849-43ea-b1e4-efa3eeae9f71')
jun xiee31dbe92014-01-13 18:10:37 +0800147 def test_create_security_group_rule_with_additional_args(self):
sridhargaddam510f8962014-09-08 23:37:16 +0530148 """Verify security group rule with additional arguments works.
jun xiee31dbe92014-01-13 18:10:37 +0800149
sridhargaddam510f8962014-09-08 23:37:16 +0530150 direction:ingress, ethertype:[IPv4/IPv6],
151 protocol:tcp, port_range_min:77, port_range_max:77
152 """
153 group_create_body, _ = self._create_security_group()
154 sg_id = group_create_body['security_group']['id']
jun xiee31dbe92014-01-13 18:10:37 +0800155 direction = 'ingress'
156 protocol = 'tcp'
157 port_range_min = 77
158 port_range_max = 77
sridhargaddam510f8962014-09-08 23:37:16 +0530159 self._create_verify_security_group_rule(sg_id, direction,
160 self.ethertype, protocol,
161 port_range_min,
162 port_range_max)
jun xiee31dbe92014-01-13 18:10:37 +0800163
sridhargaddam510f8962014-09-08 23:37:16 +0530164 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800165 @test.idempotent_id('c9463db8-b44d-4f52-b6c0-8dbda99f26ce')
sridhargaddam510f8962014-09-08 23:37:16 +0530166 def test_create_security_group_rule_with_icmp_type_code(self):
167 """Verify security group rule for icmp protocol works.
jun xiee31dbe92014-01-13 18:10:37 +0800168
sridhargaddam510f8962014-09-08 23:37:16 +0530169 Specify icmp type (port_range_min) and icmp code
Tingting Bao2b513342015-02-15 22:54:55 -0800170 (port_range_max) with different values. A separate testcase
sridhargaddam510f8962014-09-08 23:37:16 +0530171 is added for icmp protocol as icmp validation would be
172 different from tcp/udp.
173 """
174 group_create_body, _ = self._create_security_group()
175
176 sg_id = group_create_body['security_group']['id']
177 direction = 'ingress'
178 protocol = 'icmp'
179 icmp_type_codes = [(3, 2), (2, 3), (3, 0), (2, None)]
180 for icmp_type, icmp_code in icmp_type_codes:
181 self._create_verify_security_group_rule(sg_id, direction,
182 self.ethertype, protocol,
183 icmp_type, icmp_code)
184
185 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800186 @test.idempotent_id('c2ed2deb-7a0c-44d8-8b4c-a5825b5c310b')
sridhargaddam510f8962014-09-08 23:37:16 +0530187 def test_create_security_group_rule_with_remote_group_id(self):
188 # Verify creating security group rule with remote_group_id works
189 sg1_body, _ = self._create_security_group()
190 sg2_body, _ = self._create_security_group()
191
192 sg_id = sg1_body['security_group']['id']
193 direction = 'ingress'
194 protocol = 'udp'
195 port_range_min = 50
196 port_range_max = 55
197 remote_id = sg2_body['security_group']['id']
198 self._create_verify_security_group_rule(sg_id, direction,
199 self.ethertype, protocol,
200 port_range_min,
201 port_range_max,
202 remote_group_id=remote_id)
203
204 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800205 @test.idempotent_id('16459776-5da2-4634-bce4-4b55ee3ec188')
sridhargaddam510f8962014-09-08 23:37:16 +0530206 def test_create_security_group_rule_with_remote_ip_prefix(self):
207 # Verify creating security group rule with remote_ip_prefix works
208 sg1_body, _ = self._create_security_group()
209
210 sg_id = sg1_body['security_group']['id']
211 direction = 'ingress'
212 protocol = 'tcp'
213 port_range_min = 76
214 port_range_max = 77
215 ip_prefix = self._tenant_network_cidr
216 self._create_verify_security_group_rule(sg_id, direction,
217 self.ethertype, protocol,
218 port_range_min,
219 port_range_max,
220 remote_ip_prefix=ip_prefix)
jun xiee31dbe92014-01-13 18:10:37 +0800221
Ashish Gupta1d3712f2014-07-17 04:10:43 -0700222 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800223 @test.idempotent_id('0a307599-6655-4220-bebc-fd70c64f2290')
Ashish Gupta1d3712f2014-07-17 04:10:43 -0700224 def test_create_security_group_rule_with_protocol_integer_value(self):
225 # Verify creating security group rule with the
226 # protocol as integer value
227 # arguments : "protocol": 17
228 group_create_body, _ = self._create_security_group()
229 direction = 'ingress'
230 protocol = 17
231 security_group_id = group_create_body['security_group']['id']
David Kranz34e88122014-12-11 15:24:05 -0500232 rule_create_body = self.client.create_security_group_rule(
Ashish Gupta1d3712f2014-07-17 04:10:43 -0700233 security_group_id=security_group_id,
234 direction=direction,
235 protocol=protocol
236 )
237 sec_group_rule = rule_create_body['security_group_rule']
238 self.assertEqual(sec_group_rule['direction'], direction)
239 self.assertEqual(int(sec_group_rule['protocol']), protocol)
240
Yair Friedf37dae32013-09-01 15:35:14 +0300241
sridhargaddam510f8962014-09-08 23:37:16 +0530242class SecGroupIPv6Test(SecGroupTest):
243 _ip_version = 6
244 _tenant_network_cidr = CONF.network.tenant_network_v6_cidr