blob: 3e26f4683a3c1b28209a98b52c84669532d8d82e [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
nayna-patel1c76bc92014-01-28 09:24:16 +000017from tempest.common.utils import data_utils
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090018from tempest import test
Yair Friedf37dae32013-09-01 15:35:14 +030019
20
Yair Friedd5479822013-10-14 15:33:32 +030021class SecGroupTest(base.BaseSecGroupTest):
Yair Friedf37dae32013-09-01 15:35:14 +030022 _interface = 'json'
23
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090024 @classmethod
25 def setUpClass(cls):
26 super(SecGroupTest, cls).setUpClass()
27 if not test.is_extension_enabled('security-group', 'network'):
28 msg = "security-group extension not enabled."
29 raise cls.skipException(msg)
30
31 @test.attr(type='smoke')
Yair Friedf37dae32013-09-01 15:35:14 +030032 def test_list_security_groups(self):
33 # Verify the that security group belonging to tenant exist in list
34 resp, body = self.client.list_security_groups()
35 self.assertEqual('200', resp['status'])
36 security_groups = body['security_groups']
37 found = None
38 for n in security_groups:
39 if (n['name'] == 'default'):
40 found = n['id']
41 msg = "Security-group list doesn't contain default security-group"
42 self.assertIsNotNone(found, msg)
43
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090044 @test.attr(type='smoke')
nayna-patel1c76bc92014-01-28 09:24:16 +000045 def test_create_list_update_show_delete_security_group(self):
Yair Friedd5479822013-10-14 15:33:32 +030046 group_create_body, name = self._create_security_group()
Yair Friedf37dae32013-09-01 15:35:14 +030047
Yair Friedf37dae32013-09-01 15:35:14 +030048 # List security groups and verify if created group is there in response
49 resp, list_body = self.client.list_security_groups()
50 self.assertEqual('200', resp['status'])
51 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')
58 resp, update_body = self.client.update_security_group(
59 group_create_body['security_group']['id'],
60 name=new_name,
61 description=new_description)
62 # Verify if security group is updated
63 self.assertEqual('200', resp['status'])
64 self.assertEqual(update_body['security_group']['name'], new_name)
65 self.assertEqual(update_body['security_group']['description'],
66 new_description)
67 # Show details of the updated security group
68 resp, show_body = self.client.show_security_group(
69 group_create_body['security_group']['id'])
70 self.assertEqual(show_body['security_group']['name'], new_name)
71 self.assertEqual(show_body['security_group']['description'],
72 new_description)
Yair Friedbcdcb3b2013-10-11 09:08:15 +030073
Yoshihiro Kaneko05670262014-01-18 19:22:44 +090074 @test.attr(type='smoke')
Yair Friedbcdcb3b2013-10-11 09:08:15 +030075 def test_create_show_delete_security_group_rule(self):
Yair Friedd5479822013-10-14 15:33:32 +030076 group_create_body, _ = self._create_security_group()
Yair Friedbcdcb3b2013-10-11 09:08:15 +030077
78 # Create rules for each protocol
79 protocols = ['tcp', 'udp', 'icmp']
80 for protocol in protocols:
81 resp, rule_create_body = self.client.create_security_group_rule(
nayna-patel3e361372014-01-29 10:25:41 +000082 security_group_id=group_create_body['security_group']['id'],
83 protocol=protocol,
84 direction='ingress'
Yair Friedbcdcb3b2013-10-11 09:08:15 +030085 )
86 self.assertEqual('201', resp['status'])
87 self.addCleanup(self._delete_security_group_rule,
88 rule_create_body['security_group_rule']['id']
89 )
90
Yair Friedf37dae32013-09-01 15:35:14 +030091 # Show details of the created security rule
92 resp, show_rule_body = self.client.show_security_group_rule(
93 rule_create_body['security_group_rule']['id']
94 )
95 self.assertEqual('200', resp['status'])
96
97 # List rules and verify created rule is in response
98 resp, rule_list_body = self.client.list_security_group_rules()
99 self.assertEqual('200', resp['status'])
100 rule_list = [rule['id']
101 for rule in rule_list_body['security_group_rules']]
102 self.assertIn(rule_create_body['security_group_rule']['id'], rule_list)
103
Yoshihiro Kaneko05670262014-01-18 19:22:44 +0900104 @test.attr(type='smoke')
jun xiee31dbe92014-01-13 18:10:37 +0800105 def test_create_security_group_rule_with_additional_args(self):
106 # Verify creating security group rule with the following
107 # arguments works: "protocol": "tcp", "port_range_max": 77,
108 # "port_range_min": 77, "direction":"ingress".
109 group_create_body, _ = self._create_security_group()
110
111 direction = 'ingress'
112 protocol = 'tcp'
113 port_range_min = 77
114 port_range_max = 77
115 resp, rule_create_body = self.client.create_security_group_rule(
nayna-patel3e361372014-01-29 10:25:41 +0000116 security_group_id=group_create_body['security_group']['id'],
jun xiee31dbe92014-01-13 18:10:37 +0800117 direction=direction,
118 protocol=protocol,
119 port_range_min=port_range_min,
120 port_range_max=port_range_max
121 )
122
123 self.assertEqual('201', resp['status'])
124 sec_group_rule = rule_create_body['security_group_rule']
125 self.addCleanup(self._delete_security_group_rule,
126 sec_group_rule['id']
127 )
128
129 self.assertEqual(sec_group_rule['direction'], direction)
130 self.assertEqual(sec_group_rule['protocol'], protocol)
131 self.assertEqual(int(sec_group_rule['port_range_min']), port_range_min)
132 self.assertEqual(int(sec_group_rule['port_range_max']), port_range_max)
133
Yair Friedf37dae32013-09-01 15:35:14 +0300134
135class SecGroupTestXML(SecGroupTest):
136 _interface = 'xml'