blob: 7d0765ee824a11fe1bb85e62dc9a7b32b9c9885f [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
Fei Long Wangd39431f2015-05-14 11:30:48 +120019from 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):
sridhargaddam510f8962014-09-08 23:37:16 +053027 _tenant_network_cidr = CONF.network.tenant_network_cidr
Yair Friedf37dae32013-09-01 15:35:14 +030028
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090029 @classmethod
Rohan Kanadea565e452015-01-27 14:00:13 +053030 def skip_checks(cls):
31 super(SecGroupTest, cls).skip_checks()
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090032 if not test.is_extension_enabled('security-group', 'network'):
33 msg = "security-group extension not enabled."
34 raise cls.skipException(msg)
35
sridhargaddam510f8962014-09-08 23:37:16 +053036 def _create_verify_security_group_rule(self, sg_id, direction,
37 ethertype, protocol,
38 port_range_min,
39 port_range_max,
40 remote_group_id=None,
41 remote_ip_prefix=None):
42 # Create Security Group rule with the input params and validate
43 # that SG rule is created with the same parameters.
John Warren456d9ae2016-01-12 15:36:33 -050044 sec_group_rules_client = self.security_group_rules_client
45 rule_create_body = sec_group_rules_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
John Warrenf9606e92015-12-10 12:12:42 -050075 body = self.security_groups_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
John Warrenf9606e92015-12-10 12:12:42 -050090 list_body = self.security_groups_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')
John Warrenf9606e92015-12-10 12:12:42 -050098 update_body = self.security_groups_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
John Warrenf9606e92015-12-10 12:12:42 -0500107 show_body = self.security_groups_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']
John Warren456d9ae2016-01-12 15:36:33 -0500120 client = self.security_group_rules_client
Yair Friedbcdcb3b2013-10-11 09:08:15 +0300121 for protocol in protocols:
John Warren456d9ae2016-01-12 15:36:33 -0500122 rule_create_body = client.create_security_group_rule(
nayna-patel3e361372014-01-29 10:25:41 +0000123 security_group_id=group_create_body['security_group']['id'],
124 protocol=protocol,
sridhargaddam510f8962014-09-08 23:37:16 +0530125 direction='ingress',
126 ethertype=self.ethertype
Yair Friedbcdcb3b2013-10-11 09:08:15 +0300127 )
Yair Friedbcdcb3b2013-10-11 09:08:15 +0300128
sridhargaddam4dbbc962014-05-14 02:10:56 +0530129 # Show details of the created security rule
John Warren456d9ae2016-01-12 15:36:33 -0500130 show_rule_body = client.show_security_group_rule(
sridhargaddam4dbbc962014-05-14 02:10:56 +0530131 rule_create_body['security_group_rule']['id']
132 )
sridhargaddam4dbbc962014-05-14 02:10:56 +0530133 create_dict = rule_create_body['security_group_rule']
134 for key, value in six.iteritems(create_dict):
135 self.assertEqual(value,
136 show_rule_body['security_group_rule'][key],
137 "%s does not match." % key)
Yair Friedf37dae32013-09-01 15:35:14 +0300138
sridhargaddam4dbbc962014-05-14 02:10:56 +0530139 # List rules and verify created rule is in response
John Warren456d9ae2016-01-12 15:36:33 -0500140 rule_list_body = (
141 self.security_group_rules_client.list_security_group_rules())
sridhargaddam4dbbc962014-05-14 02:10:56 +0530142 rule_list = [rule['id']
143 for rule in rule_list_body['security_group_rules']]
144 self.assertIn(rule_create_body['security_group_rule']['id'],
145 rule_list)
Yair Friedf37dae32013-09-01 15:35:14 +0300146
Chris Hoge7579c1a2015-02-26 14:12:15 -0800147 @test.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
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'
Tong Liuec20aeb2015-02-25 00:14:35 +0000179 icmp_type_codes = [(3, 2), (3, 0), (8, 0), (0, 0), (11, None)]
sridhargaddam510f8962014-09-08 23:37:16 +0530180 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
Chris Hoge7579c1a2015-02-26 14:12:15 -0800185 @test.idempotent_id('c2ed2deb-7a0c-44d8-8b4c-a5825b5c310b')
sridhargaddam510f8962014-09-08 23:37:16 +0530186 def test_create_security_group_rule_with_remote_group_id(self):
187 # Verify creating security group rule with remote_group_id works
188 sg1_body, _ = self._create_security_group()
189 sg2_body, _ = self._create_security_group()
190
191 sg_id = sg1_body['security_group']['id']
192 direction = 'ingress'
193 protocol = 'udp'
194 port_range_min = 50
195 port_range_max = 55
196 remote_id = sg2_body['security_group']['id']
197 self._create_verify_security_group_rule(sg_id, direction,
198 self.ethertype, protocol,
199 port_range_min,
200 port_range_max,
201 remote_group_id=remote_id)
202
Chris Hoge7579c1a2015-02-26 14:12:15 -0800203 @test.idempotent_id('16459776-5da2-4634-bce4-4b55ee3ec188')
sridhargaddam510f8962014-09-08 23:37:16 +0530204 def test_create_security_group_rule_with_remote_ip_prefix(self):
205 # Verify creating security group rule with remote_ip_prefix works
206 sg1_body, _ = self._create_security_group()
207
208 sg_id = sg1_body['security_group']['id']
209 direction = 'ingress'
210 protocol = 'tcp'
211 port_range_min = 76
212 port_range_max = 77
213 ip_prefix = self._tenant_network_cidr
214 self._create_verify_security_group_rule(sg_id, direction,
215 self.ethertype, protocol,
216 port_range_min,
217 port_range_max,
218 remote_ip_prefix=ip_prefix)
jun xiee31dbe92014-01-13 18:10:37 +0800219
Chris Hoge7579c1a2015-02-26 14:12:15 -0800220 @test.idempotent_id('0a307599-6655-4220-bebc-fd70c64f2290')
Ashish Gupta1d3712f2014-07-17 04:10:43 -0700221 def test_create_security_group_rule_with_protocol_integer_value(self):
222 # Verify creating security group rule with the
223 # protocol as integer value
224 # arguments : "protocol": 17
225 group_create_body, _ = self._create_security_group()
226 direction = 'ingress'
227 protocol = 17
228 security_group_id = group_create_body['security_group']['id']
John Warren456d9ae2016-01-12 15:36:33 -0500229 client = self.security_group_rules_client
230 rule_create_body = client.create_security_group_rule(
Ashish Gupta1d3712f2014-07-17 04:10:43 -0700231 security_group_id=security_group_id,
232 direction=direction,
233 protocol=protocol
234 )
235 sec_group_rule = rule_create_body['security_group_rule']
236 self.assertEqual(sec_group_rule['direction'], direction)
237 self.assertEqual(int(sec_group_rule['protocol']), protocol)
238
Yair Friedf37dae32013-09-01 15:35:14 +0300239
sridhargaddam510f8962014-09-08 23:37:16 +0530240class SecGroupIPv6Test(SecGroupTest):
241 _ip_version = 6
242 _tenant_network_cidr = CONF.network.tenant_network_v6_cidr