Best Python code snippet using tempest_python
test_preprov_creds.py
Source:test_preprov_creds.py
1# Copyright 2014 Hewlett-Packard Development Company, L.P.2#3# Licensed under the Apache License, Version 2.0 (the "License"); you may4# not use this file except in compliance with the License. You may obtain5# a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the12# License for the specific language governing permissions and limitations13# under the License.14import hashlib15import os16import mock17from oslo_concurrency.fixture import lockutils as lockutils_fixtures18from oslo_concurrency import lockutils19from oslo_config import cfg20from oslotest import mockpatch21import six22from tempest_lib import auth23from tempest_lib.services.identity.v2 import token_client24from tempest.common import cred_provider25from tempest.common import preprov_creds26from tempest import config27from tempest import exceptions28from tempest.tests import base29from tempest.tests import fake_config30from tempest.tests import fake_http31from tempest.tests import fake_identity32class TestPreProvisionedCredentials(base.TestCase):33 def setUp(self):34 super(TestPreProvisionedCredentials, self).setUp()35 self.useFixture(fake_config.ConfigFixture())36 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)37 self.fake_http = fake_http.fake_httplib2(return_type=200)38 self.stubs.Set(token_client.TokenClient, 'raw_request',39 fake_identity._fake_v2_response)40 self.useFixture(lockutils_fixtures.ExternalLockFixture())41 self.test_accounts = [42 {'username': 'test_user1', 'tenant_name': 'test_tenant1',43 'password': 'p'},44 {'username': 'test_user2', 'tenant_name': 'test_tenant2',45 'password': 'p'},46 {'username': 'test_user3', 'tenant_name': 'test_tenant3',47 'password': 'p'},48 {'username': 'test_user4', 'tenant_name': 'test_tenant4',49 'password': 'p'},50 {'username': 'test_user5', 'tenant_name': 'test_tenant5',51 'password': 'p'},52 {'username': 'test_user6', 'tenant_name': 'test_tenant6',53 'password': 'p', 'roles': ['role1', 'role2']},54 {'username': 'test_user7', 'tenant_name': 'test_tenant7',55 'password': 'p', 'roles': ['role2', 'role3']},56 {'username': 'test_user8', 'tenant_name': 'test_tenant8',57 'password': 'p', 'roles': ['role4', 'role1']},58 {'username': 'test_user9', 'tenant_name': 'test_tenant9',59 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},60 {'username': 'test_user10', 'tenant_name': 'test_tenant10',61 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},62 {'username': 'test_user11', 'tenant_name': 'test_tenant11',63 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]},64 {'username': 'test_user12', 'tenant_name': 'test_tenant12',65 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]},66 ]67 self.accounts_mock = self.useFixture(mockpatch.Patch(68 'tempest.common.preprov_creds.read_accounts_yaml',69 return_value=self.test_accounts))70 cfg.CONF.set_default('test_accounts_file', 'fake_path', group='auth')71 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))72 def _get_hash_list(self, accounts_list):73 hash_list = []74 for account in accounts_list:75 hash = hashlib.md5()76 hash.update(six.text_type(account).encode('utf-8'))77 temp_hash = hash.hexdigest()78 hash_list.append(temp_hash)79 return hash_list80 def test_get_hash(self):81 self.stubs.Set(token_client.TokenClient, 'raw_request',82 fake_identity._fake_v2_response)83 test_account_class = preprov_creds.PreProvisionedCredentialProvider(84 'v2', 'test_name')85 hash_list = self._get_hash_list(self.test_accounts)86 test_cred_dict = self.test_accounts[3]87 test_creds = auth.get_credentials(fake_identity.FAKE_AUTH_URL,88 **test_cred_dict)89 results = test_account_class.get_hash(test_creds)90 self.assertEqual(hash_list[3], results)91 def test_get_hash_dict(self):92 test_account_class = preprov_creds.PreProvisionedCredentialProvider(93 'v2', '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:97 self.assertIn(hash, hash_dict['creds'].keys())98 self.assertIn(hash_dict['creds'][hash], self.test_accounts)99 def test_create_hash_file_previous_file(self):100 # Emulate the lock existing on the filesystem101 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))102 with mock.patch('six.moves.builtins.open', mock.mock_open(),103 create=True):104 test_account_class = (105 preprov_creds.PreProvisionedCredentialProvider(106 'v2', 'test_name'))107 res = test_account_class._create_hash_file('12345')108 self.assertFalse(res, "_create_hash_file should return False if the "109 "pseudo-lock file already exists")110 def test_create_hash_file_no_previous_file(self):111 # Emulate the lock not existing on the filesystem112 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))113 with mock.patch('six.moves.builtins.open', mock.mock_open(),114 create=True):115 test_account_class = (116 preprov_creds.PreProvisionedCredentialProvider(117 'v2', 'test_name'))118 res = test_account_class._create_hash_file('12345')119 self.assertTrue(res, "_create_hash_file should return True if the "120 "pseudo-lock doesn't already exist")121 @mock.patch('oslo_concurrency.lockutils.lock')122 def test_get_free_hash_no_previous_accounts(self, lock_mock):123 # Emulate no pre-existing lock124 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=False))125 hash_list = self._get_hash_list(self.test_accounts)126 mkdir_mock = self.useFixture(mockpatch.Patch('os.mkdir'))127 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))128 test_account_class = preprov_creds.PreProvisionedCredentialProvider(129 'v2', 'test_name')130 with mock.patch('six.moves.builtins.open', mock.mock_open(),131 create=True) as open_mock:132 test_account_class._get_free_hash(hash_list)133 lock_path = os.path.join(lockutils.get_lock_path(134 preprov_creds.CONF), 'test_accounts', hash_list[0])135 open_mock.assert_called_once_with(lock_path, 'w')136 mkdir_path = os.path.join(137 preprov_creds.CONF.oslo_concurrency.lock_path, 'test_accounts')138 mkdir_mock.mock.assert_called_once_with(mkdir_path)139 @mock.patch('oslo_concurrency.lockutils.lock')140 def test_get_free_hash_no_free_accounts(self, lock_mock):141 hash_list = self._get_hash_list(self.test_accounts)142 # Emulate pre-existing lock dir143 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))144 # Emulate all lcoks in list are in use145 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))146 test_account_class = preprov_creds.PreProvisionedCredentialProvider(147 'v2', 'test_name')148 with mock.patch('six.moves.builtins.open', mock.mock_open(),149 create=True):150 self.assertRaises(exceptions.InvalidConfiguration,151 test_account_class._get_free_hash, hash_list)152 @mock.patch('oslo_concurrency.lockutils.lock')153 def test_get_free_hash_some_in_use_accounts(self, lock_mock):154 # Emulate no pre-existing lock155 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))156 hash_list = self._get_hash_list(self.test_accounts)157 test_account_class = preprov_creds.PreProvisionedCredentialProvider(158 'v2', 'test_name')159 def _fake_is_file(path):160 # Fake isfile() to return that the path exists unless a specific161 # hash is in the path162 if hash_list[3] in path:163 return False164 return True165 self.stubs.Set(os.path, 'isfile', _fake_is_file)166 with mock.patch('six.moves.builtins.open', mock.mock_open(),167 create=True) as open_mock:168 test_account_class._get_free_hash(hash_list)169 lock_path = os.path.join(170 lockutils.get_lock_path(preprov_creds.CONF),171 'test_accounts', hash_list[3])172 open_mock.assert_has_calls([mock.call(lock_path, 'w')])173 @mock.patch('oslo_concurrency.lockutils.lock')174 def test_remove_hash_last_account(self, lock_mock):175 hash_list = self._get_hash_list(self.test_accounts)176 # Pretend the pseudo-lock is there177 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))178 # Pretend the lock dir is empty179 self.useFixture(mockpatch.Patch('os.listdir', return_value=[]))180 test_account_class = preprov_creds.PreProvisionedCredentialProvider(181 'v2', '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(lockutils.get_lock_path(preprov_creds.CONF),186 'test_accounts',187 hash_list[2])188 lock_path = os.path.join(preprov_creds.CONF.oslo_concurrency.lock_path,189 'test_accounts')190 remove_mock.mock.assert_called_once_with(hash_path)191 rmdir_mock.mock.assert_called_once_with(lock_path)192 @mock.patch('oslo_concurrency.lockutils.lock')193 def test_remove_hash_not_last_account(self, lock_mock):194 hash_list = self._get_hash_list(self.test_accounts)195 # Pretend the pseudo-lock is there196 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))197 # Pretend the lock dir is empty198 self.useFixture(mockpatch.Patch('os.listdir', return_value=[199 hash_list[1], hash_list[4]]))200 test_account_class = preprov_creds.PreProvisionedCredentialProvider(201 'v2', 'test_name')202 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))203 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))204 test_account_class.remove_hash(hash_list[2])205 hash_path = os.path.join(lockutils.get_lock_path(preprov_creds.CONF),206 'test_accounts',207 hash_list[2])208 remove_mock.mock.assert_called_once_with(hash_path)209 rmdir_mock.mock.assert_not_called()210 def test_is_multi_user(self):211 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(212 'v2', 'test_name')213 self.assertTrue(test_accounts_class.is_multi_user())214 def test_is_not_multi_user(self):215 self.test_accounts = [self.test_accounts[0]]216 self.useFixture(mockpatch.Patch(217 'tempest.common.preprov_creds.read_accounts_yaml',218 return_value=self.test_accounts))219 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(220 'v2', 'test_name')221 self.assertFalse(test_accounts_class.is_multi_user())222 def test__get_creds_by_roles_one_role(self):223 self.useFixture(mockpatch.Patch(224 'tempest.common.preprov_creds.read_accounts_yaml',225 return_value=self.test_accounts))226 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(227 'v2', 'test_name')228 hashes = test_accounts_class.hash_dict['roles']['role4']229 temp_hash = hashes[0]230 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(231 test_accounts_class, '_get_free_hash', return_value=temp_hash))232 # Test a single role returns all matching roles233 test_accounts_class._get_creds(roles=['role4'])234 calls = get_free_hash_mock.mock.mock_calls235 self.assertEqual(len(calls), 1)236 args = calls[0][1][0]237 for i in hashes:238 self.assertIn(i, args)239 def test__get_creds_by_roles_list_role(self):240 self.useFixture(mockpatch.Patch(241 'tempest.common.preprov_creds.read_accounts_yaml',242 return_value=self.test_accounts))243 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(244 'v2', 'test_name')245 hashes = test_accounts_class.hash_dict['roles']['role4']246 hashes2 = test_accounts_class.hash_dict['roles']['role2']247 hashes = list(set(hashes) & set(hashes2))248 temp_hash = hashes[0]249 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(250 test_accounts_class, '_get_free_hash', return_value=temp_hash))251 # Test an intersection of multiple roles252 test_accounts_class._get_creds(roles=['role2', 'role4'])253 calls = get_free_hash_mock.mock.mock_calls254 self.assertEqual(len(calls), 1)255 args = calls[0][1][0]256 for i in hashes:257 self.assertIn(i, args)258 def test__get_creds_by_roles_no_admin(self):259 self.useFixture(mockpatch.Patch(260 'tempest.common.preprov_creds.read_accounts_yaml',261 return_value=self.test_accounts))262 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(263 'v2', 'test_name')264 hashes = list(test_accounts_class.hash_dict['creds'].keys())265 admin_hashes = test_accounts_class.hash_dict['roles'][266 cfg.CONF.identity.admin_role]267 temp_hash = hashes[0]268 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(269 test_accounts_class, '_get_free_hash', return_value=temp_hash))270 # Test an intersection of multiple roles271 test_accounts_class._get_creds()272 calls = get_free_hash_mock.mock.mock_calls273 self.assertEqual(len(calls), 1)274 args = calls[0][1][0]275 self.assertEqual(len(args), 10)276 for i in admin_hashes:277 self.assertNotIn(i, args)278 def test_networks_returned_with_creds(self):279 test_accounts = [280 {'username': 'test_user13', 'tenant_name': 'test_tenant13',281 'password': 'p', 'resources': {'network': 'network-1'}},282 {'username': 'test_user14', 'tenant_name': 'test_tenant14',283 'password': 'p', 'roles': ['role-7', 'role-11'],284 'resources': {'network': 'network-2'}}]285 self.useFixture(mockpatch.Patch(286 'tempest.common.preprov_creds.read_accounts_yaml',287 return_value=test_accounts))288 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(289 'v2', 'test_name')290 with mock.patch('tempest.services.compute.json.networks_client.'291 'NetworksClient.list_networks',292 return_value={'networks': [{'name': 'network-2',293 'id': 'fake-id',294 'label': 'network-2'}]}):295 creds = test_accounts_class.get_creds_by_roles(['role-7'])296 self.assertTrue(isinstance(creds, cred_provider.TestResources))297 network = creds.network298 self.assertIsNotNone(network)299 self.assertIn('name', network)300 self.assertIn('id', network)301 self.assertEqual('fake-id', network['id'])302 self.assertEqual('network-2', network['name'])303class TestNotLockingAccount(base.TestCase):304 def setUp(self):305 super(TestNotLockingAccount, self).setUp()306 self.useFixture(fake_config.ConfigFixture())307 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)308 self.useFixture(lockutils_fixtures.ExternalLockFixture())309 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.preprov_creds.read_accounts_yaml',319 return_value=self.test_accounts))320 cfg.CONF.set_default('test_accounts_file', '', group='auth')321 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))322 def test_get_creds_roles_nonlocking_invalid(self):323 test_accounts_class = preprov_creds.NonLockingCredentialProvider(324 'v2', 'test_name')325 self.assertRaises(exceptions.InvalidConfiguration,326 test_accounts_class.get_creds_by_roles,...
test_accounts.py
Source:test_accounts.py
1# Copyright 2014 Hewlett-Packard Development Company, L.P.2#3# Licensed under the Apache License, Version 2.0 (the "License"); you may4# not use this file except in compliance with the License. You may obtain5# a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the12# License for the specific language governing permissions and limitations13# under the License.14import hashlib15import os16import mock17from oslo_concurrency.fixture import lockutils as lockutils_fixtures18from oslo_concurrency import lockutils19from oslo_config import cfg20from oslotest import mockpatch21import six22from tempest_lib import auth23from tempest_lib.services.identity.v2 import token_client24from tempest.common import accounts25from tempest.common import cred_provider26from tempest import config27from tempest import exceptions28from tempest.tests import base29from tempest.tests import fake_config30from tempest.tests import fake_http31from tempest.tests import fake_identity32class TestAccount(base.TestCase):33 def setUp(self):34 super(TestAccount, self).setUp()35 self.useFixture(fake_config.ConfigFixture())36 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)37 self.fake_http = fake_http.fake_httplib2(return_type=200)38 self.stubs.Set(token_client.TokenClientJSON, 'raw_request',39 fake_identity._fake_v2_response)40 self.useFixture(lockutils_fixtures.ExternalLockFixture())41 self.test_accounts = [42 {'username': 'test_user1', 'tenant_name': 'test_tenant1',43 'password': 'p'},44 {'username': 'test_user2', 'tenant_name': 'test_tenant2',45 'password': 'p'},46 {'username': 'test_user3', 'tenant_name': 'test_tenant3',47 'password': 'p'},48 {'username': 'test_user4', 'tenant_name': 'test_tenant4',49 'password': 'p'},50 {'username': 'test_user5', 'tenant_name': 'test_tenant5',51 'password': 'p'},52 {'username': 'test_user6', 'tenant_name': 'test_tenant6',53 'password': 'p', 'roles': ['role1', 'role2']},54 {'username': 'test_user7', 'tenant_name': 'test_tenant7',55 'password': 'p', 'roles': ['role2', 'role3']},56 {'username': 'test_user8', 'tenant_name': 'test_tenant8',57 'password': 'p', 'roles': ['role4', 'role1']},58 {'username': 'test_user9', 'tenant_name': 'test_tenant9',59 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},60 {'username': 'test_user10', 'tenant_name': 'test_tenant10',61 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},62 {'username': 'test_user11', 'tenant_name': 'test_tenant11',63 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]},64 {'username': 'test_user12', 'tenant_name': 'test_tenant12',65 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]},66 ]67 self.accounts_mock = self.useFixture(mockpatch.Patch(68 'tempest.common.accounts.read_accounts_yaml',69 return_value=self.test_accounts))70 cfg.CONF.set_default('test_accounts_file', 'fake_path', group='auth')71 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))72 def _get_hash_list(self, accounts_list):73 hash_list = []74 for account in accounts_list:75 hash = hashlib.md5()76 hash.update(six.text_type(account).encode('utf-8'))77 temp_hash = hash.hexdigest()78 hash_list.append(temp_hash)79 return hash_list80 def test_get_hash(self):81 self.stubs.Set(token_client.TokenClientJSON, 'raw_request',82 fake_identity._fake_v2_response)83 test_account_class = accounts.Accounts('v2', 'test_name')84 hash_list = self._get_hash_list(self.test_accounts)85 test_cred_dict = self.test_accounts[3]86 test_creds = auth.get_credentials(fake_identity.FAKE_AUTH_URL,87 **test_cred_dict)88 results = test_account_class.get_hash(test_creds)89 self.assertEqual(hash_list[3], results)90 def test_get_hash_dict(self):91 test_account_class = accounts.Accounts('v2', 'test_name')92 hash_dict = test_account_class.get_hash_dict(self.test_accounts)93 hash_list = self._get_hash_list(self.test_accounts)94 for hash in hash_list:95 self.assertIn(hash, hash_dict['creds'].keys())96 self.assertIn(hash_dict['creds'][hash], self.test_accounts)97 def test_create_hash_file_previous_file(self):98 # Emulate the lock existing on the filesystem99 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))100 with mock.patch('six.moves.builtins.open', mock.mock_open(),101 create=True):102 test_account_class = accounts.Accounts('v2', 'test_name')103 res = test_account_class._create_hash_file('12345')104 self.assertFalse(res, "_create_hash_file should return False if the "105 "pseudo-lock file already exists")106 def test_create_hash_file_no_previous_file(self):107 # Emulate the lock not existing on the filesystem108 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))109 with mock.patch('six.moves.builtins.open', mock.mock_open(),110 create=True):111 test_account_class = accounts.Accounts('v2', 'test_name')112 res = test_account_class._create_hash_file('12345')113 self.assertTrue(res, "_create_hash_file should return True if the "114 "pseudo-lock doesn't already exist")115 @mock.patch('oslo_concurrency.lockutils.lock')116 def test_get_free_hash_no_previous_accounts(self, lock_mock):117 # Emulate no pre-existing lock118 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=False))119 hash_list = self._get_hash_list(self.test_accounts)120 mkdir_mock = self.useFixture(mockpatch.Patch('os.mkdir'))121 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))122 test_account_class = accounts.Accounts('v2', 'test_name')123 with mock.patch('six.moves.builtins.open', mock.mock_open(),124 create=True) as open_mock:125 test_account_class._get_free_hash(hash_list)126 lock_path = os.path.join(lockutils.get_lock_path(accounts.CONF),127 'test_accounts',128 hash_list[0])129 open_mock.assert_called_once_with(lock_path, 'w')130 mkdir_path = os.path.join(accounts.CONF.oslo_concurrency.lock_path,131 'test_accounts')132 mkdir_mock.mock.assert_called_once_with(mkdir_path)133 @mock.patch('oslo_concurrency.lockutils.lock')134 def test_get_free_hash_no_free_accounts(self, lock_mock):135 hash_list = self._get_hash_list(self.test_accounts)136 # Emulate pre-existing lock dir137 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))138 # Emulate all lcoks in list are in use139 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))140 test_account_class = accounts.Accounts('v2', 'test_name')141 with mock.patch('six.moves.builtins.open', mock.mock_open(),142 create=True):143 self.assertRaises(exceptions.InvalidConfiguration,144 test_account_class._get_free_hash, hash_list)145 @mock.patch('oslo_concurrency.lockutils.lock')146 def test_get_free_hash_some_in_use_accounts(self, lock_mock):147 # Emulate no pre-existing lock148 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))149 hash_list = self._get_hash_list(self.test_accounts)150 test_account_class = accounts.Accounts('v2', 'test_name')151 def _fake_is_file(path):152 # Fake isfile() to return that the path exists unless a specific153 # hash is in the path154 if hash_list[3] in path:155 return False156 return True157 self.stubs.Set(os.path, 'isfile', _fake_is_file)158 with mock.patch('six.moves.builtins.open', mock.mock_open(),159 create=True) as open_mock:160 test_account_class._get_free_hash(hash_list)161 lock_path = os.path.join(lockutils.get_lock_path(accounts.CONF),162 'test_accounts',163 hash_list[3])164 open_mock.assert_has_calls([mock.call(lock_path, 'w')])165 @mock.patch('oslo_concurrency.lockutils.lock')166 def test_remove_hash_last_account(self, lock_mock):167 hash_list = self._get_hash_list(self.test_accounts)168 # Pretend the pseudo-lock is there169 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))170 # Pretend the lock dir is empty171 self.useFixture(mockpatch.Patch('os.listdir', return_value=[]))172 test_account_class = accounts.Accounts('v2', 'test_name')173 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))174 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))175 test_account_class.remove_hash(hash_list[2])176 hash_path = os.path.join(lockutils.get_lock_path(accounts.CONF),177 'test_accounts',178 hash_list[2])179 lock_path = os.path.join(accounts.CONF.oslo_concurrency.lock_path,180 'test_accounts')181 remove_mock.mock.assert_called_once_with(hash_path)182 rmdir_mock.mock.assert_called_once_with(lock_path)183 @mock.patch('oslo_concurrency.lockutils.lock')184 def test_remove_hash_not_last_account(self, lock_mock):185 hash_list = self._get_hash_list(self.test_accounts)186 # Pretend the pseudo-lock is there187 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))188 # Pretend the lock dir is empty189 self.useFixture(mockpatch.Patch('os.listdir', return_value=[190 hash_list[1], hash_list[4]]))191 test_account_class = accounts.Accounts('v2', '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])195 hash_path = os.path.join(lockutils.get_lock_path(accounts.CONF),196 'test_accounts',197 hash_list[2])198 remove_mock.mock.assert_called_once_with(hash_path)199 rmdir_mock.mock.assert_not_called()200 def test_is_multi_user(self):201 test_accounts_class = accounts.Accounts('v2', 'test_name')202 self.assertTrue(test_accounts_class.is_multi_user())203 def test_is_not_multi_user(self):204 self.test_accounts = [self.test_accounts[0]]205 self.useFixture(mockpatch.Patch(206 'tempest.common.accounts.read_accounts_yaml',207 return_value=self.test_accounts))208 test_accounts_class = accounts.Accounts('v2', 'test_name')209 self.assertFalse(test_accounts_class.is_multi_user())210 def test__get_creds_by_roles_one_role(self):211 self.useFixture(mockpatch.Patch(212 'tempest.common.accounts.read_accounts_yaml',213 return_value=self.test_accounts))214 test_accounts_class = accounts.Accounts('v2', 'test_name')215 hashes = test_accounts_class.hash_dict['roles']['role4']216 temp_hash = hashes[0]217 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(218 test_accounts_class, '_get_free_hash', return_value=temp_hash))219 # Test a single role returns all matching roles220 test_accounts_class._get_creds(roles=['role4'])221 calls = get_free_hash_mock.mock.mock_calls222 self.assertEqual(len(calls), 1)223 args = calls[0][1][0]224 for i in hashes:225 self.assertIn(i, args)226 def test__get_creds_by_roles_list_role(self):227 self.useFixture(mockpatch.Patch(228 'tempest.common.accounts.read_accounts_yaml',229 return_value=self.test_accounts))230 test_accounts_class = accounts.Accounts('v2', 'test_name')231 hashes = test_accounts_class.hash_dict['roles']['role4']232 hashes2 = test_accounts_class.hash_dict['roles']['role2']233 hashes = list(set(hashes) & set(hashes2))234 temp_hash = hashes[0]235 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(236 test_accounts_class, '_get_free_hash', return_value=temp_hash))237 # Test an intersection of multiple roles238 test_accounts_class._get_creds(roles=['role2', 'role4'])239 calls = get_free_hash_mock.mock.mock_calls240 self.assertEqual(len(calls), 1)241 args = calls[0][1][0]242 for i in hashes:243 self.assertIn(i, args)244 def test__get_creds_by_roles_no_admin(self):245 self.useFixture(mockpatch.Patch(246 'tempest.common.accounts.read_accounts_yaml',247 return_value=self.test_accounts))248 test_accounts_class = accounts.Accounts('v2', 'test_name')249 hashes = list(test_accounts_class.hash_dict['creds'].keys())250 admin_hashes = test_accounts_class.hash_dict['roles'][251 cfg.CONF.identity.admin_role]252 temp_hash = hashes[0]253 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(254 test_accounts_class, '_get_free_hash', return_value=temp_hash))255 # Test an intersection of multiple roles256 test_accounts_class._get_creds()257 calls = get_free_hash_mock.mock.mock_calls258 self.assertEqual(len(calls), 1)259 args = calls[0][1][0]260 self.assertEqual(len(args), 10)261 for i in admin_hashes:262 self.assertNotIn(i, args)263 def test_networks_returned_with_creds(self):264 test_accounts = [265 {'username': 'test_user13', 'tenant_name': 'test_tenant13',266 'password': 'p', 'resources': {'network': 'network-1'}},267 {'username': 'test_user14', 'tenant_name': 'test_tenant14',268 'password': 'p', 'roles': ['role-7', 'role-11'],269 'resources': {'network': 'network-2'}}]270 self.useFixture(mockpatch.Patch(271 'tempest.common.accounts.read_accounts_yaml',272 return_value=test_accounts))273 test_accounts_class = accounts.Accounts('v2', 'test_name')274 with mock.patch('tempest.services.compute.json.networks_client.'275 'NetworksClient.list_networks',276 return_value=[{'name': 'network-2', 'id': 'fake-id',277 'label': 'network-2'}]):278 creds = test_accounts_class.get_creds_by_roles(['role-7'])279 self.assertTrue(isinstance(creds, cred_provider.TestResources))280 network = creds.network281 self.assertIsNotNone(network)282 self.assertIn('name', network)283 self.assertIn('id', network)284 self.assertEqual('fake-id', network['id'])285 self.assertEqual('network-2', network['name'])286class TestNotLockingAccount(base.TestCase):287 def setUp(self):288 super(TestNotLockingAccount, self).setUp()289 self.useFixture(fake_config.ConfigFixture())290 self.stubs.Set(config, 'TempestConfigPrivate', fake_config.FakePrivate)291 self.useFixture(lockutils_fixtures.ExternalLockFixture())292 self.test_accounts = [293 {'username': 'test_user1', 'tenant_name': 'test_tenant1',294 'password': 'p'},295 {'username': 'test_user2', 'tenant_name': 'test_tenant2',296 'password': 'p'},297 {'username': 'test_user3', 'tenant_name': 'test_tenant3',298 'password': 'p'},299 ]300 self.useFixture(mockpatch.Patch(301 'tempest.common.accounts.read_accounts_yaml',302 return_value=self.test_accounts))303 cfg.CONF.set_default('test_accounts_file', '', group='auth')304 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))305 def test_get_creds_roles_nonlocking_invalid(self):306 test_accounts_class = accounts.NotLockingAccounts('v2', 'test_name')307 self.assertRaises(exceptions.InvalidConfiguration,308 test_accounts_class.get_creds_by_roles,...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!