blob: a1218644da0d2d1811f95531256d7404eeb5e1ed [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
Yair Friedd5479822013-10-14 15:33:32 +030016from tempest.api.network import base_security_groups as base
sridhargaddam510f8962014-09-08 23:37:16 +053017from tempest import config
Ken'ichi Ohmichif50e4df2017-03-10 10:52:53 -080018from tempest.lib.common.utils import data_utils
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -080019from tempest.lib import decorators
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090020from tempest import test
Yair Friedf37dae32013-09-01 15:35:14 +030021
sridhargaddam510f8962014-09-08 23:37:16 +053022CONF = config.CONF
23
Yair Friedf37dae32013-09-01 15:35:14 +030024
Yair Friedd5479822013-10-14 15:33:32 +030025class SecGroupTest(base.BaseSecGroupTest):
Sean Dagueed6e5862016-04-04 10:49:13 -040026 _project_network_cidr = CONF.network.project_network_cidr
Yair Friedf37dae32013-09-01 15:35:14 +030027
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090028 @classmethod
Rohan Kanadea565e452015-01-27 14:00:13 +053029 def skip_checks(cls):
30 super(SecGroupTest, cls).skip_checks()
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090031 if not test.is_extension_enabled('security-group', 'network'):
32 msg = "security-group extension not enabled."
33 raise cls.skipException(msg)
34
sridhargaddam510f8962014-09-08 23:37:16 +053035 def _create_verify_security_group_rule(self, sg_id, direction,
36 ethertype, protocol,
37 port_range_min,
38 port_range_max,
39 remote_group_id=None,
40 remote_ip_prefix=None):
41 # Create Security Group rule with the input params and validate
42 # that SG rule is created with the same parameters.
John Warren456d9ae2016-01-12 15:36:33 -050043 sec_group_rules_client = self.security_group_rules_client
44 rule_create_body = sec_group_rules_client.create_security_group_rule(
sridhargaddam510f8962014-09-08 23:37:16 +053045 security_group_id=sg_id,
46 direction=direction,
47 ethertype=ethertype,
48 protocol=protocol,
49 port_range_min=port_range_min,
50 port_range_max=port_range_max,
51 remote_group_id=remote_group_id,
52 remote_ip_prefix=remote_ip_prefix
53 )
54
55 sec_group_rule = rule_create_body['security_group_rule']
56 self.addCleanup(self._delete_security_group_rule,
57 sec_group_rule['id'])
58
59 expected = {'direction': direction, 'protocol': protocol,
60 'ethertype': ethertype, 'port_range_min': port_range_min,
61 'port_range_max': port_range_max,
62 'remote_group_id': remote_group_id,
63 'remote_ip_prefix': remote_ip_prefix}
guo yunxian7bbbec12016-08-21 20:03:10 +080064 for key, value in expected.items():
sridhargaddam510f8962014-09-08 23:37:16 +053065 self.assertEqual(value, sec_group_rule[key],
66 "Field %s of the created security group "
67 "rule does not match with %s." %
68 (key, value))
69
Jordan Pittier3b46d272017-04-12 16:17:28 +020070 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -080071 @decorators.idempotent_id('e30abd17-fef9-4739-8617-dc26da88e686')
Yair Friedf37dae32013-09-01 15:35:14 +030072 def test_list_security_groups(self):
melissamlca689db2016-10-18 10:37:38 +080073 # Verify the security group belonging to project exist in list
John Warrenf9606e92015-12-10 12:12:42 -050074 body = self.security_groups_client.list_security_groups()
Yair Friedf37dae32013-09-01 15:35:14 +030075 security_groups = body['security_groups']
76 found = None
77 for n in security_groups:
78 if (n['name'] == 'default'):
79 found = n['id']
80 msg = "Security-group list doesn't contain default security-group"
81 self.assertIsNotNone(found, msg)
82
Jordan Pittier3b46d272017-04-12 16:17:28 +020083 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -080084 @decorators.idempotent_id('bfd128e5-3c92-44b6-9d66-7fe29d22c802')
nayna-patel1c76bc92014-01-28 09:24:16 +000085 def test_create_list_update_show_delete_security_group(self):
Ferenc Horváthe52bee72017-06-14 15:02:23 +020086 group_create_body, _ = self._create_security_group()
Yair Friedf37dae32013-09-01 15:35:14 +030087
Yair Friedf37dae32013-09-01 15:35:14 +030088 # List security groups and verify if created group is there in response
John Warrenf9606e92015-12-10 12:12:42 -050089 list_body = self.security_groups_client.list_security_groups()
Yair Friedf37dae32013-09-01 15:35:14 +030090 secgroup_list = list()
91 for secgroup in list_body['security_groups']:
92 secgroup_list.append(secgroup['id'])
93 self.assertIn(group_create_body['security_group']['id'], secgroup_list)
nayna-patel1c76bc92014-01-28 09:24:16 +000094 # Update the security group
95 new_name = data_utils.rand_name('security-')
96 new_description = data_utils.rand_name('security-description')
John Warrenf9606e92015-12-10 12:12:42 -050097 update_body = self.security_groups_client.update_security_group(
nayna-patel1c76bc92014-01-28 09:24:16 +000098 group_create_body['security_group']['id'],
99 name=new_name,
100 description=new_description)
101 # Verify if security group is updated
nayna-patel1c76bc92014-01-28 09:24:16 +0000102 self.assertEqual(update_body['security_group']['name'], new_name)
103 self.assertEqual(update_body['security_group']['description'],
104 new_description)
105 # Show details of the updated security group
John Warrenf9606e92015-12-10 12:12:42 -0500106 show_body = self.security_groups_client.show_security_group(
nayna-patel1c76bc92014-01-28 09:24:16 +0000107 group_create_body['security_group']['id'])
108 self.assertEqual(show_body['security_group']['name'], new_name)
109 self.assertEqual(show_body['security_group']['description'],
110 new_description)
Yair Friedbcdcb3b2013-10-11 09:08:15 +0300111
Jordan Pittier3b46d272017-04-12 16:17:28 +0200112 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800113 @decorators.idempotent_id('cfb99e0e-7410-4a3d-8a0c-959a63ee77e9')
Yair Friedbcdcb3b2013-10-11 09:08:15 +0300114 def test_create_show_delete_security_group_rule(self):
Yair Friedd5479822013-10-14 15:33:32 +0300115 group_create_body, _ = self._create_security_group()
Yair Friedbcdcb3b2013-10-11 09:08:15 +0300116
117 # Create rules for each protocol
118 protocols = ['tcp', 'udp', 'icmp']
John Warren456d9ae2016-01-12 15:36:33 -0500119 client = self.security_group_rules_client
Yair Friedbcdcb3b2013-10-11 09:08:15 +0300120 for protocol in protocols:
John Warren456d9ae2016-01-12 15:36:33 -0500121 rule_create_body = 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
John Warren456d9ae2016-01-12 15:36:33 -0500129 show_rule_body = 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']
guo yunxian7bbbec12016-08-21 20:03:10 +0800133 for key, value in create_dict.items():
sridhargaddam4dbbc962014-05-14 02:10:56 +0530134 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
John Warren456d9ae2016-01-12 15:36:33 -0500139 rule_list_body = (
140 self.security_group_rules_client.list_security_group_rules())
sridhargaddam4dbbc962014-05-14 02:10:56 +0530141 rule_list = [rule['id']
142 for rule in rule_list_body['security_group_rules']]
143 self.assertIn(rule_create_body['security_group_rule']['id'],
144 rule_list)
Yair Friedf37dae32013-09-01 15:35:14 +0300145
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800146 @decorators.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
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800164 @decorators.idempotent_id('c9463db8-b44d-4f52-b6c0-8dbda99f26ce')
sridhargaddam510f8962014-09-08 23:37:16 +0530165 def test_create_security_group_rule_with_icmp_type_code(self):
166 """Verify security group rule for icmp protocol works.
jun xiee31dbe92014-01-13 18:10:37 +0800167
sridhargaddam510f8962014-09-08 23:37:16 +0530168 Specify icmp type (port_range_min) and icmp code
Tingting Bao2b513342015-02-15 22:54:55 -0800169 (port_range_max) with different values. A separate testcase
sridhargaddam510f8962014-09-08 23:37:16 +0530170 is added for icmp protocol as icmp validation would be
171 different from tcp/udp.
172 """
173 group_create_body, _ = self._create_security_group()
174
175 sg_id = group_create_body['security_group']['id']
176 direction = 'ingress'
177 protocol = 'icmp'
Tong Liuec20aeb2015-02-25 00:14:35 +0000178 icmp_type_codes = [(3, 2), (3, 0), (8, 0), (0, 0), (11, None)]
sridhargaddam510f8962014-09-08 23:37:16 +0530179 for icmp_type, icmp_code in icmp_type_codes:
180 self._create_verify_security_group_rule(sg_id, direction,
181 self.ethertype, protocol,
182 icmp_type, icmp_code)
183
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800184 @decorators.idempotent_id('c2ed2deb-7a0c-44d8-8b4c-a5825b5c310b')
sridhargaddam510f8962014-09-08 23:37:16 +0530185 def test_create_security_group_rule_with_remote_group_id(self):
186 # Verify creating security group rule with remote_group_id works
187 sg1_body, _ = self._create_security_group()
188 sg2_body, _ = self._create_security_group()
189
190 sg_id = sg1_body['security_group']['id']
191 direction = 'ingress'
192 protocol = 'udp'
193 port_range_min = 50
194 port_range_max = 55
195 remote_id = sg2_body['security_group']['id']
196 self._create_verify_security_group_rule(sg_id, direction,
197 self.ethertype, protocol,
198 port_range_min,
199 port_range_max,
200 remote_group_id=remote_id)
201
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800202 @decorators.idempotent_id('16459776-5da2-4634-bce4-4b55ee3ec188')
sridhargaddam510f8962014-09-08 23:37:16 +0530203 def test_create_security_group_rule_with_remote_ip_prefix(self):
204 # Verify creating security group rule with remote_ip_prefix works
205 sg1_body, _ = self._create_security_group()
206
207 sg_id = sg1_body['security_group']['id']
208 direction = 'ingress'
209 protocol = 'tcp'
210 port_range_min = 76
211 port_range_max = 77
Sean Dagueed6e5862016-04-04 10:49:13 -0400212 ip_prefix = self._project_network_cidr
sridhargaddam510f8962014-09-08 23:37:16 +0530213 self._create_verify_security_group_rule(sg_id, direction,
214 self.ethertype, protocol,
215 port_range_min,
216 port_range_max,
217 remote_ip_prefix=ip_prefix)
jun xiee31dbe92014-01-13 18:10:37 +0800218
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800219 @decorators.idempotent_id('0a307599-6655-4220-bebc-fd70c64f2290')
Ashish Gupta1d3712f2014-07-17 04:10:43 -0700220 def test_create_security_group_rule_with_protocol_integer_value(self):
221 # Verify creating security group rule with the
222 # protocol as integer value
223 # arguments : "protocol": 17
224 group_create_body, _ = self._create_security_group()
225 direction = 'ingress'
226 protocol = 17
227 security_group_id = group_create_body['security_group']['id']
John Warren456d9ae2016-01-12 15:36:33 -0500228 client = self.security_group_rules_client
229 rule_create_body = client.create_security_group_rule(
Ashish Gupta1d3712f2014-07-17 04:10:43 -0700230 security_group_id=security_group_id,
231 direction=direction,
232 protocol=protocol
233 )
234 sec_group_rule = rule_create_body['security_group_rule']
235 self.assertEqual(sec_group_rule['direction'], direction)
236 self.assertEqual(int(sec_group_rule['protocol']), protocol)
237
Yair Friedf37dae32013-09-01 15:35:14 +0300238
sridhargaddam510f8962014-09-08 23:37:16 +0530239class SecGroupIPv6Test(SecGroupTest):
240 _ip_version = 6
Sean Dagueed6e5862016-04-04 10:49:13 -0400241 _project_network_cidr = CONF.network.project_network_v6_cidr