Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 1 | # Copyright 2014 Hewlett-Packard Development Company, L.P. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | import hashlib |
| 16 | import os |
Andrea Frittoli (andreaf) | 16d4a9a | 2016-06-02 17:12:44 +0100 | [diff] [blame] | 17 | import testtools |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 18 | |
| 19 | import mock |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 20 | from oslo_concurrency.fixture import lockutils as lockutils_fixtures |
| 21 | from oslo_config import cfg |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 22 | from oslotest import mockpatch |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 23 | import shutil |
Matthew Treinish | 1c517a2 | 2015-04-23 11:39:44 -0400 | [diff] [blame] | 24 | import six |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 25 | |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 26 | from tempest.common import preprov_creds |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 27 | from tempest import config |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 28 | from tempest.lib import auth |
Matthew Treinish | 00ab6be | 2016-10-07 16:29:18 -0400 | [diff] [blame] | 29 | from tempest.lib.common import cred_provider |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 30 | from tempest.lib import exceptions as lib_exc |
Matthew Treinish | ffad78a | 2016-04-16 14:39:52 -0400 | [diff] [blame] | 31 | from tempest.tests import base |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 32 | from tempest.tests import fake_config |
Jordan Pittier | 00f2596 | 2016-03-18 17:10:07 +0100 | [diff] [blame] | 33 | from tempest.tests.lib import fake_identity |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 34 | |
| 35 | |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 36 | class TestPreProvisionedCredentials(base.TestCase): |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 37 | |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 38 | fixed_params = {'name': 'test class', |
Andrea Frittoli (andreaf) | 29491a7 | 2015-10-13 11:24:17 +0100 | [diff] [blame] | 39 | 'identity_version': 'v2', |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 40 | 'test_accounts_file': 'fake_accounts_file', |
| 41 | 'accounts_lock_dir': 'fake_locks_dir', |
| 42 | 'admin_role': 'admin', |
| 43 | 'object_storage_operator_role': 'operator', |
| 44 | 'object_storage_reseller_admin_role': 'reseller'} |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 45 | |
Andrea Frittoli (andreaf) | 52deb8b | 2016-05-18 19:14:22 +0100 | [diff] [blame] | 46 | identity_response = fake_identity._fake_v2_response |
| 47 | token_client = ('tempest.lib.services.identity.v2.token_client' |
| 48 | '.TokenClient.raw_request') |
| 49 | |
| 50 | @classmethod |
| 51 | def _fake_accounts(cls, admin_role): |
| 52 | return [ |
| 53 | {'username': 'test_user1', 'tenant_name': 'test_tenant1', |
| 54 | 'password': 'p'}, |
| 55 | {'username': 'test_user2', 'project_name': 'test_tenant2', |
| 56 | 'password': 'p'}, |
| 57 | {'username': 'test_user3', 'tenant_name': 'test_tenant3', |
| 58 | 'password': 'p'}, |
| 59 | {'username': 'test_user4', 'project_name': 'test_tenant4', |
| 60 | 'password': 'p'}, |
| 61 | {'username': 'test_user5', 'tenant_name': 'test_tenant5', |
| 62 | 'password': 'p'}, |
| 63 | {'username': 'test_user6', 'project_name': 'test_tenant6', |
| 64 | 'password': 'p', 'roles': ['role1', 'role2']}, |
| 65 | {'username': 'test_user7', 'tenant_name': 'test_tenant7', |
| 66 | 'password': 'p', 'roles': ['role2', 'role3']}, |
| 67 | {'username': 'test_user8', 'project_name': 'test_tenant8', |
| 68 | 'password': 'p', 'roles': ['role4', 'role1']}, |
| 69 | {'username': 'test_user9', 'tenant_name': 'test_tenant9', |
| 70 | 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']}, |
| 71 | {'username': 'test_user10', 'project_name': 'test_tenant10', |
| 72 | 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']}, |
Andrea Frittoli (andreaf) | 16d4a9a | 2016-06-02 17:12:44 +0100 | [diff] [blame] | 73 | {'username': 'test_admin1', 'tenant_name': 'test_tenant11', |
Andrea Frittoli (andreaf) | 52deb8b | 2016-05-18 19:14:22 +0100 | [diff] [blame] | 74 | 'password': 'p', 'roles': [admin_role]}, |
Andrea Frittoli (andreaf) | 16d4a9a | 2016-06-02 17:12:44 +0100 | [diff] [blame] | 75 | {'username': 'test_admin2', 'project_name': 'test_tenant12', |
| 76 | 'password': 'p', 'roles': [admin_role]}, |
| 77 | {'username': 'test_admin3', 'project_name': 'test_tenant13', |
| 78 | 'password': 'p', 'types': ['admin']}] |
Andrea Frittoli (andreaf) | 52deb8b | 2016-05-18 19:14:22 +0100 | [diff] [blame] | 79 | |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 80 | def setUp(self): |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 81 | super(TestPreProvisionedCredentials, self).setUp() |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 82 | self.useFixture(fake_config.ConfigFixture()) |
Jordan Pittier | 0021c29 | 2016-03-29 21:33:34 +0200 | [diff] [blame] | 83 | self.patchobject(config, 'TempestConfigPrivate', |
| 84 | fake_config.FakePrivate) |
Andrea Frittoli (andreaf) | 52deb8b | 2016-05-18 19:14:22 +0100 | [diff] [blame] | 85 | self.patch(self.token_client, side_effect=self.identity_response) |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 86 | self.useFixture(lockutils_fixtures.ExternalLockFixture()) |
Andrea Frittoli (andreaf) | 52deb8b | 2016-05-18 19:14:22 +0100 | [diff] [blame] | 87 | self.test_accounts = self._fake_accounts(cfg.CONF.identity.admin_role) |
Matthew Treinish | a59bd0c | 2015-04-20 12:02:48 -0400 | [diff] [blame] | 88 | self.accounts_mock = self.useFixture(mockpatch.Patch( |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 89 | 'tempest.common.preprov_creds.read_accounts_yaml', |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 90 | return_value=self.test_accounts)) |
Matthew Treinish | b19eeb8 | 2014-09-04 09:57:46 -0400 | [diff] [blame] | 91 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True)) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 92 | |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 93 | def tearDown(self): |
| 94 | super(TestPreProvisionedCredentials, self).tearDown() |
| 95 | shutil.rmtree(self.fixed_params['accounts_lock_dir'], |
| 96 | ignore_errors=True) |
| 97 | |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 98 | def _get_hash_list(self, accounts_list): |
| 99 | hash_list = [] |
Andrea Frittoli (andreaf) | 52deb8b | 2016-05-18 19:14:22 +0100 | [diff] [blame] | 100 | hash_fields = ( |
| 101 | preprov_creds.PreProvisionedCredentialProvider.HASH_CRED_FIELDS) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 102 | for account in accounts_list: |
| 103 | hash = hashlib.md5() |
guo yunxian | 7bbbec1 | 2016-08-21 20:03:10 +0800 | [diff] [blame] | 104 | account_for_hash = dict((k, v) for (k, v) in account.items() |
Andrea Frittoli (andreaf) | 52deb8b | 2016-05-18 19:14:22 +0100 | [diff] [blame] | 105 | if k in hash_fields) |
| 106 | hash.update(six.text_type(account_for_hash).encode('utf-8')) |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 107 | temp_hash = hash.hexdigest() |
| 108 | hash_list.append(temp_hash) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 109 | return hash_list |
| 110 | |
| 111 | def test_get_hash(self): |
Andrea Frittoli (andreaf) | 52deb8b | 2016-05-18 19:14:22 +0100 | [diff] [blame] | 112 | # Test with all accounts to make sure we try all combinations |
| 113 | # and hide no race conditions |
| 114 | hash_index = 0 |
| 115 | for test_cred_dict in self.test_accounts: |
| 116 | test_account_class = ( |
| 117 | preprov_creds.PreProvisionedCredentialProvider( |
| 118 | **self.fixed_params)) |
| 119 | hash_list = self._get_hash_list(self.test_accounts) |
| 120 | test_creds = auth.get_credentials( |
| 121 | fake_identity.FAKE_AUTH_URL, |
| 122 | identity_version=self.fixed_params['identity_version'], |
| 123 | **test_cred_dict) |
| 124 | results = test_account_class.get_hash(test_creds) |
| 125 | self.assertEqual(hash_list[hash_index], results) |
| 126 | hash_index += 1 |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 127 | |
| 128 | def test_get_hash_dict(self): |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 129 | test_account_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 130 | **self.fixed_params) |
Andrea Frittoli (andreaf) | 29491a7 | 2015-10-13 11:24:17 +0100 | [diff] [blame] | 131 | hash_dict = test_account_class.get_hash_dict( |
| 132 | self.test_accounts, self.fixed_params['admin_role']) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 133 | hash_list = self._get_hash_list(self.test_accounts) |
| 134 | for hash in hash_list: |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 135 | self.assertIn(hash, hash_dict['creds'].keys()) |
| 136 | self.assertIn(hash_dict['creds'][hash], self.test_accounts) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 137 | |
| 138 | def test_create_hash_file_previous_file(self): |
| 139 | # Emulate the lock existing on the filesystem |
| 140 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True)) |
Matthew Treinish | 53d0dc0 | 2015-04-24 15:57:27 -0400 | [diff] [blame] | 141 | with mock.patch('six.moves.builtins.open', mock.mock_open(), |
| 142 | create=True): |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 143 | test_account_class = ( |
| 144 | preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 145 | **self.fixed_params)) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 146 | res = test_account_class._create_hash_file('12345') |
| 147 | self.assertFalse(res, "_create_hash_file should return False if the " |
| 148 | "pseudo-lock file already exists") |
| 149 | |
| 150 | def test_create_hash_file_no_previous_file(self): |
| 151 | # Emulate the lock not existing on the filesystem |
| 152 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False)) |
Matthew Treinish | 53d0dc0 | 2015-04-24 15:57:27 -0400 | [diff] [blame] | 153 | with mock.patch('six.moves.builtins.open', mock.mock_open(), |
| 154 | create=True): |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 155 | test_account_class = ( |
| 156 | preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 157 | **self.fixed_params)) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 158 | res = test_account_class._create_hash_file('12345') |
| 159 | self.assertTrue(res, "_create_hash_file should return True if the " |
| 160 | "pseudo-lock doesn't already exist") |
| 161 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 162 | @mock.patch('oslo_concurrency.lockutils.lock') |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 163 | def test_get_free_hash_no_previous_accounts(self, lock_mock): |
| 164 | # Emulate no pre-existing lock |
| 165 | self.useFixture(mockpatch.Patch('os.path.isdir', return_value=False)) |
| 166 | hash_list = self._get_hash_list(self.test_accounts) |
| 167 | mkdir_mock = self.useFixture(mockpatch.Patch('os.mkdir')) |
| 168 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False)) |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 169 | test_account_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 170 | **self.fixed_params) |
Matthew Treinish | 53d0dc0 | 2015-04-24 15:57:27 -0400 | [diff] [blame] | 171 | with mock.patch('six.moves.builtins.open', mock.mock_open(), |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 172 | create=True) as open_mock: |
| 173 | test_account_class._get_free_hash(hash_list) |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 174 | lock_path = os.path.join(self.fixed_params['accounts_lock_dir'], |
| 175 | hash_list[0]) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 176 | open_mock.assert_called_once_with(lock_path, 'w') |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 177 | mkdir_path = os.path.join(self.fixed_params['accounts_lock_dir']) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 178 | mkdir_mock.mock.assert_called_once_with(mkdir_path) |
| 179 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 180 | @mock.patch('oslo_concurrency.lockutils.lock') |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 181 | def test_get_free_hash_no_free_accounts(self, lock_mock): |
| 182 | hash_list = self._get_hash_list(self.test_accounts) |
| 183 | # Emulate pre-existing lock dir |
| 184 | self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True)) |
| 185 | # Emulate all lcoks in list are in use |
| 186 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True)) |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 187 | test_account_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 188 | **self.fixed_params) |
Matthew Treinish | 53d0dc0 | 2015-04-24 15:57:27 -0400 | [diff] [blame] | 189 | with mock.patch('six.moves.builtins.open', mock.mock_open(), |
| 190 | create=True): |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 191 | self.assertRaises(lib_exc.InvalidCredentials, |
Matthew Treinish | 4041b26 | 2015-02-27 11:18:54 -0500 | [diff] [blame] | 192 | test_account_class._get_free_hash, hash_list) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 193 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 194 | @mock.patch('oslo_concurrency.lockutils.lock') |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 195 | def test_get_free_hash_some_in_use_accounts(self, lock_mock): |
| 196 | # Emulate no pre-existing lock |
| 197 | self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True)) |
| 198 | hash_list = self._get_hash_list(self.test_accounts) |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 199 | test_account_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 200 | **self.fixed_params) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 201 | |
| 202 | def _fake_is_file(path): |
| 203 | # Fake isfile() to return that the path exists unless a specific |
| 204 | # hash is in the path |
| 205 | if hash_list[3] in path: |
| 206 | return False |
| 207 | return True |
| 208 | |
Jordan Pittier | 0021c29 | 2016-03-29 21:33:34 +0200 | [diff] [blame] | 209 | self.patchobject(os.path, 'isfile', _fake_is_file) |
Matthew Treinish | 53d0dc0 | 2015-04-24 15:57:27 -0400 | [diff] [blame] | 210 | with mock.patch('six.moves.builtins.open', mock.mock_open(), |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 211 | create=True) as open_mock: |
| 212 | test_account_class._get_free_hash(hash_list) |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 213 | lock_path = os.path.join(self.fixed_params['accounts_lock_dir'], |
| 214 | hash_list[3]) |
Matthew Treinish | 4041b26 | 2015-02-27 11:18:54 -0500 | [diff] [blame] | 215 | open_mock.assert_has_calls([mock.call(lock_path, 'w')]) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 216 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 217 | @mock.patch('oslo_concurrency.lockutils.lock') |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 218 | def test_remove_hash_last_account(self, lock_mock): |
| 219 | hash_list = self._get_hash_list(self.test_accounts) |
| 220 | # Pretend the pseudo-lock is there |
| 221 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True)) |
| 222 | # Pretend the lock dir is empty |
| 223 | self.useFixture(mockpatch.Patch('os.listdir', return_value=[])) |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 224 | test_account_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 225 | **self.fixed_params) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 226 | remove_mock = self.useFixture(mockpatch.Patch('os.remove')) |
| 227 | rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir')) |
| 228 | test_account_class.remove_hash(hash_list[2]) |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 229 | hash_path = os.path.join(self.fixed_params['accounts_lock_dir'], |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 230 | hash_list[2]) |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 231 | lock_path = self.fixed_params['accounts_lock_dir'] |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 232 | remove_mock.mock.assert_called_once_with(hash_path) |
| 233 | rmdir_mock.mock.assert_called_once_with(lock_path) |
| 234 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 235 | @mock.patch('oslo_concurrency.lockutils.lock') |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 236 | def test_remove_hash_not_last_account(self, lock_mock): |
| 237 | hash_list = self._get_hash_list(self.test_accounts) |
| 238 | # Pretend the pseudo-lock is there |
| 239 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True)) |
| 240 | # Pretend the lock dir is empty |
| 241 | self.useFixture(mockpatch.Patch('os.listdir', return_value=[ |
| 242 | hash_list[1], hash_list[4]])) |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 243 | test_account_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 244 | **self.fixed_params) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 245 | remove_mock = self.useFixture(mockpatch.Patch('os.remove')) |
| 246 | rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir')) |
| 247 | test_account_class.remove_hash(hash_list[2]) |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 248 | hash_path = os.path.join(self.fixed_params['accounts_lock_dir'], |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 249 | hash_list[2]) |
| 250 | remove_mock.mock.assert_called_once_with(hash_path) |
| 251 | rmdir_mock.mock.assert_not_called() |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 252 | |
| 253 | def test_is_multi_user(self): |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 254 | test_accounts_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 255 | **self.fixed_params) |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 256 | self.assertTrue(test_accounts_class.is_multi_user()) |
| 257 | |
| 258 | def test_is_not_multi_user(self): |
| 259 | self.test_accounts = [self.test_accounts[0]] |
| 260 | self.useFixture(mockpatch.Patch( |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 261 | 'tempest.common.preprov_creds.read_accounts_yaml', |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 262 | return_value=self.test_accounts)) |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 263 | test_accounts_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 264 | **self.fixed_params) |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 265 | self.assertFalse(test_accounts_class.is_multi_user()) |
Andrea Frittoli | b1c23fc | 2014-09-03 13:40:08 +0100 | [diff] [blame] | 266 | |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 267 | def test__get_creds_by_roles_one_role(self): |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 268 | test_accounts_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 269 | **self.fixed_params) |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 270 | hashes = test_accounts_class.hash_dict['roles']['role4'] |
| 271 | temp_hash = hashes[0] |
| 272 | get_free_hash_mock = self.useFixture(mockpatch.PatchObject( |
| 273 | test_accounts_class, '_get_free_hash', return_value=temp_hash)) |
| 274 | # Test a single role returns all matching roles |
| 275 | test_accounts_class._get_creds(roles=['role4']) |
| 276 | calls = get_free_hash_mock.mock.mock_calls |
| 277 | self.assertEqual(len(calls), 1) |
| 278 | args = calls[0][1][0] |
| 279 | for i in hashes: |
| 280 | self.assertIn(i, args) |
| 281 | |
| 282 | def test__get_creds_by_roles_list_role(self): |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 283 | test_accounts_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 284 | **self.fixed_params) |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 285 | hashes = test_accounts_class.hash_dict['roles']['role4'] |
| 286 | hashes2 = test_accounts_class.hash_dict['roles']['role2'] |
| 287 | hashes = list(set(hashes) & set(hashes2)) |
| 288 | temp_hash = hashes[0] |
| 289 | get_free_hash_mock = self.useFixture(mockpatch.PatchObject( |
| 290 | test_accounts_class, '_get_free_hash', return_value=temp_hash)) |
| 291 | # Test an intersection of multiple roles |
| 292 | test_accounts_class._get_creds(roles=['role2', 'role4']) |
| 293 | calls = get_free_hash_mock.mock.mock_calls |
| 294 | self.assertEqual(len(calls), 1) |
| 295 | args = calls[0][1][0] |
| 296 | for i in hashes: |
| 297 | self.assertIn(i, args) |
| 298 | |
| 299 | def test__get_creds_by_roles_no_admin(self): |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 300 | test_accounts_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 301 | **self.fixed_params) |
Matthew Treinish | 1c517a2 | 2015-04-23 11:39:44 -0400 | [diff] [blame] | 302 | hashes = list(test_accounts_class.hash_dict['creds'].keys()) |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 303 | admin_hashes = test_accounts_class.hash_dict['roles'][ |
| 304 | cfg.CONF.identity.admin_role] |
| 305 | temp_hash = hashes[0] |
| 306 | get_free_hash_mock = self.useFixture(mockpatch.PatchObject( |
| 307 | test_accounts_class, '_get_free_hash', return_value=temp_hash)) |
| 308 | # Test an intersection of multiple roles |
| 309 | test_accounts_class._get_creds() |
| 310 | calls = get_free_hash_mock.mock.mock_calls |
| 311 | self.assertEqual(len(calls), 1) |
| 312 | args = calls[0][1][0] |
Matthew Treinish | a59bd0c | 2015-04-20 12:02:48 -0400 | [diff] [blame] | 313 | self.assertEqual(len(args), 10) |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 314 | for i in admin_hashes: |
| 315 | self.assertNotIn(i, args) |
| 316 | |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 317 | def test_networks_returned_with_creds(self): |
Matthew Treinish | a59bd0c | 2015-04-20 12:02:48 -0400 | [diff] [blame] | 318 | test_accounts = [ |
| 319 | {'username': 'test_user13', 'tenant_name': 'test_tenant13', |
| 320 | 'password': 'p', 'resources': {'network': 'network-1'}}, |
| 321 | {'username': 'test_user14', 'tenant_name': 'test_tenant14', |
| 322 | 'password': 'p', 'roles': ['role-7', 'role-11'], |
| 323 | 'resources': {'network': 'network-2'}}] |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 324 | self.useFixture(mockpatch.Patch( |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 325 | 'tempest.common.preprov_creds.read_accounts_yaml', |
Matthew Treinish | a59bd0c | 2015-04-20 12:02:48 -0400 | [diff] [blame] | 326 | return_value=test_accounts)) |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 327 | test_accounts_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 328 | **self.fixed_params) |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 329 | with mock.patch('tempest.lib.services.compute.networks_client.' |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 330 | 'NetworksClient.list_networks', |
ghanshyam | f0f7cfc | 2015-08-24 16:21:18 +0900 | [diff] [blame] | 331 | return_value={'networks': [{'name': 'network-2', |
| 332 | 'id': 'fake-id', |
| 333 | 'label': 'network-2'}]}): |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 334 | creds = test_accounts_class.get_creds_by_roles(['role-7']) |
Shuquan Huang | 29e9cab | 2015-12-30 22:43:49 +0800 | [diff] [blame] | 335 | self.assertIsInstance(creds, cred_provider.TestResources) |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 336 | network = creds.network |
| 337 | self.assertIsNotNone(network) |
| 338 | self.assertIn('name', network) |
| 339 | self.assertIn('id', network) |
| 340 | self.assertEqual('fake-id', network['id']) |
| 341 | self.assertEqual('network-2', network['name']) |
Andrea Frittoli (andreaf) | 52deb8b | 2016-05-18 19:14:22 +0100 | [diff] [blame] | 342 | |
Andrea Frittoli (andreaf) | 16d4a9a | 2016-06-02 17:12:44 +0100 | [diff] [blame] | 343 | def test_get_primary_creds(self): |
| 344 | test_accounts_class = preprov_creds.PreProvisionedCredentialProvider( |
| 345 | **self.fixed_params) |
| 346 | primary_creds = test_accounts_class.get_primary_creds() |
| 347 | self.assertNotIn('test_admin', primary_creds.username) |
| 348 | |
| 349 | def test_get_primary_creds_none_available(self): |
| 350 | admin_accounts = [x for x in self.test_accounts if 'test_admin' |
| 351 | in x['username']] |
| 352 | self.useFixture(mockpatch.Patch( |
| 353 | 'tempest.common.preprov_creds.read_accounts_yaml', |
| 354 | return_value=admin_accounts)) |
| 355 | test_accounts_class = preprov_creds.PreProvisionedCredentialProvider( |
| 356 | **self.fixed_params) |
| 357 | with testtools.ExpectedException(lib_exc.InvalidCredentials): |
| 358 | # Get one more |
| 359 | test_accounts_class.get_primary_creds() |
| 360 | |
| 361 | def test_get_alt_creds(self): |
| 362 | test_accounts_class = preprov_creds.PreProvisionedCredentialProvider( |
| 363 | **self.fixed_params) |
| 364 | alt_creds = test_accounts_class.get_alt_creds() |
| 365 | self.assertNotIn('test_admin', alt_creds.username) |
| 366 | |
| 367 | def test_get_alt_creds_none_available(self): |
| 368 | admin_accounts = [x for x in self.test_accounts if 'test_admin' |
| 369 | in x['username']] |
| 370 | self.useFixture(mockpatch.Patch( |
| 371 | 'tempest.common.preprov_creds.read_accounts_yaml', |
| 372 | return_value=admin_accounts)) |
| 373 | test_accounts_class = preprov_creds.PreProvisionedCredentialProvider( |
| 374 | **self.fixed_params) |
| 375 | with testtools.ExpectedException(lib_exc.InvalidCredentials): |
| 376 | # Get one more |
| 377 | test_accounts_class.get_alt_creds() |
| 378 | |
| 379 | def test_get_admin_creds(self): |
| 380 | test_accounts_class = preprov_creds.PreProvisionedCredentialProvider( |
| 381 | **self.fixed_params) |
| 382 | admin_creds = test_accounts_class.get_admin_creds() |
| 383 | self.assertIn('test_admin', admin_creds.username) |
| 384 | |
| 385 | def test_get_admin_creds_by_type(self): |
| 386 | test_accounts = [ |
| 387 | {'username': 'test_user10', 'project_name': 'test_tenant10', |
| 388 | 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']}, |
| 389 | {'username': 'test_admin1', 'tenant_name': 'test_tenant11', |
| 390 | 'password': 'p', 'types': ['admin']}] |
| 391 | self.useFixture(mockpatch.Patch( |
| 392 | 'tempest.common.preprov_creds.read_accounts_yaml', |
| 393 | return_value=test_accounts)) |
| 394 | test_accounts_class = preprov_creds.PreProvisionedCredentialProvider( |
| 395 | **self.fixed_params) |
| 396 | admin_creds = test_accounts_class.get_admin_creds() |
| 397 | self.assertIn('test_admin', admin_creds.username) |
| 398 | |
| 399 | def test_get_admin_creds_by_role(self): |
| 400 | test_accounts = [ |
| 401 | {'username': 'test_user10', 'project_name': 'test_tenant10', |
| 402 | 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']}, |
| 403 | {'username': 'test_admin1', 'tenant_name': 'test_tenant11', |
| 404 | 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]}] |
| 405 | self.useFixture(mockpatch.Patch( |
| 406 | 'tempest.common.preprov_creds.read_accounts_yaml', |
| 407 | return_value=test_accounts)) |
| 408 | test_accounts_class = preprov_creds.PreProvisionedCredentialProvider( |
| 409 | **self.fixed_params) |
| 410 | admin_creds = test_accounts_class.get_admin_creds() |
| 411 | self.assertIn('test_admin', admin_creds.username) |
| 412 | |
| 413 | def test_get_admin_creds_none_available(self): |
| 414 | non_admin_accounts = [x for x in self.test_accounts if 'test_admin' |
| 415 | not in x['username']] |
| 416 | self.useFixture(mockpatch.Patch( |
| 417 | 'tempest.common.preprov_creds.read_accounts_yaml', |
| 418 | return_value=non_admin_accounts)) |
| 419 | test_accounts_class = preprov_creds.PreProvisionedCredentialProvider( |
| 420 | **self.fixed_params) |
| 421 | with testtools.ExpectedException(lib_exc.InvalidCredentials): |
| 422 | # Get one more |
| 423 | test_accounts_class.get_admin_creds() |
| 424 | |
Andrea Frittoli (andreaf) | 52deb8b | 2016-05-18 19:14:22 +0100 | [diff] [blame] | 425 | |
| 426 | class TestPreProvisionedCredentialsV3(TestPreProvisionedCredentials): |
| 427 | |
| 428 | fixed_params = {'name': 'test class', |
| 429 | 'identity_version': 'v3', |
| 430 | 'test_accounts_file': 'fake_accounts_file', |
Jordan Pittier | 04a888b | 2016-05-28 15:06:35 +0200 | [diff] [blame] | 431 | 'accounts_lock_dir': 'fake_locks_dir_v3', |
Andrea Frittoli (andreaf) | 52deb8b | 2016-05-18 19:14:22 +0100 | [diff] [blame] | 432 | 'admin_role': 'admin', |
| 433 | 'object_storage_operator_role': 'operator', |
| 434 | 'object_storage_reseller_admin_role': 'reseller'} |
| 435 | |
| 436 | identity_response = fake_identity._fake_v3_response |
| 437 | token_client = ('tempest.lib.services.identity.v3.token_client' |
| 438 | '.V3TokenClient.raw_request') |
| 439 | |
| 440 | @classmethod |
| 441 | def _fake_accounts(cls, admin_role): |
| 442 | return [ |
| 443 | {'username': 'test_user1', 'project_name': 'test_project1', |
| 444 | 'domain_name': 'domain', 'password': 'p'}, |
| 445 | {'username': 'test_user2', 'project_name': 'test_project2', |
| 446 | 'domain_name': 'domain', 'password': 'p'}, |
| 447 | {'username': 'test_user3', 'project_name': 'test_project3', |
| 448 | 'domain_name': 'domain', 'password': 'p'}, |
| 449 | {'username': 'test_user4', 'project_name': 'test_project4', |
| 450 | 'domain_name': 'domain', 'password': 'p'}, |
| 451 | {'username': 'test_user5', 'project_name': 'test_project5', |
| 452 | 'domain_name': 'domain', 'password': 'p'}, |
| 453 | {'username': 'test_user6', 'project_name': 'test_project6', |
| 454 | 'domain_name': 'domain', 'password': 'p', |
| 455 | 'roles': ['role1', 'role2']}, |
| 456 | {'username': 'test_user7', 'project_name': 'test_project7', |
| 457 | 'domain_name': 'domain', 'password': 'p', |
| 458 | 'roles': ['role2', 'role3']}, |
| 459 | {'username': 'test_user8', 'project_name': 'test_project8', |
| 460 | 'domain_name': 'domain', 'password': 'p', |
| 461 | 'roles': ['role4', 'role1']}, |
| 462 | {'username': 'test_user9', 'project_name': 'test_project9', |
| 463 | 'domain_name': 'domain', 'password': 'p', |
| 464 | 'roles': ['role1', 'role2', 'role3', 'role4']}, |
| 465 | {'username': 'test_user10', 'project_name': 'test_project10', |
| 466 | 'domain_name': 'domain', 'password': 'p', |
| 467 | 'roles': ['role1', 'role2', 'role3', 'role4']}, |
Andrea Frittoli (andreaf) | 16d4a9a | 2016-06-02 17:12:44 +0100 | [diff] [blame] | 468 | {'username': 'test_admin1', 'project_name': 'test_project11', |
| 469 | 'domain_name': 'domain', 'password': 'p', 'roles': [admin_role]}, |
| 470 | {'username': 'test_admin2', 'project_name': 'test_project12', |
| 471 | 'domain_name': 'domain', 'password': 'p', 'roles': [admin_role]}, |
| 472 | {'username': 'test_admin3', 'project_name': 'test_tenant13', |
| 473 | 'domain_name': 'domain', 'password': 'p', 'types': ['admin']}] |