blob: 0800b47bfb4020a7ec8776749171832049a3d259 [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
17import tempfile
18
19import mock
20from oslo.config import cfg
21from 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
Ken'ichi Ohmichi03010dc2015-01-08 08:16:25 +000027from tempest.services.identity.json import identity_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)
39 self.temp_dir = tempfile.mkdtemp()
40 cfg.CONF.set_default('lock_path', self.temp_dir)
41 self.addCleanup(os.rmdir, self.temp_dir)
42 self.test_accounts = [
43 {'username': 'test_user1', 'tenant_name': 'test_tenant1',
44 'password': 'p'},
45 {'username': 'test_user2', 'tenant_name': 'test_tenant2',
46 'password': 'p'},
47 {'username': 'test_user3', 'tenant_name': 'test_tenant3',
48 'password': 'p'},
49 {'username': 'test_user4', 'tenant_name': 'test_tenant4',
50 'password': 'p'},
51 {'username': 'test_user5', 'tenant_name': 'test_tenant5',
52 'password': 'p'},
53 {'username': 'test_user6', 'tenant_name': 'test_tenant6',
54 'password': 'p'},
55 ]
56 self.useFixture(mockpatch.Patch(
57 'tempest.common.accounts.read_accounts_yaml',
58 return_value=self.test_accounts))
59 cfg.CONF.set_default('test_accounts_file', '', group='auth')
Matthew Treinishb19eeb82014-09-04 09:57:46 -040060 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Matthew Treinishc791ac42014-07-16 09:15:23 -040061
62 def _get_hash_list(self, accounts_list):
63 hash_list = []
64 for account in accounts_list:
65 hash = hashlib.md5()
66 hash.update(str(account))
67 hash_list.append(hash.hexdigest())
68 return hash_list
69
70 def test_get_hash(self):
Ken'ichi Ohmichi03010dc2015-01-08 08:16:25 +000071 self.stubs.Set(identity_client.TokenClientJSON, 'raw_request',
Matthew Treinishc791ac42014-07-16 09:15:23 -040072 fake_identity._fake_v2_response)
73 test_account_class = accounts.Accounts('test_name')
74 hash_list = self._get_hash_list(self.test_accounts)
75 test_cred_dict = self.test_accounts[3]
76 test_creds = auth.get_credentials(**test_cred_dict)
77 results = test_account_class.get_hash(test_creds)
78 self.assertEqual(hash_list[3], results)
79
80 def test_get_hash_dict(self):
81 test_account_class = accounts.Accounts('test_name')
82 hash_dict = test_account_class.get_hash_dict(self.test_accounts)
83 hash_list = self._get_hash_list(self.test_accounts)
84 for hash in hash_list:
85 self.assertIn(hash, hash_dict.keys())
86 self.assertIn(hash_dict[hash], self.test_accounts)
87
88 def test_create_hash_file_previous_file(self):
89 # Emulate the lock existing on the filesystem
90 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
91 with mock.patch('__builtin__.open', mock.mock_open(), create=True):
92 test_account_class = accounts.Accounts('test_name')
93 res = test_account_class._create_hash_file('12345')
94 self.assertFalse(res, "_create_hash_file should return False if the "
95 "pseudo-lock file already exists")
96
97 def test_create_hash_file_no_previous_file(self):
98 # Emulate the lock not existing on the filesystem
99 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
100 with mock.patch('__builtin__.open', mock.mock_open(), create=True):
101 test_account_class = accounts.Accounts('test_name')
102 res = test_account_class._create_hash_file('12345')
103 self.assertTrue(res, "_create_hash_file should return True if the "
104 "pseudo-lock doesn't already exist")
105
106 @mock.patch('tempest.openstack.common.lockutils.lock')
107 def test_get_free_hash_no_previous_accounts(self, lock_mock):
108 # Emulate no pre-existing lock
109 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=False))
110 hash_list = self._get_hash_list(self.test_accounts)
111 mkdir_mock = self.useFixture(mockpatch.Patch('os.mkdir'))
112 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
113 test_account_class = accounts.Accounts('test_name')
114 with mock.patch('__builtin__.open', mock.mock_open(),
115 create=True) as open_mock:
116 test_account_class._get_free_hash(hash_list)
117 lock_path = os.path.join(accounts.CONF.lock_path, 'test_accounts',
118 hash_list[0])
119 open_mock.assert_called_once_with(lock_path, 'w')
120 mkdir_path = os.path.join(accounts.CONF.lock_path, 'test_accounts')
121 mkdir_mock.mock.assert_called_once_with(mkdir_path)
122
123 @mock.patch('tempest.openstack.common.lockutils.lock')
124 def test_get_free_hash_no_free_accounts(self, lock_mock):
125 hash_list = self._get_hash_list(self.test_accounts)
126 # Emulate pre-existing lock dir
127 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))
128 # Emulate all lcoks in list are in use
129 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
130 test_account_class = accounts.Accounts('test_name')
131 self.assertRaises(exceptions.InvalidConfiguration,
132 test_account_class._get_free_hash, hash_list)
133
134 @mock.patch('tempest.openstack.common.lockutils.lock')
135 def test_get_free_hash_some_in_use_accounts(self, lock_mock):
136 # Emulate no pre-existing lock
137 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))
138 hash_list = self._get_hash_list(self.test_accounts)
139 test_account_class = accounts.Accounts('test_name')
140
141 def _fake_is_file(path):
142 # Fake isfile() to return that the path exists unless a specific
143 # hash is in the path
144 if hash_list[3] in path:
145 return False
146 return True
147
148 self.stubs.Set(os.path, 'isfile', _fake_is_file)
149 with mock.patch('__builtin__.open', mock.mock_open(),
150 create=True) as open_mock:
151 test_account_class._get_free_hash(hash_list)
152 lock_path = os.path.join(accounts.CONF.lock_path, 'test_accounts',
153 hash_list[3])
154 open_mock.assert_called_once_with(lock_path, 'w')
155
156 @mock.patch('tempest.openstack.common.lockutils.lock')
157 def test_remove_hash_last_account(self, lock_mock):
158 hash_list = self._get_hash_list(self.test_accounts)
159 # Pretend the pseudo-lock is there
160 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
161 # Pretend the lock dir is empty
162 self.useFixture(mockpatch.Patch('os.listdir', return_value=[]))
163 test_account_class = accounts.Accounts('test_name')
164 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
165 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
166 test_account_class.remove_hash(hash_list[2])
167 hash_path = os.path.join(accounts.CONF.lock_path, 'test_accounts',
168 hash_list[2])
169 lock_path = os.path.join(accounts.CONF.lock_path, 'test_accounts')
170 remove_mock.mock.assert_called_once_with(hash_path)
171 rmdir_mock.mock.assert_called_once_with(lock_path)
172
173 @mock.patch('tempest.openstack.common.lockutils.lock')
174 def test_remove_hash_not_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=[
180 hash_list[1], hash_list[4]]))
181 test_account_class = accounts.Accounts('test_name')
182 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
183 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
184 test_account_class.remove_hash(hash_list[2])
185 hash_path = os.path.join(accounts.CONF.lock_path, 'test_accounts',
186 hash_list[2])
187 remove_mock.mock.assert_called_once_with(hash_path)
188 rmdir_mock.mock.assert_not_called()
Matthew Treinish09f17832014-08-15 15:22:50 -0400189
190 def test_is_multi_user(self):
191 test_accounts_class = accounts.Accounts('test_name')
192 self.assertTrue(test_accounts_class.is_multi_user())
193
194 def test_is_not_multi_user(self):
195 self.test_accounts = [self.test_accounts[0]]
196 self.useFixture(mockpatch.Patch(
197 'tempest.common.accounts.read_accounts_yaml',
198 return_value=self.test_accounts))
199 test_accounts_class = accounts.Accounts('test_name')
200 self.assertFalse(test_accounts_class.is_multi_user())
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100201
202
203class TestNotLockingAccount(base.TestCase):
204
205 def setUp(self):
206 super(TestNotLockingAccount, self).setUp()
207 self.useFixture(fake_config.ConfigFixture())
208 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)
209 self.temp_dir = tempfile.mkdtemp()
210 cfg.CONF.set_default('lock_path', self.temp_dir)
211 self.addCleanup(os.rmdir, self.temp_dir)
212 self.test_accounts = [
213 {'username': 'test_user1', 'tenant_name': 'test_tenant1',
214 'password': 'p'},
215 {'username': 'test_user2', 'tenant_name': 'test_tenant2',
216 'password': 'p'},
217 {'username': 'test_user3', 'tenant_name': 'test_tenant3',
218 'password': 'p'},
219 ]
220 self.useFixture(mockpatch.Patch(
221 'tempest.common.accounts.read_accounts_yaml',
222 return_value=self.test_accounts))
223 cfg.CONF.set_default('test_accounts_file', '', group='auth')
Matthew Treinishb19eeb82014-09-04 09:57:46 -0400224 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100225
226 def test_get_creds(self):
227 test_accounts_class = accounts.NotLockingAccounts('test_name')
228 for i in xrange(len(self.test_accounts)):
229 creds = test_accounts_class.get_creds(i)
230 msg = "Empty credentials returned for ID %s" % str(i)
231 self.assertIsNotNone(creds, msg)
232 self.assertRaises(exceptions.InvalidConfiguration,
233 test_accounts_class.get_creds,
Matthew Treinishb19eeb82014-09-04 09:57:46 -0400234 id=len(self.test_accounts))