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