blob: 1acef8a593c5b154d4c45aee2faaa54451311404 [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 Treinishc791ac42014-07-16 09:15:23 -040027from tempest.common import accounts
Matthew Treinishf83f35c2015-04-10 11:59:11 -040028from tempest.common import cred_provider
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
37class TestAccount(base.TestCase):
38
39 def setUp(self):
40 super(TestAccount, self).setUp()
41 self.useFixture(fake_config.ConfigFixture())
42 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)
Matthew Treinishf83f35c2015-04-10 11:59:11 -040043 self.fake_http = fake_http.fake_httplib2(return_type=200)
44 self.stubs.Set(token_client.TokenClientJSON, 'raw_request',
45 fake_identity._fake_v2_response)
Doug Hellmann583ce2c2015-03-11 14:55:46 +000046 self.useFixture(lockutils_fixtures.ExternalLockFixture())
Matthew Treinishc791ac42014-07-16 09:15:23 -040047 self.test_accounts = [
48 {'username': 'test_user1', 'tenant_name': 'test_tenant1',
49 'password': 'p'},
50 {'username': 'test_user2', 'tenant_name': 'test_tenant2',
51 'password': 'p'},
52 {'username': 'test_user3', 'tenant_name': 'test_tenant3',
53 'password': 'p'},
54 {'username': 'test_user4', 'tenant_name': 'test_tenant4',
55 'password': 'p'},
56 {'username': 'test_user5', 'tenant_name': 'test_tenant5',
57 'password': 'p'},
58 {'username': 'test_user6', 'tenant_name': 'test_tenant6',
Matthew Treinish976e8df2014-12-19 14:21:54 -050059 'password': 'p', 'roles': ['role1', 'role2']},
60 {'username': 'test_user7', 'tenant_name': 'test_tenant7',
61 'password': 'p', 'roles': ['role2', 'role3']},
62 {'username': 'test_user8', 'tenant_name': 'test_tenant8',
63 'password': 'p', 'roles': ['role4', 'role1']},
64 {'username': 'test_user9', 'tenant_name': 'test_tenant9',
65 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},
66 {'username': 'test_user10', 'tenant_name': 'test_tenant10',
67 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},
68 {'username': 'test_user11', 'tenant_name': 'test_tenant11',
69 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]},
70 {'username': 'test_user12', 'tenant_name': 'test_tenant12',
71 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]},
Matthew Treinishc791ac42014-07-16 09:15:23 -040072 ]
Matthew Treinisha59bd0c2015-04-20 12:02:48 -040073 self.accounts_mock = self.useFixture(mockpatch.Patch(
Matthew Treinishc791ac42014-07-16 09:15:23 -040074 'tempest.common.accounts.read_accounts_yaml',
75 return_value=self.test_accounts))
Aaron Rosen48070042015-03-30 16:17:11 -070076 cfg.CONF.set_default('test_accounts_file', 'fake_path', group='auth')
Matthew Treinishb19eeb82014-09-04 09:57:46 -040077 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Matthew Treinishc791ac42014-07-16 09:15:23 -040078
79 def _get_hash_list(self, accounts_list):
80 hash_list = []
81 for account in accounts_list:
82 hash = hashlib.md5()
Matthew Treinish1c517a22015-04-23 11:39:44 -040083 hash.update(six.text_type(account).encode('utf-8'))
Matthew Treinish976e8df2014-12-19 14:21:54 -050084 temp_hash = hash.hexdigest()
85 hash_list.append(temp_hash)
Matthew Treinishc791ac42014-07-16 09:15:23 -040086 return hash_list
87
88 def test_get_hash(self):
ghanshyamc0edda02015-02-06 15:51:40 +090089 self.stubs.Set(token_client.TokenClientJSON, 'raw_request',
Matthew Treinishc791ac42014-07-16 09:15:23 -040090 fake_identity._fake_v2_response)
Andrea Frittolic3280152015-02-26 12:42:34 +000091 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -040092 hash_list = self._get_hash_list(self.test_accounts)
93 test_cred_dict = self.test_accounts[3]
ghanshyam5ff763f2015-02-18 16:15:58 +090094 test_creds = auth.get_credentials(fake_identity.FAKE_AUTH_URL,
95 **test_cred_dict)
Matthew Treinishc791ac42014-07-16 09:15:23 -040096 results = test_account_class.get_hash(test_creds)
97 self.assertEqual(hash_list[3], results)
98
99 def test_get_hash_dict(self):
Andrea Frittolic3280152015-02-26 12:42:34 +0000100 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400101 hash_dict = test_account_class.get_hash_dict(self.test_accounts)
102 hash_list = self._get_hash_list(self.test_accounts)
103 for hash in hash_list:
Matthew Treinish976e8df2014-12-19 14:21:54 -0500104 self.assertIn(hash, hash_dict['creds'].keys())
105 self.assertIn(hash_dict['creds'][hash], self.test_accounts)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400106
107 def test_create_hash_file_previous_file(self):
108 # Emulate the lock existing on the filesystem
109 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400110 with mock.patch('six.moves.builtins.open', mock.mock_open(),
111 create=True):
Andrea Frittolic3280152015-02-26 12:42:34 +0000112 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400113 res = test_account_class._create_hash_file('12345')
114 self.assertFalse(res, "_create_hash_file should return False if the "
115 "pseudo-lock file already exists")
116
117 def test_create_hash_file_no_previous_file(self):
118 # Emulate the lock not existing on the filesystem
119 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400120 with mock.patch('six.moves.builtins.open', mock.mock_open(),
121 create=True):
Andrea Frittolic3280152015-02-26 12:42:34 +0000122 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400123 res = test_account_class._create_hash_file('12345')
124 self.assertTrue(res, "_create_hash_file should return True if the "
125 "pseudo-lock doesn't already exist")
126
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000127 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400128 def test_get_free_hash_no_previous_accounts(self, lock_mock):
129 # Emulate no pre-existing lock
130 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=False))
131 hash_list = self._get_hash_list(self.test_accounts)
132 mkdir_mock = self.useFixture(mockpatch.Patch('os.mkdir'))
133 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
Andrea Frittolic3280152015-02-26 12:42:34 +0000134 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400135 with mock.patch('six.moves.builtins.open', mock.mock_open(),
Matthew Treinishc791ac42014-07-16 09:15:23 -0400136 create=True) as open_mock:
137 test_account_class._get_free_hash(hash_list)
Matthew Treinishb503f032015-03-12 15:48:49 -0400138 lock_path = os.path.join(lockutils.get_lock_path(accounts.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000139 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400140 hash_list[0])
141 open_mock.assert_called_once_with(lock_path, 'w')
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000142 mkdir_path = os.path.join(accounts.CONF.oslo_concurrency.lock_path,
143 'test_accounts')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400144 mkdir_mock.mock.assert_called_once_with(mkdir_path)
145
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000146 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400147 def test_get_free_hash_no_free_accounts(self, lock_mock):
148 hash_list = self._get_hash_list(self.test_accounts)
149 # Emulate pre-existing lock dir
150 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))
151 # Emulate all lcoks in list are in use
152 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Andrea Frittolic3280152015-02-26 12:42:34 +0000153 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400154 with mock.patch('six.moves.builtins.open', mock.mock_open(),
155 create=True):
Matthew Treinish4041b262015-02-27 11:18:54 -0500156 self.assertRaises(exceptions.InvalidConfiguration,
157 test_account_class._get_free_hash, hash_list)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400158
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000159 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400160 def test_get_free_hash_some_in_use_accounts(self, lock_mock):
161 # Emulate no pre-existing lock
162 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))
163 hash_list = self._get_hash_list(self.test_accounts)
Andrea Frittolic3280152015-02-26 12:42:34 +0000164 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400165
166 def _fake_is_file(path):
167 # Fake isfile() to return that the path exists unless a specific
168 # hash is in the path
169 if hash_list[3] in path:
170 return False
171 return True
172
173 self.stubs.Set(os.path, 'isfile', _fake_is_file)
Matthew Treinish53d0dc02015-04-24 15:57:27 -0400174 with mock.patch('six.moves.builtins.open', mock.mock_open(),
Matthew Treinishc791ac42014-07-16 09:15:23 -0400175 create=True) as open_mock:
176 test_account_class._get_free_hash(hash_list)
Matthew Treinishb503f032015-03-12 15:48:49 -0400177 lock_path = os.path.join(lockutils.get_lock_path(accounts.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000178 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400179 hash_list[3])
Matthew Treinish4041b262015-02-27 11:18:54 -0500180 open_mock.assert_has_calls([mock.call(lock_path, 'w')])
Matthew Treinishc791ac42014-07-16 09:15:23 -0400181
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000182 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400183 def test_remove_hash_last_account(self, lock_mock):
184 hash_list = self._get_hash_list(self.test_accounts)
185 # Pretend the pseudo-lock is there
186 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
187 # Pretend the lock dir is empty
188 self.useFixture(mockpatch.Patch('os.listdir', return_value=[]))
Andrea Frittolic3280152015-02-26 12:42:34 +0000189 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400190 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
191 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
192 test_account_class.remove_hash(hash_list[2])
Matthew Treinishb503f032015-03-12 15:48:49 -0400193 hash_path = os.path.join(lockutils.get_lock_path(accounts.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000194 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400195 hash_list[2])
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000196 lock_path = os.path.join(accounts.CONF.oslo_concurrency.lock_path,
197 'test_accounts')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400198 remove_mock.mock.assert_called_once_with(hash_path)
199 rmdir_mock.mock.assert_called_once_with(lock_path)
200
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000201 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400202 def test_remove_hash_not_last_account(self, lock_mock):
203 hash_list = self._get_hash_list(self.test_accounts)
204 # Pretend the pseudo-lock is there
205 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
206 # Pretend the lock dir is empty
207 self.useFixture(mockpatch.Patch('os.listdir', return_value=[
208 hash_list[1], hash_list[4]]))
Andrea Frittolic3280152015-02-26 12:42:34 +0000209 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400210 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
211 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
212 test_account_class.remove_hash(hash_list[2])
Matthew Treinishb503f032015-03-12 15:48:49 -0400213 hash_path = os.path.join(lockutils.get_lock_path(accounts.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000214 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400215 hash_list[2])
216 remove_mock.mock.assert_called_once_with(hash_path)
217 rmdir_mock.mock.assert_not_called()
Matthew Treinish09f17832014-08-15 15:22:50 -0400218
219 def test_is_multi_user(self):
Andrea Frittolic3280152015-02-26 12:42:34 +0000220 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish09f17832014-08-15 15:22:50 -0400221 self.assertTrue(test_accounts_class.is_multi_user())
222
223 def test_is_not_multi_user(self):
224 self.test_accounts = [self.test_accounts[0]]
225 self.useFixture(mockpatch.Patch(
226 'tempest.common.accounts.read_accounts_yaml',
227 return_value=self.test_accounts))
Andrea Frittolic3280152015-02-26 12:42:34 +0000228 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish09f17832014-08-15 15:22:50 -0400229 self.assertFalse(test_accounts_class.is_multi_user())
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100230
Matthew Treinish976e8df2014-12-19 14:21:54 -0500231 def test__get_creds_by_roles_one_role(self):
232 self.useFixture(mockpatch.Patch(
233 'tempest.common.accounts.read_accounts_yaml',
234 return_value=self.test_accounts))
Andrea Frittolic3280152015-02-26 12:42:34 +0000235 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish976e8df2014-12-19 14:21:54 -0500236 hashes = test_accounts_class.hash_dict['roles']['role4']
237 temp_hash = hashes[0]
238 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
239 test_accounts_class, '_get_free_hash', return_value=temp_hash))
240 # Test a single role returns all matching roles
241 test_accounts_class._get_creds(roles=['role4'])
242 calls = get_free_hash_mock.mock.mock_calls
243 self.assertEqual(len(calls), 1)
244 args = calls[0][1][0]
245 for i in hashes:
246 self.assertIn(i, args)
247
248 def test__get_creds_by_roles_list_role(self):
249 self.useFixture(mockpatch.Patch(
250 'tempest.common.accounts.read_accounts_yaml',
251 return_value=self.test_accounts))
Andrea Frittolic3280152015-02-26 12:42:34 +0000252 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish976e8df2014-12-19 14:21:54 -0500253 hashes = test_accounts_class.hash_dict['roles']['role4']
254 hashes2 = test_accounts_class.hash_dict['roles']['role2']
255 hashes = list(set(hashes) & set(hashes2))
256 temp_hash = hashes[0]
257 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
258 test_accounts_class, '_get_free_hash', return_value=temp_hash))
259 # Test an intersection of multiple roles
260 test_accounts_class._get_creds(roles=['role2', 'role4'])
261 calls = get_free_hash_mock.mock.mock_calls
262 self.assertEqual(len(calls), 1)
263 args = calls[0][1][0]
264 for i in hashes:
265 self.assertIn(i, args)
266
267 def test__get_creds_by_roles_no_admin(self):
268 self.useFixture(mockpatch.Patch(
269 'tempest.common.accounts.read_accounts_yaml',
270 return_value=self.test_accounts))
Andrea Frittolic3280152015-02-26 12:42:34 +0000271 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish1c517a22015-04-23 11:39:44 -0400272 hashes = list(test_accounts_class.hash_dict['creds'].keys())
Matthew Treinish976e8df2014-12-19 14:21:54 -0500273 admin_hashes = test_accounts_class.hash_dict['roles'][
274 cfg.CONF.identity.admin_role]
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()
280 calls = get_free_hash_mock.mock.mock_calls
281 self.assertEqual(len(calls), 1)
282 args = calls[0][1][0]
Matthew Treinisha59bd0c2015-04-20 12:02:48 -0400283 self.assertEqual(len(args), 10)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500284 for i in admin_hashes:
285 self.assertNotIn(i, args)
286
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400287 def test_networks_returned_with_creds(self):
Matthew Treinisha59bd0c2015-04-20 12:02:48 -0400288 test_accounts = [
289 {'username': 'test_user13', 'tenant_name': 'test_tenant13',
290 'password': 'p', 'resources': {'network': 'network-1'}},
291 {'username': 'test_user14', 'tenant_name': 'test_tenant14',
292 'password': 'p', 'roles': ['role-7', 'role-11'],
293 'resources': {'network': 'network-2'}}]
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400294 self.useFixture(mockpatch.Patch(
295 'tempest.common.accounts.read_accounts_yaml',
Matthew Treinisha59bd0c2015-04-20 12:02:48 -0400296 return_value=test_accounts))
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400297 test_accounts_class = accounts.Accounts('v2', 'test_name')
298 with mock.patch('tempest.services.compute.json.networks_client.'
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +0000299 'NetworksClient.list_networks',
ghanshyamf0f7cfc2015-08-24 16:21:18 +0900300 return_value={'networks': [{'name': 'network-2',
301 'id': 'fake-id',
302 'label': 'network-2'}]}):
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400303 creds = test_accounts_class.get_creds_by_roles(['role-7'])
304 self.assertTrue(isinstance(creds, cred_provider.TestResources))
305 network = creds.network
306 self.assertIsNotNone(network)
307 self.assertIn('name', network)
308 self.assertIn('id', network)
309 self.assertEqual('fake-id', network['id'])
310 self.assertEqual('network-2', network['name'])
311
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100312
313class TestNotLockingAccount(base.TestCase):
314
315 def setUp(self):
316 super(TestNotLockingAccount, self).setUp()
317 self.useFixture(fake_config.ConfigFixture())
318 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000319 self.useFixture(lockutils_fixtures.ExternalLockFixture())
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100320 self.test_accounts = [
321 {'username': 'test_user1', 'tenant_name': 'test_tenant1',
322 'password': 'p'},
323 {'username': 'test_user2', 'tenant_name': 'test_tenant2',
324 'password': 'p'},
325 {'username': 'test_user3', 'tenant_name': 'test_tenant3',
326 'password': 'p'},
327 ]
328 self.useFixture(mockpatch.Patch(
329 'tempest.common.accounts.read_accounts_yaml',
330 return_value=self.test_accounts))
331 cfg.CONF.set_default('test_accounts_file', '', group='auth')
Matthew Treinishb19eeb82014-09-04 09:57:46 -0400332 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100333
Matthew Treinishfc7cd8f2015-03-30 11:51:55 -0400334 def test_get_creds_roles_nonlocking_invalid(self):
Andrea Frittolic3280152015-02-26 12:42:34 +0000335 test_accounts_class = accounts.NotLockingAccounts('v2', 'test_name')
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100336 self.assertRaises(exceptions.InvalidConfiguration,
Matthew Treinishfc7cd8f2015-03-30 11:51:55 -0400337 test_accounts_class.get_creds_by_roles,
338 ['fake_role'])