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