Abhijeet Malawade | 3e06738 | 2013-12-08 21:54:56 -0800 | [diff] [blame] | 1 | # 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 Malawade | 3e06738 | 2013-12-08 21:54:56 -0800 | [diff] [blame] | 16 | from tempest.api.compute import base |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 17 | from tempest.common import utils |
Ken'ichi Ohmichi | 757833a | 2017-03-10 10:30:30 -0800 | [diff] [blame] | 18 | from tempest.lib.common.utils import data_utils |
Ken'ichi Ohmichi | ebbfd1c | 2017-01-27 16:37:00 -0800 | [diff] [blame] | 19 | from tempest.lib import decorators |
Abhijeet Malawade | 3e06738 | 2013-12-08 21:54:56 -0800 | [diff] [blame] | 20 | |
| 21 | |
| 22 | class SecurityGroupsTestAdminJSON(base.BaseV2ComputeAdminTest): |
zhufl | 95d7525 | 2018-03-28 09:55:04 +0800 | [diff] [blame] | 23 | max_microversion = '2.35' |
Abhijeet Malawade | 3e06738 | 2013-12-08 21:54:56 -0800 | [diff] [blame] | 24 | |
| 25 | @classmethod |
Rohan Kanade | 60b7309 | 2015-02-04 17:58:19 +0530 | [diff] [blame] | 26 | def setup_clients(cls): |
| 27 | super(SecurityGroupsTestAdminJSON, cls).setup_clients() |
Jordan Pittier | 8160d31 | 2017-04-18 11:52:23 +0200 | [diff] [blame] | 28 | cls.adm_client = cls.os_admin.compute_security_groups_client |
Abhijeet Malawade | 3e06738 | 2013-12-08 21:54:56 -0800 | [diff] [blame] | 29 | cls.client = cls.security_groups_client |
| 30 | |
| 31 | def _delete_security_group(self, securitygroup_id, admin=True): |
| 32 | if admin: |
David Kranz | 9964b4e | 2015-02-06 15:45:29 -0500 | [diff] [blame] | 33 | self.adm_client.delete_security_group(securitygroup_id) |
Abhijeet Malawade | 3e06738 | 2013-12-08 21:54:56 -0800 | [diff] [blame] | 34 | else: |
David Kranz | 9964b4e | 2015-02-06 15:45:29 -0500 | [diff] [blame] | 35 | self.client.delete_security_group(securitygroup_id) |
Abhijeet Malawade | 3e06738 | 2013-12-08 21:54:56 -0800 | [diff] [blame] | 36 | |
Ken'ichi Ohmichi | ebbfd1c | 2017-01-27 16:37:00 -0800 | [diff] [blame] | 37 | @decorators.idempotent_id('49667619-5af9-4c63-ab5d-2cfdd1c8f7f1') |
Andrea Frittoli | cd36841 | 2017-08-14 21:37:56 +0100 | [diff] [blame] | 38 | @utils.services('network') |
Abhijeet Malawade | 3e06738 | 2013-12-08 21:54:56 -0800 | [diff] [blame] | 39 | def test_list_security_groups_list_all_tenants_filter(self): |
| 40 | # Admin can list security groups of all tenants |
| 41 | # List of all security groups created |
| 42 | security_group_list = [] |
| 43 | # Create two security groups for a non-admin tenant |
zhufl | 4311dc4 | 2017-01-26 16:26:18 +0800 | [diff] [blame] | 44 | for _ in range(2): |
Ken'ichi Ohmichi | 4937f56 | 2015-03-23 00:15:01 +0000 | [diff] [blame] | 45 | name = data_utils.rand_name('securitygroup') |
| 46 | description = data_utils.rand_name('description') |
ghanshyam | b610b77 | 2015-08-24 17:29:38 +0900 | [diff] [blame] | 47 | securitygroup = self.client.create_security_group( |
| 48 | name=name, description=description)['security_group'] |
Abhijeet Malawade | 3e06738 | 2013-12-08 21:54:56 -0800 | [diff] [blame] | 49 | self.addCleanup(self._delete_security_group, |
| 50 | securitygroup['id'], admin=False) |
| 51 | security_group_list.append(securitygroup) |
| 52 | |
| 53 | client_tenant_id = securitygroup['tenant_id'] |
| 54 | # Create two security groups for admin tenant |
zhufl | 4311dc4 | 2017-01-26 16:26:18 +0800 | [diff] [blame] | 55 | for _ in range(2): |
Ken'ichi Ohmichi | 4937f56 | 2015-03-23 00:15:01 +0000 | [diff] [blame] | 56 | name = data_utils.rand_name('securitygroup') |
| 57 | description = data_utils.rand_name('description') |
Ken'ichi Ohmichi | 34563cc | 2015-07-21 00:53:17 +0000 | [diff] [blame] | 58 | adm_securitygroup = self.adm_client.create_security_group( |
ghanshyam | b610b77 | 2015-08-24 17:29:38 +0900 | [diff] [blame] | 59 | name=name, description=description)['security_group'] |
Abhijeet Malawade | 3e06738 | 2013-12-08 21:54:56 -0800 | [diff] [blame] | 60 | self.addCleanup(self._delete_security_group, |
| 61 | adm_securitygroup['id']) |
| 62 | security_group_list.append(adm_securitygroup) |
| 63 | |
| 64 | # Fetch all security groups based on 'all_tenants' search filter |
ghanshyam | b610b77 | 2015-08-24 17:29:38 +0900 | [diff] [blame] | 65 | fetched_list = self.adm_client.list_security_groups( |
| 66 | all_tenants='true')['security_groups'] |
Sirushti Murugesan | 935f2cc | 2016-07-12 19:48:24 +0530 | [diff] [blame] | 67 | sec_group_id_list = [sg['id'] for sg in fetched_list] |
Abhijeet Malawade | 3e06738 | 2013-12-08 21:54:56 -0800 | [diff] [blame] | 68 | # Now check if all created Security Groups are present in fetched list |
| 69 | for sec_group in security_group_list: |
| 70 | self.assertIn(sec_group['id'], sec_group_id_list) |
| 71 | |
| 72 | # Fetch all security groups for non-admin user with 'all_tenants' |
| 73 | # search filter |
ghanshyam | b610b77 | 2015-08-24 17:29:38 +0900 | [diff] [blame] | 74 | fetched_list = (self.client.list_security_groups(all_tenants='true') |
| 75 | ['security_groups']) |
zhufl | b9b4f4f | 2018-10-19 11:26:21 +0800 | [diff] [blame] | 76 | sec_group_id_list = [sg['id'] for sg in fetched_list] |
| 77 | # Now check that 'all_tenants='true' filter for non-admin user only |
| 78 | # provide the requested non-admin user's created security groups, |
| 79 | # not all security groups which include security groups created by |
| 80 | # other users. |
| 81 | for sec_group in security_group_list: |
| 82 | if sec_group['tenant_id'] == client_tenant_id: |
| 83 | self.assertIn(sec_group['id'], sec_group_id_list, |
| 84 | "Failed to get all security groups for " |
| 85 | "non admin user.") |
| 86 | else: |
| 87 | self.assertNotIn(sec_group['id'], sec_group_id_list, |
| 88 | "Non admin user shouldn't get other user's " |
| 89 | "security groups.") |