blob: f0a6a84e1d2a42755cbb6716a1b5c38392ebadd8 [file] [log] [blame]
Abhijeet Malawade3e067382013-12-08 21:54:56 -08001# Copyright 2013 NTT Data
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
Abhijeet Malawade3e067382013-12-08 21:54:56 -080016from tempest.api.compute import base
Andrea Frittolicd368412017-08-14 21:37:56 +010017from tempest.common import utils
Ken'ichi Ohmichi757833a2017-03-10 10:30:30 -080018from tempest.lib.common.utils import data_utils
Ken'ichi Ohmichiebbfd1c2017-01-27 16:37:00 -080019from tempest.lib import decorators
Abhijeet Malawade3e067382013-12-08 21:54:56 -080020
21
22class SecurityGroupsTestAdminJSON(base.BaseV2ComputeAdminTest):
zhufl27f410a2020-08-13 15:27:59 +080023 """Test security groups API that requires admin privilege
24
25 Test security groups API that requires admin privilege with compute
26 microversion less than 2.36
27 """
28
zhufl95d75252018-03-28 09:55:04 +080029 max_microversion = '2.35'
Abhijeet Malawade3e067382013-12-08 21:54:56 -080030
31 @classmethod
Rohan Kanade60b73092015-02-04 17:58:19 +053032 def setup_clients(cls):
33 super(SecurityGroupsTestAdminJSON, cls).setup_clients()
Jordan Pittier8160d312017-04-18 11:52:23 +020034 cls.adm_client = cls.os_admin.compute_security_groups_client
Abhijeet Malawade3e067382013-12-08 21:54:56 -080035 cls.client = cls.security_groups_client
36
37 def _delete_security_group(self, securitygroup_id, admin=True):
38 if admin:
David Kranz9964b4e2015-02-06 15:45:29 -050039 self.adm_client.delete_security_group(securitygroup_id)
Abhijeet Malawade3e067382013-12-08 21:54:56 -080040 else:
David Kranz9964b4e2015-02-06 15:45:29 -050041 self.client.delete_security_group(securitygroup_id)
Abhijeet Malawade3e067382013-12-08 21:54:56 -080042
Ken'ichi Ohmichiebbfd1c2017-01-27 16:37:00 -080043 @decorators.idempotent_id('49667619-5af9-4c63-ab5d-2cfdd1c8f7f1')
Andrea Frittolicd368412017-08-14 21:37:56 +010044 @utils.services('network')
Abhijeet Malawade3e067382013-12-08 21:54:56 -080045 def test_list_security_groups_list_all_tenants_filter(self):
zhufl27f410a2020-08-13 15:27:59 +080046 """Test listing security groups with all_tenants filter
47
48 1. Create two security groups for non-admin user
49 2. Create two security groups for admin user
50 3. Fetch all security groups based on 'all_tenants' search filter by
51 admin, check that all four created security groups are present in
52 fetched list
53 4. Fetch all security groups based on 'all_tenants' search filter by
54 non-admin, check only two security groups created by the provided
55 non-admin user are present in fetched list
56 """
Abhijeet Malawade3e067382013-12-08 21:54:56 -080057 # List of all security groups created
58 security_group_list = []
59 # Create two security groups for a non-admin tenant
zhufl4311dc42017-01-26 16:26:18 +080060 for _ in range(2):
Ken'ichi Ohmichi4937f562015-03-23 00:15:01 +000061 name = data_utils.rand_name('securitygroup')
62 description = data_utils.rand_name('description')
ghanshyamb610b772015-08-24 17:29:38 +090063 securitygroup = self.client.create_security_group(
64 name=name, description=description)['security_group']
Abhijeet Malawade3e067382013-12-08 21:54:56 -080065 self.addCleanup(self._delete_security_group,
66 securitygroup['id'], admin=False)
67 security_group_list.append(securitygroup)
68
69 client_tenant_id = securitygroup['tenant_id']
70 # Create two security groups for admin tenant
zhufl4311dc42017-01-26 16:26:18 +080071 for _ in range(2):
Ken'ichi Ohmichi4937f562015-03-23 00:15:01 +000072 name = data_utils.rand_name('securitygroup')
73 description = data_utils.rand_name('description')
Ken'ichi Ohmichi34563cc2015-07-21 00:53:17 +000074 adm_securitygroup = self.adm_client.create_security_group(
ghanshyamb610b772015-08-24 17:29:38 +090075 name=name, description=description)['security_group']
Abhijeet Malawade3e067382013-12-08 21:54:56 -080076 self.addCleanup(self._delete_security_group,
77 adm_securitygroup['id'])
78 security_group_list.append(adm_securitygroup)
79
80 # Fetch all security groups based on 'all_tenants' search filter
ghanshyamb610b772015-08-24 17:29:38 +090081 fetched_list = self.adm_client.list_security_groups(
82 all_tenants='true')['security_groups']
Sirushti Murugesan935f2cc2016-07-12 19:48:24 +053083 sec_group_id_list = [sg['id'] for sg in fetched_list]
Abhijeet Malawade3e067382013-12-08 21:54:56 -080084 # Now check if all created Security Groups are present in fetched list
85 for sec_group in security_group_list:
86 self.assertIn(sec_group['id'], sec_group_id_list)
87
88 # Fetch all security groups for non-admin user with 'all_tenants'
89 # search filter
ghanshyamb610b772015-08-24 17:29:38 +090090 fetched_list = (self.client.list_security_groups(all_tenants='true')
91 ['security_groups'])
zhuflb9b4f4f2018-10-19 11:26:21 +080092 sec_group_id_list = [sg['id'] for sg in fetched_list]
93 # Now check that 'all_tenants='true' filter for non-admin user only
94 # provide the requested non-admin user's created security groups,
95 # not all security groups which include security groups created by
96 # other users.
97 for sec_group in security_group_list:
98 if sec_group['tenant_id'] == client_tenant_id:
99 self.assertIn(sec_group['id'], sec_group_id_list,
100 "Failed to get all security groups for "
101 "non admin user.")
102 else:
103 self.assertNotIn(sec_group['id'], sec_group_id_list,
104 "Non admin user shouldn't get other user's "
105 "security groups.")