blob: 8a014af0514ae66df09d1fd22d2e63345a0f44c3 [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',
Andrea Frittoli (andreaf)29491a72015-10-13 11:24:17 +010040 'identity_version': 'v2',
41 'admin_role': 'admin'}
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +010042
Matthew Treinishc791ac42014-07-16 09:15:23 -040043 def setUp(self):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -070044 super(TestPreProvisionedCredentials, self).setUp()
Matthew Treinishc791ac42014-07-16 09:15:23 -040045 self.useFixture(fake_config.ConfigFixture())
46 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)
Matthew Treinishf83f35c2015-04-10 11:59:11 -040047 self.fake_http = fake_http.fake_httplib2(return_type=200)
Ken'ichi Ohmichi96e72792015-09-09 04:05:41 +000048 self.stubs.Set(token_client.TokenClient, 'raw_request',
Matthew Treinishf83f35c2015-04-10 11:59:11 -040049 fake_identity._fake_v2_response)
Doug Hellmann583ce2c2015-03-11 14:55:46 +000050 self.useFixture(lockutils_fixtures.ExternalLockFixture())
Matthew Treinishc791ac42014-07-16 09:15:23 -040051 self.test_accounts = [
52 {'username': 'test_user1', 'tenant_name': 'test_tenant1',
53 'password': 'p'},
54 {'username': 'test_user2', 'tenant_name': 'test_tenant2',
55 'password': 'p'},
56 {'username': 'test_user3', 'tenant_name': 'test_tenant3',
57 'password': 'p'},
58 {'username': 'test_user4', 'tenant_name': 'test_tenant4',
59 'password': 'p'},
60 {'username': 'test_user5', 'tenant_name': 'test_tenant5',
61 'password': 'p'},
62 {'username': 'test_user6', 'tenant_name': 'test_tenant6',
Matthew Treinish976e8df2014-12-19 14:21:54 -050063 'password': 'p', 'roles': ['role1', 'role2']},
64 {'username': 'test_user7', 'tenant_name': 'test_tenant7',
65 'password': 'p', 'roles': ['role2', 'role3']},
66 {'username': 'test_user8', 'tenant_name': 'test_tenant8',
67 'password': 'p', 'roles': ['role4', 'role1']},
68 {'username': 'test_user9', 'tenant_name': 'test_tenant9',
69 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},
70 {'username': 'test_user10', 'tenant_name': 'test_tenant10',
71 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},
72 {'username': 'test_user11', 'tenant_name': 'test_tenant11',
73 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]},
74 {'username': 'test_user12', 'tenant_name': 'test_tenant12',
75 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]},
Matthew Treinishc791ac42014-07-16 09:15:23 -040076 ]
Matthew Treinisha59bd0c2015-04-20 12:02:48 -040077 self.accounts_mock = self.useFixture(mockpatch.Patch(
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -070078 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinishc791ac42014-07-16 09:15:23 -040079 return_value=self.test_accounts))
Aaron Rosen48070042015-03-30 16:17:11 -070080 cfg.CONF.set_default('test_accounts_file', 'fake_path', group='auth')
Matthew Treinishb19eeb82014-09-04 09:57:46 -040081 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Matthew Treinishc791ac42014-07-16 09:15:23 -040082
83 def _get_hash_list(self, accounts_list):
84 hash_list = []
85 for account in accounts_list:
86 hash = hashlib.md5()
Matthew Treinish1c517a22015-04-23 11:39:44 -040087 hash.update(six.text_type(account).encode('utf-8'))
Matthew Treinish976e8df2014-12-19 14:21:54 -050088 temp_hash = hash.hexdigest()
89 hash_list.append(temp_hash)
Matthew Treinishc791ac42014-07-16 09:15:23 -040090 return hash_list
91
92 def test_get_hash(self):
Ken'ichi Ohmichi96e72792015-09-09 04:05:41 +000093 self.stubs.Set(token_client.TokenClient, 'raw_request',
Matthew Treinishc791ac42014-07-16 09:15:23 -040094 fake_identity._fake_v2_response)
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -070095 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +010096 **self.fixed_params)
Matthew Treinishc791ac42014-07-16 09:15:23 -040097 hash_list = self._get_hash_list(self.test_accounts)
98 test_cred_dict = self.test_accounts[3]
ghanshyam5ff763f2015-02-18 16:15:58 +090099 test_creds = auth.get_credentials(fake_identity.FAKE_AUTH_URL,
100 **test_cred_dict)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400101 results = test_account_class.get_hash(test_creds)
102 self.assertEqual(hash_list[3], results)
103
104 def test_get_hash_dict(self):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700105 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100106 **self.fixed_params)
Andrea Frittoli (andreaf)29491a72015-10-13 11:24:17 +0100107 hash_dict = test_account_class.get_hash_dict(
108 self.test_accounts, self.fixed_params['admin_role'])
Matthew Treinishc791ac42014-07-16 09:15:23 -0400109 hash_list = self._get_hash_list(self.test_accounts)
110 for hash in hash_list:
Matthew Treinish976e8df2014-12-19 14:21:54 -0500111 self.assertIn(hash, hash_dict['creds'].keys())
112 self.assertIn(hash_dict['creds'][hash], self.test_accounts)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400113
114 def test_create_hash_file_previous_file(self):
115 # Emulate the lock existing on the filesystem
116 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400117 with mock.patch('six.moves.builtins.open', mock.mock_open(),
118 create=True):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700119 test_account_class = (
120 preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100121 **self.fixed_params))
Matthew Treinishc791ac42014-07-16 09:15:23 -0400122 res = test_account_class._create_hash_file('12345')
123 self.assertFalse(res, "_create_hash_file should return False if the "
124 "pseudo-lock file already exists")
125
126 def test_create_hash_file_no_previous_file(self):
127 # Emulate the lock not existing on the filesystem
128 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400129 with mock.patch('six.moves.builtins.open', mock.mock_open(),
130 create=True):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700131 test_account_class = (
132 preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100133 **self.fixed_params))
Matthew Treinishc791ac42014-07-16 09:15:23 -0400134 res = test_account_class._create_hash_file('12345')
135 self.assertTrue(res, "_create_hash_file should return True if the "
136 "pseudo-lock doesn't already exist")
137
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000138 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400139 def test_get_free_hash_no_previous_accounts(self, lock_mock):
140 # Emulate no pre-existing lock
141 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=False))
142 hash_list = self._get_hash_list(self.test_accounts)
143 mkdir_mock = self.useFixture(mockpatch.Patch('os.mkdir'))
144 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700145 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100146 **self.fixed_params)
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400147 with mock.patch('six.moves.builtins.open', mock.mock_open(),
Matthew Treinishc791ac42014-07-16 09:15:23 -0400148 create=True) as open_mock:
149 test_account_class._get_free_hash(hash_list)
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700150 lock_path = os.path.join(lockutils.get_lock_path(
151 preprov_creds.CONF), 'test_accounts', hash_list[0])
Matthew Treinishc791ac42014-07-16 09:15:23 -0400152 open_mock.assert_called_once_with(lock_path, 'w')
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700153 mkdir_path = os.path.join(
154 preprov_creds.CONF.oslo_concurrency.lock_path, 'test_accounts')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400155 mkdir_mock.mock.assert_called_once_with(mkdir_path)
156
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000157 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400158 def test_get_free_hash_no_free_accounts(self, lock_mock):
159 hash_list = self._get_hash_list(self.test_accounts)
160 # Emulate pre-existing lock dir
161 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))
162 # Emulate all lcoks in list are in use
163 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700164 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100165 **self.fixed_params)
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400166 with mock.patch('six.moves.builtins.open', mock.mock_open(),
167 create=True):
Matthew Treinish4041b262015-02-27 11:18:54 -0500168 self.assertRaises(exceptions.InvalidConfiguration,
169 test_account_class._get_free_hash, hash_list)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400170
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000171 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400172 def test_get_free_hash_some_in_use_accounts(self, lock_mock):
173 # Emulate no pre-existing lock
174 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))
175 hash_list = self._get_hash_list(self.test_accounts)
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700176 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100177 **self.fixed_params)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400178
179 def _fake_is_file(path):
180 # Fake isfile() to return that the path exists unless a specific
181 # hash is in the path
182 if hash_list[3] in path:
183 return False
184 return True
185
186 self.stubs.Set(os.path, 'isfile', _fake_is_file)
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400187 with mock.patch('six.moves.builtins.open', mock.mock_open(),
Matthew Treinishc791ac42014-07-16 09:15:23 -0400188 create=True) as open_mock:
189 test_account_class._get_free_hash(hash_list)
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700190 lock_path = os.path.join(
191 lockutils.get_lock_path(preprov_creds.CONF),
192 'test_accounts', hash_list[3])
Matthew Treinish4041b262015-02-27 11:18:54 -0500193 open_mock.assert_has_calls([mock.call(lock_path, 'w')])
Matthew Treinishc791ac42014-07-16 09:15:23 -0400194
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000195 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400196 def test_remove_hash_last_account(self, lock_mock):
197 hash_list = self._get_hash_list(self.test_accounts)
198 # Pretend the pseudo-lock is there
199 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
200 # Pretend the lock dir is empty
201 self.useFixture(mockpatch.Patch('os.listdir', return_value=[]))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700202 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100203 **self.fixed_params)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400204 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
205 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
206 test_account_class.remove_hash(hash_list[2])
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700207 hash_path = os.path.join(lockutils.get_lock_path(preprov_creds.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000208 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400209 hash_list[2])
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700210 lock_path = os.path.join(preprov_creds.CONF.oslo_concurrency.lock_path,
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000211 'test_accounts')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400212 remove_mock.mock.assert_called_once_with(hash_path)
213 rmdir_mock.mock.assert_called_once_with(lock_path)
214
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000215 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400216 def test_remove_hash_not_last_account(self, lock_mock):
217 hash_list = self._get_hash_list(self.test_accounts)
218 # Pretend the pseudo-lock is there
219 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
220 # Pretend the lock dir is empty
221 self.useFixture(mockpatch.Patch('os.listdir', return_value=[
222 hash_list[1], hash_list[4]]))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700223 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100224 **self.fixed_params)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400225 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
226 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
227 test_account_class.remove_hash(hash_list[2])
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700228 hash_path = os.path.join(lockutils.get_lock_path(preprov_creds.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000229 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400230 hash_list[2])
231 remove_mock.mock.assert_called_once_with(hash_path)
232 rmdir_mock.mock.assert_not_called()
Matthew Treinish09f17832014-08-15 15:22:50 -0400233
234 def test_is_multi_user(self):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700235 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100236 **self.fixed_params)
Matthew Treinish09f17832014-08-15 15:22:50 -0400237 self.assertTrue(test_accounts_class.is_multi_user())
238
239 def test_is_not_multi_user(self):
240 self.test_accounts = [self.test_accounts[0]]
241 self.useFixture(mockpatch.Patch(
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700242 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinish09f17832014-08-15 15:22:50 -0400243 return_value=self.test_accounts))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700244 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100245 **self.fixed_params)
Matthew Treinish09f17832014-08-15 15:22:50 -0400246 self.assertFalse(test_accounts_class.is_multi_user())
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100247
Matthew Treinish976e8df2014-12-19 14:21:54 -0500248 def test__get_creds_by_roles_one_role(self):
249 self.useFixture(mockpatch.Patch(
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700250 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinish976e8df2014-12-19 14:21:54 -0500251 return_value=self.test_accounts))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700252 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100253 **self.fixed_params)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500254 hashes = test_accounts_class.hash_dict['roles']['role4']
255 temp_hash = hashes[0]
256 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
257 test_accounts_class, '_get_free_hash', return_value=temp_hash))
258 # Test a single role returns all matching roles
259 test_accounts_class._get_creds(roles=['role4'])
260 calls = get_free_hash_mock.mock.mock_calls
261 self.assertEqual(len(calls), 1)
262 args = calls[0][1][0]
263 for i in hashes:
264 self.assertIn(i, args)
265
266 def test__get_creds_by_roles_list_role(self):
267 self.useFixture(mockpatch.Patch(
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700268 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinish976e8df2014-12-19 14:21:54 -0500269 return_value=self.test_accounts))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700270 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100271 **self.fixed_params)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500272 hashes = test_accounts_class.hash_dict['roles']['role4']
273 hashes2 = test_accounts_class.hash_dict['roles']['role2']
274 hashes = list(set(hashes) & set(hashes2))
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(roles=['role2', 'role4'])
280 calls = get_free_hash_mock.mock.mock_calls
281 self.assertEqual(len(calls), 1)
282 args = calls[0][1][0]
283 for i in hashes:
284 self.assertIn(i, args)
285
286 def test__get_creds_by_roles_no_admin(self):
287 self.useFixture(mockpatch.Patch(
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700288 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinish976e8df2014-12-19 14:21:54 -0500289 return_value=self.test_accounts))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700290 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100291 **self.fixed_params)
Matthew Treinish1c517a22015-04-23 11:39:44 -0400292 hashes = list(test_accounts_class.hash_dict['creds'].keys())
Matthew Treinish976e8df2014-12-19 14:21:54 -0500293 admin_hashes = test_accounts_class.hash_dict['roles'][
294 cfg.CONF.identity.admin_role]
295 temp_hash = hashes[0]
296 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
297 test_accounts_class, '_get_free_hash', return_value=temp_hash))
298 # Test an intersection of multiple roles
299 test_accounts_class._get_creds()
300 calls = get_free_hash_mock.mock.mock_calls
301 self.assertEqual(len(calls), 1)
302 args = calls[0][1][0]
Matthew Treinisha59bd0c2015-04-20 12:02:48 -0400303 self.assertEqual(len(args), 10)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500304 for i in admin_hashes:
305 self.assertNotIn(i, args)
306
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400307 def test_networks_returned_with_creds(self):
Matthew Treinisha59bd0c2015-04-20 12:02:48 -0400308 test_accounts = [
309 {'username': 'test_user13', 'tenant_name': 'test_tenant13',
310 'password': 'p', 'resources': {'network': 'network-1'}},
311 {'username': 'test_user14', 'tenant_name': 'test_tenant14',
312 'password': 'p', 'roles': ['role-7', 'role-11'],
313 'resources': {'network': 'network-2'}}]
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400314 self.useFixture(mockpatch.Patch(
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700315 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinisha59bd0c2015-04-20 12:02:48 -0400316 return_value=test_accounts))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700317 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100318 **self.fixed_params)
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400319 with mock.patch('tempest.services.compute.json.networks_client.'
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +0000320 'NetworksClient.list_networks',
ghanshyamf0f7cfc2015-08-24 16:21:18 +0900321 return_value={'networks': [{'name': 'network-2',
322 'id': 'fake-id',
323 'label': 'network-2'}]}):
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400324 creds = test_accounts_class.get_creds_by_roles(['role-7'])
325 self.assertTrue(isinstance(creds, cred_provider.TestResources))
326 network = creds.network
327 self.assertIsNotNone(network)
328 self.assertIn('name', network)
329 self.assertIn('id', network)
330 self.assertEqual('fake-id', network['id'])
331 self.assertEqual('network-2', network['name'])
332
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100333
334class TestNotLockingAccount(base.TestCase):
335
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100336 fixed_params = {'name': 'test class',
Andrea Frittoli (andreaf)29491a72015-10-13 11:24:17 +0100337 'identity_version': 'v2',
338 'admin_role': 'admin'}
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100339
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100340 def setUp(self):
341 super(TestNotLockingAccount, self).setUp()
342 self.useFixture(fake_config.ConfigFixture())
343 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000344 self.useFixture(lockutils_fixtures.ExternalLockFixture())
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100345 self.test_accounts = [
346 {'username': 'test_user1', 'tenant_name': 'test_tenant1',
347 'password': 'p'},
348 {'username': 'test_user2', 'tenant_name': 'test_tenant2',
349 'password': 'p'},
350 {'username': 'test_user3', 'tenant_name': 'test_tenant3',
351 'password': 'p'},
352 ]
353 self.useFixture(mockpatch.Patch(
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700354 'tempest.common.preprov_creds.read_accounts_yaml',
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100355 return_value=self.test_accounts))
356 cfg.CONF.set_default('test_accounts_file', '', group='auth')
Matthew Treinishb19eeb82014-09-04 09:57:46 -0400357 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100358
Matthew Treinishfc7cd8f2015-03-30 11:51:55 -0400359 def test_get_creds_roles_nonlocking_invalid(self):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700360 test_accounts_class = preprov_creds.NonLockingCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100361 **self.fixed_params)
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100362 self.assertRaises(exceptions.InvalidConfiguration,
Matthew Treinishfc7cd8f2015-03-30 11:51:55 -0400363 test_accounts_class.get_creds_by_roles,
364 ['fake_role'])