Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 1 | # 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 | |
| 15 | import hashlib |
| 16 | import os |
| 17 | import tempfile |
| 18 | |
| 19 | import mock |
| 20 | from oslo.config import cfg |
| 21 | from oslotest import mockpatch |
| 22 | |
| 23 | from tempest import auth |
| 24 | from tempest.common import accounts |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 25 | from tempest import config |
| 26 | from tempest import exceptions |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 27 | from tempest.services.identity.json import token_client |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 28 | from tempest.tests import base |
| 29 | from tempest.tests import fake_config |
| 30 | from tempest.tests import fake_identity |
| 31 | |
| 32 | |
| 33 | class 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 Treinish | b19eeb8 | 2014-09-04 09:57:46 -0400 | [diff] [blame] | 60 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True)) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 61 | |
| 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): |
ghanshyam | c0edda0 | 2015-02-06 15:51:40 +0900 | [diff] [blame] | 71 | self.stubs.Set(token_client.TokenClientJSON, 'raw_request', |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 72 | 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] |
ghanshyam | 5ff763f | 2015-02-18 16:15:58 +0900 | [diff] [blame^] | 76 | test_creds = auth.get_credentials(fake_identity.FAKE_AUTH_URL, |
| 77 | **test_cred_dict) |
Matthew Treinish | c791ac4 | 2014-07-16 09:15:23 -0400 | [diff] [blame] | 78 | 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 Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame] | 190 | |
| 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 Frittoli | b1c23fc | 2014-09-03 13:40:08 +0100 | [diff] [blame] | 202 | |
| 203 | |
| 204 | class 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 Treinish | b19eeb8 | 2014-09-04 09:57:46 -0400 | [diff] [blame] | 225 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True)) |
Andrea Frittoli | b1c23fc | 2014-09-03 13:40:08 +0100 | [diff] [blame] | 226 | |
| 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 Treinish | b19eeb8 | 2014-09-04 09:57:46 -0400 | [diff] [blame] | 235 | id=len(self.test_accounts)) |