How to use _fake_accounts method in tempest

Best Python code snippet using tempest_python

test_preprov_creds.py

Source:test_preprov_creds.py Github

copy

Full Screen

...39 identity_response = fake_identity._fake_v2_response40 token_client = ('tempest.lib.services.identity.v2.token_client'41 '.TokenClient.raw_request')42 @classmethod43 def _fake_accounts(cls, admin_role):44 return [45 {'username': 'test_user1', 'tenant_name': 'test_tenant1',46 'password': 'p'},47 {'username': 'test_user2', 'project_name': 'test_tenant2',48 'password': 'p'},49 {'username': 'test_user3', 'tenant_name': 'test_tenant3',50 'password': 'p'},51 {'username': 'test_user4', 'project_name': 'test_tenant4',52 'password': 'p'},53 {'username': 'test_user5', 'tenant_name': 'test_tenant5',54 'password': 'p'},55 {'username': 'test_user6', 'project_name': 'test_tenant6',56 'password': 'p', 'roles': ['role1', 'role2']},57 {'username': 'test_user7', 'tenant_name': 'test_tenant7',58 'password': 'p', 'roles': ['role2', 'role3']},59 {'username': 'test_user8', 'project_name': 'test_tenant8',60 'password': 'p', 'roles': ['role4', 'role1']},61 {'username': 'test_user9', 'tenant_name': 'test_tenant9',62 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},63 {'username': 'test_user10', 'project_name': 'test_tenant10',64 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},65 {'username': 'test_admin1', 'tenant_name': 'test_tenant11',66 'password': 'p', 'roles': [admin_role]},67 {'username': 'test_admin2', 'project_name': 'test_tenant12',68 'password': 'p', 'roles': [admin_role]},69 {'username': 'test_admin3', 'project_name': 'test_tenant13',70 'password': 'p', 'types': ['admin']}]71 def setUp(self):72 super(TestPreProvisionedCredentials, self).setUp()73 self.useFixture(fake_config.ConfigFixture())74 self.patchobject(config, 'TempestConfigPrivate',75 fake_config.FakePrivate)76 self.patch(self.token_client, side_effect=self.identity_response)77 self.useFixture(lockutils_fixtures.ExternalLockFixture())78 self.test_accounts = self._fake_accounts(cfg.CONF.identity.admin_role)79 self.accounts_mock = self.useFixture(mockpatch.Patch(80 'tempest.common.preprov_creds.read_accounts_yaml',81 return_value=self.test_accounts))82 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))83 def tearDown(self):84 super(TestPreProvisionedCredentials, self).tearDown()85 shutil.rmtree(self.fixed_params['accounts_lock_dir'],86 ignore_errors=True)87 def _get_hash_list(self, accounts_list):88 hash_list = []89 hash_fields = (90 preprov_creds.PreProvisionedCredentialProvider.HASH_CRED_FIELDS)91 for account in accounts_list:92 hash = hashlib.md5()93 account_for_hash = dict((k, v) for (k, v) in account.items()94 if k in hash_fields)95 hash.update(six.text_type(account_for_hash).encode('utf-8'))96 temp_hash = hash.hexdigest()97 hash_list.append(temp_hash)98 return hash_list99 def test_get_hash(self):100 # Test with all accounts to make sure we try all combinations101 # and hide no race conditions102 hash_index = 0103 for test_cred_dict in self.test_accounts:104 test_account_class = (105 preprov_creds.PreProvisionedCredentialProvider(106 **self.fixed_params))107 hash_list = self._get_hash_list(self.test_accounts)108 test_creds = auth.get_credentials(109 fake_identity.FAKE_AUTH_URL,110 identity_version=self.fixed_params['identity_version'],111 **test_cred_dict)112 results = test_account_class.get_hash(test_creds)113 self.assertEqual(hash_list[hash_index], results)114 hash_index += 1115 def test_get_hash_dict(self):116 test_account_class = preprov_creds.PreProvisionedCredentialProvider(117 **self.fixed_params)118 hash_dict = test_account_class.get_hash_dict(119 self.test_accounts, self.fixed_params['admin_role'])120 hash_list = self._get_hash_list(self.test_accounts)121 for hash in hash_list:122 self.assertIn(hash, hash_dict['creds'].keys())123 self.assertIn(hash_dict['creds'][hash], self.test_accounts)124 def test_create_hash_file_previous_file(self):125 # Emulate the lock existing on the filesystem126 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))127 with mock.patch('six.moves.builtins.open', mock.mock_open(),128 create=True):129 test_account_class = (130 preprov_creds.PreProvisionedCredentialProvider(131 **self.fixed_params))132 res = test_account_class._create_hash_file('12345')133 self.assertFalse(res, "_create_hash_file should return False if the "134 "pseudo-lock file already exists")135 def test_create_hash_file_no_previous_file(self):136 # Emulate the lock not existing on the filesystem137 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))138 with mock.patch('six.moves.builtins.open', mock.mock_open(),139 create=True):140 test_account_class = (141 preprov_creds.PreProvisionedCredentialProvider(142 **self.fixed_params))143 res = test_account_class._create_hash_file('12345')144 self.assertTrue(res, "_create_hash_file should return True if the "145 "pseudo-lock doesn't already exist")146 @mock.patch('oslo_concurrency.lockutils.lock')147 def test_get_free_hash_no_previous_accounts(self, lock_mock):148 # Emulate no pre-existing lock149 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=False))150 hash_list = self._get_hash_list(self.test_accounts)151 mkdir_mock = self.useFixture(mockpatch.Patch('os.mkdir'))152 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=False))153 test_account_class = preprov_creds.PreProvisionedCredentialProvider(154 **self.fixed_params)155 with mock.patch('six.moves.builtins.open', mock.mock_open(),156 create=True) as open_mock:157 test_account_class._get_free_hash(hash_list)158 lock_path = os.path.join(self.fixed_params['accounts_lock_dir'],159 hash_list[0])160 open_mock.assert_called_once_with(lock_path, 'w')161 mkdir_path = os.path.join(self.fixed_params['accounts_lock_dir'])162 mkdir_mock.mock.assert_called_once_with(mkdir_path)163 @mock.patch('oslo_concurrency.lockutils.lock')164 def test_get_free_hash_no_free_accounts(self, lock_mock):165 hash_list = self._get_hash_list(self.test_accounts)166 # Emulate pre-existing lock dir167 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))168 # Emulate all lcoks in list are in use169 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))170 test_account_class = preprov_creds.PreProvisionedCredentialProvider(171 **self.fixed_params)172 with mock.patch('six.moves.builtins.open', mock.mock_open(),173 create=True):174 self.assertRaises(lib_exc.InvalidCredentials,175 test_account_class._get_free_hash, hash_list)176 @mock.patch('oslo_concurrency.lockutils.lock')177 def test_get_free_hash_some_in_use_accounts(self, lock_mock):178 # Emulate no pre-existing lock179 self.useFixture(mockpatch.Patch('os.path.isdir', return_value=True))180 hash_list = self._get_hash_list(self.test_accounts)181 test_account_class = preprov_creds.PreProvisionedCredentialProvider(182 **self.fixed_params)183 def _fake_is_file(path):184 # Fake isfile() to return that the path exists unless a specific185 # hash is in the path186 if hash_list[3] in path:187 return False188 return True189 self.patchobject(os.path, 'isfile', _fake_is_file)190 with mock.patch('six.moves.builtins.open', mock.mock_open(),191 create=True) as open_mock:192 test_account_class._get_free_hash(hash_list)193 lock_path = os.path.join(self.fixed_params['accounts_lock_dir'],194 hash_list[3])195 open_mock.assert_has_calls([mock.call(lock_path, 'w')])196 @mock.patch('oslo_concurrency.lockutils.lock')197 def test_remove_hash_last_account(self, lock_mock):198 hash_list = self._get_hash_list(self.test_accounts)199 # Pretend the pseudo-lock is there200 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))201 # Pretend the lock dir is empty202 self.useFixture(mockpatch.Patch('os.listdir', return_value=[]))203 test_account_class = preprov_creds.PreProvisionedCredentialProvider(204 **self.fixed_params)205 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))206 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))207 test_account_class.remove_hash(hash_list[2])208 hash_path = os.path.join(self.fixed_params['accounts_lock_dir'],209 hash_list[2])210 lock_path = self.fixed_params['accounts_lock_dir']211 remove_mock.mock.assert_called_once_with(hash_path)212 rmdir_mock.mock.assert_called_once_with(lock_path)213 @mock.patch('oslo_concurrency.lockutils.lock')214 def test_remove_hash_not_last_account(self, lock_mock):215 hash_list = self._get_hash_list(self.test_accounts)216 # Pretend the pseudo-lock is there217 self.useFixture(mockpatch.Patch('os.path.isfile', return_value=True))218 # Pretend the lock dir is empty219 self.useFixture(mockpatch.Patch('os.listdir', return_value=[220 hash_list[1], hash_list[4]]))221 test_account_class = preprov_creds.PreProvisionedCredentialProvider(222 **self.fixed_params)223 remove_mock = self.useFixture(mockpatch.Patch('os.remove'))224 rmdir_mock = self.useFixture(mockpatch.Patch('os.rmdir'))225 test_account_class.remove_hash(hash_list[2])226 hash_path = os.path.join(self.fixed_params['accounts_lock_dir'],227 hash_list[2])228 remove_mock.mock.assert_called_once_with(hash_path)229 rmdir_mock.mock.assert_not_called()230 def test_is_multi_user(self):231 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(232 **self.fixed_params)233 self.assertTrue(test_accounts_class.is_multi_user())234 def test_is_not_multi_user(self):235 self.test_accounts = [self.test_accounts[0]]236 self.useFixture(mockpatch.Patch(237 'tempest.common.preprov_creds.read_accounts_yaml',238 return_value=self.test_accounts))239 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(240 **self.fixed_params)241 self.assertFalse(test_accounts_class.is_multi_user())242 def test__get_creds_by_roles_one_role(self):243 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(244 **self.fixed_params)245 hashes = test_accounts_class.hash_dict['roles']['role4']246 temp_hash = hashes[0]247 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(248 test_accounts_class, '_get_free_hash', return_value=temp_hash))249 # Test a single role returns all matching roles250 test_accounts_class._get_creds(roles=['role4'])251 calls = get_free_hash_mock.mock.mock_calls252 self.assertEqual(len(calls), 1)253 args = calls[0][1][0]254 for i in hashes:255 self.assertIn(i, args)256 def test__get_creds_by_roles_list_role(self):257 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(258 **self.fixed_params)259 hashes = test_accounts_class.hash_dict['roles']['role4']260 hashes2 = test_accounts_class.hash_dict['roles']['role2']261 hashes = list(set(hashes) & set(hashes2))262 temp_hash = hashes[0]263 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(264 test_accounts_class, '_get_free_hash', return_value=temp_hash))265 # Test an intersection of multiple roles266 test_accounts_class._get_creds(roles=['role2', 'role4'])267 calls = get_free_hash_mock.mock.mock_calls268 self.assertEqual(len(calls), 1)269 args = calls[0][1][0]270 for i in hashes:271 self.assertIn(i, args)272 def test__get_creds_by_roles_no_admin(self):273 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(274 **self.fixed_params)275 hashes = list(test_accounts_class.hash_dict['creds'].keys())276 admin_hashes = test_accounts_class.hash_dict['roles'][277 cfg.CONF.identity.admin_role]278 temp_hash = hashes[0]279 get_free_hash_mock = self.useFixture(mockpatch.PatchObject(280 test_accounts_class, '_get_free_hash', return_value=temp_hash))281 # Test an intersection of multiple roles282 test_accounts_class._get_creds()283 calls = get_free_hash_mock.mock.mock_calls284 self.assertEqual(len(calls), 1)285 args = calls[0][1][0]286 self.assertEqual(len(args), 10)287 for i in admin_hashes:288 self.assertNotIn(i, args)289 def test_networks_returned_with_creds(self):290 test_accounts = [291 {'username': 'test_user13', 'tenant_name': 'test_tenant13',292 'password': 'p', 'resources': {'network': 'network-1'}},293 {'username': 'test_user14', 'tenant_name': 'test_tenant14',294 'password': 'p', 'roles': ['role-7', 'role-11'],295 'resources': {'network': 'network-2'}}]296 self.useFixture(mockpatch.Patch(297 'tempest.common.preprov_creds.read_accounts_yaml',298 return_value=test_accounts))299 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(300 **self.fixed_params)301 with mock.patch('tempest.lib.services.compute.networks_client.'302 'NetworksClient.list_networks',303 return_value={'networks': [{'name': 'network-2',304 'id': 'fake-id',305 'label': 'network-2'}]}):306 creds = test_accounts_class.get_creds_by_roles(['role-7'])307 self.assertIsInstance(creds, cred_provider.TestResources)308 network = creds.network309 self.assertIsNotNone(network)310 self.assertIn('name', network)311 self.assertIn('id', network)312 self.assertEqual('fake-id', network['id'])313 self.assertEqual('network-2', network['name'])314 def test_get_primary_creds(self):315 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(316 **self.fixed_params)317 primary_creds = test_accounts_class.get_primary_creds()318 self.assertNotIn('test_admin', primary_creds.username)319 def test_get_primary_creds_none_available(self):320 admin_accounts = [x for x in self.test_accounts if 'test_admin'321 in x['username']]322 self.useFixture(mockpatch.Patch(323 'tempest.common.preprov_creds.read_accounts_yaml',324 return_value=admin_accounts))325 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(326 **self.fixed_params)327 with testtools.ExpectedException(lib_exc.InvalidCredentials):328 # Get one more329 test_accounts_class.get_primary_creds()330 def test_get_alt_creds(self):331 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(332 **self.fixed_params)333 alt_creds = test_accounts_class.get_alt_creds()334 self.assertNotIn('test_admin', alt_creds.username)335 def test_get_alt_creds_none_available(self):336 admin_accounts = [x for x in self.test_accounts if 'test_admin'337 in x['username']]338 self.useFixture(mockpatch.Patch(339 'tempest.common.preprov_creds.read_accounts_yaml',340 return_value=admin_accounts))341 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(342 **self.fixed_params)343 with testtools.ExpectedException(lib_exc.InvalidCredentials):344 # Get one more345 test_accounts_class.get_alt_creds()346 def test_get_admin_creds(self):347 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(348 **self.fixed_params)349 admin_creds = test_accounts_class.get_admin_creds()350 self.assertIn('test_admin', admin_creds.username)351 def test_get_admin_creds_by_type(self):352 test_accounts = [353 {'username': 'test_user10', 'project_name': 'test_tenant10',354 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},355 {'username': 'test_admin1', 'tenant_name': 'test_tenant11',356 'password': 'p', 'types': ['admin']}]357 self.useFixture(mockpatch.Patch(358 'tempest.common.preprov_creds.read_accounts_yaml',359 return_value=test_accounts))360 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(361 **self.fixed_params)362 admin_creds = test_accounts_class.get_admin_creds()363 self.assertIn('test_admin', admin_creds.username)364 def test_get_admin_creds_by_role(self):365 test_accounts = [366 {'username': 'test_user10', 'project_name': 'test_tenant10',367 'password': 'p', 'roles': ['role1', 'role2', 'role3', 'role4']},368 {'username': 'test_admin1', 'tenant_name': 'test_tenant11',369 'password': 'p', 'roles': [cfg.CONF.identity.admin_role]}]370 self.useFixture(mockpatch.Patch(371 'tempest.common.preprov_creds.read_accounts_yaml',372 return_value=test_accounts))373 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(374 **self.fixed_params)375 admin_creds = test_accounts_class.get_admin_creds()376 self.assertIn('test_admin', admin_creds.username)377 def test_get_admin_creds_none_available(self):378 non_admin_accounts = [x for x in self.test_accounts if 'test_admin'379 not in x['username']]380 self.useFixture(mockpatch.Patch(381 'tempest.common.preprov_creds.read_accounts_yaml',382 return_value=non_admin_accounts))383 test_accounts_class = preprov_creds.PreProvisionedCredentialProvider(384 **self.fixed_params)385 with testtools.ExpectedException(lib_exc.InvalidCredentials):386 # Get one more387 test_accounts_class.get_admin_creds()388class TestPreProvisionedCredentialsV3(TestPreProvisionedCredentials):389 fixed_params = {'name': 'test class',390 'identity_version': 'v3',391 'test_accounts_file': 'fake_accounts_file',392 'accounts_lock_dir': 'fake_locks_dir_v3',393 'admin_role': 'admin',394 'object_storage_operator_role': 'operator',395 'object_storage_reseller_admin_role': 'reseller'}396 identity_response = fake_identity._fake_v3_response397 token_client = ('tempest.lib.services.identity.v3.token_client'398 '.V3TokenClient.raw_request')399 @classmethod400 def _fake_accounts(cls, admin_role):401 return [402 {'username': 'test_user1', 'project_name': 'test_project1',403 'domain_name': 'domain', 'password': 'p'},404 {'username': 'test_user2', 'project_name': 'test_project2',405 'domain_name': 'domain', 'password': 'p'},406 {'username': 'test_user3', 'project_name': 'test_project3',407 'domain_name': 'domain', 'password': 'p'},408 {'username': 'test_user4', 'project_name': 'test_project4',409 'domain_name': 'domain', 'password': 'p'},410 {'username': 'test_user5', 'project_name': 'test_project5',411 'domain_name': 'domain', 'password': 'p'},412 {'username': 'test_user6', 'project_name': 'test_project6',413 'domain_name': 'domain', 'password': 'p',414 'roles': ['role1', 'role2']},...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tempest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful