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 |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 17 | |
| 18 | import mock |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 19 | from oslo_concurrency.fixture import lockutils as lockutils_fixtures |
| 20 | from oslo_config import cfg |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 21 | from oslotest import mockpatch |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 22 | import shutil |
Matthew Treinish | 1c517a2 | 2015-04-23 11:39:44 -0400 | [diff] [blame] | 23 | import six |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 24 | |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 25 | from tempest.common import cred_provider |
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 |
| 29 | from tempest.lib import exceptions as lib_exc |
| 30 | from tempest.lib.services.identity.v2 import token_client |
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 | |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 46 | def setUp(self): |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 47 | super(TestPreProvisionedCredentials, self).setUp() |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 48 | self.useFixture(fake_config.ConfigFixture()) |
Jordan Pittier | 0021c29 | 2016-03-29 21:33:34 +0200 | [diff] [blame] | 49 | self.patchobject(config, 'TempestConfigPrivate', |
| 50 | fake_config.FakePrivate) |
| 51 | self.patchobject(token_client.TokenClient, 'raw_request', |
| 52 | fake_identity._fake_v2_response) |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 53 | self.useFixture(lockutils_fixtures.ExternalLockFixture()) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 54 | self.test_accounts = [ |
| 55 | {'username': 'test_user1', 'tenant_name': 'test_tenant1', |
| 56 | 'password': 'p'}, |
| 57 | {'username': 'test_user2', 'tenant_name': 'test_tenant2', |
| 58 | 'password': 'p'}, |
| 59 | {'username': 'test_user3', 'tenant_name': 'test_tenant3', |
| 60 | 'password': 'p'}, |
| 61 | {'username': 'test_user4', 'tenant_name': 'test_tenant4', |
| 62 | 'password': 'p'}, |
| 63 | {'username': 'test_user5', 'tenant_name': 'test_tenant5', |
| 64 | 'password': 'p'}, |
| 65 | {'username': 'test_user6', 'tenant_name': 'test_tenant6', |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 66 | 'password': 'p', 'roles': ['role1', 'role2']}, |
| 67 | {'username': 'test_user7', 'tenant_name': 'test_tenant7', |
| 68 | 'password': 'p', 'roles': ['role2', 'role3']}, |
| 69 | {'username': 'test_user8', 'tenant_name': 'test_tenant8', |
| 70 | 'password': 'p', 'roles': ['role4', 'role1']}, |
| 71 | {'username': 'test_user9', 'tenant_name': 'test_tenant9', |
| 72 | 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']}, |
| 73 | {'username': 'test_user10', 'tenant_name': 'test_tenant10', |
| 74 | 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']}, |
| 75 | {'username': 'test_user11', 'tenant_name': 'test_tenant11', |
| 76 | 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]}, |
| 77 | {'username': 'test_user12', 'tenant_name': 'test_tenant12', |
| 78 | 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]}, |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 79 | ] |
Matthew Treinish | a59bd0c | 2015-04-20 12:02:48 -0400 | [diff] [blame] | 80 | self.accounts_mock = self.useFixture(mockpatch.Patch( |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 81 | 'tempest.common.preprov_creds.read_accounts_yaml', |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 82 | return_value=self.test_accounts)) |
Matthew Treinish | b19eeb8 | 2014-09-04 09:57:46 -0400 | [diff] [blame] | 83 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True)) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 84 | |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 85 | def tearDown(self): |
| 86 | super(TestPreProvisionedCredentials, self).tearDown() |
| 87 | shutil.rmtree(self.fixed_params['accounts_lock_dir'], |
| 88 | ignore_errors=True) |
| 89 | |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 90 | def _get_hash_list(self, accounts_list): |
| 91 | hash_list = [] |
| 92 | for account in accounts_list: |
| 93 | hash = hashlib.md5() |
Matthew Treinish | 1c517a2 | 2015-04-23 11:39:44 -0400 | [diff] [blame] | 94 | hash.update(six.text_type(account).encode('utf-8')) |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 95 | temp_hash = hash.hexdigest() |
| 96 | hash_list.append(temp_hash) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 97 | return hash_list |
| 98 | |
| 99 | def test_get_hash(self): |
Jordan Pittier | 0021c29 | 2016-03-29 21:33:34 +0200 | [diff] [blame] | 100 | self.patchobject(token_client.TokenClient, 'raw_request', |
| 101 | fake_identity._fake_v2_response) |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 102 | test_account_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 103 | **self.fixed_params) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 104 | hash_list = self._get_hash_list(self.test_accounts) |
| 105 | test_cred_dict = self.test_accounts[3] |
ghanshyam | 5ff763f | 2015-02-18 16:15:58 +0900 | [diff] [blame] | 106 | test_creds = auth.get_credentials(fake_identity.FAKE_AUTH_URL, |
| 107 | **test_cred_dict) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 108 | results = test_account_class.get_hash(test_creds) |
| 109 | self.assertEqual(hash_list[3], results) |
| 110 | |
| 111 | def test_get_hash_dict(self): |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 112 | test_account_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 113 | **self.fixed_params) |
Andrea Frittoli (andreaf) | 29491a7 | 2015-10-13 11:24:17 +0100 | [diff] [blame] | 114 | hash_dict = test_account_class.get_hash_dict( |
| 115 | self.test_accounts, self.fixed_params['admin_role']) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 116 | hash_list = self._get_hash_list(self.test_accounts) |
| 117 | for hash in hash_list: |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 118 | self.assertIn(hash, hash_dict['creds'].keys()) |
| 119 | self.assertIn(hash_dict['creds'][hash], self.test_accounts) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 120 | |
| 121 | def test_create_hash_file_previous_file(self): |
| 122 | # Emulate the lock existing on the filesystem |
| 123 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True)) |
Matthew Treinish | 53d0dc0 | 2015-04-24 15:57:27 -0400 | [diff] [blame] | 124 | with mock.patch('six.moves.builtins.open', mock.mock_open(), |
| 125 | create=True): |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 126 | test_account_class = ( |
| 127 | preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 128 | **self.fixed_params)) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 129 | res = test_account_class._create_hash_file('12345') |
| 130 | self.assertFalse(res, "_create_hash_file should return False if the " |
| 131 | "pseudo-lock file already exists") |
| 132 | |
| 133 | def test_create_hash_file_no_previous_file(self): |
| 134 | # Emulate the lock not existing on the filesystem |
| 135 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False)) |
Matthew Treinish | 53d0dc0 | 2015-04-24 15:57:27 -0400 | [diff] [blame] | 136 | with mock.patch('six.moves.builtins.open', mock.mock_open(), |
| 137 | create=True): |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 138 | test_account_class = ( |
| 139 | preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 140 | **self.fixed_params)) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 141 | res = test_account_class._create_hash_file('12345') |
| 142 | self.assertTrue(res, "_create_hash_file should return True if the " |
| 143 | "pseudo-lock doesn't already exist") |
| 144 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 145 | @mock.patch('oslo_concurrency.lockutils.lock') |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 146 | def test_get_free_hash_no_previous_accounts(self, lock_mock): |
| 147 | # Emulate no pre-existing lock |
| 148 | self.useFixture(mockpatch.Patch('os.path.isdir', return_value=False)) |
| 149 | hash_list = self._get_hash_list(self.test_accounts) |
| 150 | mkdir_mock = self.useFixture(mockpatch.Patch('os.mkdir')) |
| 151 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False)) |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 152 | test_account_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 153 | **self.fixed_params) |
Matthew Treinish | 53d0dc0 | 2015-04-24 15:57:27 -0400 | [diff] [blame] | 154 | with mock.patch('six.moves.builtins.open', mock.mock_open(), |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 155 | create=True) as open_mock: |
| 156 | test_account_class._get_free_hash(hash_list) |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 157 | lock_path = os.path.join(self.fixed_params['accounts_lock_dir'], |
| 158 | hash_list[0]) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 159 | open_mock.assert_called_once_with(lock_path, 'w') |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 160 | mkdir_path = os.path.join(self.fixed_params['accounts_lock_dir']) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 161 | mkdir_mock.mock.assert_called_once_with(mkdir_path) |
| 162 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 163 | @mock.patch('oslo_concurrency.lockutils.lock') |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 164 | def test_get_free_hash_no_free_accounts(self, lock_mock): |
| 165 | hash_list = self._get_hash_list(self.test_accounts) |
| 166 | # Emulate pre-existing lock dir |
| 167 | self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True)) |
| 168 | # Emulate all lcoks in list are in use |
| 169 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True)) |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 170 | test_account_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 171 | **self.fixed_params) |
Matthew Treinish | 53d0dc0 | 2015-04-24 15:57:27 -0400 | [diff] [blame] | 172 | with mock.patch('six.moves.builtins.open', mock.mock_open(), |
| 173 | create=True): |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 174 | self.assertRaises(lib_exc.InvalidCredentials, |
Matthew Treinish | 4041b26 | 2015-02-27 11:18:54 -0500 | [diff] [blame] | 175 | test_account_class._get_free_hash, hash_list) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 176 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 177 | @mock.patch('oslo_concurrency.lockutils.lock') |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 178 | def test_get_free_hash_some_in_use_accounts(self, lock_mock): |
| 179 | # Emulate no pre-existing lock |
| 180 | self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True)) |
| 181 | hash_list = self._get_hash_list(self.test_accounts) |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 182 | test_account_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 183 | **self.fixed_params) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 184 | |
| 185 | def _fake_is_file(path): |
| 186 | # Fake isfile() to return that the path exists unless a specific |
| 187 | # hash is in the path |
| 188 | if hash_list[3] in path: |
| 189 | return False |
| 190 | return True |
| 191 | |
Jordan Pittier | 0021c29 | 2016-03-29 21:33:34 +0200 | [diff] [blame] | 192 | self.patchobject(os.path, 'isfile', _fake_is_file) |
Matthew Treinish | 53d0dc0 | 2015-04-24 15:57:27 -0400 | [diff] [blame] | 193 | with mock.patch('six.moves.builtins.open', mock.mock_open(), |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 194 | create=True) as open_mock: |
| 195 | test_account_class._get_free_hash(hash_list) |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 196 | lock_path = os.path.join(self.fixed_params['accounts_lock_dir'], |
| 197 | hash_list[3]) |
Matthew Treinish | 4041b26 | 2015-02-27 11:18:54 -0500 | [diff] [blame] | 198 | open_mock.assert_has_calls([mock.call(lock_path, 'w')]) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 199 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 200 | @mock.patch('oslo_concurrency.lockutils.lock') |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 201 | def test_remove_hash_last_account(self, lock_mock): |
| 202 | hash_list = self._get_hash_list(self.test_accounts) |
| 203 | # Pretend the pseudo-lock is there |
| 204 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True)) |
| 205 | # Pretend the lock dir is empty |
| 206 | self.useFixture(mockpatch.Patch('os.listdir', return_value=[])) |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 207 | test_account_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 208 | **self.fixed_params) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 209 | remove_mock = self.useFixture(mockpatch.Patch('os.remove')) |
| 210 | rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir')) |
| 211 | test_account_class.remove_hash(hash_list[2]) |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 212 | hash_path = os.path.join(self.fixed_params['accounts_lock_dir'], |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 213 | hash_list[2]) |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 214 | lock_path = self.fixed_params['accounts_lock_dir'] |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 215 | remove_mock.mock.assert_called_once_with(hash_path) |
| 216 | rmdir_mock.mock.assert_called_once_with(lock_path) |
| 217 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 218 | @mock.patch('oslo_concurrency.lockutils.lock') |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 219 | def test_remove_hash_not_last_account(self, lock_mock): |
| 220 | hash_list = self._get_hash_list(self.test_accounts) |
| 221 | # Pretend the pseudo-lock is there |
| 222 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True)) |
| 223 | # Pretend the lock dir is empty |
| 224 | self.useFixture(mockpatch.Patch('os.listdir', return_value=[ |
| 225 | hash_list[1], hash_list[4]])) |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 226 | test_account_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 227 | **self.fixed_params) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 228 | remove_mock = self.useFixture(mockpatch.Patch('os.remove')) |
| 229 | rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir')) |
| 230 | test_account_class.remove_hash(hash_list[2]) |
Andrea Frittoli (andreaf) | 848e348 | 2015-10-12 14:17:21 +0100 | [diff] [blame] | 231 | hash_path = os.path.join(self.fixed_params['accounts_lock_dir'], |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 232 | hash_list[2]) |
| 233 | remove_mock.mock.assert_called_once_with(hash_path) |
| 234 | rmdir_mock.mock.assert_not_called() |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 235 | |
| 236 | def test_is_multi_user(self): |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 237 | test_accounts_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 238 | **self.fixed_params) |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 239 | self.assertTrue(test_accounts_class.is_multi_user()) |
| 240 | |
| 241 | def test_is_not_multi_user(self): |
| 242 | self.test_accounts = [self.test_accounts[0]] |
| 243 | self.useFixture(mockpatch.Patch( |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 244 | 'tempest.common.preprov_creds.read_accounts_yaml', |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 245 | return_value=self.test_accounts)) |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 246 | test_accounts_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 247 | **self.fixed_params) |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 248 | self.assertFalse(test_accounts_class.is_multi_user()) |
Andrea Frittoli | b1c23fc | 2014-09-03 13:40:08 +0100 | [diff] [blame] | 249 | |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 250 | def test__get_creds_by_roles_one_role(self): |
| 251 | self.useFixture(mockpatch.Patch( |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 252 | 'tempest.common.preprov_creds.read_accounts_yaml', |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 253 | return_value=self.test_accounts)) |
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 | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 256 | hashes = test_accounts_class.hash_dict['roles']['role4'] |
| 257 | temp_hash = hashes[0] |
| 258 | get_free_hash_mock = self.useFixture(mockpatch.PatchObject( |
| 259 | test_accounts_class, '_get_free_hash', return_value=temp_hash)) |
| 260 | # Test a single role returns all matching roles |
| 261 | test_accounts_class._get_creds(roles=['role4']) |
| 262 | calls = get_free_hash_mock.mock.mock_calls |
| 263 | self.assertEqual(len(calls), 1) |
| 264 | args = calls[0][1][0] |
| 265 | for i in hashes: |
| 266 | self.assertIn(i, args) |
| 267 | |
| 268 | def test__get_creds_by_roles_list_role(self): |
| 269 | self.useFixture(mockpatch.Patch( |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 270 | 'tempest.common.preprov_creds.read_accounts_yaml', |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 271 | return_value=self.test_accounts)) |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 272 | test_accounts_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 273 | **self.fixed_params) |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 274 | hashes = test_accounts_class.hash_dict['roles']['role4'] |
| 275 | hashes2 = test_accounts_class.hash_dict['roles']['role2'] |
| 276 | hashes = list(set(hashes) & set(hashes2)) |
| 277 | temp_hash = hashes[0] |
| 278 | get_free_hash_mock = self.useFixture(mockpatch.PatchObject( |
| 279 | test_accounts_class, '_get_free_hash', return_value=temp_hash)) |
| 280 | # Test an intersection of multiple roles |
| 281 | test_accounts_class._get_creds(roles=['role2', 'role4']) |
| 282 | calls = get_free_hash_mock.mock.mock_calls |
| 283 | self.assertEqual(len(calls), 1) |
| 284 | args = calls[0][1][0] |
| 285 | for i in hashes: |
| 286 | self.assertIn(i, args) |
| 287 | |
| 288 | def test__get_creds_by_roles_no_admin(self): |
| 289 | self.useFixture(mockpatch.Patch( |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 290 | 'tempest.common.preprov_creds.read_accounts_yaml', |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 291 | return_value=self.test_accounts)) |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 292 | test_accounts_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 293 | **self.fixed_params) |
Matthew Treinish | 1c517a2 | 2015-04-23 11:39:44 -0400 | [diff] [blame] | 294 | hashes = list(test_accounts_class.hash_dict['creds'].keys()) |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 295 | admin_hashes = test_accounts_class.hash_dict['roles'][ |
| 296 | cfg.CONF.identity.admin_role] |
| 297 | temp_hash = hashes[0] |
| 298 | get_free_hash_mock = self.useFixture(mockpatch.PatchObject( |
| 299 | test_accounts_class, '_get_free_hash', return_value=temp_hash)) |
| 300 | # Test an intersection of multiple roles |
| 301 | test_accounts_class._get_creds() |
| 302 | calls = get_free_hash_mock.mock.mock_calls |
| 303 | self.assertEqual(len(calls), 1) |
| 304 | args = calls[0][1][0] |
Matthew Treinish | a59bd0c | 2015-04-20 12:02:48 -0400 | [diff] [blame] | 305 | self.assertEqual(len(args), 10) |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 306 | for i in admin_hashes: |
| 307 | self.assertNotIn(i, args) |
| 308 | |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 309 | def test_networks_returned_with_creds(self): |
Matthew Treinish | a59bd0c | 2015-04-20 12:02:48 -0400 | [diff] [blame] | 310 | test_accounts = [ |
| 311 | {'username': 'test_user13', 'tenant_name': 'test_tenant13', |
| 312 | 'password': 'p', 'resources': {'network': 'network-1'}}, |
| 313 | {'username': 'test_user14', 'tenant_name': 'test_tenant14', |
| 314 | 'password': 'p', 'roles': ['role-7', 'role-11'], |
| 315 | 'resources': {'network': 'network-2'}}] |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 316 | self.useFixture(mockpatch.Patch( |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 317 | 'tempest.common.preprov_creds.read_accounts_yaml', |
Matthew Treinish | a59bd0c | 2015-04-20 12:02:48 -0400 | [diff] [blame] | 318 | return_value=test_accounts)) |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 319 | test_accounts_class = preprov_creds.PreProvisionedCredentialProvider( |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 320 | **self.fixed_params) |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 321 | with mock.patch('tempest.lib.services.compute.networks_client.' |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 322 | 'NetworksClient.list_networks', |
ghanshyam | f0f7cfc | 2015-08-24 16:21:18 +0900 | [diff] [blame] | 323 | return_value={'networks': [{'name': 'network-2', |
| 324 | 'id': 'fake-id', |
| 325 | 'label': 'network-2'}]}): |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 326 | creds = test_accounts_class.get_creds_by_roles(['role-7']) |
Shuquan Huang | 29e9cab | 2015-12-30 22:43:49 +0800 | [diff] [blame] | 327 | self.assertIsInstance(creds, cred_provider.TestResources) |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 328 | network = creds.network |
| 329 | self.assertIsNotNone(network) |
| 330 | self.assertIn('name', network) |
| 331 | self.assertIn('id', network) |
| 332 | self.assertEqual('fake-id', network['id']) |
| 333 | self.assertEqual('network-2', network['name']) |