blob: e813b2edd27eb5040724a7bf934175a7f0a4ce14 [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
Matthew Treinishc791ac42014-07-16 09:15:23 -040017
18import mock
Doug Hellmann583ce2c2015-03-11 14:55:46 +000019from oslo_concurrency.fixture import lockutils as lockutils_fixtures
Matthew Treinishb503f032015-03-12 15:48:49 -040020from oslo_concurrency import lockutils
Doug Hellmann583ce2c2015-03-11 14:55:46 +000021from oslo_config import cfg
Matthew Treinishc791ac42014-07-16 09:15:23 -040022from oslotest import mockpatch
Matthew Treinish1c517a22015-04-23 11:39:44 -040023import six
andreafb8a52282015-03-19 22:21:54 +000024from tempest_lib import auth
25from tempest_lib.services.identity.v2 import token_client
Matthew Treinishc791ac42014-07-16 09:15:23 -040026
Matthew Treinishf83f35c2015-04-10 11:59:11 -040027from tempest.common import cred_provider
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -070028from tempest.common import preprov_creds
Matthew Treinishc791ac42014-07-16 09:15:23 -040029from tempest import config
30from tempest import exceptions
31from tempest.tests import base
32from tempest.tests import fake_config
Matthew Treinishf83f35c2015-04-10 11:59:11 -040033from tempest.tests import fake_http
Matthew Treinishc791ac42014-07-16 09:15:23 -040034from tempest.tests import fake_identity
35
36
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -070037class TestPreProvisionedCredentials(base.TestCase):
Matthew Treinishc791ac42014-07-16 09:15:23 -040038
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +010039 fixed_params = {'name': 'test class',
40 'identity_version': 'v2'}
41
Matthew Treinishc791ac42014-07-16 09:15:23 -040042 def setUp(self):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -070043 super(TestPreProvisionedCredentials, self).setUp()
Matthew Treinishc791ac42014-07-16 09:15:23 -040044 self.useFixture(fake_config.ConfigFixture())
45 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)
Matthew Treinishf83f35c2015-04-10 11:59:11 -040046 self.fake_http = fake_http.fake_httplib2(return_type=200)
Ken'ichi Ohmichi96e72792015-09-09 04:05:41 +000047 self.stubs.Set(token_client.TokenClient, 'raw_request',
Matthew Treinishf83f35c2015-04-10 11:59:11 -040048 fake_identity._fake_v2_response)
Doug Hellmann583ce2c2015-03-11 14:55:46 +000049 self.useFixture(lockutils_fixtures.ExternalLockFixture())
Matthew Treinishc791ac42014-07-16 09:15:23 -040050 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 Treinish976e8df2014-12-19 14:21:54 -050062 '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 Treinishc791ac42014-07-16 09:15:23 -040075 ]
Matthew Treinisha59bd0c2015-04-20 12:02:48 -040076 self.accounts_mock = self.useFixture(mockpatch.Patch(
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -070077 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinishc791ac42014-07-16 09:15:23 -040078 return_value=self.test_accounts))
Aaron Rosen48070042015-03-30 16:17:11 -070079 cfg.CONF.set_default('test_accounts_file', 'fake_path', group='auth')
Matthew Treinishb19eeb82014-09-04 09:57:46 -040080 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Matthew Treinishc791ac42014-07-16 09:15:23 -040081
82 def _get_hash_list(self, accounts_list):
83 hash_list = []
84 for account in accounts_list:
85 hash = hashlib.md5()
Matthew Treinish1c517a22015-04-23 11:39:44 -040086 hash.update(six.text_type(account).encode('utf-8'))
Matthew Treinish976e8df2014-12-19 14:21:54 -050087 temp_hash = hash.hexdigest()
88 hash_list.append(temp_hash)
Matthew Treinishc791ac42014-07-16 09:15:23 -040089 return hash_list
90
91 def test_get_hash(self):
Ken'ichi Ohmichi96e72792015-09-09 04:05:41 +000092 self.stubs.Set(token_client.TokenClient, 'raw_request',
Matthew Treinishc791ac42014-07-16 09:15:23 -040093 fake_identity._fake_v2_response)
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -070094 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +010095 **self.fixed_params)
Matthew Treinishc791ac42014-07-16 09:15:23 -040096 hash_list = self._get_hash_list(self.test_accounts)
97 test_cred_dict = self.test_accounts[3]
ghanshyam5ff763f2015-02-18 16:15:58 +090098 test_creds = auth.get_credentials(fake_identity.FAKE_AUTH_URL,
99 **test_cred_dict)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400100 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)f9e01262015-05-22 10:24:12 -0700104 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100105 **self.fixed_params)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400106 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 Treinish976e8df2014-12-19 14:21:54 -0500109 self.assertIn(hash, hash_dict['creds'].keys())
110 self.assertIn(hash_dict['creds'][hash], self.test_accounts)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400111
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 Treinish53d0dc02015-04-24 15:57:27 -0400115 with mock.patch('six.moves.builtins.open', mock.mock_open(),
116 create=True):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700117 test_account_class = (
118 preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100119 **self.fixed_params))
Matthew Treinishc791ac42014-07-16 09:15:23 -0400120 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 Treinish53d0dc02015-04-24 15:57:27 -0400127 with mock.patch('six.moves.builtins.open', mock.mock_open(),
128 create=True):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700129 test_account_class = (
130 preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100131 **self.fixed_params))
Matthew Treinishc791ac42014-07-16 09:15:23 -0400132 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 Hellmann583ce2c2015-03-11 14:55:46 +0000136 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400137 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)f9e01262015-05-22 10:24:12 -0700143 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100144 **self.fixed_params)
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400145 with mock.patch('six.moves.builtins.open', mock.mock_open(),
Matthew Treinishc791ac42014-07-16 09:15:23 -0400146 create=True) as open_mock:
147 test_account_class._get_free_hash(hash_list)
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700148 lock_path = os.path.join(lockutils.get_lock_path(
149 preprov_creds.CONF), 'test_accounts', hash_list[0])
Matthew Treinishc791ac42014-07-16 09:15:23 -0400150 open_mock.assert_called_once_with(lock_path, 'w')
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700151 mkdir_path = os.path.join(
152 preprov_creds.CONF.oslo_concurrency.lock_path, 'test_accounts')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400153 mkdir_mock.mock.assert_called_once_with(mkdir_path)
154
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000155 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400156 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)f9e01262015-05-22 10:24:12 -0700162 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100163 **self.fixed_params)
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400164 with mock.patch('six.moves.builtins.open', mock.mock_open(),
165 create=True):
Matthew Treinish4041b262015-02-27 11:18:54 -0500166 self.assertRaises(exceptions.InvalidConfiguration,
167 test_account_class._get_free_hash, hash_list)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400168
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000169 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400170 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)f9e01262015-05-22 10:24:12 -0700174 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100175 **self.fixed_params)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400176
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 Treinish53d0dc02015-04-24 15:57:27 -0400185 with mock.patch('six.moves.builtins.open', mock.mock_open(),
Matthew Treinishc791ac42014-07-16 09:15:23 -0400186 create=True) as open_mock:
187 test_account_class._get_free_hash(hash_list)
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700188 lock_path = os.path.join(
189 lockutils.get_lock_path(preprov_creds.CONF),
190 'test_accounts', hash_list[3])
Matthew Treinish4041b262015-02-27 11:18:54 -0500191 open_mock.assert_has_calls([mock.call(lock_path, 'w')])
Matthew Treinishc791ac42014-07-16 09:15:23 -0400192
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000193 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400194 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)f9e01262015-05-22 10:24:12 -0700200 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100201 **self.fixed_params)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400202 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)f9e01262015-05-22 10:24:12 -0700205 hash_path = os.path.join(lockutils.get_lock_path(preprov_creds.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000206 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400207 hash_list[2])
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700208 lock_path = os.path.join(preprov_creds.CONF.oslo_concurrency.lock_path,
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000209 'test_accounts')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400210 remove_mock.mock.assert_called_once_with(hash_path)
211 rmdir_mock.mock.assert_called_once_with(lock_path)
212
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000213 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400214 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)f9e01262015-05-22 10:24:12 -0700221 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100222 **self.fixed_params)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400223 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)f9e01262015-05-22 10:24:12 -0700226 hash_path = os.path.join(lockutils.get_lock_path(preprov_creds.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000227 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400228 hash_list[2])
229 remove_mock.mock.assert_called_once_with(hash_path)
230 rmdir_mock.mock.assert_not_called()
Matthew Treinish09f17832014-08-15 15:22:50 -0400231
232 def test_is_multi_user(self):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700233 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100234 **self.fixed_params)
Matthew Treinish09f17832014-08-15 15:22:50 -0400235 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)f9e01262015-05-22 10:24:12 -0700240 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinish09f17832014-08-15 15:22:50 -0400241 return_value=self.test_accounts))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700242 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100243 **self.fixed_params)
Matthew Treinish09f17832014-08-15 15:22:50 -0400244 self.assertFalse(test_accounts_class.is_multi_user())
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100245
Matthew Treinish976e8df2014-12-19 14:21:54 -0500246 def test__get_creds_by_roles_one_role(self):
247 self.useFixture(mockpatch.Patch(
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700248 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinish976e8df2014-12-19 14:21:54 -0500249 return_value=self.test_accounts))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700250 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100251 **self.fixed_params)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500252 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)f9e01262015-05-22 10:24:12 -0700266 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinish976e8df2014-12-19 14:21:54 -0500267 return_value=self.test_accounts))
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 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)f9e01262015-05-22 10:24:12 -0700286 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinish976e8df2014-12-19 14:21:54 -0500287 return_value=self.test_accounts))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700288 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100289 **self.fixed_params)
Matthew Treinish1c517a22015-04-23 11:39:44 -0400290 hashes = list(test_accounts_class.hash_dict['creds'].keys())
Matthew Treinish976e8df2014-12-19 14:21:54 -0500291 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 Treinisha59bd0c2015-04-20 12:02:48 -0400301 self.assertEqual(len(args), 10)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500302 for i in admin_hashes:
303 self.assertNotIn(i, args)
304
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400305 def test_networks_returned_with_creds(self):
Matthew Treinisha59bd0c2015-04-20 12:02:48 -0400306 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 Treinishf83f35c2015-04-10 11:59:11 -0400312 self.useFixture(mockpatch.Patch(
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700313 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinisha59bd0c2015-04-20 12:02:48 -0400314 return_value=test_accounts))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700315 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100316 **self.fixed_params)
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400317 with mock.patch('tempest.services.compute.json.networks_client.'
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +0000318 'NetworksClient.list_networks',
ghanshyamf0f7cfc2015-08-24 16:21:18 +0900319 return_value={'networks': [{'name': 'network-2',
320 'id': 'fake-id',
321 'label': 'network-2'}]}):
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400322 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 Frittolib1c23fc2014-09-03 13:40:08 +0100331
332class TestNotLockingAccount(base.TestCase):
333
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100334 fixed_params = {'name': 'test class',
335 'identity_version': 'v2'}
336
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100337 def setUp(self):
338 super(TestNotLockingAccount, self).setUp()
339 self.useFixture(fake_config.ConfigFixture())
340 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000341 self.useFixture(lockutils_fixtures.ExternalLockFixture())
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100342 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)f9e01262015-05-22 10:24:12 -0700351 'tempest.common.preprov_creds.read_accounts_yaml',
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100352 return_value=self.test_accounts))
353 cfg.CONF.set_default('test_accounts_file', '', group='auth')
Matthew Treinishb19eeb82014-09-04 09:57:46 -0400354 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100355
Matthew Treinishfc7cd8f2015-03-30 11:51:55 -0400356 def test_get_creds_roles_nonlocking_invalid(self):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700357 test_accounts_class = preprov_creds.NonLockingCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100358 **self.fixed_params)
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100359 self.assertRaises(exceptions.InvalidConfiguration,
Matthew Treinishfc7cd8f2015-03-30 11:51:55 -0400360 test_accounts_class.get_creds_by_roles,
361 ['fake_role'])