blob: f25e05dd2906e41836f0f04723dd5695f162391b [file] [log] [blame]
Felipe Monteiroffa47e62017-07-05 03:37:55 +01001# Copyright 2017 AT&T Corporation.
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
16"""Fixtures for Patrole tests."""
17from __future__ import absolute_import
18
19import fixtures
20import mock
21
22from tempest import config
23
24from patrole_tempest_plugin import rbac_utils
25
26
27CONF = config.CONF
28
29
30class ConfPatcher(fixtures.Fixture):
31 """Fixture to patch and restore global CONF. Adopted from Nova.
32
33 This also resets overrides for everything that is patched during
34 its teardown.
35 """
36
37 def __init__(self, **kwargs):
38 """Constructor
39
40 :params group: if specified all config options apply to that group.
41 :params **kwargs: the rest of the kwargs are processed as a
42 set of key/value pairs to be set as configuration override.
43 """
44 super(ConfPatcher, self).__init__()
45 self.group = kwargs.pop('group', None)
46 self.args = kwargs
47
48 def setUp(self):
49 super(ConfPatcher, self).setUp()
50 for k, v in self.args.items():
51 self.addCleanup(CONF.clear_override, k, self.group)
52 CONF.set_override(k, v, self.group)
53
54
55class RbacUtilsFixture(fixtures.Fixture):
56 """Fixture for RbacUtils class."""
57
58 USER_ID = mock.sentinel.user_id
59 PROJECT_ID = mock.sentinel.project_id
60
61 def __init__(self, **kwargs):
62 super(RbacUtilsFixture, self).__init__()
63 self.available_roles = None
64
65 def setUp(self):
66 super(RbacUtilsFixture, self).setUp()
67
68 self.useFixture(ConfPatcher(rbac_test_role='member', group='rbac'))
69 self.useFixture(ConfPatcher(
70 admin_role='admin', auth_version='v3', group='identity'))
71
72 test_obj_kwargs = {
73 'os_primary.credentials.user_id': self.USER_ID,
74 'os_primary.credentials.tenant_id': self.PROJECT_ID,
75 'os_primary.credentials.project_id': self.PROJECT_ID,
76 'get_identity_version.return_value': 'v3'
77 }
78 self.mock_test_obj = mock.Mock(**test_obj_kwargs)
79 self.mock_time = mock.patch.object(rbac_utils, 'time').start()
80
81 self.roles_v3_client = (
82 self.mock_test_obj.get_client_manager.return_value.roles_v3_client)
83
84 def switch_role(self, *role_toggles):
85 """Instantiate `rbac_utils.RbacUtils` and call `switch_role`.
86
87 Create an instance of `rbac_utils.RbacUtils` and call `switch_role`
88 for each boolean value in `role_toggles`. The number of calls to
89 `switch_role` is always 1 + len(role_toggles) because the
90 `rbac_utils.RbacUtils` constructor automatically calls `switch_role`.
91
92 :param role_toggles: the list of boolean values iterated over and
93 passed to `switch_role`.
94 """
95 if not self.available_roles:
96 self.set_roles('admin', 'member')
97
98 self.fake_rbac_utils = rbac_utils.RbacUtils(self.mock_test_obj)
99 for role_toggle in role_toggles:
100 self.fake_rbac_utils.switch_role(self.mock_test_obj, role_toggle)
101
102 def set_roles(self, *roles):
103 """Set the list of available roles in the system to `roles`."""
104 self.available_roles = {
105 'roles': [{'name': role, 'id': '%s_id' % role} for role in roles]
106 }
107 self.roles_v3_client.list_user_roles_on_project.return_value =\
108 self.available_roles
109 self.roles_v3_client.list_roles.return_value = self.available_roles