blob: 1c9982c04531893fdacfda57e0f268774a99971f [file] [log] [blame]
Matthew Treinishc791ac42014-07-16 09:15:23 -04001# 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
15import hashlib
16import os
Andrea Frittoli (andreaf)16d4a9a2016-06-02 17:12:44 +010017import testtools
Matthew Treinishc791ac42014-07-16 09:15:23 -040018
19import mock
Doug Hellmann583ce2c2015-03-11 14:55:46 +000020from oslo_concurrency.fixture import lockutils as lockutils_fixtures
21from oslo_config import cfg
Matthew Treinishc791ac42014-07-16 09:15:23 -040022from oslotest import mockpatch
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +010023import shutil
Matthew Treinish1c517a22015-04-23 11:39:44 -040024import six
Matthew Treinishc791ac42014-07-16 09:15:23 -040025
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -070026from tempest.common import preprov_creds
Matthew Treinishc791ac42014-07-16 09:15:23 -040027from tempest import config
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050028from tempest.lib import auth
Matthew Treinish00ab6be2016-10-07 16:29:18 -040029from tempest.lib.common import cred_provider
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050030from tempest.lib import exceptions as lib_exc
Matthew Treinishffad78a2016-04-16 14:39:52 -040031from tempest.tests import base
Matthew Treinishc791ac42014-07-16 09:15:23 -040032from tempest.tests import fake_config
Jordan Pittier00f25962016-03-18 17:10:07 +010033from tempest.tests.lib import fake_identity
Matthew Treinishc791ac42014-07-16 09:15:23 -040034
35
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -070036class TestPreProvisionedCredentials(base.TestCase):
Matthew Treinishc791ac42014-07-16 09:15:23 -040037
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +010038 fixed_params = {'name': 'test class',
Andrea Frittoli (andreaf)29491a72015-10-13 11:24:17 +010039 'identity_version': 'v2',
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +010040 '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)32d0de12015-10-09 14:43:53 +010045
Andrea Frittoli (andreaf)52deb8b2016-05-18 19:14:22 +010046 identity_response = fake_identity._fake_v2_response
47 token_client = ('tempest.lib.services.identity.v2.token_client'
48 '.TokenClient.raw_request')
49
50 @classmethod
51 def _fake_accounts(cls, admin_role):
52 return [
53 {'username': 'test_user1', 'tenant_name': 'test_tenant1',
54 'password': 'p'},
55 {'username': 'test_user2', 'project_name': 'test_tenant2',
56 'password': 'p'},
57 {'username': 'test_user3', 'tenant_name': 'test_tenant3',
58 'password': 'p'},
59 {'username': 'test_user4', 'project_name': 'test_tenant4',
60 'password': 'p'},
61 {'username': 'test_user5', 'tenant_name': 'test_tenant5',
62 'password': 'p'},
63 {'username': 'test_user6', 'project_name': 'test_tenant6',
64 'password': 'p', 'roles': ['role1', 'role2']},
65 {'username': 'test_user7', 'tenant_name': 'test_tenant7',
66 'password': 'p', 'roles': ['role2', 'role3']},
67 {'username': 'test_user8', 'project_name': 'test_tenant8',
68 'password': 'p', 'roles': ['role4', 'role1']},
69 {'username': 'test_user9', 'tenant_name': 'test_tenant9',
70 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},
71 {'username': 'test_user10', 'project_name': 'test_tenant10',
72 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},
Andrea Frittoli (andreaf)16d4a9a2016-06-02 17:12:44 +010073 {'username': 'test_admin1', 'tenant_name': 'test_tenant11',
Andrea Frittoli (andreaf)52deb8b2016-05-18 19:14:22 +010074 'password': 'p', 'roles': [admin_role]},
Andrea Frittoli (andreaf)16d4a9a2016-06-02 17:12:44 +010075 {'username': 'test_admin2', 'project_name': 'test_tenant12',
76 'password': 'p', 'roles': [admin_role]},
77 {'username': 'test_admin3', 'project_name': 'test_tenant13',
78 'password': 'p', 'types': ['admin']}]
Andrea Frittoli (andreaf)52deb8b2016-05-18 19:14:22 +010079
Matthew Treinishc791ac42014-07-16 09:15:23 -040080 def setUp(self):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -070081 super(TestPreProvisionedCredentials, self).setUp()
Matthew Treinishc791ac42014-07-16 09:15:23 -040082 self.useFixture(fake_config.ConfigFixture())
Jordan Pittier0021c292016-03-29 21:33:34 +020083 self.patchobject(config, 'TempestConfigPrivate',
84 fake_config.FakePrivate)
Andrea Frittoli (andreaf)52deb8b2016-05-18 19:14:22 +010085 self.patch(self.token_client, side_effect=self.identity_response)
Doug Hellmann583ce2c2015-03-11 14:55:46 +000086 self.useFixture(lockutils_fixtures.ExternalLockFixture())
Andrea Frittoli (andreaf)52deb8b2016-05-18 19:14:22 +010087 self.test_accounts = self._fake_accounts(cfg.CONF.identity.admin_role)
Matthew Treinisha59bd0c2015-04-20 12:02:48 -040088 self.accounts_mock = self.useFixture(mockpatch.Patch(
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -070089 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinishc791ac42014-07-16 09:15:23 -040090 return_value=self.test_accounts))
Matthew Treinishb19eeb82014-09-04 09:57:46 -040091 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Matthew Treinishc791ac42014-07-16 09:15:23 -040092
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +010093 def tearDown(self):
94 super(TestPreProvisionedCredentials, self).tearDown()
95 shutil.rmtree(self.fixed_params['accounts_lock_dir'],
96 ignore_errors=True)
97
Matthew Treinishc791ac42014-07-16 09:15:23 -040098 def _get_hash_list(self, accounts_list):
99 hash_list = []
Andrea Frittoli (andreaf)52deb8b2016-05-18 19:14:22 +0100100 hash_fields = (
101 preprov_creds.PreProvisionedCredentialProvider.HASH_CRED_FIELDS)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400102 for account in accounts_list:
103 hash = hashlib.md5()
guo yunxian7bbbec12016-08-21 20:03:10 +0800104 account_for_hash = dict((k, v) for (k, v) in account.items()
Andrea Frittoli (andreaf)52deb8b2016-05-18 19:14:22 +0100105 if k in hash_fields)
106 hash.update(six.text_type(account_for_hash).encode('utf-8'))
Matthew Treinish976e8df2014-12-19 14:21:54 -0500107 temp_hash = hash.hexdigest()
108 hash_list.append(temp_hash)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400109 return hash_list
110
111 def test_get_hash(self):
Andrea Frittoli (andreaf)52deb8b2016-05-18 19:14:22 +0100112 # Test with all accounts to make sure we try all combinations
113 # and hide no race conditions
114 hash_index = 0
115 for test_cred_dict in self.test_accounts:
116 test_account_class = (
117 preprov_creds.PreProvisionedCredentialProvider(
118 **self.fixed_params))
119 hash_list = self._get_hash_list(self.test_accounts)
120 test_creds = auth.get_credentials(
121 fake_identity.FAKE_AUTH_URL,
122 identity_version=self.fixed_params['identity_version'],
123 **test_cred_dict)
124 results = test_account_class.get_hash(test_creds)
125 self.assertEqual(hash_list[hash_index], results)
126 hash_index += 1
Matthew Treinishc791ac42014-07-16 09:15:23 -0400127
128 def test_get_hash_dict(self):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700129 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100130 **self.fixed_params)
Andrea Frittoli (andreaf)29491a72015-10-13 11:24:17 +0100131 hash_dict = test_account_class.get_hash_dict(
132 self.test_accounts, self.fixed_params['admin_role'])
Matthew Treinishc791ac42014-07-16 09:15:23 -0400133 hash_list = self._get_hash_list(self.test_accounts)
134 for hash in hash_list:
Matthew Treinish976e8df2014-12-19 14:21:54 -0500135 self.assertIn(hash, hash_dict['creds'].keys())
136 self.assertIn(hash_dict['creds'][hash], self.test_accounts)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400137
138 def test_create_hash_file_previous_file(self):
139 # Emulate the lock existing on the filesystem
140 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400141 with mock.patch('six.moves.builtins.open', mock.mock_open(),
142 create=True):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700143 test_account_class = (
144 preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100145 **self.fixed_params))
Matthew Treinishc791ac42014-07-16 09:15:23 -0400146 res = test_account_class._create_hash_file('12345')
147 self.assertFalse(res, "_create_hash_file should return False if the "
148 "pseudo-lock file already exists")
149
150 def test_create_hash_file_no_previous_file(self):
151 # Emulate the lock not existing on the filesystem
152 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400153 with mock.patch('six.moves.builtins.open', mock.mock_open(),
154 create=True):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700155 test_account_class = (
156 preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100157 **self.fixed_params))
Matthew Treinishc791ac42014-07-16 09:15:23 -0400158 res = test_account_class._create_hash_file('12345')
159 self.assertTrue(res, "_create_hash_file should return True if the "
160 "pseudo-lock doesn't already exist")
161
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000162 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400163 def test_get_free_hash_no_previous_accounts(self, lock_mock):
164 # Emulate no pre-existing lock
165 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=False))
166 hash_list = self._get_hash_list(self.test_accounts)
167 mkdir_mock = self.useFixture(mockpatch.Patch('os.mkdir'))
168 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700169 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100170 **self.fixed_params)
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400171 with mock.patch('six.moves.builtins.open', mock.mock_open(),
Matthew Treinishc791ac42014-07-16 09:15:23 -0400172 create=True) as open_mock:
173 test_account_class._get_free_hash(hash_list)
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +0100174 lock_path = os.path.join(self.fixed_params['accounts_lock_dir'],
175 hash_list[0])
Matthew Treinishc791ac42014-07-16 09:15:23 -0400176 open_mock.assert_called_once_with(lock_path, 'w')
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +0100177 mkdir_path = os.path.join(self.fixed_params['accounts_lock_dir'])
Matthew Treinishc791ac42014-07-16 09:15:23 -0400178 mkdir_mock.mock.assert_called_once_with(mkdir_path)
179
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000180 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400181 def test_get_free_hash_no_free_accounts(self, lock_mock):
182 hash_list = self._get_hash_list(self.test_accounts)
183 # Emulate pre-existing lock dir
184 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))
185 # Emulate all lcoks in list are in use
186 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700187 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100188 **self.fixed_params)
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400189 with mock.patch('six.moves.builtins.open', mock.mock_open(),
190 create=True):
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +0100191 self.assertRaises(lib_exc.InvalidCredentials,
Matthew Treinish4041b262015-02-27 11:18:54 -0500192 test_account_class._get_free_hash, hash_list)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400193
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000194 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400195 def test_get_free_hash_some_in_use_accounts(self, lock_mock):
196 # Emulate no pre-existing lock
197 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))
198 hash_list = self._get_hash_list(self.test_accounts)
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700199 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100200 **self.fixed_params)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400201
202 def _fake_is_file(path):
203 # Fake isfile() to return that the path exists unless a specific
204 # hash is in the path
205 if hash_list[3] in path:
206 return False
207 return True
208
Jordan Pittier0021c292016-03-29 21:33:34 +0200209 self.patchobject(os.path, 'isfile', _fake_is_file)
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400210 with mock.patch('six.moves.builtins.open', mock.mock_open(),
Matthew Treinishc791ac42014-07-16 09:15:23 -0400211 create=True) as open_mock:
212 test_account_class._get_free_hash(hash_list)
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +0100213 lock_path = os.path.join(self.fixed_params['accounts_lock_dir'],
214 hash_list[3])
Matthew Treinish4041b262015-02-27 11:18:54 -0500215 open_mock.assert_has_calls([mock.call(lock_path, 'w')])
Matthew Treinishc791ac42014-07-16 09:15:23 -0400216
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000217 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400218 def test_remove_hash_last_account(self, lock_mock):
219 hash_list = self._get_hash_list(self.test_accounts)
220 # Pretend the pseudo-lock is there
221 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
222 # Pretend the lock dir is empty
223 self.useFixture(mockpatch.Patch('os.listdir', return_value=[]))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700224 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100225 **self.fixed_params)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400226 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
227 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
228 test_account_class.remove_hash(hash_list[2])
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +0100229 hash_path = os.path.join(self.fixed_params['accounts_lock_dir'],
Matthew Treinishc791ac42014-07-16 09:15:23 -0400230 hash_list[2])
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +0100231 lock_path = self.fixed_params['accounts_lock_dir']
Matthew Treinishc791ac42014-07-16 09:15:23 -0400232 remove_mock.mock.assert_called_once_with(hash_path)
233 rmdir_mock.mock.assert_called_once_with(lock_path)
234
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000235 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400236 def test_remove_hash_not_last_account(self, lock_mock):
237 hash_list = self._get_hash_list(self.test_accounts)
238 # Pretend the pseudo-lock is there
239 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
240 # Pretend the lock dir is empty
241 self.useFixture(mockpatch.Patch('os.listdir', return_value=[
242 hash_list[1], hash_list[4]]))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700243 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100244 **self.fixed_params)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400245 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
246 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
247 test_account_class.remove_hash(hash_list[2])
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +0100248 hash_path = os.path.join(self.fixed_params['accounts_lock_dir'],
Matthew Treinishc791ac42014-07-16 09:15:23 -0400249 hash_list[2])
250 remove_mock.mock.assert_called_once_with(hash_path)
251 rmdir_mock.mock.assert_not_called()
Matthew Treinish09f17832014-08-15 15:22:50 -0400252
253 def test_is_multi_user(self):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700254 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100255 **self.fixed_params)
Matthew Treinish09f17832014-08-15 15:22:50 -0400256 self.assertTrue(test_accounts_class.is_multi_user())
257
258 def test_is_not_multi_user(self):
259 self.test_accounts = [self.test_accounts[0]]
260 self.useFixture(mockpatch.Patch(
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700261 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinish09f17832014-08-15 15:22:50 -0400262 return_value=self.test_accounts))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700263 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100264 **self.fixed_params)
Matthew Treinish09f17832014-08-15 15:22:50 -0400265 self.assertFalse(test_accounts_class.is_multi_user())
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100266
Matthew Treinish976e8df2014-12-19 14:21:54 -0500267 def test__get_creds_by_roles_one_role(self):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700268 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100269 **self.fixed_params)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500270 hashes = test_accounts_class.hash_dict['roles']['role4']
271 temp_hash = hashes[0]
272 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
273 test_accounts_class, '_get_free_hash', return_value=temp_hash))
274 # Test a single role returns all matching roles
275 test_accounts_class._get_creds(roles=['role4'])
276 calls = get_free_hash_mock.mock.mock_calls
277 self.assertEqual(len(calls), 1)
278 args = calls[0][1][0]
279 for i in hashes:
280 self.assertIn(i, args)
281
282 def test__get_creds_by_roles_list_role(self):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700283 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100284 **self.fixed_params)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500285 hashes = test_accounts_class.hash_dict['roles']['role4']
286 hashes2 = test_accounts_class.hash_dict['roles']['role2']
287 hashes = list(set(hashes) & set(hashes2))
288 temp_hash = hashes[0]
289 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
290 test_accounts_class, '_get_free_hash', return_value=temp_hash))
291 # Test an intersection of multiple roles
292 test_accounts_class._get_creds(roles=['role2', 'role4'])
293 calls = get_free_hash_mock.mock.mock_calls
294 self.assertEqual(len(calls), 1)
295 args = calls[0][1][0]
296 for i in hashes:
297 self.assertIn(i, args)
298
299 def test__get_creds_by_roles_no_admin(self):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700300 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100301 **self.fixed_params)
Matthew Treinish1c517a22015-04-23 11:39:44 -0400302 hashes = list(test_accounts_class.hash_dict['creds'].keys())
Matthew Treinish976e8df2014-12-19 14:21:54 -0500303 admin_hashes = test_accounts_class.hash_dict['roles'][
304 cfg.CONF.identity.admin_role]
305 temp_hash = hashes[0]
306 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
307 test_accounts_class, '_get_free_hash', return_value=temp_hash))
308 # Test an intersection of multiple roles
309 test_accounts_class._get_creds()
310 calls = get_free_hash_mock.mock.mock_calls
311 self.assertEqual(len(calls), 1)
312 args = calls[0][1][0]
Matthew Treinisha59bd0c2015-04-20 12:02:48 -0400313 self.assertEqual(len(args), 10)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500314 for i in admin_hashes:
315 self.assertNotIn(i, args)
316
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400317 def test_networks_returned_with_creds(self):
Matthew Treinisha59bd0c2015-04-20 12:02:48 -0400318 test_accounts = [
319 {'username': 'test_user13', 'tenant_name': 'test_tenant13',
320 'password': 'p', 'resources': {'network': 'network-1'}},
321 {'username': 'test_user14', 'tenant_name': 'test_tenant14',
322 'password': 'p', 'roles': ['role-7', 'role-11'],
323 'resources': {'network': 'network-2'}}]
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400324 self.useFixture(mockpatch.Patch(
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700325 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinisha59bd0c2015-04-20 12:02:48 -0400326 return_value=test_accounts))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700327 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100328 **self.fixed_params)
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -0500329 with mock.patch('tempest.lib.services.compute.networks_client.'
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +0000330 'NetworksClient.list_networks',
ghanshyamf0f7cfc2015-08-24 16:21:18 +0900331 return_value={'networks': [{'name': 'network-2',
332 'id': 'fake-id',
333 'label': 'network-2'}]}):
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400334 creds = test_accounts_class.get_creds_by_roles(['role-7'])
Shuquan Huang29e9cab2015-12-30 22:43:49 +0800335 self.assertIsInstance(creds, cred_provider.TestResources)
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400336 network = creds.network
337 self.assertIsNotNone(network)
338 self.assertIn('name', network)
339 self.assertIn('id', network)
340 self.assertEqual('fake-id', network['id'])
341 self.assertEqual('network-2', network['name'])
Andrea Frittoli (andreaf)52deb8b2016-05-18 19:14:22 +0100342
Andrea Frittoli (andreaf)16d4a9a2016-06-02 17:12:44 +0100343 def test_get_primary_creds(self):
344 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
345 **self.fixed_params)
346 primary_creds = test_accounts_class.get_primary_creds()
347 self.assertNotIn('test_admin', primary_creds.username)
348
349 def test_get_primary_creds_none_available(self):
350 admin_accounts = [x for x in self.test_accounts if 'test_admin'
351 in x['username']]
352 self.useFixture(mockpatch.Patch(
353 'tempest.common.preprov_creds.read_accounts_yaml',
354 return_value=admin_accounts))
355 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
356 **self.fixed_params)
357 with testtools.ExpectedException(lib_exc.InvalidCredentials):
358 # Get one more
359 test_accounts_class.get_primary_creds()
360
361 def test_get_alt_creds(self):
362 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
363 **self.fixed_params)
364 alt_creds = test_accounts_class.get_alt_creds()
365 self.assertNotIn('test_admin', alt_creds.username)
366
367 def test_get_alt_creds_none_available(self):
368 admin_accounts = [x for x in self.test_accounts if 'test_admin'
369 in x['username']]
370 self.useFixture(mockpatch.Patch(
371 'tempest.common.preprov_creds.read_accounts_yaml',
372 return_value=admin_accounts))
373 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
374 **self.fixed_params)
375 with testtools.ExpectedException(lib_exc.InvalidCredentials):
376 # Get one more
377 test_accounts_class.get_alt_creds()
378
379 def test_get_admin_creds(self):
380 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
381 **self.fixed_params)
382 admin_creds = test_accounts_class.get_admin_creds()
383 self.assertIn('test_admin', admin_creds.username)
384
385 def test_get_admin_creds_by_type(self):
386 test_accounts = [
387 {'username': 'test_user10', 'project_name': 'test_tenant10',
388 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},
389 {'username': 'test_admin1', 'tenant_name': 'test_tenant11',
390 'password': 'p', 'types': ['admin']}]
391 self.useFixture(mockpatch.Patch(
392 'tempest.common.preprov_creds.read_accounts_yaml',
393 return_value=test_accounts))
394 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
395 **self.fixed_params)
396 admin_creds = test_accounts_class.get_admin_creds()
397 self.assertIn('test_admin', admin_creds.username)
398
399 def test_get_admin_creds_by_role(self):
400 test_accounts = [
401 {'username': 'test_user10', 'project_name': 'test_tenant10',
402 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},
403 {'username': 'test_admin1', 'tenant_name': 'test_tenant11',
404 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]}]
405 self.useFixture(mockpatch.Patch(
406 'tempest.common.preprov_creds.read_accounts_yaml',
407 return_value=test_accounts))
408 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
409 **self.fixed_params)
410 admin_creds = test_accounts_class.get_admin_creds()
411 self.assertIn('test_admin', admin_creds.username)
412
413 def test_get_admin_creds_none_available(self):
414 non_admin_accounts = [x for x in self.test_accounts if 'test_admin'
415 not in x['username']]
416 self.useFixture(mockpatch.Patch(
417 'tempest.common.preprov_creds.read_accounts_yaml',
418 return_value=non_admin_accounts))
419 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
420 **self.fixed_params)
421 with testtools.ExpectedException(lib_exc.InvalidCredentials):
422 # Get one more
423 test_accounts_class.get_admin_creds()
424
Andrea Frittoli (andreaf)52deb8b2016-05-18 19:14:22 +0100425
426class TestPreProvisionedCredentialsV3(TestPreProvisionedCredentials):
427
428 fixed_params = {'name': 'test class',
429 'identity_version': 'v3',
430 'test_accounts_file': 'fake_accounts_file',
Jordan Pittier04a888b2016-05-28 15:06:35 +0200431 'accounts_lock_dir': 'fake_locks_dir_v3',
Andrea Frittoli (andreaf)52deb8b2016-05-18 19:14:22 +0100432 'admin_role': 'admin',
433 'object_storage_operator_role': 'operator',
434 'object_storage_reseller_admin_role': 'reseller'}
435
436 identity_response = fake_identity._fake_v3_response
437 token_client = ('tempest.lib.services.identity.v3.token_client'
438 '.V3TokenClient.raw_request')
439
440 @classmethod
441 def _fake_accounts(cls, admin_role):
442 return [
443 {'username': 'test_user1', 'project_name': 'test_project1',
444 'domain_name': 'domain', 'password': 'p'},
445 {'username': 'test_user2', 'project_name': 'test_project2',
446 'domain_name': 'domain', 'password': 'p'},
447 {'username': 'test_user3', 'project_name': 'test_project3',
448 'domain_name': 'domain', 'password': 'p'},
449 {'username': 'test_user4', 'project_name': 'test_project4',
450 'domain_name': 'domain', 'password': 'p'},
451 {'username': 'test_user5', 'project_name': 'test_project5',
452 'domain_name': 'domain', 'password': 'p'},
453 {'username': 'test_user6', 'project_name': 'test_project6',
454 'domain_name': 'domain', 'password': 'p',
455 'roles': ['role1', 'role2']},
456 {'username': 'test_user7', 'project_name': 'test_project7',
457 'domain_name': 'domain', 'password': 'p',
458 'roles': ['role2', 'role3']},
459 {'username': 'test_user8', 'project_name': 'test_project8',
460 'domain_name': 'domain', 'password': 'p',
461 'roles': ['role4', 'role1']},
462 {'username': 'test_user9', 'project_name': 'test_project9',
463 'domain_name': 'domain', 'password': 'p',
464 'roles': ['role1', 'role2', 'role3', 'role4']},
465 {'username': 'test_user10', 'project_name': 'test_project10',
466 'domain_name': 'domain', 'password': 'p',
467 'roles': ['role1', 'role2', 'role3', 'role4']},
Andrea Frittoli (andreaf)16d4a9a2016-06-02 17:12:44 +0100468 {'username': 'test_admin1', 'project_name': 'test_project11',
469 'domain_name': 'domain', 'password': 'p', 'roles': [admin_role]},
470 {'username': 'test_admin2', 'project_name': 'test_project12',
471 'domain_name': 'domain', 'password': 'p', 'roles': [admin_role]},
472 {'username': 'test_admin3', 'project_name': 'test_tenant13',
473 'domain_name': 'domain', 'password': 'p', 'types': ['admin']}]