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