blob: 6371e494b324dd16e20c749b38d11b01df2491ab [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 Treinishc791ac42014-07-16 09:15:23 -040026from tempest import config
27from tempest import exceptions
Jamie Lennoxc429e6a2015-02-24 10:42:42 +110028from tempest.services.identity.v2.json import token_client
Matthew Treinishc791ac42014-07-16 09:15:23 -040029from tempest.tests import base
30from tempest.tests import fake_config
31from tempest.tests import fake_identity
32
33
34class TestAccount(base.TestCase):
35
36 def setUp(self):
37 super(TestAccount, self).setUp()
38 self.useFixture(fake_config.ConfigFixture())
39 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)
Doug Hellmann583ce2c2015-03-11 14:55:46 +000040 self.useFixture(lockutils_fixtures.ExternalLockFixture())
Matthew Treinishc791ac42014-07-16 09:15:23 -040041 self.test_accounts = [
42 {'username': 'test_user1', 'tenant_name': 'test_tenant1',
43 'password': 'p'},
44 {'username': 'test_user2', 'tenant_name': 'test_tenant2',
45 'password': 'p'},
46 {'username': 'test_user3', 'tenant_name': 'test_tenant3',
47 'password': 'p'},
48 {'username': 'test_user4', 'tenant_name': 'test_tenant4',
49 'password': 'p'},
50 {'username': 'test_user5', 'tenant_name': 'test_tenant5',
51 'password': 'p'},
52 {'username': 'test_user6', 'tenant_name': 'test_tenant6',
Matthew Treinish976e8df2014-12-19 14:21:54 -050053 'password': 'p', 'roles': ['role1', 'role2']},
54 {'username': 'test_user7', 'tenant_name': 'test_tenant7',
55 'password': 'p', 'roles': ['role2', 'role3']},
56 {'username': 'test_user8', 'tenant_name': 'test_tenant8',
57 'password': 'p', 'roles': ['role4', 'role1']},
58 {'username': 'test_user9', 'tenant_name': 'test_tenant9',
59 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},
60 {'username': 'test_user10', 'tenant_name': 'test_tenant10',
61 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},
62 {'username': 'test_user11', 'tenant_name': 'test_tenant11',
63 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]},
64 {'username': 'test_user12', 'tenant_name': 'test_tenant12',
65 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]},
Matthew Treinishc791ac42014-07-16 09:15:23 -040066 ]
67 self.useFixture(mockpatch.Patch(
68 'tempest.common.accounts.read_accounts_yaml',
69 return_value=self.test_accounts))
Aaron Rosen48070042015-03-30 16:17:11 -070070 cfg.CONF.set_default('test_accounts_file', 'fake_path', group='auth')
Matthew Treinishb19eeb82014-09-04 09:57:46 -040071 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Matthew Treinishc791ac42014-07-16 09:15:23 -040072
73 def _get_hash_list(self, accounts_list):
74 hash_list = []
75 for account in accounts_list:
76 hash = hashlib.md5()
77 hash.update(str(account))
Matthew Treinish976e8df2014-12-19 14:21:54 -050078 temp_hash = hash.hexdigest()
79 hash_list.append(temp_hash)
Matthew Treinishc791ac42014-07-16 09:15:23 -040080 return hash_list
81
82 def test_get_hash(self):
ghanshyamc0edda02015-02-06 15:51:40 +090083 self.stubs.Set(token_client.TokenClientJSON, 'raw_request',
Matthew Treinishc791ac42014-07-16 09:15:23 -040084 fake_identity._fake_v2_response)
Andrea Frittolic3280152015-02-26 12:42:34 +000085 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -040086 hash_list = self._get_hash_list(self.test_accounts)
87 test_cred_dict = self.test_accounts[3]
ghanshyam5ff763f2015-02-18 16:15:58 +090088 test_creds = auth.get_credentials(fake_identity.FAKE_AUTH_URL,
89 **test_cred_dict)
Matthew Treinishc791ac42014-07-16 09:15:23 -040090 results = test_account_class.get_hash(test_creds)
91 self.assertEqual(hash_list[3], results)
92
93 def test_get_hash_dict(self):
Andrea Frittolic3280152015-02-26 12:42:34 +000094 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -040095 hash_dict = test_account_class.get_hash_dict(self.test_accounts)
96 hash_list = self._get_hash_list(self.test_accounts)
97 for hash in hash_list:
Matthew Treinish976e8df2014-12-19 14:21:54 -050098 self.assertIn(hash, hash_dict['creds'].keys())
99 self.assertIn(hash_dict['creds'][hash], self.test_accounts)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400100
101 def test_create_hash_file_previous_file(self):
102 # Emulate the lock existing on the filesystem
103 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
104 with mock.patch('__builtin__.open', mock.mock_open(), create=True):
Andrea Frittolic3280152015-02-26 12:42:34 +0000105 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400106 res = test_account_class._create_hash_file('12345')
107 self.assertFalse(res, "_create_hash_file should return False if the "
108 "pseudo-lock file already exists")
109
110 def test_create_hash_file_no_previous_file(self):
111 # Emulate the lock not existing on the filesystem
112 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
113 with mock.patch('__builtin__.open', mock.mock_open(), create=True):
Andrea Frittolic3280152015-02-26 12:42:34 +0000114 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400115 res = test_account_class._create_hash_file('12345')
116 self.assertTrue(res, "_create_hash_file should return True if the "
117 "pseudo-lock doesn't already exist")
118
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000119 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400120 def test_get_free_hash_no_previous_accounts(self, lock_mock):
121 # Emulate no pre-existing lock
122 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=False))
123 hash_list = self._get_hash_list(self.test_accounts)
124 mkdir_mock = self.useFixture(mockpatch.Patch('os.mkdir'))
125 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
Andrea Frittolic3280152015-02-26 12:42:34 +0000126 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400127 with mock.patch('__builtin__.open', mock.mock_open(),
128 create=True) as open_mock:
129 test_account_class._get_free_hash(hash_list)
Matthew Treinishb503f032015-03-12 15:48:49 -0400130 lock_path = os.path.join(lockutils.get_lock_path(accounts.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000131 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400132 hash_list[0])
133 open_mock.assert_called_once_with(lock_path, 'w')
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000134 mkdir_path = os.path.join(accounts.CONF.oslo_concurrency.lock_path,
135 'test_accounts')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400136 mkdir_mock.mock.assert_called_once_with(mkdir_path)
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_free_accounts(self, lock_mock):
140 hash_list = self._get_hash_list(self.test_accounts)
141 # Emulate pre-existing lock dir
142 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))
143 # Emulate all lcoks in list are in use
144 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Andrea Frittolic3280152015-02-26 12:42:34 +0000145 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish4041b262015-02-27 11:18:54 -0500146 with mock.patch('__builtin__.open', mock.mock_open(), create=True):
147 self.assertRaises(exceptions.InvalidConfiguration,
148 test_account_class._get_free_hash, hash_list)
Matthew Treinishc791ac42014-07-16 09:15:23 -0400149
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000150 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400151 def test_get_free_hash_some_in_use_accounts(self, lock_mock):
152 # Emulate no pre-existing lock
153 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))
154 hash_list = self._get_hash_list(self.test_accounts)
Andrea Frittolic3280152015-02-26 12:42:34 +0000155 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400156
157 def _fake_is_file(path):
158 # Fake isfile() to return that the path exists unless a specific
159 # hash is in the path
160 if hash_list[3] in path:
161 return False
162 return True
163
164 self.stubs.Set(os.path, 'isfile', _fake_is_file)
165 with mock.patch('__builtin__.open', mock.mock_open(),
166 create=True) as open_mock:
167 test_account_class._get_free_hash(hash_list)
Matthew Treinishb503f032015-03-12 15:48:49 -0400168 lock_path = os.path.join(lockutils.get_lock_path(accounts.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000169 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400170 hash_list[3])
Matthew Treinish4041b262015-02-27 11:18:54 -0500171 open_mock.assert_has_calls([mock.call(lock_path, 'w')])
Matthew Treinishc791ac42014-07-16 09:15:23 -0400172
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000173 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400174 def test_remove_hash_last_account(self, lock_mock):
175 hash_list = self._get_hash_list(self.test_accounts)
176 # Pretend the pseudo-lock is there
177 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
178 # Pretend the lock dir is empty
179 self.useFixture(mockpatch.Patch('os.listdir', return_value=[]))
Andrea Frittolic3280152015-02-26 12:42:34 +0000180 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400181 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
182 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
183 test_account_class.remove_hash(hash_list[2])
Matthew Treinishb503f032015-03-12 15:48:49 -0400184 hash_path = os.path.join(lockutils.get_lock_path(accounts.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000185 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400186 hash_list[2])
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000187 lock_path = os.path.join(accounts.CONF.oslo_concurrency.lock_path,
188 'test_accounts')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400189 remove_mock.mock.assert_called_once_with(hash_path)
190 rmdir_mock.mock.assert_called_once_with(lock_path)
191
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000192 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400193 def test_remove_hash_not_last_account(self, lock_mock):
194 hash_list = self._get_hash_list(self.test_accounts)
195 # Pretend the pseudo-lock is there
196 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
197 # Pretend the lock dir is empty
198 self.useFixture(mockpatch.Patch('os.listdir', return_value=[
199 hash_list[1], hash_list[4]]))
Andrea Frittolic3280152015-02-26 12:42:34 +0000200 test_account_class = accounts.Accounts('v2', 'test_name')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400201 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
202 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
203 test_account_class.remove_hash(hash_list[2])
Matthew Treinishb503f032015-03-12 15:48:49 -0400204 hash_path = os.path.join(lockutils.get_lock_path(accounts.CONF),
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000205 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400206 hash_list[2])
207 remove_mock.mock.assert_called_once_with(hash_path)
208 rmdir_mock.mock.assert_not_called()
Matthew Treinish09f17832014-08-15 15:22:50 -0400209
210 def test_is_multi_user(self):
Andrea Frittolic3280152015-02-26 12:42:34 +0000211 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish09f17832014-08-15 15:22:50 -0400212 self.assertTrue(test_accounts_class.is_multi_user())
213
214 def test_is_not_multi_user(self):
215 self.test_accounts = [self.test_accounts[0]]
216 self.useFixture(mockpatch.Patch(
217 'tempest.common.accounts.read_accounts_yaml',
218 return_value=self.test_accounts))
Andrea Frittolic3280152015-02-26 12:42:34 +0000219 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish09f17832014-08-15 15:22:50 -0400220 self.assertFalse(test_accounts_class.is_multi_user())
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100221
Matthew Treinish976e8df2014-12-19 14:21:54 -0500222 def test__get_creds_by_roles_one_role(self):
223 self.useFixture(mockpatch.Patch(
224 'tempest.common.accounts.read_accounts_yaml',
225 return_value=self.test_accounts))
Andrea Frittolic3280152015-02-26 12:42:34 +0000226 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish976e8df2014-12-19 14:21:54 -0500227 hashes = test_accounts_class.hash_dict['roles']['role4']
228 temp_hash = hashes[0]
229 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
230 test_accounts_class, '_get_free_hash', return_value=temp_hash))
231 # Test a single role returns all matching roles
232 test_accounts_class._get_creds(roles=['role4'])
233 calls = get_free_hash_mock.mock.mock_calls
234 self.assertEqual(len(calls), 1)
235 args = calls[0][1][0]
236 for i in hashes:
237 self.assertIn(i, args)
238
239 def test__get_creds_by_roles_list_role(self):
240 self.useFixture(mockpatch.Patch(
241 'tempest.common.accounts.read_accounts_yaml',
242 return_value=self.test_accounts))
Andrea Frittolic3280152015-02-26 12:42:34 +0000243 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish976e8df2014-12-19 14:21:54 -0500244 hashes = test_accounts_class.hash_dict['roles']['role4']
245 hashes2 = test_accounts_class.hash_dict['roles']['role2']
246 hashes = list(set(hashes) & set(hashes2))
247 temp_hash = hashes[0]
248 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
249 test_accounts_class, '_get_free_hash', return_value=temp_hash))
250 # Test an intersection of multiple roles
251 test_accounts_class._get_creds(roles=['role2', 'role4'])
252 calls = get_free_hash_mock.mock.mock_calls
253 self.assertEqual(len(calls), 1)
254 args = calls[0][1][0]
255 for i in hashes:
256 self.assertIn(i, args)
257
258 def test__get_creds_by_roles_no_admin(self):
259 self.useFixture(mockpatch.Patch(
260 'tempest.common.accounts.read_accounts_yaml',
261 return_value=self.test_accounts))
Andrea Frittolic3280152015-02-26 12:42:34 +0000262 test_accounts_class = accounts.Accounts('v2', 'test_name')
Matthew Treinish976e8df2014-12-19 14:21:54 -0500263 hashes = test_accounts_class.hash_dict['creds'].keys()
264 admin_hashes = test_accounts_class.hash_dict['roles'][
265 cfg.CONF.identity.admin_role]
266 temp_hash = hashes[0]
267 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
268 test_accounts_class, '_get_free_hash', return_value=temp_hash))
269 # Test an intersection of multiple roles
270 test_accounts_class._get_creds()
271 calls = get_free_hash_mock.mock.mock_calls
272 self.assertEqual(len(calls), 1)
273 args = calls[0][1][0]
274 self.assertEqual(len(args), 10)
275 for i in admin_hashes:
276 self.assertNotIn(i, args)
277
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100278
279class TestNotLockingAccount(base.TestCase):
280
281 def setUp(self):
282 super(TestNotLockingAccount, self).setUp()
283 self.useFixture(fake_config.ConfigFixture())
284 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000285 self.useFixture(lockutils_fixtures.ExternalLockFixture())
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100286 self.test_accounts = [
287 {'username': 'test_user1', 'tenant_name': 'test_tenant1',
288 'password': 'p'},
289 {'username': 'test_user2', 'tenant_name': 'test_tenant2',
290 'password': 'p'},
291 {'username': 'test_user3', 'tenant_name': 'test_tenant3',
292 'password': 'p'},
293 ]
294 self.useFixture(mockpatch.Patch(
295 'tempest.common.accounts.read_accounts_yaml',
296 return_value=self.test_accounts))
297 cfg.CONF.set_default('test_accounts_file', '', group='auth')
Matthew Treinishb19eeb82014-09-04 09:57:46 -0400298 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100299
Matthew Treinishfc7cd8f2015-03-30 11:51:55 -0400300 def test_get_creds_roles_nonlocking_invalid(self):
Andrea Frittolic3280152015-02-26 12:42:34 +0000301 test_accounts_class = accounts.NotLockingAccounts('v2', 'test_name')
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100302 self.assertRaises(exceptions.InvalidConfiguration,
Matthew Treinishfc7cd8f2015-03-30 11:51:55 -0400303 test_accounts_class.get_creds_by_roles,
304 ['fake_role'])