blob: 29fe902ca4d46f68f29154830420cf585ef2ff55 [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
22
23from tempest import auth
24from tempest.common import accounts
Matthew Treinishc791ac42014-07-16 09:15:23 -040025from tempest import config
26from tempest import exceptions
Jamie Lennoxc429e6a2015-02-24 10:42:42 +110027from tempest.services.identity.v2.json import token_client
Matthew Treinishc791ac42014-07-16 09:15:23 -040028from tempest.tests import base
29from tempest.tests import fake_config
30from tempest.tests import fake_identity
31
32
33class TestAccount(base.TestCase):
34
35 def setUp(self):
36 super(TestAccount, self).setUp()
37 self.useFixture(fake_config.ConfigFixture())
38 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)
Doug Hellmann583ce2c2015-03-11 14:55:46 +000039 self.useFixture(lockutils_fixtures.ExternalLockFixture())
Matthew Treinishc791ac42014-07-16 09:15:23 -040040 self.test_accounts = [
41 {'username': 'test_user1', 'tenant_name': 'test_tenant1',
42 'password': 'p'},
43 {'username': 'test_user2', 'tenant_name': 'test_tenant2',
44 'password': 'p'},
45 {'username': 'test_user3', 'tenant_name': 'test_tenant3',
46 'password': 'p'},
47 {'username': 'test_user4', 'tenant_name': 'test_tenant4',
48 'password': 'p'},
49 {'username': 'test_user5', 'tenant_name': 'test_tenant5',
50 'password': 'p'},
51 {'username': 'test_user6', 'tenant_name': 'test_tenant6',
Matthew Treinish976e8df2014-12-19 14:21:54 -050052 'password': 'p', 'roles': ['role1', 'role2']},
53 {'username': 'test_user7', 'tenant_name': 'test_tenant7',
54 'password': 'p', 'roles': ['role2', 'role3']},
55 {'username': 'test_user8', 'tenant_name': 'test_tenant8',
56 'password': 'p', 'roles': ['role4', 'role1']},
57 {'username': 'test_user9', 'tenant_name': 'test_tenant9',
58 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},
59 {'username': 'test_user10', 'tenant_name': 'test_tenant10',
60 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},
61 {'username': 'test_user11', 'tenant_name': 'test_tenant11',
62 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]},
63 {'username': 'test_user12', 'tenant_name': 'test_tenant12',
64 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]},
Matthew Treinishc791ac42014-07-16 09:15:23 -040065 ]
66 self.useFixture(mockpatch.Patch(
67 'tempest.common.accounts.read_accounts_yaml',
68 return_value=self.test_accounts))
69 cfg.CONF.set_default('test_accounts_file', '', group='auth')
Matthew Treinishb19eeb82014-09-04 09:57:46 -040070 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Matthew Treinishc791ac42014-07-16 09:15:23 -040071
72 def _get_hash_list(self, accounts_list):
73 hash_list = []
74 for account in accounts_list:
75 hash = hashlib.md5()
76 hash.update(str(account))
Matthew Treinish976e8df2014-12-19 14:21:54 -050077 temp_hash = hash.hexdigest()
78 hash_list.append(temp_hash)
Matthew Treinishc791ac42014-07-16 09:15:23 -040079 return hash_list
80
81 def test_get_hash(self):
ghanshyamc0edda02015-02-06 15:51:40 +090082 self.stubs.Set(token_client.TokenClientJSON, 'raw_request',
Matthew Treinishc791ac42014-07-16 09:15:23 -040083 fake_identity._fake_v2_response)
84 test_account_class = accounts.Accounts('test_name')
85 hash_list = self._get_hash_list(self.test_accounts)
86 test_cred_dict = self.test_accounts[3]
ghanshyam5ff763f2015-02-18 16:15:58 +090087 test_creds = auth.get_credentials(fake_identity.FAKE_AUTH_URL,
88 **test_cred_dict)
Matthew Treinishc791ac42014-07-16 09:15:23 -040089 results = test_account_class.get_hash(test_creds)
90 self.assertEqual(hash_list[3], results)
91
92 def test_get_hash_dict(self):
93 test_account_class = accounts.Accounts('test_name')
94 hash_dict = test_account_class.get_hash_dict(self.test_accounts)
95 hash_list = self._get_hash_list(self.test_accounts)
96 for hash in hash_list:
Matthew Treinish976e8df2014-12-19 14:21:54 -050097 self.assertIn(hash, hash_dict['creds'].keys())
98 self.assertIn(hash_dict['creds'][hash], self.test_accounts)
Matthew Treinishc791ac42014-07-16 09:15:23 -040099
100 def test_create_hash_file_previous_file(self):
101 # Emulate the lock existing on the filesystem
102 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
103 with mock.patch('__builtin__.open', mock.mock_open(), create=True):
104 test_account_class = accounts.Accounts('test_name')
105 res = test_account_class._create_hash_file('12345')
106 self.assertFalse(res, "_create_hash_file should return False if the "
107 "pseudo-lock file already exists")
108
109 def test_create_hash_file_no_previous_file(self):
110 # Emulate the lock not existing on the filesystem
111 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
112 with mock.patch('__builtin__.open', mock.mock_open(), create=True):
113 test_account_class = accounts.Accounts('test_name')
114 res = test_account_class._create_hash_file('12345')
115 self.assertTrue(res, "_create_hash_file should return True if the "
116 "pseudo-lock doesn't already exist")
117
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000118 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400119 def test_get_free_hash_no_previous_accounts(self, lock_mock):
120 # Emulate no pre-existing lock
121 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=False))
122 hash_list = self._get_hash_list(self.test_accounts)
123 mkdir_mock = self.useFixture(mockpatch.Patch('os.mkdir'))
124 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
125 test_account_class = accounts.Accounts('test_name')
126 with mock.patch('__builtin__.open', mock.mock_open(),
127 create=True) as open_mock:
128 test_account_class._get_free_hash(hash_list)
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000129 # FIXME(dhellmann): The configuration option is not part
130 # of the API of the library, because if we change the
131 # option name or group it will break this use. Tempest
132 # needs to set this value somewhere that it owns, and then
133 # use lockutils.set_defaults() to tell oslo.concurrency
134 # what value to use.
135 lock_path = os.path.join(accounts.CONF.oslo_concurrency.lock_path,
136 '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))
150 test_account_class = accounts.Accounts('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)
160 test_account_class = accounts.Accounts('test_name')
161
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)
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000173 # FIXME(dhellmann): The configuration option is not part
174 # of the API of the library, because if we change the
175 # option name or group it will break this use. Tempest
176 # needs to set this value somewhere that it owns, and then
177 # use lockutils.set_defaults() to tell oslo.concurrency
178 # what value to use.
179 lock_path = os.path.join(accounts.CONF.oslo_concurrency.lock_path,
180 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400181 hash_list[3])
Matthew Treinish4041b262015-02-27 11:18:54 -0500182 open_mock.assert_has_calls([mock.call(lock_path, 'w')])
Matthew Treinishc791ac42014-07-16 09:15:23 -0400183
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000184 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400185 def test_remove_hash_last_account(self, lock_mock):
186 hash_list = self._get_hash_list(self.test_accounts)
187 # Pretend the pseudo-lock is there
188 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
189 # Pretend the lock dir is empty
190 self.useFixture(mockpatch.Patch('os.listdir', return_value=[]))
191 test_account_class = accounts.Accounts('test_name')
192 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
193 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
194 test_account_class.remove_hash(hash_list[2])
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000195 # FIXME(dhellmann): The configuration option is not part of
196 # the API of the library, because if we change the option name
197 # or group it will break this use. Tempest needs to set this
198 # value somewhere that it owns, and then use
199 # lockutils.set_defaults() to tell oslo.concurrency what value
200 # to use.
201 hash_path = os.path.join(accounts.CONF.oslo_concurrency.lock_path,
202 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400203 hash_list[2])
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000204 lock_path = os.path.join(accounts.CONF.oslo_concurrency.lock_path,
205 'test_accounts')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400206 remove_mock.mock.assert_called_once_with(hash_path)
207 rmdir_mock.mock.assert_called_once_with(lock_path)
208
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000209 @mock.patch('oslo_concurrency.lockutils.lock')
Matthew Treinishc791ac42014-07-16 09:15:23 -0400210 def test_remove_hash_not_last_account(self, lock_mock):
211 hash_list = self._get_hash_list(self.test_accounts)
212 # Pretend the pseudo-lock is there
213 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
214 # Pretend the lock dir is empty
215 self.useFixture(mockpatch.Patch('os.listdir', return_value=[
216 hash_list[1], hash_list[4]]))
217 test_account_class = accounts.Accounts('test_name')
218 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
219 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
220 test_account_class.remove_hash(hash_list[2])
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000221 # FIXME(dhellmann): The configuration option is not part of
222 # the API of the library, because if we change the option name
223 # or group it will break this use. Tempest needs to set this
224 # value somewhere that it owns, and then use
225 # lockutils.set_defaults() to tell oslo.concurrency what value
226 # to use.
227 hash_path = os.path.join(accounts.CONF.oslo_concurrency.lock_path,
228 'test_accounts',
Matthew Treinishc791ac42014-07-16 09:15:23 -0400229 hash_list[2])
230 remove_mock.mock.assert_called_once_with(hash_path)
231 rmdir_mock.mock.assert_not_called()
Matthew Treinish09f17832014-08-15 15:22:50 -0400232
233 def test_is_multi_user(self):
234 test_accounts_class = accounts.Accounts('test_name')
235 self.assertTrue(test_accounts_class.is_multi_user())
236
237 def test_is_not_multi_user(self):
238 self.test_accounts = [self.test_accounts[0]]
239 self.useFixture(mockpatch.Patch(
240 'tempest.common.accounts.read_accounts_yaml',
241 return_value=self.test_accounts))
242 test_accounts_class = accounts.Accounts('test_name')
243 self.assertFalse(test_accounts_class.is_multi_user())
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100244
Matthew Treinish976e8df2014-12-19 14:21:54 -0500245 def test__get_creds_by_roles_one_role(self):
246 self.useFixture(mockpatch.Patch(
247 'tempest.common.accounts.read_accounts_yaml',
248 return_value=self.test_accounts))
249 test_accounts_class = accounts.Accounts('test_name')
250 hashes = test_accounts_class.hash_dict['roles']['role4']
251 temp_hash = hashes[0]
252 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
253 test_accounts_class, '_get_free_hash', return_value=temp_hash))
254 # Test a single role returns all matching roles
255 test_accounts_class._get_creds(roles=['role4'])
256 calls = get_free_hash_mock.mock.mock_calls
257 self.assertEqual(len(calls), 1)
258 args = calls[0][1][0]
259 for i in hashes:
260 self.assertIn(i, args)
261
262 def test__get_creds_by_roles_list_role(self):
263 self.useFixture(mockpatch.Patch(
264 'tempest.common.accounts.read_accounts_yaml',
265 return_value=self.test_accounts))
266 test_accounts_class = accounts.Accounts('test_name')
267 hashes = test_accounts_class.hash_dict['roles']['role4']
268 hashes2 = test_accounts_class.hash_dict['roles']['role2']
269 hashes = list(set(hashes) & set(hashes2))
270 temp_hash = hashes[0]
271 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
272 test_accounts_class, '_get_free_hash', return_value=temp_hash))
273 # Test an intersection of multiple roles
274 test_accounts_class._get_creds(roles=['role2', 'role4'])
275 calls = get_free_hash_mock.mock.mock_calls
276 self.assertEqual(len(calls), 1)
277 args = calls[0][1][0]
278 for i in hashes:
279 self.assertIn(i, args)
280
281 def test__get_creds_by_roles_no_admin(self):
282 self.useFixture(mockpatch.Patch(
283 'tempest.common.accounts.read_accounts_yaml',
284 return_value=self.test_accounts))
285 test_accounts_class = accounts.Accounts('test_name')
286 hashes = test_accounts_class.hash_dict['creds'].keys()
287 admin_hashes = test_accounts_class.hash_dict['roles'][
288 cfg.CONF.identity.admin_role]
289 temp_hash = hashes[0]
290 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(
291 test_accounts_class, '_get_free_hash', return_value=temp_hash))
292 # Test an intersection of multiple roles
293 test_accounts_class._get_creds()
294 calls = get_free_hash_mock.mock.mock_calls
295 self.assertEqual(len(calls), 1)
296 args = calls[0][1][0]
297 self.assertEqual(len(args), 10)
298 for i in admin_hashes:
299 self.assertNotIn(i, args)
300
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100301
302class TestNotLockingAccount(base.TestCase):
303
304 def setUp(self):
305 super(TestNotLockingAccount, self).setUp()
306 self.useFixture(fake_config.ConfigFixture())
307 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)
Doug Hellmann583ce2c2015-03-11 14:55:46 +0000308 self.useFixture(lockutils_fixtures.ExternalLockFixture())
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100309 self.test_accounts = [
310 {'username': 'test_user1', 'tenant_name': 'test_tenant1',
311 'password': 'p'},
312 {'username': 'test_user2', 'tenant_name': 'test_tenant2',
313 'password': 'p'},
314 {'username': 'test_user3', 'tenant_name': 'test_tenant3',
315 'password': 'p'},
316 ]
317 self.useFixture(mockpatch.Patch(
318 'tempest.common.accounts.read_accounts_yaml',
319 return_value=self.test_accounts))
320 cfg.CONF.set_default('test_accounts_file', '', group='auth')
Matthew Treinishb19eeb82014-09-04 09:57:46 -0400321 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100322
323 def test_get_creds(self):
324 test_accounts_class = accounts.NotLockingAccounts('test_name')
325 for i in xrange(len(self.test_accounts)):
326 creds = test_accounts_class.get_creds(i)
327 msg = "Empty credentials returned for ID %s" % str(i)
328 self.assertIsNotNone(creds, msg)
329 self.assertRaises(exceptions.InvalidConfiguration,
330 test_accounts_class.get_creds,
Matthew Treinishb19eeb82014-09-04 09:57:46 -0400331 id=len(self.test_accounts))