blob: b17667540421e5110ea3f2a9ab3c68e5ed511e00 [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
23
24from tempest import auth
25from tempest.common import accounts
Matthew Treinishf83f35c2015-04-10 11:59:11 -040026from tempest.common import cred_provider
Matthew Treinishc791ac42014-07-16 09:15:23 -040027from tempest import config
28from tempest import exceptions
Jamie Lennoxc429e6a2015-02-24 10:42:42 +110029from tempest.services.identity.v2.json import token_client
Matthew Treinishc791ac42014-07-16 09:15:23 -040030from 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 Treinishf83f35c2015-04-10 11:59:11 -040071 {'username': 'test_user13', 'tenant_name': 'test_tenant13',
72 'password': 'p', 'resources': {'network': 'network-1'}},
73 {'username': 'test_user14', 'tenant_name': 'test_tenant14',
74 'password': 'p', 'roles': ['role-7', 'role-11'],
75 'resources': {'network': 'network-2'}},
Matthew Treinishc791ac42014-07-16 09:15:23 -040076 ]
77 self.useFixture(mockpatch.Patch(
78 'tempest.common.accounts.read_accounts_yaml',
79 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()
87 hash.update(str(account))
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):
ghanshyamc0edda02015-02-06 15:51:40 +090093 self.stubs.Set(token_client.TokenClientJSON, 'raw_request',
Matthew Treinishc791ac42014-07-16 09:15:23 -040094 fake_identity._fake_v2_response)
Andrea Frittolic3280152015-02-26 12:42:34 +000095 test_account_class = accounts.Accounts('v2', 'test_name')
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 Frittolic3280152015-02-26 12:42:34 +0000104 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400105 hash_dict = test_account_class.get_hash_dict(self.test_accounts)
106 hash_list = self._get_hash_list(self.test_accounts)
107 for hash in hash_list:
Matthew Treinish976e8df2014-12-19 14:21:54 -0500108 self.assertIn(hash, hash_dict['creds'].keys())
109 self.assertIn(hash_dict['creds'][hash], self.test_accounts)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400110
111 def test_create_hash_file_previous_file(self):
112 # Emulate the lock existing on the filesystem
113 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
114 with mock.patch('__builtin__.open', mock.mock_open(), create=True):
Andrea Frittolic3280152015-02-26 12:42:34 +0000115 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400116 res = test_account_class._create_hash_file('12345')
117 self.assertFalse(res, "_create_hash_file should return False if the "
118 "pseudo-lock file already exists")
119
120 def test_create_hash_file_no_previous_file(self):
121 # Emulate the lock not existing on the filesystem
122 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
123 with mock.patch('__builtin__.open', mock.mock_open(), create=True):
Andrea Frittolic3280152015-02-26 12:42:34 +0000124 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400125 res = test_account_class._create_hash_file('12345')
126 self.assertTrue(res, "_create_hash_file should return True if the "
127 "pseudo-lock doesn't already exist")
128
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000129 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400130 def test_get_free_hash_no_previous_accounts(self, lock_mock):
131 # Emulate no pre-existing lock
132 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=False))
133 hash_list = self._get_hash_list(self.test_accounts)
134 mkdir_mock = self.useFixture(mockpatch.Patch('os.mkdir'))
135 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
Andrea Frittolic3280152015-02-26 12:42:34 +0000136 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400137 with mock.patch('__builtin__.open', mock.mock_open(),
138 create=True) as open_mock:
139 test_account_class._get_free_hash(hash_list)
Matthew Treinishb503f032015-03-12 15:48:49 -0400140 lock_path = os.path.join(lockutils.get_lock_path(accounts.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000141 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400142 hash_list[0])
143 open_mock.assert_called_once_with(lock_path, 'w')
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000144 mkdir_path = os.path.join(accounts.CONF.oslo_concurrency.lock_path,
145 'test_accounts')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400146 mkdir_mock.mock.assert_called_once_with(mkdir_path)
147
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000148 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400149 def test_get_free_hash_no_free_accounts(self, lock_mock):
150 hash_list = self._get_hash_list(self.test_accounts)
151 # Emulate pre-existing lock dir
152 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))
153 # Emulate all lcoks in list are in use
154 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Andrea Frittolic3280152015-02-26 12:42:34 +0000155 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish4041b262015-02-27 11:18:54 -0500156 with mock.patch('__builtin__.open', mock.mock_open(), create=True):
157 self.assertRaises(exceptions.InvalidConfiguration,
158 test_account_class._get_free_hash, hash_list)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400159
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000160 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400161 def test_get_free_hash_some_in_use_accounts(self, lock_mock):
162 # Emulate no pre-existing lock
163 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))
164 hash_list = self._get_hash_list(self.test_accounts)
Andrea Frittolic3280152015-02-26 12:42:34 +0000165 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400166
167 def _fake_is_file(path):
168 # Fake isfile() to return that the path exists unless a specific
169 # hash is in the path
170 if hash_list[3] in path:
171 return False
172 return True
173
174 self.stubs.Set(os.path, 'isfile', _fake_is_file)
175 with mock.patch('__builtin__.open', mock.mock_open(),
176 create=True) as open_mock:
177 test_account_class._get_free_hash(hash_list)
Matthew Treinishb503f032015-03-12 15:48:49 -0400178 lock_path = os.path.join(lockutils.get_lock_path(accounts.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000179 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400180 hash_list[3])
Matthew Treinish4041b262015-02-27 11:18:54 -0500181 open_mock.assert_has_calls([mock.call(lock_path, 'w')])
Matthew Treinishc791ac42014-07-16 09:15:23 -0400182
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000183 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400184 def test_remove_hash_last_account(self, lock_mock):
185 hash_list = self._get_hash_list(self.test_accounts)
186 # Pretend the pseudo-lock is there
187 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
188 # Pretend the lock dir is empty
189 self.useFixture(mockpatch.Patch('os.listdir', return_value=[]))
Andrea Frittolic3280152015-02-26 12:42:34 +0000190 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400191 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
192 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
193 test_account_class.remove_hash(hash_list[2])
Matthew Treinishb503f032015-03-12 15:48:49 -0400194 hash_path = os.path.join(lockutils.get_lock_path(accounts.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000195 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400196 hash_list[2])
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000197 lock_path = os.path.join(accounts.CONF.oslo_concurrency.lock_path,
198 'test_accounts')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400199 remove_mock.mock.assert_called_once_with(hash_path)
200 rmdir_mock.mock.assert_called_once_with(lock_path)
201
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000202 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400203 def test_remove_hash_not_last_account(self, lock_mock):
204 hash_list = self._get_hash_list(self.test_accounts)
205 # Pretend the pseudo-lock is there
206 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
207 # Pretend the lock dir is empty
208 self.useFixture(mockpatch.Patch('os.listdir', return_value=[
209 hash_list[1], hash_list[4]]))
Andrea Frittolic3280152015-02-26 12:42:34 +0000210 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400211 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
212 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
213 test_account_class.remove_hash(hash_list[2])
Matthew Treinishb503f032015-03-12 15:48:49 -0400214 hash_path = os.path.join(lockutils.get_lock_path(accounts.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000215 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400216 hash_list[2])
217 remove_mock.mock.assert_called_once_with(hash_path)
218 rmdir_mock.mock.assert_not_called()
Matthew Treinish09f17832014-08-15 15:22:50 -0400219
220 def test_is_multi_user(self):
Andrea Frittolic3280152015-02-26 12:42:34 +0000221 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish09f17832014-08-15 15:22:50 -0400222 self.assertTrue(test_accounts_class.is_multi_user())
223
224 def test_is_not_multi_user(self):
225 self.test_accounts = [self.test_accounts[0]]
226 self.useFixture(mockpatch.Patch(
227 'tempest.common.accounts.read_accounts_yaml',
228 return_value=self.test_accounts))
Andrea Frittolic3280152015-02-26 12:42:34 +0000229 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish09f17832014-08-15 15:22:50 -0400230 self.assertFalse(test_accounts_class.is_multi_user())
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100231
Matthew Treinish976e8df2014-12-19 14:21:54 -0500232 def test__get_creds_by_roles_one_role(self):
233 self.useFixture(mockpatch.Patch(
234 'tempest.common.accounts.read_accounts_yaml',
235 return_value=self.test_accounts))
Andrea Frittolic3280152015-02-26 12:42:34 +0000236 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish976e8df2014-12-19 14:21:54 -0500237 hashes = test_accounts_class.hash_dict['roles']['role4']
238 temp_hash = hashes[0]
239 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
240 test_accounts_class, '_get_free_hash', return_value=temp_hash))
241 # Test a single role returns all matching roles
242 test_accounts_class._get_creds(roles=['role4'])
243 calls = get_free_hash_mock.mock.mock_calls
244 self.assertEqual(len(calls), 1)
245 args = calls[0][1][0]
246 for i in hashes:
247 self.assertIn(i, args)
248
249 def test__get_creds_by_roles_list_role(self):
250 self.useFixture(mockpatch.Patch(
251 'tempest.common.accounts.read_accounts_yaml',
252 return_value=self.test_accounts))
Andrea Frittolic3280152015-02-26 12:42:34 +0000253 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish976e8df2014-12-19 14:21:54 -0500254 hashes = test_accounts_class.hash_dict['roles']['role4']
255 hashes2 = test_accounts_class.hash_dict['roles']['role2']
256 hashes = list(set(hashes) & set(hashes2))
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 an intersection of multiple roles
261 test_accounts_class._get_creds(roles=['role2', '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_no_admin(self):
269 self.useFixture(mockpatch.Patch(
270 'tempest.common.accounts.read_accounts_yaml',
271 return_value=self.test_accounts))
Andrea Frittolic3280152015-02-26 12:42:34 +0000272 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish976e8df2014-12-19 14:21:54 -0500273 hashes = test_accounts_class.hash_dict['creds'].keys()
274 admin_hashes = test_accounts_class.hash_dict['roles'][
275 cfg.CONF.identity.admin_role]
276 temp_hash = hashes[0]
277 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
278 test_accounts_class, '_get_free_hash', return_value=temp_hash))
279 # Test an intersection of multiple roles
280 test_accounts_class._get_creds()
281 calls = get_free_hash_mock.mock.mock_calls
282 self.assertEqual(len(calls), 1)
283 args = calls[0][1][0]
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400284 self.assertEqual(len(args), 12)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500285 for i in admin_hashes:
286 self.assertNotIn(i, args)
287
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400288 def test_networks_returned_with_creds(self):
289 self.useFixture(mockpatch.Patch(
290 'tempest.common.accounts.read_accounts_yaml',
291 return_value=self.test_accounts))
292 test_accounts_class = accounts.Accounts('v2', 'test_name')
293 with mock.patch('tempest.services.compute.json.networks_client.'
294 'NetworksClientJSON.list_networks',
295 return_value=[{'name': 'network-2', 'id': 'fake-id'}]):
296 creds = test_accounts_class.get_creds_by_roles(['role-7'])
297 self.assertTrue(isinstance(creds, cred_provider.TestResources))
298 network = creds.network
299 self.assertIsNotNone(network)
300 self.assertIn('name', network)
301 self.assertIn('id', network)
302 self.assertEqual('fake-id', network['id'])
303 self.assertEqual('network-2', network['name'])
304
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100305
306class TestNotLockingAccount(base.TestCase):
307
308 def setUp(self):
309 super(TestNotLockingAccount, self).setUp()
310 self.useFixture(fake_config.ConfigFixture())
311 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000312 self.useFixture(lockutils_fixtures.ExternalLockFixture())
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100313 self.test_accounts = [
314 {'username': 'test_user1', 'tenant_name': 'test_tenant1',
315 'password': 'p'},
316 {'username': 'test_user2', 'tenant_name': 'test_tenant2',
317 'password': 'p'},
318 {'username': 'test_user3', 'tenant_name': 'test_tenant3',
319 'password': 'p'},
320 ]
321 self.useFixture(mockpatch.Patch(
322 'tempest.common.accounts.read_accounts_yaml',
323 return_value=self.test_accounts))
324 cfg.CONF.set_default('test_accounts_file', '', group='auth')
Matthew Treinishb19eeb82014-09-04 09:57:46 -0400325 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100326
Matthew Treinishfc7cd8f2015-03-30 11:51:55 -0400327 def test_get_creds_roles_nonlocking_invalid(self):
Andrea Frittolic3280152015-02-26 12:42:34 +0000328 test_accounts_class = accounts.NotLockingAccounts('v2', 'test_name')
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100329 self.assertRaises(exceptions.InvalidConfiguration,
Matthew Treinishfc7cd8f2015-03-30 11:51:55 -0400330 test_accounts_class.get_creds_by_roles,
331 ['fake_role'])