blob: f357bf457e4ae08d1aa2fc9b13ba8c2dc5c98205 [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
andreafb8a52282015-03-19 22:21:54 +000023from tempest_lib import auth
24from tempest_lib.services.identity.v2 import token_client
Matthew Treinishc791ac42014-07-16 09:15:23 -040025
Matthew Treinishc791ac42014-07-16 09:15:23 -040026from tempest.common import accounts
Matthew Treinishf83f35c2015-04-10 11:59:11 -040027from tempest.common import cred_provider
Matthew Treinishc791ac42014-07-16 09:15:23 -040028from tempest import config
29from tempest import exceptions
30from tempest.tests import base
31from tempest.tests import fake_config
Matthew Treinishf83f35c2015-04-10 11:59:11 -040032from tempest.tests import fake_http
Matthew Treinishc791ac42014-07-16 09:15:23 -040033from tempest.tests import fake_identity
34
35
36class TestAccount(base.TestCase):
37
38 def setUp(self):
39 super(TestAccount, self).setUp()
40 self.useFixture(fake_config.ConfigFixture())
41 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)
Matthew Treinishf83f35c2015-04-10 11:59:11 -040042 self.fake_http = fake_http.fake_httplib2(return_type=200)
43 self.stubs.Set(token_client.TokenClientJSON, 'raw_request',
44 fake_identity._fake_v2_response)
Doug Hellmann583ce2c2015-03-11 14:55:46 +000045 self.useFixture(lockutils_fixtures.ExternalLockFixture())
Matthew Treinishc791ac42014-07-16 09:15:23 -040046 self.test_accounts = [
47 {'username': 'test_user1', 'tenant_name': 'test_tenant1',
48 'password': 'p'},
49 {'username': 'test_user2', 'tenant_name': 'test_tenant2',
50 'password': 'p'},
51 {'username': 'test_user3', 'tenant_name': 'test_tenant3',
52 'password': 'p'},
53 {'username': 'test_user4', 'tenant_name': 'test_tenant4',
54 'password': 'p'},
55 {'username': 'test_user5', 'tenant_name': 'test_tenant5',
56 'password': 'p'},
57 {'username': 'test_user6', 'tenant_name': 'test_tenant6',
Matthew Treinish976e8df2014-12-19 14:21:54 -050058 'password': 'p', 'roles': ['role1', 'role2']},
59 {'username': 'test_user7', 'tenant_name': 'test_tenant7',
60 'password': 'p', 'roles': ['role2', 'role3']},
61 {'username': 'test_user8', 'tenant_name': 'test_tenant8',
62 'password': 'p', 'roles': ['role4', 'role1']},
63 {'username': 'test_user9', 'tenant_name': 'test_tenant9',
64 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},
65 {'username': 'test_user10', 'tenant_name': 'test_tenant10',
66 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},
67 {'username': 'test_user11', 'tenant_name': 'test_tenant11',
68 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]},
69 {'username': 'test_user12', 'tenant_name': 'test_tenant12',
70 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]},
Matthew Treinishc791ac42014-07-16 09:15:23 -040071 ]
Matthew Treinisha59bd0c2015-04-20 12:02:48 -040072 self.accounts_mock = self.useFixture(mockpatch.Patch(
Matthew Treinishc791ac42014-07-16 09:15:23 -040073 'tempest.common.accounts.read_accounts_yaml',
74 return_value=self.test_accounts))
Aaron Rosen48070042015-03-30 16:17:11 -070075 cfg.CONF.set_default('test_accounts_file', 'fake_path', group='auth')
Matthew Treinishb19eeb82014-09-04 09:57:46 -040076 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Matthew Treinishc791ac42014-07-16 09:15:23 -040077
78 def _get_hash_list(self, accounts_list):
79 hash_list = []
80 for account in accounts_list:
81 hash = hashlib.md5()
82 hash.update(str(account))
Matthew Treinish976e8df2014-12-19 14:21:54 -050083 temp_hash = hash.hexdigest()
84 hash_list.append(temp_hash)
Matthew Treinishc791ac42014-07-16 09:15:23 -040085 return hash_list
86
87 def test_get_hash(self):
ghanshyamc0edda02015-02-06 15:51:40 +090088 self.stubs.Set(token_client.TokenClientJSON, 'raw_request',
Matthew Treinishc791ac42014-07-16 09:15:23 -040089 fake_identity._fake_v2_response)
Andrea Frittolic3280152015-02-26 12:42:34 +000090 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -040091 hash_list = self._get_hash_list(self.test_accounts)
92 test_cred_dict = self.test_accounts[3]
ghanshyam5ff763f2015-02-18 16:15:58 +090093 test_creds = auth.get_credentials(fake_identity.FAKE_AUTH_URL,
94 **test_cred_dict)
Matthew Treinishc791ac42014-07-16 09:15:23 -040095 results = test_account_class.get_hash(test_creds)
96 self.assertEqual(hash_list[3], results)
97
98 def test_get_hash_dict(self):
Andrea Frittolic3280152015-02-26 12:42:34 +000099 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400100 hash_dict = test_account_class.get_hash_dict(self.test_accounts)
101 hash_list = self._get_hash_list(self.test_accounts)
102 for hash in hash_list:
Matthew Treinish976e8df2014-12-19 14:21:54 -0500103 self.assertIn(hash, hash_dict['creds'].keys())
104 self.assertIn(hash_dict['creds'][hash], self.test_accounts)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400105
106 def test_create_hash_file_previous_file(self):
107 # Emulate the lock existing on the filesystem
108 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
109 with mock.patch('__builtin__.open', mock.mock_open(), create=True):
Andrea Frittolic3280152015-02-26 12:42:34 +0000110 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400111 res = test_account_class._create_hash_file('12345')
112 self.assertFalse(res, "_create_hash_file should return False if the "
113 "pseudo-lock file already exists")
114
115 def test_create_hash_file_no_previous_file(self):
116 # Emulate the lock not existing on the filesystem
117 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
118 with mock.patch('__builtin__.open', mock.mock_open(), create=True):
Andrea Frittolic3280152015-02-26 12:42:34 +0000119 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400120 res = test_account_class._create_hash_file('12345')
121 self.assertTrue(res, "_create_hash_file should return True if the "
122 "pseudo-lock doesn't already exist")
123
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000124 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400125 def test_get_free_hash_no_previous_accounts(self, lock_mock):
126 # Emulate no pre-existing lock
127 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=False))
128 hash_list = self._get_hash_list(self.test_accounts)
129 mkdir_mock = self.useFixture(mockpatch.Patch('os.mkdir'))
130 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
Andrea Frittolic3280152015-02-26 12:42:34 +0000131 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400132 with mock.patch('__builtin__.open', mock.mock_open(),
133 create=True) as open_mock:
134 test_account_class._get_free_hash(hash_list)
Matthew Treinishb503f032015-03-12 15:48:49 -0400135 lock_path = os.path.join(lockutils.get_lock_path(accounts.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000136 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400137 hash_list[0])
138 open_mock.assert_called_once_with(lock_path, 'w')
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000139 mkdir_path = os.path.join(accounts.CONF.oslo_concurrency.lock_path,
140 'test_accounts')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400141 mkdir_mock.mock.assert_called_once_with(mkdir_path)
142
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000143 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400144 def test_get_free_hash_no_free_accounts(self, lock_mock):
145 hash_list = self._get_hash_list(self.test_accounts)
146 # Emulate pre-existing lock dir
147 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))
148 # Emulate all lcoks in list are in use
149 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Andrea Frittolic3280152015-02-26 12:42:34 +0000150 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish4041b262015-02-27 11:18:54 -0500151 with mock.patch('__builtin__.open', mock.mock_open(), create=True):
152 self.assertRaises(exceptions.InvalidConfiguration,
153 test_account_class._get_free_hash, hash_list)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400154
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_some_in_use_accounts(self, lock_mock):
157 # Emulate no pre-existing lock
158 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))
159 hash_list = self._get_hash_list(self.test_accounts)
Andrea Frittolic3280152015-02-26 12:42:34 +0000160 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400161
162 def _fake_is_file(path):
163 # Fake isfile() to return that the path exists unless a specific
164 # hash is in the path
165 if hash_list[3] in path:
166 return False
167 return True
168
169 self.stubs.Set(os.path, 'isfile', _fake_is_file)
170 with mock.patch('__builtin__.open', mock.mock_open(),
171 create=True) as open_mock:
172 test_account_class._get_free_hash(hash_list)
Matthew Treinishb503f032015-03-12 15:48:49 -0400173 lock_path = os.path.join(lockutils.get_lock_path(accounts.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000174 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400175 hash_list[3])
Matthew Treinish4041b262015-02-27 11:18:54 -0500176 open_mock.assert_has_calls([mock.call(lock_path, 'w')])
Matthew Treinishc791ac42014-07-16 09:15:23 -0400177
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000178 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400179 def test_remove_hash_last_account(self, lock_mock):
180 hash_list = self._get_hash_list(self.test_accounts)
181 # Pretend the pseudo-lock is there
182 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
183 # Pretend the lock dir is empty
184 self.useFixture(mockpatch.Patch('os.listdir', return_value=[]))
Andrea Frittolic3280152015-02-26 12:42:34 +0000185 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400186 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
187 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
188 test_account_class.remove_hash(hash_list[2])
Matthew Treinishb503f032015-03-12 15:48:49 -0400189 hash_path = os.path.join(lockutils.get_lock_path(accounts.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000190 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400191 hash_list[2])
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000192 lock_path = os.path.join(accounts.CONF.oslo_concurrency.lock_path,
193 'test_accounts')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400194 remove_mock.mock.assert_called_once_with(hash_path)
195 rmdir_mock.mock.assert_called_once_with(lock_path)
196
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000197 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400198 def test_remove_hash_not_last_account(self, lock_mock):
199 hash_list = self._get_hash_list(self.test_accounts)
200 # Pretend the pseudo-lock is there
201 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
202 # Pretend the lock dir is empty
203 self.useFixture(mockpatch.Patch('os.listdir', return_value=[
204 hash_list[1], hash_list[4]]))
Andrea Frittolic3280152015-02-26 12:42:34 +0000205 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400206 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
207 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
208 test_account_class.remove_hash(hash_list[2])
Matthew Treinishb503f032015-03-12 15:48:49 -0400209 hash_path = os.path.join(lockutils.get_lock_path(accounts.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000210 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400211 hash_list[2])
212 remove_mock.mock.assert_called_once_with(hash_path)
213 rmdir_mock.mock.assert_not_called()
Matthew Treinish09f17832014-08-15 15:22:50 -0400214
215 def test_is_multi_user(self):
Andrea Frittolic3280152015-02-26 12:42:34 +0000216 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish09f17832014-08-15 15:22:50 -0400217 self.assertTrue(test_accounts_class.is_multi_user())
218
219 def test_is_not_multi_user(self):
220 self.test_accounts = [self.test_accounts[0]]
221 self.useFixture(mockpatch.Patch(
222 'tempest.common.accounts.read_accounts_yaml',
223 return_value=self.test_accounts))
Andrea Frittolic3280152015-02-26 12:42:34 +0000224 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish09f17832014-08-15 15:22:50 -0400225 self.assertFalse(test_accounts_class.is_multi_user())
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100226
Matthew Treinish976e8df2014-12-19 14:21:54 -0500227 def test__get_creds_by_roles_one_role(self):
228 self.useFixture(mockpatch.Patch(
229 'tempest.common.accounts.read_accounts_yaml',
230 return_value=self.test_accounts))
Andrea Frittolic3280152015-02-26 12:42:34 +0000231 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish976e8df2014-12-19 14:21:54 -0500232 hashes = test_accounts_class.hash_dict['roles']['role4']
233 temp_hash = hashes[0]
234 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
235 test_accounts_class, '_get_free_hash', return_value=temp_hash))
236 # Test a single role returns all matching roles
237 test_accounts_class._get_creds(roles=['role4'])
238 calls = get_free_hash_mock.mock.mock_calls
239 self.assertEqual(len(calls), 1)
240 args = calls[0][1][0]
241 for i in hashes:
242 self.assertIn(i, args)
243
244 def test__get_creds_by_roles_list_role(self):
245 self.useFixture(mockpatch.Patch(
246 'tempest.common.accounts.read_accounts_yaml',
247 return_value=self.test_accounts))
Andrea Frittolic3280152015-02-26 12:42:34 +0000248 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish976e8df2014-12-19 14:21:54 -0500249 hashes = test_accounts_class.hash_dict['roles']['role4']
250 hashes2 = test_accounts_class.hash_dict['roles']['role2']
251 hashes = list(set(hashes) & set(hashes2))
252 temp_hash = hashes[0]
253 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
254 test_accounts_class, '_get_free_hash', return_value=temp_hash))
255 # Test an intersection of multiple roles
256 test_accounts_class._get_creds(roles=['role2', 'role4'])
257 calls = get_free_hash_mock.mock.mock_calls
258 self.assertEqual(len(calls), 1)
259 args = calls[0][1][0]
260 for i in hashes:
261 self.assertIn(i, args)
262
263 def test__get_creds_by_roles_no_admin(self):
264 self.useFixture(mockpatch.Patch(
265 'tempest.common.accounts.read_accounts_yaml',
266 return_value=self.test_accounts))
Andrea Frittolic3280152015-02-26 12:42:34 +0000267 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish976e8df2014-12-19 14:21:54 -0500268 hashes = test_accounts_class.hash_dict['creds'].keys()
269 admin_hashes = test_accounts_class.hash_dict['roles'][
270 cfg.CONF.identity.admin_role]
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 an intersection of multiple roles
275 test_accounts_class._get_creds()
276 calls = get_free_hash_mock.mock.mock_calls
277 self.assertEqual(len(calls), 1)
278 args = calls[0][1][0]
Matthew Treinisha59bd0c2015-04-20 12:02:48 -0400279 self.assertEqual(len(args), 10)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500280 for i in admin_hashes:
281 self.assertNotIn(i, args)
282
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400283 def test_networks_returned_with_creds(self):
Matthew Treinisha59bd0c2015-04-20 12:02:48 -0400284 test_accounts = [
285 {'username': 'test_user13', 'tenant_name': 'test_tenant13',
286 'password': 'p', 'resources': {'network': 'network-1'}},
287 {'username': 'test_user14', 'tenant_name': 'test_tenant14',
288 'password': 'p', 'roles': ['role-7', 'role-11'],
289 'resources': {'network': 'network-2'}}]
290 # Clear previous mock using self.test_accounts
291 self.accounts_mock.cleanUp()
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400292 self.useFixture(mockpatch.Patch(
293 'tempest.common.accounts.read_accounts_yaml',
Matthew Treinisha59bd0c2015-04-20 12:02:48 -0400294 return_value=test_accounts))
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400295 test_accounts_class = accounts.Accounts('v2', 'test_name')
296 with mock.patch('tempest.services.compute.json.networks_client.'
297 'NetworksClientJSON.list_networks',
298 return_value=[{'name': 'network-2', 'id': 'fake-id'}]):
299 creds = test_accounts_class.get_creds_by_roles(['role-7'])
300 self.assertTrue(isinstance(creds, cred_provider.TestResources))
301 network = creds.network
302 self.assertIsNotNone(network)
303 self.assertIn('name', network)
304 self.assertIn('id', network)
305 self.assertEqual('fake-id', network['id'])
306 self.assertEqual('network-2', network['name'])
307
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100308
309class TestNotLockingAccount(base.TestCase):
310
311 def setUp(self):
312 super(TestNotLockingAccount, self).setUp()
313 self.useFixture(fake_config.ConfigFixture())
314 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000315 self.useFixture(lockutils_fixtures.ExternalLockFixture())
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100316 self.test_accounts = [
317 {'username': 'test_user1', 'tenant_name': 'test_tenant1',
318 'password': 'p'},
319 {'username': 'test_user2', 'tenant_name': 'test_tenant2',
320 'password': 'p'},
321 {'username': 'test_user3', 'tenant_name': 'test_tenant3',
322 'password': 'p'},
323 ]
324 self.useFixture(mockpatch.Patch(
325 'tempest.common.accounts.read_accounts_yaml',
326 return_value=self.test_accounts))
327 cfg.CONF.set_default('test_accounts_file', '', group='auth')
Matthew Treinishb19eeb82014-09-04 09:57:46 -0400328 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100329
Matthew Treinishfc7cd8f2015-03-30 11:51:55 -0400330 def test_get_creds_roles_nonlocking_invalid(self):
Andrea Frittolic3280152015-02-26 12:42:34 +0000331 test_accounts_class = accounts.NotLockingAccounts('v2', 'test_name')
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100332 self.assertRaises(exceptions.InvalidConfiguration,
Matthew Treinishfc7cd8f2015-03-30 11:51:55 -0400333 test_accounts_class.get_creds_by_roles,
334 ['fake_role'])