blob: ccc52321ab5fa156e919344d76512a802522bf9e [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.
David Kranz34e88122014-12-11 15:24:05 -050044 rule_create_body = self.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}
64 for key, value in six.iteritems(expected):
65 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
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090070 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -080071 @test.idempotent_id('e30abd17-fef9-4739-8617-dc26da88e686')
Yair Friedf37dae32013-09-01 15:35:14 +030072 def test_list_security_groups(self):
73 # Verify the that security group belonging to tenant exist in list
David Kranz34e88122014-12-11 15:24:05 -050074 body = self.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
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090083 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -080084 @test.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):
Yair Friedd5479822013-10-14 15:33:32 +030086 group_create_body, name = 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
David Kranz34e88122014-12-11 15:24:05 -050089 list_body = self.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')
David Kranz34e88122014-12-11 15:24:05 -050097 update_body = self.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
David Kranz34e88122014-12-11 15:24:05 -0500106 show_body = self.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
Yoshihiro Kaneko05670262014-01-18 19:22:44 +0900112 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800113 @test.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']
119 for protocol in protocols:
David Kranz34e88122014-12-11 15:24:05 -0500120 rule_create_body = self.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
David Kranz34e88122014-12-11 15:24:05 -0500128 show_rule_body = self.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']
132 for key, value in six.iteritems(create_dict):
133 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
David Kranz34e88122014-12-11 15:24:05 -0500138 rule_list_body = self.client.list_security_group_rules()
sridhargaddam4dbbc962014-05-14 02:10:56 +0530139 rule_list = [rule['id']
140 for rule in rule_list_body['security_group_rules']]
141 self.assertIn(rule_create_body['security_group_rule']['id'],
142 rule_list)
Yair Friedf37dae32013-09-01 15:35:14 +0300143
Chris Hoge7579c1a2015-02-26 14:12:15 -0800144 @test.idempotent_id('87dfbcf9-1849-43ea-b1e4-efa3eeae9f71')
jun xiee31dbe92014-01-13 18:10:37 +0800145 def test_create_security_group_rule_with_additional_args(self):
sridhargaddam510f8962014-09-08 23:37:16 +0530146 """Verify security group rule with additional arguments works.
jun xiee31dbe92014-01-13 18:10:37 +0800147
sridhargaddam510f8962014-09-08 23:37:16 +0530148 direction:ingress, ethertype:[IPv4/IPv6],
149 protocol:tcp, port_range_min:77, port_range_max:77
150 """
151 group_create_body, _ = self._create_security_group()
152 sg_id = group_create_body['security_group']['id']
jun xiee31dbe92014-01-13 18:10:37 +0800153 direction = 'ingress'
154 protocol = 'tcp'
155 port_range_min = 77
156 port_range_max = 77
sridhargaddam510f8962014-09-08 23:37:16 +0530157 self._create_verify_security_group_rule(sg_id, direction,
158 self.ethertype, protocol,
159 port_range_min,
160 port_range_max)
jun xiee31dbe92014-01-13 18:10:37 +0800161
Chris Hoge7579c1a2015-02-26 14:12:15 -0800162 @test.idempotent_id('c9463db8-b44d-4f52-b6c0-8dbda99f26ce')
sridhargaddam510f8962014-09-08 23:37:16 +0530163 def test_create_security_group_rule_with_icmp_type_code(self):
164 """Verify security group rule for icmp protocol works.
jun xiee31dbe92014-01-13 18:10:37 +0800165
sridhargaddam510f8962014-09-08 23:37:16 +0530166 Specify icmp type (port_range_min) and icmp code
Tingting Bao2b513342015-02-15 22:54:55 -0800167 (port_range_max) with different values. A separate testcase
sridhargaddam510f8962014-09-08 23:37:16 +0530168 is added for icmp protocol as icmp validation would be
169 different from tcp/udp.
170 """
171 group_create_body, _ = self._create_security_group()
172
173 sg_id = group_create_body['security_group']['id']
174 direction = 'ingress'
175 protocol = 'icmp'
Tong Liuec20aeb2015-02-25 00:14:35 +0000176 icmp_type_codes = [(3, 2), (3, 0), (8, 0), (0, 0), (11, None)]
sridhargaddam510f8962014-09-08 23:37:16 +0530177 for icmp_type, icmp_code in icmp_type_codes:
178 self._create_verify_security_group_rule(sg_id, direction,
179 self.ethertype, protocol,
180 icmp_type, icmp_code)
181
Chris Hoge7579c1a2015-02-26 14:12:15 -0800182 @test.idempotent_id('c2ed2deb-7a0c-44d8-8b4c-a5825b5c310b')
sridhargaddam510f8962014-09-08 23:37:16 +0530183 def test_create_security_group_rule_with_remote_group_id(self):
184 # Verify creating security group rule with remote_group_id works
185 sg1_body, _ = self._create_security_group()
186 sg2_body, _ = self._create_security_group()
187
188 sg_id = sg1_body['security_group']['id']
189 direction = 'ingress'
190 protocol = 'udp'
191 port_range_min = 50
192 port_range_max = 55
193 remote_id = sg2_body['security_group']['id']
194 self._create_verify_security_group_rule(sg_id, direction,
195 self.ethertype, protocol,
196 port_range_min,
197 port_range_max,
198 remote_group_id=remote_id)
199
Chris Hoge7579c1a2015-02-26 14:12:15 -0800200 @test.idempotent_id('16459776-5da2-4634-bce4-4b55ee3ec188')
sridhargaddam510f8962014-09-08 23:37:16 +0530201 def test_create_security_group_rule_with_remote_ip_prefix(self):
202 # Verify creating security group rule with remote_ip_prefix works
203 sg1_body, _ = self._create_security_group()
204
205 sg_id = sg1_body['security_group']['id']
206 direction = 'ingress'
207 protocol = 'tcp'
208 port_range_min = 76
209 port_range_max = 77
210 ip_prefix = self._tenant_network_cidr
211 self._create_verify_security_group_rule(sg_id, direction,
212 self.ethertype, protocol,
213 port_range_min,
214 port_range_max,
215 remote_ip_prefix=ip_prefix)
jun xiee31dbe92014-01-13 18:10:37 +0800216
Chris Hoge7579c1a2015-02-26 14:12:15 -0800217 @test.idempotent_id('0a307599-6655-4220-bebc-fd70c64f2290')
Ashish Gupta1d3712f2014-07-17 04:10:43 -0700218 def test_create_security_group_rule_with_protocol_integer_value(self):
219 # Verify creating security group rule with the
220 # protocol as integer value
221 # arguments : "protocol": 17
222 group_create_body, _ = self._create_security_group()
223 direction = 'ingress'
224 protocol = 17
225 security_group_id = group_create_body['security_group']['id']
David Kranz34e88122014-12-11 15:24:05 -0500226 rule_create_body = self.client.create_security_group_rule(
Ashish Gupta1d3712f2014-07-17 04:10:43 -0700227 security_group_id=security_group_id,
228 direction=direction,
229 protocol=protocol
230 )
231 sec_group_rule = rule_create_body['security_group_rule']
232 self.assertEqual(sec_group_rule['direction'], direction)
233 self.assertEqual(int(sec_group_rule['protocol']), protocol)
234
Yair Friedf37dae32013-09-01 15:35:14 +0300235
sridhargaddam510f8962014-09-08 23:37:16 +0530236class SecGroupIPv6Test(SecGroupTest):
237 _ip_version = 6
238 _tenant_network_cidr = CONF.network.tenant_network_v6_cidr