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 |
| 25 | from tempest.common import http |
| 26 | from tempest import config |
| 27 | from tempest import exceptions |
| 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') |
| 60 | |
| 61 | def _get_hash_list(self, accounts_list): |
| 62 | hash_list = [] |
| 63 | for account in accounts_list: |
| 64 | hash = hashlib.md5() |
| 65 | hash.update(str(account)) |
| 66 | hash_list.append(hash.hexdigest()) |
| 67 | return hash_list |
| 68 | |
| 69 | def test_get_hash(self): |
| 70 | self.stubs.Set(http.ClosingHttp, 'request', |
| 71 | fake_identity._fake_v2_response) |
| 72 | test_account_class = accounts.Accounts('test_name') |
| 73 | hash_list = self._get_hash_list(self.test_accounts) |
| 74 | test_cred_dict = self.test_accounts[3] |
| 75 | test_creds = auth.get_credentials(**test_cred_dict) |
| 76 | results = test_account_class.get_hash(test_creds) |
| 77 | self.assertEqual(hash_list[3], results) |
| 78 | |
| 79 | def test_get_hash_dict(self): |
| 80 | test_account_class = accounts.Accounts('test_name') |
| 81 | hash_dict = test_account_class.get_hash_dict(self.test_accounts) |
| 82 | hash_list = self._get_hash_list(self.test_accounts) |
| 83 | for hash in hash_list: |
| 84 | self.assertIn(hash, hash_dict.keys()) |
| 85 | self.assertIn(hash_dict[hash], self.test_accounts) |
| 86 | |
| 87 | def test_create_hash_file_previous_file(self): |
| 88 | # Emulate the lock existing on the filesystem |
| 89 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True)) |
| 90 | with mock.patch('__builtin__.open', mock.mock_open(), create=True): |
| 91 | test_account_class = accounts.Accounts('test_name') |
| 92 | res = test_account_class._create_hash_file('12345') |
| 93 | self.assertFalse(res, "_create_hash_file should return False if the " |
| 94 | "pseudo-lock file already exists") |
| 95 | |
| 96 | def test_create_hash_file_no_previous_file(self): |
| 97 | # Emulate the lock not existing on the filesystem |
| 98 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False)) |
| 99 | with mock.patch('__builtin__.open', mock.mock_open(), create=True): |
| 100 | test_account_class = accounts.Accounts('test_name') |
| 101 | res = test_account_class._create_hash_file('12345') |
| 102 | self.assertTrue(res, "_create_hash_file should return True if the " |
| 103 | "pseudo-lock doesn't already exist") |
| 104 | |
| 105 | @mock.patch('tempest.openstack.common.lockutils.lock') |
| 106 | def test_get_free_hash_no_previous_accounts(self, lock_mock): |
| 107 | # Emulate no pre-existing lock |
| 108 | self.useFixture(mockpatch.Patch('os.path.isdir', return_value=False)) |
| 109 | hash_list = self._get_hash_list(self.test_accounts) |
| 110 | mkdir_mock = self.useFixture(mockpatch.Patch('os.mkdir')) |
| 111 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False)) |
| 112 | test_account_class = accounts.Accounts('test_name') |
| 113 | with mock.patch('__builtin__.open', mock.mock_open(), |
| 114 | create=True) as open_mock: |
| 115 | test_account_class._get_free_hash(hash_list) |
| 116 | lock_path = os.path.join(accounts.CONF.lock_path, 'test_accounts', |
| 117 | hash_list[0]) |
| 118 | open_mock.assert_called_once_with(lock_path, 'w') |
| 119 | mkdir_path = os.path.join(accounts.CONF.lock_path, 'test_accounts') |
| 120 | mkdir_mock.mock.assert_called_once_with(mkdir_path) |
| 121 | |
| 122 | @mock.patch('tempest.openstack.common.lockutils.lock') |
| 123 | def test_get_free_hash_no_free_accounts(self, lock_mock): |
| 124 | hash_list = self._get_hash_list(self.test_accounts) |
| 125 | # Emulate pre-existing lock dir |
| 126 | self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True)) |
| 127 | # Emulate all lcoks in list are in use |
| 128 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True)) |
| 129 | test_account_class = accounts.Accounts('test_name') |
| 130 | self.assertRaises(exceptions.InvalidConfiguration, |
| 131 | test_account_class._get_free_hash, hash_list) |
| 132 | |
| 133 | @mock.patch('tempest.openstack.common.lockutils.lock') |
| 134 | def test_get_free_hash_some_in_use_accounts(self, lock_mock): |
| 135 | # Emulate no pre-existing lock |
| 136 | self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True)) |
| 137 | hash_list = self._get_hash_list(self.test_accounts) |
| 138 | test_account_class = accounts.Accounts('test_name') |
| 139 | |
| 140 | def _fake_is_file(path): |
| 141 | # Fake isfile() to return that the path exists unless a specific |
| 142 | # hash is in the path |
| 143 | if hash_list[3] in path: |
| 144 | return False |
| 145 | return True |
| 146 | |
| 147 | self.stubs.Set(os.path, 'isfile', _fake_is_file) |
| 148 | with mock.patch('__builtin__.open', mock.mock_open(), |
| 149 | create=True) as open_mock: |
| 150 | test_account_class._get_free_hash(hash_list) |
| 151 | lock_path = os.path.join(accounts.CONF.lock_path, 'test_accounts', |
| 152 | hash_list[3]) |
| 153 | open_mock.assert_called_once_with(lock_path, 'w') |
| 154 | |
| 155 | @mock.patch('tempest.openstack.common.lockutils.lock') |
| 156 | def test_remove_hash_last_account(self, lock_mock): |
| 157 | hash_list = self._get_hash_list(self.test_accounts) |
| 158 | # Pretend the pseudo-lock is there |
| 159 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True)) |
| 160 | # Pretend the lock dir is empty |
| 161 | self.useFixture(mockpatch.Patch('os.listdir', return_value=[])) |
| 162 | test_account_class = accounts.Accounts('test_name') |
| 163 | remove_mock = self.useFixture(mockpatch.Patch('os.remove')) |
| 164 | rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir')) |
| 165 | test_account_class.remove_hash(hash_list[2]) |
| 166 | hash_path = os.path.join(accounts.CONF.lock_path, 'test_accounts', |
| 167 | hash_list[2]) |
| 168 | lock_path = os.path.join(accounts.CONF.lock_path, 'test_accounts') |
| 169 | remove_mock.mock.assert_called_once_with(hash_path) |
| 170 | rmdir_mock.mock.assert_called_once_with(lock_path) |
| 171 | |
| 172 | @mock.patch('tempest.openstack.common.lockutils.lock') |
| 173 | def test_remove_hash_not_last_account(self, lock_mock): |
| 174 | hash_list = self._get_hash_list(self.test_accounts) |
| 175 | # Pretend the pseudo-lock is there |
| 176 | self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True)) |
| 177 | # Pretend the lock dir is empty |
| 178 | self.useFixture(mockpatch.Patch('os.listdir', return_value=[ |
| 179 | hash_list[1], hash_list[4]])) |
| 180 | test_account_class = accounts.Accounts('test_name') |
| 181 | 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]) |
| 184 | hash_path = os.path.join(accounts.CONF.lock_path, 'test_accounts', |
| 185 | hash_list[2]) |
| 186 | remove_mock.mock.assert_called_once_with(hash_path) |
| 187 | rmdir_mock.mock.assert_not_called() |
Matthew Treinish | 09f1783 | 2014-08-15 15:22:50 -0400 | [diff] [blame^] | 188 | |
| 189 | def test_is_multi_user(self): |
| 190 | test_accounts_class = accounts.Accounts('test_name') |
| 191 | self.assertTrue(test_accounts_class.is_multi_user()) |
| 192 | |
| 193 | def test_is_not_multi_user(self): |
| 194 | self.test_accounts = [self.test_accounts[0]] |
| 195 | self.useFixture(mockpatch.Patch( |
| 196 | 'tempest.common.accounts.read_accounts_yaml', |
| 197 | return_value=self.test_accounts)) |
| 198 | test_accounts_class = accounts.Accounts('test_name') |
| 199 | self.assertFalse(test_accounts_class.is_multi_user()) |