blob: b595c8889b70cce41a4219a96c1a2d9dfdc53063 [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
20from oslo_config import cfg
Matthew Treinishc791ac42014-07-16 09:15:23 -040021from oslotest import mockpatch
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +010022import shutil
Matthew Treinish1c517a22015-04-23 11:39:44 -040023import six
Matthew Treinishc791ac42014-07-16 09:15:23 -040024
Matthew Treinishf83f35c2015-04-10 11:59:11 -040025from tempest.common import cred_provider
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
29from tempest.lib import exceptions as lib_exc
30from tempest.lib.services.identity.v2 import token_client
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
Matthew Treinishc791ac42014-07-16 09:15:23 -040046 def setUp(self):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -070047 super(TestPreProvisionedCredentials, self).setUp()
Matthew Treinishc791ac42014-07-16 09:15:23 -040048 self.useFixture(fake_config.ConfigFixture())
Jordan Pittier0021c292016-03-29 21:33:34 +020049 self.patchobject(config, 'TempestConfigPrivate',
50 fake_config.FakePrivate)
51 self.patchobject(token_client.TokenClient, 'raw_request',
52 fake_identity._fake_v2_response)
Doug Hellmann583ce2c2015-03-11 14:55:46 +000053 self.useFixture(lockutils_fixtures.ExternalLockFixture())
Matthew Treinishc791ac42014-07-16 09:15:23 -040054 self.test_accounts = [
55 {'username': 'test_user1', 'tenant_name': 'test_tenant1',
56 'password': 'p'},
57 {'username': 'test_user2', 'tenant_name': 'test_tenant2',
58 'password': 'p'},
59 {'username': 'test_user3', 'tenant_name': 'test_tenant3',
60 'password': 'p'},
61 {'username': 'test_user4', 'tenant_name': 'test_tenant4',
62 'password': 'p'},
63 {'username': 'test_user5', 'tenant_name': 'test_tenant5',
64 'password': 'p'},
65 {'username': 'test_user6', 'tenant_name': 'test_tenant6',
Matthew Treinish976e8df2014-12-19 14:21:54 -050066 'password': 'p', 'roles': ['role1', 'role2']},
67 {'username': 'test_user7', 'tenant_name': 'test_tenant7',
68 'password': 'p', 'roles': ['role2', 'role3']},
69 {'username': 'test_user8', 'tenant_name': 'test_tenant8',
70 'password': 'p', 'roles': ['role4', 'role1']},
71 {'username': 'test_user9', 'tenant_name': 'test_tenant9',
72 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},
73 {'username': 'test_user10', 'tenant_name': 'test_tenant10',
74 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},
75 {'username': 'test_user11', 'tenant_name': 'test_tenant11',
76 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]},
77 {'username': 'test_user12', 'tenant_name': 'test_tenant12',
78 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]},
Matthew Treinishc791ac42014-07-16 09:15:23 -040079 ]
Matthew Treinisha59bd0c2015-04-20 12:02:48 -040080 self.accounts_mock = self.useFixture(mockpatch.Patch(
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -070081 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinishc791ac42014-07-16 09:15:23 -040082 return_value=self.test_accounts))
Matthew Treinishb19eeb82014-09-04 09:57:46 -040083 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Matthew Treinishc791ac42014-07-16 09:15:23 -040084
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +010085 def tearDown(self):
86 super(TestPreProvisionedCredentials, self).tearDown()
87 shutil.rmtree(self.fixed_params['accounts_lock_dir'],
88 ignore_errors=True)
89
Matthew Treinishc791ac42014-07-16 09:15:23 -040090 def _get_hash_list(self, accounts_list):
91 hash_list = []
92 for account in accounts_list:
93 hash = hashlib.md5()
Matthew Treinish1c517a22015-04-23 11:39:44 -040094 hash.update(six.text_type(account).encode('utf-8'))
Matthew Treinish976e8df2014-12-19 14:21:54 -050095 temp_hash = hash.hexdigest()
96 hash_list.append(temp_hash)
Matthew Treinishc791ac42014-07-16 09:15:23 -040097 return hash_list
98
99 def test_get_hash(self):
Jordan Pittier0021c292016-03-29 21:33:34 +0200100 self.patchobject(token_client.TokenClient, 'raw_request',
101 fake_identity._fake_v2_response)
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700102 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100103 **self.fixed_params)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400104 hash_list = self._get_hash_list(self.test_accounts)
105 test_cred_dict = self.test_accounts[3]
ghanshyam5ff763f2015-02-18 16:15:58 +0900106 test_creds = auth.get_credentials(fake_identity.FAKE_AUTH_URL,
107 **test_cred_dict)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400108 results = test_account_class.get_hash(test_creds)
109 self.assertEqual(hash_list[3], results)
110
111 def test_get_hash_dict(self):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700112 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100113 **self.fixed_params)
Andrea Frittoli (andreaf)29491a72015-10-13 11:24:17 +0100114 hash_dict = test_account_class.get_hash_dict(
115 self.test_accounts, self.fixed_params['admin_role'])
Matthew Treinishc791ac42014-07-16 09:15:23 -0400116 hash_list = self._get_hash_list(self.test_accounts)
117 for hash in hash_list:
Matthew Treinish976e8df2014-12-19 14:21:54 -0500118 self.assertIn(hash, hash_dict['creds'].keys())
119 self.assertIn(hash_dict['creds'][hash], self.test_accounts)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400120
121 def test_create_hash_file_previous_file(self):
122 # Emulate the lock existing on the filesystem
123 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400124 with mock.patch('six.moves.builtins.open', mock.mock_open(),
125 create=True):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700126 test_account_class = (
127 preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100128 **self.fixed_params))
Matthew Treinishc791ac42014-07-16 09:15:23 -0400129 res = test_account_class._create_hash_file('12345')
130 self.assertFalse(res, "_create_hash_file should return False if the "
131 "pseudo-lock file already exists")
132
133 def test_create_hash_file_no_previous_file(self):
134 # Emulate the lock not existing on the filesystem
135 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400136 with mock.patch('six.moves.builtins.open', mock.mock_open(),
137 create=True):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700138 test_account_class = (
139 preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100140 **self.fixed_params))
Matthew Treinishc791ac42014-07-16 09:15:23 -0400141 res = test_account_class._create_hash_file('12345')
142 self.assertTrue(res, "_create_hash_file should return True if the "
143 "pseudo-lock doesn't already exist")
144
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000145 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400146 def test_get_free_hash_no_previous_accounts(self, lock_mock):
147 # Emulate no pre-existing lock
148 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=False))
149 hash_list = self._get_hash_list(self.test_accounts)
150 mkdir_mock = self.useFixture(mockpatch.Patch('os.mkdir'))
151 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700152 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100153 **self.fixed_params)
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400154 with mock.patch('six.moves.builtins.open', mock.mock_open(),
Matthew Treinishc791ac42014-07-16 09:15:23 -0400155 create=True) as open_mock:
156 test_account_class._get_free_hash(hash_list)
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +0100157 lock_path = os.path.join(self.fixed_params['accounts_lock_dir'],
158 hash_list[0])
Matthew Treinishc791ac42014-07-16 09:15:23 -0400159 open_mock.assert_called_once_with(lock_path, 'w')
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +0100160 mkdir_path = os.path.join(self.fixed_params['accounts_lock_dir'])
Matthew Treinishc791ac42014-07-16 09:15:23 -0400161 mkdir_mock.mock.assert_called_once_with(mkdir_path)
162
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000163 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400164 def test_get_free_hash_no_free_accounts(self, lock_mock):
165 hash_list = self._get_hash_list(self.test_accounts)
166 # Emulate pre-existing lock dir
167 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))
168 # Emulate all lcoks in list are in use
169 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700170 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100171 **self.fixed_params)
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400172 with mock.patch('six.moves.builtins.open', mock.mock_open(),
173 create=True):
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +0100174 self.assertRaises(lib_exc.InvalidCredentials,
Matthew Treinish4041b262015-02-27 11:18:54 -0500175 test_account_class._get_free_hash, hash_list)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400176
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000177 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400178 def test_get_free_hash_some_in_use_accounts(self, lock_mock):
179 # Emulate no pre-existing lock
180 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))
181 hash_list = self._get_hash_list(self.test_accounts)
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700182 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100183 **self.fixed_params)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400184
185 def _fake_is_file(path):
186 # Fake isfile() to return that the path exists unless a specific
187 # hash is in the path
188 if hash_list[3] in path:
189 return False
190 return True
191
Jordan Pittier0021c292016-03-29 21:33:34 +0200192 self.patchobject(os.path, 'isfile', _fake_is_file)
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400193 with mock.patch('six.moves.builtins.open', mock.mock_open(),
Matthew Treinishc791ac42014-07-16 09:15:23 -0400194 create=True) as open_mock:
195 test_account_class._get_free_hash(hash_list)
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +0100196 lock_path = os.path.join(self.fixed_params['accounts_lock_dir'],
197 hash_list[3])
Matthew Treinish4041b262015-02-27 11:18:54 -0500198 open_mock.assert_has_calls([mock.call(lock_path, 'w')])
Matthew Treinishc791ac42014-07-16 09:15:23 -0400199
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000200 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400201 def test_remove_hash_last_account(self, lock_mock):
202 hash_list = self._get_hash_list(self.test_accounts)
203 # Pretend the pseudo-lock is there
204 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
205 # Pretend the lock dir is empty
206 self.useFixture(mockpatch.Patch('os.listdir', return_value=[]))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700207 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100208 **self.fixed_params)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400209 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
210 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
211 test_account_class.remove_hash(hash_list[2])
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +0100212 hash_path = os.path.join(self.fixed_params['accounts_lock_dir'],
Matthew Treinishc791ac42014-07-16 09:15:23 -0400213 hash_list[2])
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +0100214 lock_path = self.fixed_params['accounts_lock_dir']
Matthew Treinishc791ac42014-07-16 09:15:23 -0400215 remove_mock.mock.assert_called_once_with(hash_path)
216 rmdir_mock.mock.assert_called_once_with(lock_path)
217
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000218 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400219 def test_remove_hash_not_last_account(self, lock_mock):
220 hash_list = self._get_hash_list(self.test_accounts)
221 # Pretend the pseudo-lock is there
222 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
223 # Pretend the lock dir is empty
224 self.useFixture(mockpatch.Patch('os.listdir', return_value=[
225 hash_list[1], hash_list[4]]))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700226 test_account_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100227 **self.fixed_params)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400228 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
229 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
230 test_account_class.remove_hash(hash_list[2])
Andrea Frittoli (andreaf)848e3482015-10-12 14:17:21 +0100231 hash_path = os.path.join(self.fixed_params['accounts_lock_dir'],
Matthew Treinishc791ac42014-07-16 09:15:23 -0400232 hash_list[2])
233 remove_mock.mock.assert_called_once_with(hash_path)
234 rmdir_mock.mock.assert_not_called()
Matthew Treinish09f17832014-08-15 15:22:50 -0400235
236 def test_is_multi_user(self):
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700237 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100238 **self.fixed_params)
Matthew Treinish09f17832014-08-15 15:22:50 -0400239 self.assertTrue(test_accounts_class.is_multi_user())
240
241 def test_is_not_multi_user(self):
242 self.test_accounts = [self.test_accounts[0]]
243 self.useFixture(mockpatch.Patch(
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700244 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinish09f17832014-08-15 15:22:50 -0400245 return_value=self.test_accounts))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700246 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100247 **self.fixed_params)
Matthew Treinish09f17832014-08-15 15:22:50 -0400248 self.assertFalse(test_accounts_class.is_multi_user())
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100249
Matthew Treinish976e8df2014-12-19 14:21:54 -0500250 def test__get_creds_by_roles_one_role(self):
251 self.useFixture(mockpatch.Patch(
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700252 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinish976e8df2014-12-19 14:21:54 -0500253 return_value=self.test_accounts))
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 Treinish976e8df2014-12-19 14:21:54 -0500256 hashes = test_accounts_class.hash_dict['roles']['role4']
257 temp_hash = hashes[0]
258 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
259 test_accounts_class, '_get_free_hash', return_value=temp_hash))
260 # Test a single role returns all matching roles
261 test_accounts_class._get_creds(roles=['role4'])
262 calls = get_free_hash_mock.mock.mock_calls
263 self.assertEqual(len(calls), 1)
264 args = calls[0][1][0]
265 for i in hashes:
266 self.assertIn(i, args)
267
268 def test__get_creds_by_roles_list_role(self):
269 self.useFixture(mockpatch.Patch(
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700270 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinish976e8df2014-12-19 14:21:54 -0500271 return_value=self.test_accounts))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700272 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100273 **self.fixed_params)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500274 hashes = test_accounts_class.hash_dict['roles']['role4']
275 hashes2 = test_accounts_class.hash_dict['roles']['role2']
276 hashes = list(set(hashes) & set(hashes2))
277 temp_hash = hashes[0]
278 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
279 test_accounts_class, '_get_free_hash', return_value=temp_hash))
280 # Test an intersection of multiple roles
281 test_accounts_class._get_creds(roles=['role2', 'role4'])
282 calls = get_free_hash_mock.mock.mock_calls
283 self.assertEqual(len(calls), 1)
284 args = calls[0][1][0]
285 for i in hashes:
286 self.assertIn(i, args)
287
288 def test__get_creds_by_roles_no_admin(self):
289 self.useFixture(mockpatch.Patch(
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700290 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinish976e8df2014-12-19 14:21:54 -0500291 return_value=self.test_accounts))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700292 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100293 **self.fixed_params)
Matthew Treinish1c517a22015-04-23 11:39:44 -0400294 hashes = list(test_accounts_class.hash_dict['creds'].keys())
Matthew Treinish976e8df2014-12-19 14:21:54 -0500295 admin_hashes = test_accounts_class.hash_dict['roles'][
296 cfg.CONF.identity.admin_role]
297 temp_hash = hashes[0]
298 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
299 test_accounts_class, '_get_free_hash', return_value=temp_hash))
300 # Test an intersection of multiple roles
301 test_accounts_class._get_creds()
302 calls = get_free_hash_mock.mock.mock_calls
303 self.assertEqual(len(calls), 1)
304 args = calls[0][1][0]
Matthew Treinisha59bd0c2015-04-20 12:02:48 -0400305 self.assertEqual(len(args), 10)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500306 for i in admin_hashes:
307 self.assertNotIn(i, args)
308
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400309 def test_networks_returned_with_creds(self):
Matthew Treinisha59bd0c2015-04-20 12:02:48 -0400310 test_accounts = [
311 {'username': 'test_user13', 'tenant_name': 'test_tenant13',
312 'password': 'p', 'resources': {'network': 'network-1'}},
313 {'username': 'test_user14', 'tenant_name': 'test_tenant14',
314 'password': 'p', 'roles': ['role-7', 'role-11'],
315 'resources': {'network': 'network-2'}}]
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400316 self.useFixture(mockpatch.Patch(
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700317 'tempest.common.preprov_creds.read_accounts_yaml',
Matthew Treinisha59bd0c2015-04-20 12:02:48 -0400318 return_value=test_accounts))
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -0700319 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(
Andrea Frittoli (andreaf)32d0de12015-10-09 14:43:53 +0100320 **self.fixed_params)
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -0500321 with mock.patch('tempest.lib.services.compute.networks_client.'
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +0000322 'NetworksClient.list_networks',
ghanshyamf0f7cfc2015-08-24 16:21:18 +0900323 return_value={'networks': [{'name': 'network-2',
324 'id': 'fake-id',
325 'label': 'network-2'}]}):
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400326 creds = test_accounts_class.get_creds_by_roles(['role-7'])
Shuquan Huang29e9cab2015-12-30 22:43:49 +0800327 self.assertIsInstance(creds, cred_provider.TestResources)
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400328 network = creds.network
329 self.assertIsNotNone(network)
330 self.assertIn('name', network)
331 self.assertIn('id', network)
332 self.assertEqual('fake-id', network['id'])
333 self.assertEqual('network-2', network['name'])