blob: ef19122c90d6222a13f97431a72326c40b64b20e [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
Andrea Frittolicd368412017-08-14 21:37:56 +010017from tempest.common import utils
Ken'ichi Ohmichif50e4df2017-03-10 10:52:53 -080018from tempest.lib.common.utils import data_utils
Slawek Kaplonski748dd8d2019-04-16 23:38:35 +020019from tempest.lib.common.utils import test_utils
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -080020from tempest.lib import decorators
Yair Friedf37dae32013-09-01 15:35:14 +030021
22
Yair Friedd5479822013-10-14 15:33:32 +030023class SecGroupTest(base.BaseSecGroupTest):
Yair Friedf37dae32013-09-01 15:35:14 +030024
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090025 @classmethod
Rohan Kanadea565e452015-01-27 14:00:13 +053026 def skip_checks(cls):
27 super(SecGroupTest, cls).skip_checks()
Andrea Frittolicd368412017-08-14 21:37:56 +010028 if not utils.is_extension_enabled('security-group', 'network'):
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090029 msg = "security-group extension not enabled."
30 raise cls.skipException(msg)
31
sridhargaddam510f8962014-09-08 23:37:16 +053032 def _create_verify_security_group_rule(self, sg_id, direction,
33 ethertype, protocol,
34 port_range_min,
35 port_range_max,
36 remote_group_id=None,
37 remote_ip_prefix=None):
38 # Create Security Group rule with the input params and validate
39 # that SG rule is created with the same parameters.
John Warren456d9ae2016-01-12 15:36:33 -050040 sec_group_rules_client = self.security_group_rules_client
41 rule_create_body = sec_group_rules_client.create_security_group_rule(
sridhargaddam510f8962014-09-08 23:37:16 +053042 security_group_id=sg_id,
43 direction=direction,
44 ethertype=ethertype,
45 protocol=protocol,
46 port_range_min=port_range_min,
47 port_range_max=port_range_max,
48 remote_group_id=remote_group_id,
49 remote_ip_prefix=remote_ip_prefix
50 )
51
52 sec_group_rule = rule_create_body['security_group_rule']
Slawek Kaplonski748dd8d2019-04-16 23:38:35 +020053 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
54 self._delete_security_group_rule, sec_group_rule['id'])
sridhargaddam510f8962014-09-08 23:37:16 +053055
56 expected = {'direction': direction, 'protocol': protocol,
57 'ethertype': ethertype, 'port_range_min': port_range_min,
58 'port_range_max': port_range_max,
59 'remote_group_id': remote_group_id,
60 'remote_ip_prefix': remote_ip_prefix}
guo yunxian7bbbec12016-08-21 20:03:10 +080061 for key, value in expected.items():
sridhargaddam510f8962014-09-08 23:37:16 +053062 self.assertEqual(value, sec_group_rule[key],
63 "Field %s of the created security group "
64 "rule does not match with %s." %
65 (key, value))
66
Jordan Pittier3b46d272017-04-12 16:17:28 +020067 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -080068 @decorators.idempotent_id('e30abd17-fef9-4739-8617-dc26da88e686')
Yair Friedf37dae32013-09-01 15:35:14 +030069 def test_list_security_groups(self):
melissamlca689db2016-10-18 10:37:38 +080070 # Verify the security group belonging to project exist in list
John Warrenf9606e92015-12-10 12:12:42 -050071 body = self.security_groups_client.list_security_groups()
Yair Friedf37dae32013-09-01 15:35:14 +030072 security_groups = body['security_groups']
73 found = None
74 for n in security_groups:
75 if (n['name'] == 'default'):
76 found = n['id']
77 msg = "Security-group list doesn't contain default security-group"
78 self.assertIsNotNone(found, msg)
79
Jordan Pittier3b46d272017-04-12 16:17:28 +020080 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -080081 @decorators.idempotent_id('bfd128e5-3c92-44b6-9d66-7fe29d22c802')
nayna-patel1c76bc92014-01-28 09:24:16 +000082 def test_create_list_update_show_delete_security_group(self):
Ferenc Horváthe52bee72017-06-14 15:02:23 +020083 group_create_body, _ = self._create_security_group()
Yair Friedf37dae32013-09-01 15:35:14 +030084
Yair Friedf37dae32013-09-01 15:35:14 +030085 # List security groups and verify if created group is there in response
John Warrenf9606e92015-12-10 12:12:42 -050086 list_body = self.security_groups_client.list_security_groups()
Yair Friedf37dae32013-09-01 15:35:14 +030087 secgroup_list = list()
88 for secgroup in list_body['security_groups']:
89 secgroup_list.append(secgroup['id'])
90 self.assertIn(group_create_body['security_group']['id'], secgroup_list)
nayna-patel1c76bc92014-01-28 09:24:16 +000091 # Update the security group
92 new_name = data_utils.rand_name('security-')
93 new_description = data_utils.rand_name('security-description')
John Warrenf9606e92015-12-10 12:12:42 -050094 update_body = self.security_groups_client.update_security_group(
nayna-patel1c76bc92014-01-28 09:24:16 +000095 group_create_body['security_group']['id'],
96 name=new_name,
97 description=new_description)
98 # Verify if security group is updated
nayna-patel1c76bc92014-01-28 09:24:16 +000099 self.assertEqual(update_body['security_group']['name'], new_name)
100 self.assertEqual(update_body['security_group']['description'],
101 new_description)
102 # Show details of the updated security group
John Warrenf9606e92015-12-10 12:12:42 -0500103 show_body = self.security_groups_client.show_security_group(
nayna-patel1c76bc92014-01-28 09:24:16 +0000104 group_create_body['security_group']['id'])
105 self.assertEqual(show_body['security_group']['name'], new_name)
106 self.assertEqual(show_body['security_group']['description'],
107 new_description)
Slawek Kaplonski748dd8d2019-04-16 23:38:35 +0200108 # Delete security group
109 self._delete_security_group(group_create_body['security_group']['id'])
Yair Friedbcdcb3b2013-10-11 09:08:15 +0300110
Jordan Pittier3b46d272017-04-12 16:17:28 +0200111 @decorators.attr(type='smoke')
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800112 @decorators.idempotent_id('cfb99e0e-7410-4a3d-8a0c-959a63ee77e9')
Yair Friedbcdcb3b2013-10-11 09:08:15 +0300113 def test_create_show_delete_security_group_rule(self):
Yair Friedd5479822013-10-14 15:33:32 +0300114 group_create_body, _ = self._create_security_group()
Yair Friedbcdcb3b2013-10-11 09:08:15 +0300115
116 # Create rules for each protocol
117 protocols = ['tcp', 'udp', 'icmp']
John Warren456d9ae2016-01-12 15:36:33 -0500118 client = self.security_group_rules_client
Yair Friedbcdcb3b2013-10-11 09:08:15 +0300119 for protocol in protocols:
John Warren456d9ae2016-01-12 15:36:33 -0500120 rule_create_body = client.create_security_group_rule(
nayna-patel3e361372014-01-29 10:25:41 +0000121 security_group_id=group_create_body['security_group']['id'],
122 protocol=protocol,
sridhargaddam510f8962014-09-08 23:37:16 +0530123 direction='ingress',
124 ethertype=self.ethertype
Yair Friedbcdcb3b2013-10-11 09:08:15 +0300125 )
Yair Friedbcdcb3b2013-10-11 09:08:15 +0300126
sridhargaddam4dbbc962014-05-14 02:10:56 +0530127 # Show details of the created security rule
John Warren456d9ae2016-01-12 15:36:33 -0500128 show_rule_body = client.show_security_group_rule(
sridhargaddam4dbbc962014-05-14 02:10:56 +0530129 rule_create_body['security_group_rule']['id']
130 )
sridhargaddam4dbbc962014-05-14 02:10:56 +0530131 create_dict = rule_create_body['security_group_rule']
guo yunxian7bbbec12016-08-21 20:03:10 +0800132 for key, value in create_dict.items():
sridhargaddam4dbbc962014-05-14 02:10:56 +0530133 self.assertEqual(value,
134 show_rule_body['security_group_rule'][key],
135 "%s does not match." % key)
Yair Friedf37dae32013-09-01 15:35:14 +0300136
sridhargaddam4dbbc962014-05-14 02:10:56 +0530137 # List rules and verify created rule is in response
John Warren456d9ae2016-01-12 15:36:33 -0500138 rule_list_body = (
139 self.security_group_rules_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)
Slawek Kaplonski748dd8d2019-04-16 23:38:35 +0200144 self._delete_security_group_rule(
145 rule_create_body['security_group_rule']['id'])
Yair Friedf37dae32013-09-01 15:35:14 +0300146
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800147 @decorators.idempotent_id('87dfbcf9-1849-43ea-b1e4-efa3eeae9f71')
jun xiee31dbe92014-01-13 18:10:37 +0800148 def test_create_security_group_rule_with_additional_args(self):
sridhargaddam510f8962014-09-08 23:37:16 +0530149 """Verify security group rule with additional arguments works.
jun xiee31dbe92014-01-13 18:10:37 +0800150
sridhargaddam510f8962014-09-08 23:37:16 +0530151 direction:ingress, ethertype:[IPv4/IPv6],
152 protocol:tcp, port_range_min:77, port_range_max:77
153 """
154 group_create_body, _ = self._create_security_group()
155 sg_id = group_create_body['security_group']['id']
jun xiee31dbe92014-01-13 18:10:37 +0800156 direction = 'ingress'
157 protocol = 'tcp'
158 port_range_min = 77
159 port_range_max = 77
sridhargaddam510f8962014-09-08 23:37:16 +0530160 self._create_verify_security_group_rule(sg_id, direction,
161 self.ethertype, protocol,
162 port_range_min,
163 port_range_max)
jun xiee31dbe92014-01-13 18:10:37 +0800164
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800165 @decorators.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'
Brian Haley13731b02019-05-21 09:28:36 -0400178 # The Neutron API accepts 'icmp', 'icmpv6' and 'ipv6-icmp' for
179 # IPv6 ICMP protocol names, but the latter is preferred and the
180 # others considered "legacy". Use 'ipv6-icmp' as the API could
181 # change to return only that value, see
182 # https://review.opendev.org/#/c/453346/
183 # The neutron-tempest-plugin API tests pass all three and verify
184 # the output, so there is no need to duplicate that here.
185 protocol = 'ipv6-icmp' if self._ip_version == 6 else 'icmp'
Tong Liuec20aeb2015-02-25 00:14:35 +0000186 icmp_type_codes = [(3, 2), (3, 0), (8, 0), (0, 0), (11, None)]
sridhargaddam510f8962014-09-08 23:37:16 +0530187 for icmp_type, icmp_code in icmp_type_codes:
188 self._create_verify_security_group_rule(sg_id, direction,
189 self.ethertype, protocol,
190 icmp_type, icmp_code)
191
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800192 @decorators.idempotent_id('c2ed2deb-7a0c-44d8-8b4c-a5825b5c310b')
sridhargaddam510f8962014-09-08 23:37:16 +0530193 def test_create_security_group_rule_with_remote_group_id(self):
194 # Verify creating security group rule with remote_group_id works
195 sg1_body, _ = self._create_security_group()
196 sg2_body, _ = self._create_security_group()
197
198 sg_id = sg1_body['security_group']['id']
199 direction = 'ingress'
200 protocol = 'udp'
201 port_range_min = 50
202 port_range_max = 55
203 remote_id = sg2_body['security_group']['id']
204 self._create_verify_security_group_rule(sg_id, direction,
205 self.ethertype, protocol,
206 port_range_min,
207 port_range_max,
208 remote_group_id=remote_id)
209
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800210 @decorators.idempotent_id('16459776-5da2-4634-bce4-4b55ee3ec188')
sridhargaddam510f8962014-09-08 23:37:16 +0530211 def test_create_security_group_rule_with_remote_ip_prefix(self):
212 # Verify creating security group rule with remote_ip_prefix works
213 sg1_body, _ = self._create_security_group()
214
215 sg_id = sg1_body['security_group']['id']
216 direction = 'ingress'
217 protocol = 'tcp'
218 port_range_min = 76
219 port_range_max = 77
zhufl5d65cd72017-09-28 16:53:00 +0800220 ip_prefix = str(self.cidr)
sridhargaddam510f8962014-09-08 23:37:16 +0530221 self._create_verify_security_group_rule(sg_id, direction,
222 self.ethertype, protocol,
223 port_range_min,
224 port_range_max,
225 remote_ip_prefix=ip_prefix)
jun xiee31dbe92014-01-13 18:10:37 +0800226
Ken'ichi Ohmichi53b9a632017-01-27 18:04:39 -0800227 @decorators.idempotent_id('0a307599-6655-4220-bebc-fd70c64f2290')
Ashish Gupta1d3712f2014-07-17 04:10:43 -0700228 def test_create_security_group_rule_with_protocol_integer_value(self):
229 # Verify creating security group rule with the
230 # protocol as integer value
231 # arguments : "protocol": 17
232 group_create_body, _ = self._create_security_group()
233 direction = 'ingress'
234 protocol = 17
235 security_group_id = group_create_body['security_group']['id']
John Warren456d9ae2016-01-12 15:36:33 -0500236 client = self.security_group_rules_client
237 rule_create_body = client.create_security_group_rule(
Ashish Gupta1d3712f2014-07-17 04:10:43 -0700238 security_group_id=security_group_id,
239 direction=direction,
240 protocol=protocol
241 )
242 sec_group_rule = rule_create_body['security_group_rule']
243 self.assertEqual(sec_group_rule['direction'], direction)
244 self.assertEqual(int(sec_group_rule['protocol']), protocol)
245
Yair Friedf37dae32013-09-01 15:35:14 +0300246
sridhargaddam510f8962014-09-08 23:37:16 +0530247class SecGroupIPv6Test(SecGroupTest):
248 _ip_version = 6