Best Python code snippet using tempest_python
test_dynamic_creds.py
Source:test_dynamic_creds.py
...59 self.patchobject(self.token_client_class, 'raw_request',60 self.fake_response)61 cfg.CONF.set_default('operator_role', 'FakeRole',62 group='object-storage')63 self._mock_list_ec2_credentials('fake_user_id', 'fake_tenant_id')64 self.fixed_params.update(65 admin_creds=self._get_fake_admin_creds())66 def test_tempest_client(self):67 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)68 self.assertIsInstance(creds.identity_admin_client,69 self.iden_client.IdentityClient)70 def _get_fake_admin_creds(self):71 return credentials.get_credentials(72 fill_in=False,73 identity_version=self.fixed_params['identity_version'],74 username='fake_username', password='fake_password',75 tenant_name='fake_tenant')76 def _mock_user_create(self, id, name):77 user_fix = self.useFixture(mockpatch.PatchObject(78 self.users_client.UsersClient,79 'create_user',80 return_value=(rest_client.ResponseBody81 (200, {'user': {'id': id, 'name': name}}))))82 return user_fix83 def _mock_tenant_create(self, id, name):84 tenant_fix = self.useFixture(mockpatch.PatchObject(85 self.tenants_client.TenantsClient,86 'create_tenant',87 return_value=(rest_client.ResponseBody88 (200, {'tenant': {'id': id, 'name': name}}))))89 return tenant_fix90 def _mock_list_roles(self, id, name):91 roles_fix = self.useFixture(mockpatch.PatchObject(92 self.roles_client.RolesClient,93 'list_roles',94 return_value=(rest_client.ResponseBody95 (200,96 {'roles': [{'id': id, 'name': name},97 {'id': '1', 'name': 'FakeRole'},98 {'id': '2', 'name': 'Member'}]}))))99 return roles_fix100 def _mock_list_2_roles(self):101 roles_fix = self.useFixture(mockpatch.PatchObject(102 self.roles_client.RolesClient,103 'list_roles',104 return_value=(rest_client.ResponseBody105 (200,106 {'roles': [{'id': '1234', 'name': 'role1'},107 {'id': '1', 'name': 'FakeRole'},108 {'id': '12345', 'name': 'role2'}]}))))109 return roles_fix110 def _mock_assign_user_role(self):111 tenant_fix = self.useFixture(mockpatch.PatchObject(112 self.roles_client.RolesClient,113 self.assign_role_on_project,114 return_value=(rest_client.ResponseBody115 (200, {}))))116 return tenant_fix117 def _mock_list_role(self):118 roles_fix = self.useFixture(mockpatch.PatchObject(119 self.roles_client.RolesClient,120 'list_roles',121 return_value=(rest_client.ResponseBody122 (200, {'roles': [123 {'id': '1', 'name': 'FakeRole'},124 {'id': '2', 'name': 'Member'}]}))))125 return roles_fix126 def _mock_list_ec2_credentials(self, user_id, tenant_id):127 ec2_creds_fix = self.useFixture(mockpatch.PatchObject(128 self.users_client.UsersClient,129 'list_user_ec2_credentials',130 return_value=(rest_client.ResponseBody131 (200, {'credentials': [{132 'access': 'fake_access',133 'secret': 'fake_secret',134 'tenant_id': tenant_id,135 'user_id': user_id,136 'trust_id': None}]}))))137 return ec2_creds_fix138 def _mock_network_create(self, iso_creds, id, name):139 net_fix = self.useFixture(mockpatch.PatchObject(140 iso_creds.networks_admin_client,141 'create_network',142 return_value={'network': {'id': id, 'name': name}}))143 return net_fix144 def _mock_subnet_create(self, iso_creds, id, name):145 subnet_fix = self.useFixture(mockpatch.PatchObject(146 iso_creds.subnets_admin_client,147 'create_subnet',148 return_value={'subnet': {'id': id, 'name': name}}))149 return subnet_fix150 def _mock_router_create(self, id, name):151 router_fix = self.useFixture(mockpatch.PatchObject(152 routers_client.RoutersClient,153 'create_router',154 return_value={'router': {'id': id, 'name': name}}))155 return router_fix156 @mock.patch('tempest.lib.common.rest_client.RestClient')157 def test_primary_creds(self, MockRestClient):158 cfg.CONF.set_default('neutron', False, 'service_available')159 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)160 self._mock_assign_user_role()161 self._mock_list_role()162 self._mock_tenant_create('1234', 'fake_prim_tenant')163 self._mock_user_create('1234', 'fake_prim_user')164 primary_creds = creds.get_primary_creds()165 self.assertEqual(primary_creds.username, 'fake_prim_user')166 self.assertEqual(primary_creds.tenant_name, 'fake_prim_tenant')167 # Verify IDs168 self.assertEqual(primary_creds.tenant_id, '1234')169 self.assertEqual(primary_creds.user_id, '1234')170 @mock.patch('tempest.lib.common.rest_client.RestClient')171 def test_admin_creds(self, MockRestClient):172 cfg.CONF.set_default('neutron', False, 'service_available')173 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)174 self._mock_list_roles('1234', 'admin')175 self._mock_user_create('1234', 'fake_admin_user')176 self._mock_tenant_create('1234', 'fake_admin_tenant')177 user_mock = mock.patch.object(self.roles_client.RolesClient,178 self.assign_role_on_project)179 user_mock.start()180 self.addCleanup(user_mock.stop)181 with mock.patch.object(self.roles_client.RolesClient,182 self.assign_role_on_project) as user_mock:183 admin_creds = creds.get_admin_creds()184 user_mock.assert_has_calls([185 mock.call('1234', '1234', '1234')])186 self.assertEqual(admin_creds.username, 'fake_admin_user')187 self.assertEqual(admin_creds.tenant_name, 'fake_admin_tenant')188 # Verify IDs189 self.assertEqual(admin_creds.tenant_id, '1234')190 self.assertEqual(admin_creds.user_id, '1234')191 @mock.patch('tempest.lib.common.rest_client.RestClient')192 def test_role_creds(self, MockRestClient):193 cfg.CONF.set_default('neutron', False, 'service_available')194 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)195 self._mock_list_2_roles()196 self._mock_user_create('1234', 'fake_role_user')197 self._mock_tenant_create('1234', 'fake_role_tenant')198 user_mock = mock.patch.object(self.roles_client.RolesClient,199 self.assign_role_on_project)200 user_mock.start()201 self.addCleanup(user_mock.stop)202 with mock.patch.object(self.roles_client.RolesClient,203 self.assign_role_on_project) as user_mock:204 role_creds = creds.get_creds_by_roles(205 roles=['role1', 'role2'])206 calls = user_mock.mock_calls207 # Assert that the role creation is called with the 2 specified roles208 self.assertEqual(len(calls), 2)209 args = map(lambda x: x[1], calls)210 args = list(args)211 self.assertIn(('1234', '1234', '1234'), args)212 self.assertIn(('1234', '1234', '12345'), args)213 self.assertEqual(role_creds.username, 'fake_role_user')214 self.assertEqual(role_creds.tenant_name, 'fake_role_tenant')215 # Verify IDs216 self.assertEqual(role_creds.tenant_id, '1234')217 self.assertEqual(role_creds.user_id, '1234')218 @mock.patch('tempest.lib.common.rest_client.RestClient')219 def test_all_cred_cleanup(self, MockRestClient):220 cfg.CONF.set_default('neutron', False, 'service_available')221 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)222 self._mock_assign_user_role()223 self._mock_list_role()224 self._mock_tenant_create('1234', 'fake_prim_tenant')225 self._mock_user_create('1234', 'fake_prim_user')226 creds.get_primary_creds()227 self._mock_tenant_create('12345', 'fake_alt_tenant')228 self._mock_user_create('12345', 'fake_alt_user')229 creds.get_alt_creds()230 self._mock_tenant_create('123456', 'fake_admin_tenant')231 self._mock_user_create('123456', 'fake_admin_user')232 self._mock_list_roles('123456', 'admin')233 creds.get_admin_creds()234 user_mock = self.patchobject(self.users_client.UsersClient,235 'delete_user')236 tenant_mock = self.patchobject(self.tenants_client_class,237 self.delete_tenant)238 creds.clear_creds()239 # Verify user delete calls240 calls = user_mock.mock_calls241 self.assertEqual(len(calls), 3)242 args = map(lambda x: x[1][0], calls)243 args = list(args)244 self.assertIn('1234', args)245 self.assertIn('12345', args)246 self.assertIn('123456', args)247 # Verify tenant delete calls248 calls = tenant_mock.mock_calls249 self.assertEqual(len(calls), 3)250 args = map(lambda x: x[1][0], calls)251 args = list(args)252 self.assertIn('1234', args)253 self.assertIn('12345', args)254 self.assertIn('123456', args)255 @mock.patch('tempest.lib.common.rest_client.RestClient')256 def test_alt_creds(self, MockRestClient):257 cfg.CONF.set_default('neutron', False, 'service_available')258 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)259 self._mock_assign_user_role()260 self._mock_list_role()261 self._mock_user_create('1234', 'fake_alt_user')262 self._mock_tenant_create('1234', 'fake_alt_tenant')263 alt_creds = creds.get_alt_creds()264 self.assertEqual(alt_creds.username, 'fake_alt_user')265 self.assertEqual(alt_creds.tenant_name, 'fake_alt_tenant')266 # Verify IDs267 self.assertEqual(alt_creds.tenant_id, '1234')268 self.assertEqual(alt_creds.user_id, '1234')269 @mock.patch('tempest.lib.common.rest_client.RestClient')270 def test_no_network_creation_with_config_set(self, MockRestClient):271 cfg.CONF.set_default('create_isolated_networks', False, group='auth')272 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)273 self._mock_assign_user_role()274 self._mock_list_role()275 self._mock_user_create('1234', 'fake_prim_user')276 self._mock_tenant_create('1234', 'fake_prim_tenant')277 net = mock.patch.object(creds.networks_admin_client,278 'delete_network')279 net_mock = net.start()280 subnet = mock.patch.object(creds.subnets_admin_client,281 'delete_subnet')282 subnet_mock = subnet.start()283 router = mock.patch.object(creds.routers_admin_client,284 'delete_router')285 router_mock = router.start()286 primary_creds = creds.get_primary_creds()287 self.assertEqual(net_mock.mock_calls, [])288 self.assertEqual(subnet_mock.mock_calls, [])289 self.assertEqual(router_mock.mock_calls, [])290 network = primary_creds.network291 subnet = primary_creds.subnet292 router = primary_creds.router293 self.assertIsNone(network)294 self.assertIsNone(subnet)295 self.assertIsNone(router)296 @mock.patch('tempest.lib.common.rest_client.RestClient')297 def test_network_creation(self, MockRestClient):298 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)299 self._mock_assign_user_role()300 self._mock_list_role()301 self._mock_user_create('1234', 'fake_prim_user')302 self._mock_tenant_create('1234', 'fake_prim_tenant')303 self._mock_network_create(creds, '1234', 'fake_net')304 self._mock_subnet_create(creds, '1234', 'fake_subnet')305 self._mock_router_create('1234', 'fake_router')306 router_interface_mock = self.patch(307 'tempest.lib.services.network.routers_client.RoutersClient.'308 'add_router_interface')309 primary_creds = creds.get_primary_creds()310 router_interface_mock.assert_called_once_with('1234', subnet_id='1234')311 network = primary_creds.network312 subnet = primary_creds.subnet313 router = primary_creds.router314 self.assertEqual(network['id'], '1234')315 self.assertEqual(network['name'], 'fake_net')316 self.assertEqual(subnet['id'], '1234')317 self.assertEqual(subnet['name'], 'fake_subnet')318 self.assertEqual(router['id'], '1234')319 self.assertEqual(router['name'], 'fake_router')320 @mock.patch('tempest.lib.common.rest_client.RestClient')321 def test_network_cleanup(self, MockRestClient):322 def side_effect(**args):323 return {"security_groups": [{"tenant_id": args['tenant_id'],324 "name": args['name'],325 "description": args['name'],326 "security_group_rules": [],327 "id": "sg-%s" % args['tenant_id']}]}328 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)329 # Create primary tenant and network330 self._mock_assign_user_role()331 self._mock_list_role()332 self._mock_user_create('1234', 'fake_prim_user')333 self._mock_tenant_create('1234', 'fake_prim_tenant')334 self._mock_network_create(creds, '1234', 'fake_net')335 self._mock_subnet_create(creds, '1234', 'fake_subnet')336 self._mock_router_create('1234', 'fake_router')337 router_interface_mock = self.patch(338 'tempest.lib.services.network.routers_client.RoutersClient.'339 'add_router_interface')340 creds.get_primary_creds()341 router_interface_mock.assert_called_once_with('1234', subnet_id='1234')342 router_interface_mock.reset_mock()343 # Create alternate tenant and network344 self._mock_user_create('12345', 'fake_alt_user')345 self._mock_tenant_create('12345', 'fake_alt_tenant')346 self._mock_network_create(creds, '12345', 'fake_alt_net')347 self._mock_subnet_create(creds, '12345', 'fake_alt_subnet')348 self._mock_router_create('12345', 'fake_alt_router')349 creds.get_alt_creds()350 router_interface_mock.assert_called_once_with('12345',351 subnet_id='12345')352 router_interface_mock.reset_mock()353 # Create admin tenant and networks354 self._mock_user_create('123456', 'fake_admin_user')355 self._mock_tenant_create('123456', 'fake_admin_tenant')356 self._mock_network_create(creds, '123456', 'fake_admin_net')357 self._mock_subnet_create(creds, '123456', 'fake_admin_subnet')358 self._mock_router_create('123456', 'fake_admin_router')359 self._mock_list_roles('123456', 'admin')360 creds.get_admin_creds()361 self.patchobject(self.users_client.UsersClient, 'delete_user')362 self.patchobject(self.tenants_client_class, self.delete_tenant)363 net = mock.patch.object(creds.networks_admin_client, 'delete_network')364 net_mock = net.start()365 subnet = mock.patch.object(creds.subnets_admin_client, 'delete_subnet')366 subnet_mock = subnet.start()367 router = mock.patch.object(creds.routers_admin_client, 'delete_router')368 router_mock = router.start()369 remove_router_interface_mock = self.patch(370 'tempest.lib.services.network.routers_client.RoutersClient.'371 'remove_router_interface')372 return_values = ({'status': 200}, {'ports': []})373 port_list_mock = mock.patch.object(creds.ports_admin_client,374 'list_ports',375 return_value=return_values)376 port_list_mock.start()377 secgroup_list_mock = mock.patch.object(378 creds.security_groups_admin_client,379 'list_security_groups',380 side_effect=side_effect)381 secgroup_list_mock.start()382 return_values = fake_http.fake_http_response({}, status=204), ''383 remove_secgroup_mock = self.patch(384 'tempest.lib.services.network.security_groups_client.'385 'SecurityGroupsClient.delete', return_value=return_values)386 creds.clear_creds()387 # Verify default security group delete388 calls = remove_secgroup_mock.mock_calls389 self.assertEqual(len(calls), 3)390 args = map(lambda x: x[1][0], calls)391 args = list(args)392 self.assertIn('v2.0/security-groups/sg-1234', args)393 self.assertIn('v2.0/security-groups/sg-12345', args)394 self.assertIn('v2.0/security-groups/sg-123456', args)395 # Verify remove router interface calls396 calls = remove_router_interface_mock.mock_calls397 self.assertEqual(len(calls), 3)398 args = map(lambda x: (x[1][0], x[2]), calls)399 args = list(args)400 self.assertIn(('1234', {'subnet_id': '1234'}), args)401 self.assertIn(('12345', {'subnet_id': '12345'}), args)402 self.assertIn(('123456', {'subnet_id': '123456'}), args)403 # Verify network delete calls404 calls = net_mock.mock_calls405 self.assertEqual(len(calls), 3)406 args = map(lambda x: x[1][0], calls)407 args = list(args)408 self.assertIn('1234', args)409 self.assertIn('12345', args)410 self.assertIn('123456', args)411 # Verify subnet delete calls412 calls = subnet_mock.mock_calls413 self.assertEqual(len(calls), 3)414 args = map(lambda x: x[1][0], calls)415 args = list(args)416 self.assertIn('1234', args)417 self.assertIn('12345', args)418 self.assertIn('123456', args)419 # Verify router delete calls420 calls = router_mock.mock_calls421 self.assertEqual(len(calls), 3)422 args = map(lambda x: x[1][0], calls)423 args = list(args)424 self.assertIn('1234', args)425 self.assertIn('12345', args)426 self.assertIn('123456', args)427 @mock.patch('tempest.lib.common.rest_client.RestClient')428 def test_network_alt_creation(self, MockRestClient):429 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)430 self._mock_assign_user_role()431 self._mock_list_role()432 self._mock_user_create('1234', 'fake_alt_user')433 self._mock_tenant_create('1234', 'fake_alt_tenant')434 self._mock_network_create(creds, '1234', 'fake_alt_net')435 self._mock_subnet_create(creds, '1234', 'fake_alt_subnet')436 self._mock_router_create('1234', 'fake_alt_router')437 router_interface_mock = self.patch(438 'tempest.lib.services.network.routers_client.RoutersClient.'439 'add_router_interface')440 alt_creds = creds.get_alt_creds()441 router_interface_mock.assert_called_once_with('1234', subnet_id='1234')442 network = alt_creds.network443 subnet = alt_creds.subnet444 router = alt_creds.router445 self.assertEqual(network['id'], '1234')446 self.assertEqual(network['name'], 'fake_alt_net')447 self.assertEqual(subnet['id'], '1234')448 self.assertEqual(subnet['name'], 'fake_alt_subnet')449 self.assertEqual(router['id'], '1234')450 self.assertEqual(router['name'], 'fake_alt_router')451 @mock.patch('tempest.lib.common.rest_client.RestClient')452 def test_network_admin_creation(self, MockRestClient):453 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)454 self._mock_assign_user_role()455 self._mock_user_create('1234', 'fake_admin_user')456 self._mock_tenant_create('1234', 'fake_admin_tenant')457 self._mock_network_create(creds, '1234', 'fake_admin_net')458 self._mock_subnet_create(creds, '1234', 'fake_admin_subnet')459 self._mock_router_create('1234', 'fake_admin_router')460 router_interface_mock = self.patch(461 'tempest.lib.services.network.routers_client.RoutersClient.'462 'add_router_interface')463 self._mock_list_roles('123456', 'admin')464 admin_creds = creds.get_admin_creds()465 router_interface_mock.assert_called_once_with('1234', subnet_id='1234')466 network = admin_creds.network467 subnet = admin_creds.subnet468 router = admin_creds.router469 self.assertEqual(network['id'], '1234')470 self.assertEqual(network['name'], 'fake_admin_net')471 self.assertEqual(subnet['id'], '1234')472 self.assertEqual(subnet['name'], 'fake_admin_subnet')473 self.assertEqual(router['id'], '1234')474 self.assertEqual(router['name'], 'fake_admin_router')475 @mock.patch('tempest.lib.common.rest_client.RestClient')476 def test_no_network_resources(self, MockRestClient):477 net_dict = {478 'network': False,479 'router': False,480 'subnet': False,481 'dhcp': False,482 }483 creds = dynamic_creds.DynamicCredentialProvider(484 network_resources=net_dict,485 **self.fixed_params)486 self._mock_assign_user_role()487 self._mock_list_role()488 self._mock_user_create('1234', 'fake_prim_user')489 self._mock_tenant_create('1234', 'fake_prim_tenant')490 net = mock.patch.object(creds.networks_admin_client,491 'delete_network')492 net_mock = net.start()493 subnet = mock.patch.object(creds.subnets_admin_client,494 'delete_subnet')495 subnet_mock = subnet.start()496 router = mock.patch.object(creds.routers_admin_client,497 'delete_router')498 router_mock = router.start()499 primary_creds = creds.get_primary_creds()500 self.assertEqual(net_mock.mock_calls, [])501 self.assertEqual(subnet_mock.mock_calls, [])502 self.assertEqual(router_mock.mock_calls, [])503 network = primary_creds.network504 subnet = primary_creds.subnet505 router = primary_creds.router506 self.assertIsNone(network)507 self.assertIsNone(subnet)508 self.assertIsNone(router)509 @mock.patch('tempest.lib.common.rest_client.RestClient')510 def test_router_without_network(self, MockRestClient):511 net_dict = {512 'network': False,513 'router': True,514 'subnet': False,515 'dhcp': False,516 }517 creds = dynamic_creds.DynamicCredentialProvider(518 network_resources=net_dict,519 **self.fixed_params)520 self._mock_assign_user_role()521 self._mock_list_role()522 self._mock_user_create('1234', 'fake_prim_user')523 self._mock_tenant_create('1234', 'fake_prim_tenant')524 self.assertRaises(exceptions.InvalidConfiguration,525 creds.get_primary_creds)526 @mock.patch('tempest.lib.common.rest_client.RestClient')527 def test_subnet_without_network(self, MockRestClient):528 net_dict = {529 'network': False,530 'router': False,531 'subnet': True,532 'dhcp': False,533 }534 creds = dynamic_creds.DynamicCredentialProvider(535 network_resources=net_dict,536 **self.fixed_params)537 self._mock_assign_user_role()538 self._mock_list_role()539 self._mock_user_create('1234', 'fake_prim_user')540 self._mock_tenant_create('1234', 'fake_prim_tenant')541 self.assertRaises(exceptions.InvalidConfiguration,542 creds.get_primary_creds)543 @mock.patch('tempest.lib.common.rest_client.RestClient')544 def test_dhcp_without_subnet(self, MockRestClient):545 net_dict = {546 'network': False,547 'router': False,548 'subnet': False,549 'dhcp': True,550 }551 creds = dynamic_creds.DynamicCredentialProvider(552 network_resources=net_dict,553 **self.fixed_params)554 self._mock_assign_user_role()555 self._mock_list_role()556 self._mock_user_create('1234', 'fake_prim_user')557 self._mock_tenant_create('1234', 'fake_prim_tenant')558 self.assertRaises(exceptions.InvalidConfiguration,559 creds.get_primary_creds)560class TestDynamicCredentialProviderV3(TestDynamicCredentialProvider):561 fixed_params = {'name': 'test class',562 'identity_version': 'v3',563 'admin_role': 'admin'}564 token_client = v3_token_client565 iden_client = v3_iden_client566 roles_client = v3_roles_client567 tenants_client = v3_projects_client568 users_client = v3_users_client569 token_client_class = token_client.V3TokenClient570 fake_response = fake_identity._fake_v3_response571 assign_role_on_project = 'assign_user_role_on_project'572 tenants_client_class = tenants_client.ProjectsClient573 delete_tenant = 'delete_project'574 def setUp(self):575 super(TestDynamicCredentialProviderV3, self).setUp()576 self.useFixture(fake_config.ConfigFixture())577 self.useFixture(mockpatch.PatchObject(578 domains_client.DomainsClient, 'list_domains',579 return_value=dict(domains=[dict(id='default',580 name='Default')])))581 self.patchobject(self.roles_client.RolesClient,582 'assign_user_role_on_domain')583 def _mock_list_ec2_credentials(self, user_id, tenant_id):584 pass585 def _mock_tenant_create(self, id, name):586 project_fix = self.useFixture(mockpatch.PatchObject(587 self.tenants_client.ProjectsClient,588 'create_project',589 return_value=(rest_client.ResponseBody590 (200, {'project': {'id': id, 'name': name}}))))...
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!!