blob: a836a20df3c135a5a5e3b1bbbc7c071ad674db1d [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
ghanshyamc0edda02015-02-06 15:51:40 +090027from tempest.services.identity.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)
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):
ghanshyamc0edda02015-02-06 15:51:40 +090071 self.stubs.Set(token_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]
ghanshyam5ff763f2015-02-18 16:15:58 +090076 test_creds = auth.get_credentials(fake_identity.FAKE_AUTH_URL,
77 **test_cred_dict)
Matthew Treinishc791ac42014-07-16 09:15:23 -040078 results = test_account_class.get_hash(test_creds)
79 self.assertEqual(hash_list[3], results)
80
81 def test_get_hash_dict(self):
82 test_account_class = accounts.Accounts('test_name')
83 hash_dict = test_account_class.get_hash_dict(self.test_accounts)
84 hash_list = self._get_hash_list(self.test_accounts)
85 for hash in hash_list:
86 self.assertIn(hash, hash_dict.keys())
87 self.assertIn(hash_dict[hash], self.test_accounts)
88
89 def test_create_hash_file_previous_file(self):
90 # Emulate the lock existing on the filesystem
91 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
92 with mock.patch('__builtin__.open', mock.mock_open(), create=True):
93 test_account_class = accounts.Accounts('test_name')
94 res = test_account_class._create_hash_file('12345')
95 self.assertFalse(res, "_create_hash_file should return False if the "
96 "pseudo-lock file already exists")
97
98 def test_create_hash_file_no_previous_file(self):
99 # Emulate the lock not existing on the filesystem
100 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
101 with mock.patch('__builtin__.open', mock.mock_open(), create=True):
102 test_account_class = accounts.Accounts('test_name')
103 res = test_account_class._create_hash_file('12345')
104 self.assertTrue(res, "_create_hash_file should return True if the "
105 "pseudo-lock doesn't already exist")
106
107 @mock.patch('tempest.openstack.common.lockutils.lock')
108 def test_get_free_hash_no_previous_accounts(self, lock_mock):
109 # Emulate no pre-existing lock
110 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=False))
111 hash_list = self._get_hash_list(self.test_accounts)
112 mkdir_mock = self.useFixture(mockpatch.Patch('os.mkdir'))
113 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))
114 test_account_class = accounts.Accounts('test_name')
115 with mock.patch('__builtin__.open', mock.mock_open(),
116 create=True) as open_mock:
117 test_account_class._get_free_hash(hash_list)
118 lock_path = os.path.join(accounts.CONF.lock_path, 'test_accounts',
119 hash_list[0])
120 open_mock.assert_called_once_with(lock_path, 'w')
121 mkdir_path = os.path.join(accounts.CONF.lock_path, 'test_accounts')
122 mkdir_mock.mock.assert_called_once_with(mkdir_path)
123
124 @mock.patch('tempest.openstack.common.lockutils.lock')
125 def test_get_free_hash_no_free_accounts(self, lock_mock):
126 hash_list = self._get_hash_list(self.test_accounts)
127 # Emulate pre-existing lock dir
128 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))
129 # Emulate all lcoks in list are in use
130 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
131 test_account_class = accounts.Accounts('test_name')
132 self.assertRaises(exceptions.InvalidConfiguration,
133 test_account_class._get_free_hash, hash_list)
134
135 @mock.patch('tempest.openstack.common.lockutils.lock')
136 def test_get_free_hash_some_in_use_accounts(self, lock_mock):
137 # Emulate no pre-existing lock
138 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))
139 hash_list = self._get_hash_list(self.test_accounts)
140 test_account_class = accounts.Accounts('test_name')
141
142 def _fake_is_file(path):
143 # Fake isfile() to return that the path exists unless a specific
144 # hash is in the path
145 if hash_list[3] in path:
146 return False
147 return True
148
149 self.stubs.Set(os.path, 'isfile', _fake_is_file)
150 with mock.patch('__builtin__.open', mock.mock_open(),
151 create=True) as open_mock:
152 test_account_class._get_free_hash(hash_list)
153 lock_path = os.path.join(accounts.CONF.lock_path, 'test_accounts',
154 hash_list[3])
155 open_mock.assert_called_once_with(lock_path, 'w')
156
157 @mock.patch('tempest.openstack.common.lockutils.lock')
158 def test_remove_hash_last_account(self, lock_mock):
159 hash_list = self._get_hash_list(self.test_accounts)
160 # Pretend the pseudo-lock is there
161 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
162 # Pretend the lock dir is empty
163 self.useFixture(mockpatch.Patch('os.listdir', return_value=[]))
164 test_account_class = accounts.Accounts('test_name')
165 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
166 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
167 test_account_class.remove_hash(hash_list[2])
168 hash_path = os.path.join(accounts.CONF.lock_path, 'test_accounts',
169 hash_list[2])
170 lock_path = os.path.join(accounts.CONF.lock_path, 'test_accounts')
171 remove_mock.mock.assert_called_once_with(hash_path)
172 rmdir_mock.mock.assert_called_once_with(lock_path)
173
174 @mock.patch('tempest.openstack.common.lockutils.lock')
175 def test_remove_hash_not_last_account(self, lock_mock):
176 hash_list = self._get_hash_list(self.test_accounts)
177 # Pretend the pseudo-lock is there
178 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
179 # Pretend the lock dir is empty
180 self.useFixture(mockpatch.Patch('os.listdir', return_value=[
181 hash_list[1], hash_list[4]]))
182 test_account_class = accounts.Accounts('test_name')
183 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))
184 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))
185 test_account_class.remove_hash(hash_list[2])
186 hash_path = os.path.join(accounts.CONF.lock_path, 'test_accounts',
187 hash_list[2])
188 remove_mock.mock.assert_called_once_with(hash_path)
189 rmdir_mock.mock.assert_not_called()
Matthew Treinish09f17832014-08-15 15:22:50 -0400190
191 def test_is_multi_user(self):
192 test_accounts_class = accounts.Accounts('test_name')
193 self.assertTrue(test_accounts_class.is_multi_user())
194
195 def test_is_not_multi_user(self):
196 self.test_accounts = [self.test_accounts[0]]
197 self.useFixture(mockpatch.Patch(
198 'tempest.common.accounts.read_accounts_yaml',
199 return_value=self.test_accounts))
200 test_accounts_class = accounts.Accounts('test_name')
201 self.assertFalse(test_accounts_class.is_multi_user())
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100202
203
204class TestNotLockingAccount(base.TestCase):
205
206 def setUp(self):
207 super(TestNotLockingAccount, self).setUp()
208 self.useFixture(fake_config.ConfigFixture())
209 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)
210 self.temp_dir = tempfile.mkdtemp()
211 cfg.CONF.set_default('lock_path', self.temp_dir)
212 self.addCleanup(os.rmdir, self.temp_dir)
213 self.test_accounts = [
214 {'username': 'test_user1', 'tenant_name': 'test_tenant1',
215 'password': 'p'},
216 {'username': 'test_user2', 'tenant_name': 'test_tenant2',
217 'password': 'p'},
218 {'username': 'test_user3', 'tenant_name': 'test_tenant3',
219 'password': 'p'},
220 ]
221 self.useFixture(mockpatch.Patch(
222 'tempest.common.accounts.read_accounts_yaml',
223 return_value=self.test_accounts))
224 cfg.CONF.set_default('test_accounts_file', '', group='auth')
Matthew Treinishb19eeb82014-09-04 09:57:46 -0400225 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))
Andrea Frittolib1c23fc2014-09-03 13:40:08 +0100226
227 def test_get_creds(self):
228 test_accounts_class = accounts.NotLockingAccounts('test_name')
229 for i in xrange(len(self.test_accounts)):
230 creds = test_accounts_class.get_creds(i)
231 msg = "Empty credentials returned for ID %s" % str(i)
232 self.assertIsNotNone(creds, msg)
233 self.assertRaises(exceptions.InvalidConfiguration,
234 test_accounts_class.get_creds,
Matthew Treinishb19eeb82014-09-04 09:57:46 -0400235 id=len(self.test_accounts))