Yair Fried | f37dae3 | 2013-09-01 15:35:14 +0300 | [diff] [blame] | 1 | # 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 Fried | d547982 | 2013-10-14 15:33:32 +0300 | [diff] [blame] | 16 | from tempest.api.network import base_security_groups as base |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 17 | from tempest.common import utils |
Ken'ichi Ohmichi | f50e4df | 2017-03-10 10:52:53 -0800 | [diff] [blame] | 18 | from tempest.lib.common.utils import data_utils |
Slawek Kaplonski | 748dd8d | 2019-04-16 23:38:35 +0200 | [diff] [blame] | 19 | from tempest.lib.common.utils import test_utils |
Ken'ichi Ohmichi | 53b9a63 | 2017-01-27 18:04:39 -0800 | [diff] [blame] | 20 | from tempest.lib import decorators |
Yair Fried | f37dae3 | 2013-09-01 15:35:14 +0300 | [diff] [blame] | 21 | |
| 22 | |
Yair Fried | d547982 | 2013-10-14 15:33:32 +0300 | [diff] [blame] | 23 | class SecGroupTest(base.BaseSecGroupTest): |
Yair Fried | f37dae3 | 2013-09-01 15:35:14 +0300 | [diff] [blame] | 24 | |
Yoshihiro Kaneko | 0567026 | 2014-01-18 19:22:44 +0900 | [diff] [blame] | 25 | @classmethod |
Rohan Kanade | a565e45 | 2015-01-27 14:00:13 +0530 | [diff] [blame] | 26 | def skip_checks(cls): |
| 27 | super(SecGroupTest, cls).skip_checks() |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 28 | if not utils.is_extension_enabled('security-group', 'network'): |
Yoshihiro Kaneko | 0567026 | 2014-01-18 19:22:44 +0900 | [diff] [blame] | 29 | msg = "security-group extension not enabled." |
| 30 | raise cls.skipException(msg) |
| 31 | |
sridhargaddam | 510f896 | 2014-09-08 23:37:16 +0530 | [diff] [blame] | 32 | 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 Warren | 456d9ae | 2016-01-12 15:36:33 -0500 | [diff] [blame] | 40 | sec_group_rules_client = self.security_group_rules_client |
| 41 | rule_create_body = sec_group_rules_client.create_security_group_rule( |
sridhargaddam | 510f896 | 2014-09-08 23:37:16 +0530 | [diff] [blame] | 42 | 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 Kaplonski | 748dd8d | 2019-04-16 23:38:35 +0200 | [diff] [blame] | 53 | self.addCleanup(test_utils.call_and_ignore_notfound_exc, |
| 54 | self._delete_security_group_rule, sec_group_rule['id']) |
sridhargaddam | 510f896 | 2014-09-08 23:37:16 +0530 | [diff] [blame] | 55 | |
| 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 yunxian | 7bbbec1 | 2016-08-21 20:03:10 +0800 | [diff] [blame] | 61 | for key, value in expected.items(): |
sridhargaddam | 510f896 | 2014-09-08 23:37:16 +0530 | [diff] [blame] | 62 | 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 Pittier | 3b46d27 | 2017-04-12 16:17:28 +0200 | [diff] [blame] | 67 | @decorators.attr(type='smoke') |
Ken'ichi Ohmichi | 53b9a63 | 2017-01-27 18:04:39 -0800 | [diff] [blame] | 68 | @decorators.idempotent_id('e30abd17-fef9-4739-8617-dc26da88e686') |
Yair Fried | f37dae3 | 2013-09-01 15:35:14 +0300 | [diff] [blame] | 69 | def test_list_security_groups(self): |
melissaml | ca689db | 2016-10-18 10:37:38 +0800 | [diff] [blame] | 70 | # Verify the security group belonging to project exist in list |
John Warren | f9606e9 | 2015-12-10 12:12:42 -0500 | [diff] [blame] | 71 | body = self.security_groups_client.list_security_groups() |
Yair Fried | f37dae3 | 2013-09-01 15:35:14 +0300 | [diff] [blame] | 72 | 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 Pittier | 3b46d27 | 2017-04-12 16:17:28 +0200 | [diff] [blame] | 80 | @decorators.attr(type='smoke') |
Ken'ichi Ohmichi | 53b9a63 | 2017-01-27 18:04:39 -0800 | [diff] [blame] | 81 | @decorators.idempotent_id('bfd128e5-3c92-44b6-9d66-7fe29d22c802') |
nayna-patel | 1c76bc9 | 2014-01-28 09:24:16 +0000 | [diff] [blame] | 82 | def test_create_list_update_show_delete_security_group(self): |
Ferenc Horváth | e52bee7 | 2017-06-14 15:02:23 +0200 | [diff] [blame] | 83 | group_create_body, _ = self._create_security_group() |
Yair Fried | f37dae3 | 2013-09-01 15:35:14 +0300 | [diff] [blame] | 84 | |
Yair Fried | f37dae3 | 2013-09-01 15:35:14 +0300 | [diff] [blame] | 85 | # List security groups and verify if created group is there in response |
John Warren | f9606e9 | 2015-12-10 12:12:42 -0500 | [diff] [blame] | 86 | list_body = self.security_groups_client.list_security_groups() |
Yair Fried | f37dae3 | 2013-09-01 15:35:14 +0300 | [diff] [blame] | 87 | 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-patel | 1c76bc9 | 2014-01-28 09:24:16 +0000 | [diff] [blame] | 91 | # Update the security group |
| 92 | new_name = data_utils.rand_name('security-') |
| 93 | new_description = data_utils.rand_name('security-description') |
John Warren | f9606e9 | 2015-12-10 12:12:42 -0500 | [diff] [blame] | 94 | update_body = self.security_groups_client.update_security_group( |
nayna-patel | 1c76bc9 | 2014-01-28 09:24:16 +0000 | [diff] [blame] | 95 | group_create_body['security_group']['id'], |
| 96 | name=new_name, |
| 97 | description=new_description) |
| 98 | # Verify if security group is updated |
nayna-patel | 1c76bc9 | 2014-01-28 09:24:16 +0000 | [diff] [blame] | 99 | 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 Warren | f9606e9 | 2015-12-10 12:12:42 -0500 | [diff] [blame] | 103 | show_body = self.security_groups_client.show_security_group( |
nayna-patel | 1c76bc9 | 2014-01-28 09:24:16 +0000 | [diff] [blame] | 104 | 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 Kaplonski | 748dd8d | 2019-04-16 23:38:35 +0200 | [diff] [blame] | 108 | # Delete security group |
| 109 | self._delete_security_group(group_create_body['security_group']['id']) |
Yair Fried | bcdcb3b | 2013-10-11 09:08:15 +0300 | [diff] [blame] | 110 | |
Jordan Pittier | 3b46d27 | 2017-04-12 16:17:28 +0200 | [diff] [blame] | 111 | @decorators.attr(type='smoke') |
Ken'ichi Ohmichi | 53b9a63 | 2017-01-27 18:04:39 -0800 | [diff] [blame] | 112 | @decorators.idempotent_id('cfb99e0e-7410-4a3d-8a0c-959a63ee77e9') |
Yair Fried | bcdcb3b | 2013-10-11 09:08:15 +0300 | [diff] [blame] | 113 | def test_create_show_delete_security_group_rule(self): |
Yair Fried | d547982 | 2013-10-14 15:33:32 +0300 | [diff] [blame] | 114 | group_create_body, _ = self._create_security_group() |
Yair Fried | bcdcb3b | 2013-10-11 09:08:15 +0300 | [diff] [blame] | 115 | |
| 116 | # Create rules for each protocol |
| 117 | protocols = ['tcp', 'udp', 'icmp'] |
John Warren | 456d9ae | 2016-01-12 15:36:33 -0500 | [diff] [blame] | 118 | client = self.security_group_rules_client |
Yair Fried | bcdcb3b | 2013-10-11 09:08:15 +0300 | [diff] [blame] | 119 | for protocol in protocols: |
John Warren | 456d9ae | 2016-01-12 15:36:33 -0500 | [diff] [blame] | 120 | rule_create_body = client.create_security_group_rule( |
nayna-patel | 3e36137 | 2014-01-29 10:25:41 +0000 | [diff] [blame] | 121 | security_group_id=group_create_body['security_group']['id'], |
| 122 | protocol=protocol, |
sridhargaddam | 510f896 | 2014-09-08 23:37:16 +0530 | [diff] [blame] | 123 | direction='ingress', |
| 124 | ethertype=self.ethertype |
Yair Fried | bcdcb3b | 2013-10-11 09:08:15 +0300 | [diff] [blame] | 125 | ) |
Yair Fried | bcdcb3b | 2013-10-11 09:08:15 +0300 | [diff] [blame] | 126 | |
sridhargaddam | 4dbbc96 | 2014-05-14 02:10:56 +0530 | [diff] [blame] | 127 | # Show details of the created security rule |
John Warren | 456d9ae | 2016-01-12 15:36:33 -0500 | [diff] [blame] | 128 | show_rule_body = client.show_security_group_rule( |
sridhargaddam | 4dbbc96 | 2014-05-14 02:10:56 +0530 | [diff] [blame] | 129 | rule_create_body['security_group_rule']['id'] |
| 130 | ) |
sridhargaddam | 4dbbc96 | 2014-05-14 02:10:56 +0530 | [diff] [blame] | 131 | create_dict = rule_create_body['security_group_rule'] |
guo yunxian | 7bbbec1 | 2016-08-21 20:03:10 +0800 | [diff] [blame] | 132 | for key, value in create_dict.items(): |
sridhargaddam | 4dbbc96 | 2014-05-14 02:10:56 +0530 | [diff] [blame] | 133 | self.assertEqual(value, |
| 134 | show_rule_body['security_group_rule'][key], |
| 135 | "%s does not match." % key) |
Yair Fried | f37dae3 | 2013-09-01 15:35:14 +0300 | [diff] [blame] | 136 | |
sridhargaddam | 4dbbc96 | 2014-05-14 02:10:56 +0530 | [diff] [blame] | 137 | # List rules and verify created rule is in response |
John Warren | 456d9ae | 2016-01-12 15:36:33 -0500 | [diff] [blame] | 138 | rule_list_body = ( |
| 139 | self.security_group_rules_client.list_security_group_rules()) |
sridhargaddam | 4dbbc96 | 2014-05-14 02:10:56 +0530 | [diff] [blame] | 140 | 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 Kaplonski | 748dd8d | 2019-04-16 23:38:35 +0200 | [diff] [blame] | 144 | self._delete_security_group_rule( |
| 145 | rule_create_body['security_group_rule']['id']) |
Yair Fried | f37dae3 | 2013-09-01 15:35:14 +0300 | [diff] [blame] | 146 | |
Ken'ichi Ohmichi | 53b9a63 | 2017-01-27 18:04:39 -0800 | [diff] [blame] | 147 | @decorators.idempotent_id('87dfbcf9-1849-43ea-b1e4-efa3eeae9f71') |
jun xie | e31dbe9 | 2014-01-13 18:10:37 +0800 | [diff] [blame] | 148 | def test_create_security_group_rule_with_additional_args(self): |
sridhargaddam | 510f896 | 2014-09-08 23:37:16 +0530 | [diff] [blame] | 149 | """Verify security group rule with additional arguments works. |
jun xie | e31dbe9 | 2014-01-13 18:10:37 +0800 | [diff] [blame] | 150 | |
sridhargaddam | 510f896 | 2014-09-08 23:37:16 +0530 | [diff] [blame] | 151 | 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 xie | e31dbe9 | 2014-01-13 18:10:37 +0800 | [diff] [blame] | 156 | direction = 'ingress' |
| 157 | protocol = 'tcp' |
| 158 | port_range_min = 77 |
| 159 | port_range_max = 77 |
sridhargaddam | 510f896 | 2014-09-08 23:37:16 +0530 | [diff] [blame] | 160 | self._create_verify_security_group_rule(sg_id, direction, |
| 161 | self.ethertype, protocol, |
| 162 | port_range_min, |
| 163 | port_range_max) |
jun xie | e31dbe9 | 2014-01-13 18:10:37 +0800 | [diff] [blame] | 164 | |
Ken'ichi Ohmichi | 53b9a63 | 2017-01-27 18:04:39 -0800 | [diff] [blame] | 165 | @decorators.idempotent_id('c9463db8-b44d-4f52-b6c0-8dbda99f26ce') |
sridhargaddam | 510f896 | 2014-09-08 23:37:16 +0530 | [diff] [blame] | 166 | def test_create_security_group_rule_with_icmp_type_code(self): |
| 167 | """Verify security group rule for icmp protocol works. |
jun xie | e31dbe9 | 2014-01-13 18:10:37 +0800 | [diff] [blame] | 168 | |
sridhargaddam | 510f896 | 2014-09-08 23:37:16 +0530 | [diff] [blame] | 169 | Specify icmp type (port_range_min) and icmp code |
Tingting Bao | 2b51334 | 2015-02-15 22:54:55 -0800 | [diff] [blame] | 170 | (port_range_max) with different values. A separate testcase |
sridhargaddam | 510f896 | 2014-09-08 23:37:16 +0530 | [diff] [blame] | 171 | 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 Haley | 13731b0 | 2019-05-21 09:28:36 -0400 | [diff] [blame] | 178 | # 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 Liu | ec20aeb | 2015-02-25 00:14:35 +0000 | [diff] [blame] | 186 | icmp_type_codes = [(3, 2), (3, 0), (8, 0), (0, 0), (11, None)] |
sridhargaddam | 510f896 | 2014-09-08 23:37:16 +0530 | [diff] [blame] | 187 | 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 Ohmichi | 53b9a63 | 2017-01-27 18:04:39 -0800 | [diff] [blame] | 192 | @decorators.idempotent_id('c2ed2deb-7a0c-44d8-8b4c-a5825b5c310b') |
sridhargaddam | 510f896 | 2014-09-08 23:37:16 +0530 | [diff] [blame] | 193 | 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 Ohmichi | 53b9a63 | 2017-01-27 18:04:39 -0800 | [diff] [blame] | 210 | @decorators.idempotent_id('16459776-5da2-4634-bce4-4b55ee3ec188') |
sridhargaddam | 510f896 | 2014-09-08 23:37:16 +0530 | [diff] [blame] | 211 | 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 |
zhufl | 5d65cd7 | 2017-09-28 16:53:00 +0800 | [diff] [blame] | 220 | ip_prefix = str(self.cidr) |
sridhargaddam | 510f896 | 2014-09-08 23:37:16 +0530 | [diff] [blame] | 221 | 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 xie | e31dbe9 | 2014-01-13 18:10:37 +0800 | [diff] [blame] | 226 | |
Ken'ichi Ohmichi | 53b9a63 | 2017-01-27 18:04:39 -0800 | [diff] [blame] | 227 | @decorators.idempotent_id('0a307599-6655-4220-bebc-fd70c64f2290') |
Ashish Gupta | 1d3712f | 2014-07-17 04:10:43 -0700 | [diff] [blame] | 228 | 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 Warren | 456d9ae | 2016-01-12 15:36:33 -0500 | [diff] [blame] | 236 | client = self.security_group_rules_client |
| 237 | rule_create_body = client.create_security_group_rule( |
Ashish Gupta | 1d3712f | 2014-07-17 04:10:43 -0700 | [diff] [blame] | 238 | 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 Fried | f37dae3 | 2013-09-01 15:35:14 +0300 | [diff] [blame] | 246 | |
sridhargaddam | 510f896 | 2014-09-08 23:37:16 +0530 | [diff] [blame] | 247 | class SecGroupIPv6Test(SecGroupTest): |
| 248 | _ip_version = 6 |