1# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5#      http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13"""
14test_security_groups
15----------------------------------
16
17Functional tests for `shade` security_groups resource.
18"""
19
20from openstack.tests.functional import base
21
22
23class TestSecurityGroups(base.BaseFunctionalTest):
24    def test_create_list_security_groups(self):
25        sg1 = self.user_cloud.create_security_group(
26            name="sg1", description="sg1")
27        self.addCleanup(self.user_cloud.delete_security_group, sg1['id'])
28        sg2 = self.operator_cloud.create_security_group(
29            name="sg2", description="sg2")
30        self.addCleanup(self.operator_cloud.delete_security_group, sg2['id'])
31
32        if self.user_cloud.has_service('network'):
33            # Neutron defaults to all_tenants=1 when admin
34            sg_list = self.operator_cloud.list_security_groups()
35            self.assertIn(sg1['id'], [sg['id'] for sg in sg_list])
36
37            # Filter by tenant_id (filtering by project_id won't work with
38            # Keystone V2)
39            sg_list = self.operator_cloud.list_security_groups(
40                filters={'tenant_id': self.user_cloud.current_project_id})
41            self.assertIn(sg1['id'], [sg['id'] for sg in sg_list])
42            self.assertNotIn(sg2['id'], [sg['id'] for sg in sg_list])
43
44        else:
45            # Nova does not list all tenants by default
46            sg_list = self.operator_cloud.list_security_groups()
47            self.assertIn(sg2['id'], [sg['id'] for sg in sg_list])
48            self.assertNotIn(sg1['id'], [sg['id'] for sg in sg_list])
49
50            sg_list = self.operator_cloud.list_security_groups(
51                filters={'all_tenants': 1})
52            self.assertIn(sg1['id'], [sg['id'] for sg in sg_list])
53
54    def test_get_security_group_by_id(self):
55        sg = self.user_cloud.create_security_group(name='sg', description='sg')
56        self.addCleanup(self.user_cloud.delete_security_group, sg['id'])
57
58        ret_sg = self.user_cloud.get_security_group_by_id(sg['id'])
59        self.assertEqual(sg, ret_sg)
60