blob: 9764b4da7833cd0e9f568f1f2be8a8901492dee1 [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
nayna-patel1c76bc92014-01-28 09:24:16 +000019from tempest.common.utils import data_utils
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090020from tempest import test
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 _interface = 'json'
25
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090026 @classmethod
Andrea Frittolida4a2452014-09-15 13:12:08 +010027 def resource_setup(cls):
28 super(SecGroupTest, cls).resource_setup()
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090029 if not test.is_extension_enabled('security-group', 'network'):
30 msg = "security-group extension not enabled."
31 raise cls.skipException(msg)
32
33 @test.attr(type='smoke')
Yair Friedf37dae32013-09-01 15:35:14 +030034 def test_list_security_groups(self):
35 # Verify the that security group belonging to tenant exist in list
Rohan Kanadeeeb21642014-08-14 12:00:26 +020036 _, body = self.client.list_security_groups()
Yair Friedf37dae32013-09-01 15:35:14 +030037 security_groups = body['security_groups']
38 found = None
39 for n in security_groups:
40 if (n['name'] == 'default'):
41 found = n['id']
42 msg = "Security-group list doesn't contain default security-group"
43 self.assertIsNotNone(found, msg)
44
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090045 @test.attr(type='smoke')
nayna-patel1c76bc92014-01-28 09:24:16 +000046 def test_create_list_update_show_delete_security_group(self):
Yair Friedd5479822013-10-14 15:33:32 +030047 group_create_body, name = self._create_security_group()
Yair Friedf37dae32013-09-01 15:35:14 +030048
Yair Friedf37dae32013-09-01 15:35:14 +030049 # List security groups and verify if created group is there in response
Rohan Kanadeeeb21642014-08-14 12:00:26 +020050 _, list_body = self.client.list_security_groups()
Yair Friedf37dae32013-09-01 15:35:14 +030051 secgroup_list = list()
52 for secgroup in list_body['security_groups']:
53 secgroup_list.append(secgroup['id'])
54 self.assertIn(group_create_body['security_group']['id'], secgroup_list)
nayna-patel1c76bc92014-01-28 09:24:16 +000055 # Update the security group
56 new_name = data_utils.rand_name('security-')
57 new_description = data_utils.rand_name('security-description')
Rohan Kanadeeeb21642014-08-14 12:00:26 +020058 _, update_body = self.client.update_security_group(
nayna-patel1c76bc92014-01-28 09:24:16 +000059 group_create_body['security_group']['id'],
60 name=new_name,
61 description=new_description)
62 # Verify if security group is updated
nayna-patel1c76bc92014-01-28 09:24:16 +000063 self.assertEqual(update_body['security_group']['name'], new_name)
64 self.assertEqual(update_body['security_group']['description'],
65 new_description)
66 # Show details of the updated security group
67 resp, show_body = self.client.show_security_group(
68 group_create_body['security_group']['id'])
69 self.assertEqual(show_body['security_group']['name'], new_name)
70 self.assertEqual(show_body['security_group']['description'],
71 new_description)
Yair Friedbcdcb3b2013-10-11 09:08:15 +030072
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090073 @test.attr(type='smoke')
Yair Friedbcdcb3b2013-10-11 09:08:15 +030074 def test_create_show_delete_security_group_rule(self):
Yair Friedd5479822013-10-14 15:33:32 +030075 group_create_body, _ = self._create_security_group()
Yair Friedbcdcb3b2013-10-11 09:08:15 +030076
77 # Create rules for each protocol
78 protocols = ['tcp', 'udp', 'icmp']
79 for protocol in protocols:
Rohan Kanadeeeb21642014-08-14 12:00:26 +020080 _, rule_create_body = self.client.create_security_group_rule(
nayna-patel3e361372014-01-29 10:25:41 +000081 security_group_id=group_create_body['security_group']['id'],
82 protocol=protocol,
83 direction='ingress'
Yair Friedbcdcb3b2013-10-11 09:08:15 +030084 )
Yair Friedbcdcb3b2013-10-11 09:08:15 +030085
sridhargaddam4dbbc962014-05-14 02:10:56 +053086 # Show details of the created security rule
Rohan Kanadeeeb21642014-08-14 12:00:26 +020087 _, show_rule_body = self.client.show_security_group_rule(
sridhargaddam4dbbc962014-05-14 02:10:56 +053088 rule_create_body['security_group_rule']['id']
89 )
sridhargaddam4dbbc962014-05-14 02:10:56 +053090 create_dict = rule_create_body['security_group_rule']
91 for key, value in six.iteritems(create_dict):
92 self.assertEqual(value,
93 show_rule_body['security_group_rule'][key],
94 "%s does not match." % key)
Yair Friedf37dae32013-09-01 15:35:14 +030095
sridhargaddam4dbbc962014-05-14 02:10:56 +053096 # List rules and verify created rule is in response
Rohan Kanadeeeb21642014-08-14 12:00:26 +020097 _, rule_list_body = self.client.list_security_group_rules()
sridhargaddam4dbbc962014-05-14 02:10:56 +053098 rule_list = [rule['id']
99 for rule in rule_list_body['security_group_rules']]
100 self.assertIn(rule_create_body['security_group_rule']['id'],
101 rule_list)
Yair Friedf37dae32013-09-01 15:35:14 +0300102
Yoshihiro Kaneko05670262014-01-18 19:22:44 +0900103 @test.attr(type='smoke')
jun xiee31dbe92014-01-13 18:10:37 +0800104 def test_create_security_group_rule_with_additional_args(self):
105 # Verify creating security group rule with the following
106 # arguments works: "protocol": "tcp", "port_range_max": 77,
107 # "port_range_min": 77, "direction":"ingress".
108 group_create_body, _ = self._create_security_group()
109
110 direction = 'ingress'
111 protocol = 'tcp'
112 port_range_min = 77
113 port_range_max = 77
Rohan Kanadeeeb21642014-08-14 12:00:26 +0200114 _, rule_create_body = self.client.create_security_group_rule(
nayna-patel3e361372014-01-29 10:25:41 +0000115 security_group_id=group_create_body['security_group']['id'],
jun xiee31dbe92014-01-13 18:10:37 +0800116 direction=direction,
117 protocol=protocol,
118 port_range_min=port_range_min,
119 port_range_max=port_range_max
120 )
121
jun xiee31dbe92014-01-13 18:10:37 +0800122 sec_group_rule = rule_create_body['security_group_rule']
jun xiee31dbe92014-01-13 18:10:37 +0800123
124 self.assertEqual(sec_group_rule['direction'], direction)
125 self.assertEqual(sec_group_rule['protocol'], protocol)
126 self.assertEqual(int(sec_group_rule['port_range_min']), port_range_min)
127 self.assertEqual(int(sec_group_rule['port_range_max']), port_range_max)
128
Yair Friedf37dae32013-09-01 15:35:14 +0300129
130class SecGroupTestXML(SecGroupTest):
131 _interface = 'xml'