How to use _mock_list_2_roles method in tempest

Best Python code snippet using tempest_python

test_dynamic_creds.py

Source:test_dynamic_creds.py Github

copy

Full Screen

...99 {'roles': [{'id': id, 'name': name},100 {'id': '1', 'name': 'FakeRole'},101 {'id': '2', 'name': 'member'}]}))))102 return roles_fix103 def _mock_list_2_roles(self):104 roles_fix = self.useFixture(fixtures.MockPatchObject(105 self.roles_client.RolesClient,106 'list_roles',107 return_value=(rest_client.ResponseBody108 (200,109 {'roles': [{'id': '1234', 'name': 'role1'},110 {'id': '1', 'name': 'FakeRole'},111 {'id': '12345', 'name': 'role2'}]}))))112 return roles_fix113 def _mock_assign_user_role(self):114 tenant_fix = self.useFixture(fixtures.MockPatchObject(115 self.roles_client.RolesClient,116 'create_user_role_on_project',117 return_value=(rest_client.ResponseBody118 (200, {}))))119 return tenant_fix120 def _mock_list_role(self):121 roles_fix = self.useFixture(fixtures.MockPatchObject(122 self.roles_client.RolesClient,123 'list_roles',124 return_value=(rest_client.ResponseBody125 (200, {'roles': [126 {'id': '1', 'name': 'FakeRole'},127 {'id': '2', 'name': 'member'}]}))))128 return roles_fix129 def _mock_list_ec2_credentials(self, user_id, tenant_id):130 ec2_creds_fix = self.useFixture(fixtures.MockPatchObject(131 self.users_client.UsersClient,132 'list_user_ec2_credentials',133 return_value=(rest_client.ResponseBody134 (200, {'credentials': [{135 'access': 'fake_access',136 'secret': 'fake_secret',137 'tenant_id': tenant_id,138 'user_id': user_id,139 'trust_id': None}]}))))140 return ec2_creds_fix141 def _mock_network_create(self, iso_creds, id, name):142 net_fix = self.useFixture(fixtures.MockPatchObject(143 iso_creds.networks_admin_client,144 'create_network',145 return_value={'network': {'id': id, 'name': name}}))146 return net_fix147 def _mock_subnet_create(self, iso_creds, id, name):148 subnet_fix = self.useFixture(fixtures.MockPatchObject(149 iso_creds.subnets_admin_client,150 'create_subnet',151 return_value={'subnet': {'id': id, 'name': name}}))152 return subnet_fix153 def _mock_router_create(self, id, name):154 router_fix = self.useFixture(fixtures.MockPatchObject(155 routers_client.RoutersClient,156 'create_router',157 return_value={'router': {'id': id, 'name': name}}))158 return router_fix159 @mock.patch('tempest.lib.common.rest_client.RestClient')160 def test_primary_creds(self, MockRestClient):161 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)162 self._mock_assign_user_role()163 self._mock_list_role()164 self._mock_tenant_create('1234', 'fake_prim_tenant')165 self._mock_user_create('1234', 'fake_prim_user')166 primary_creds = creds.get_primary_creds()167 self.assertEqual(primary_creds.username, 'fake_prim_user')168 self.assertEqual(primary_creds.tenant_name, 'fake_prim_tenant')169 # Verify IDs170 self.assertEqual(primary_creds.tenant_id, '1234')171 self.assertEqual(primary_creds.user_id, '1234')172 @mock.patch('tempest.lib.common.rest_client.RestClient')173 def test_admin_creds(self, MockRestClient):174 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)175 self._mock_list_roles('1234', 'admin')176 self._mock_user_create('1234', 'fake_admin_user')177 self._mock_tenant_create('1234', 'fake_admin_tenant')178 user_mock = mock.patch.object(self.roles_client.RolesClient,179 'create_user_role_on_project')180 user_mock.start()181 self.addCleanup(user_mock.stop)182 with mock.patch.object(self.roles_client.RolesClient,183 'create_user_role_on_project') as user_mock:184 admin_creds = creds.get_admin_creds()185 user_mock.assert_has_calls([186 mock.call('1234', '1234', '1234')])187 self.assertEqual(admin_creds.username, 'fake_admin_user')188 self.assertEqual(admin_creds.tenant_name, 'fake_admin_tenant')189 # Verify IDs190 self.assertEqual(admin_creds.tenant_id, '1234')191 self.assertEqual(admin_creds.user_id, '1234')192 @mock.patch('tempest.lib.common.rest_client.RestClient')193 def test_project_alt_admin_creds(self, MockRestClient):194 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)195 self._mock_list_roles('1234', 'admin')196 self._mock_user_create('1234', 'fake_alt_admin_user')197 self._mock_tenant_create('1234', 'fake_alt_admin')198 user_mock = mock.patch.object(self.roles_client.RolesClient,199 'create_user_role_on_project')200 user_mock.start()201 self.addCleanup(user_mock.stop)202 with mock.patch.object(self.roles_client.RolesClient,203 'create_user_role_on_project') as user_mock:204 alt_admin_creds = creds.get_project_alt_admin_creds()205 user_mock.assert_has_calls([206 mock.call('1234', '1234', '1234')])207 self.assertEqual(alt_admin_creds.username, 'fake_alt_admin_user')208 self.assertEqual(alt_admin_creds.project_name, 'fake_alt_admin')209 # Verify IDs210 self.assertEqual(alt_admin_creds.project_id, '1234')211 self.assertEqual(alt_admin_creds.user_id, '1234')212 @mock.patch('tempest.lib.common.rest_client.RestClient')213 def test_project_alt_member_creds(self, MockRestClient):214 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)215 self._mock_assign_user_role()216 self._mock_list_role()217 self._mock_tenant_create('1234', 'fake_alt_member')218 self._mock_user_create('1234', 'fake_alt_user')219 alt_member_creds = creds.get_project_alt_member_creds()220 self.assertEqual(alt_member_creds.username, 'fake_alt_user')221 self.assertEqual(alt_member_creds.project_name, 'fake_alt_member')222 # Verify IDs223 self.assertEqual(alt_member_creds.project_id, '1234')224 self.assertEqual(alt_member_creds.user_id, '1234')225 @mock.patch('tempest.lib.common.rest_client.RestClient')226 def test_project_alt_reader_creds(self, MockRestClient):227 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)228 self._mock_assign_user_role()229 self._mock_list_roles('1234', 'reader')230 self._mock_tenant_create('1234', 'fake_alt_reader')231 self._mock_user_create('1234', 'fake_alt_user')232 alt_reader_creds = creds.get_project_alt_reader_creds()233 self.assertEqual(alt_reader_creds.username, 'fake_alt_user')234 self.assertEqual(alt_reader_creds.project_name, 'fake_alt_reader')235 # Verify IDs236 self.assertEqual(alt_reader_creds.project_id, '1234')237 self.assertEqual(alt_reader_creds.user_id, '1234')238 @mock.patch('tempest.lib.common.rest_client.RestClient')239 def test_role_creds(self, MockRestClient):240 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)241 self._mock_list_2_roles()242 self._mock_user_create('1234', 'fake_role_user')243 self._mock_tenant_create('1234', 'fake_role_tenant')244 user_mock = mock.patch.object(self.roles_client.RolesClient,245 'create_user_role_on_project')246 user_mock.start()247 self.addCleanup(user_mock.stop)248 with mock.patch.object(self.roles_client.RolesClient,249 'create_user_role_on_project') as user_mock:250 role_creds = creds.get_creds_by_roles(251 roles=['role1', 'role2'])252 calls = user_mock.mock_calls253 # Assert that the role creation is called with the 2 specified roles254 self.assertEqual(len(calls), 2)255 args = map(lambda x: x[1], calls)256 args = list(args)257 self.assertIn(('1234', '1234', '1234'), args)258 self.assertIn(('1234', '1234', '12345'), args)259 self.assertEqual(role_creds.username, 'fake_role_user')260 self.assertEqual(role_creds.tenant_name, 'fake_role_tenant')261 # Verify IDs262 self.assertEqual(role_creds.tenant_id, '1234')263 self.assertEqual(role_creds.user_id, '1234')264 @mock.patch('tempest.lib.common.rest_client.RestClient')265 def test_role_creds_with_project_scope(self, MockRestClient):266 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)267 self._mock_list_2_roles()268 self._mock_user_create('1234', 'fake_role_user')269 self._mock_tenant_create('1234', 'fake_role_project')270 user_mock = mock.patch.object(self.roles_client.RolesClient,271 'create_user_role_on_project')272 user_mock.start()273 self.addCleanup(user_mock.stop)274 with mock.patch.object(self.roles_client.RolesClient,275 'create_user_role_on_project') as user_mock:276 role_creds = creds.get_creds_by_roles(277 roles=['role1', 'role2'], scope='project')278 calls = user_mock.mock_calls279 # Assert that the role creation is called with the 2 specified roles280 self.assertEqual(len(calls), 2)281 args = map(lambda x: x[1], calls)282 args = list(args)283 self.assertIn(('1234', '1234', '1234'), args)284 self.assertIn(('1234', '1234', '12345'), args)285 self.assertEqual(role_creds.username, 'fake_role_user')286 self.assertEqual(role_creds.project_name, 'fake_role_project')287 # Verify IDs288 self.assertEqual(role_creds.project_id, '1234')289 self.assertEqual(role_creds.user_id, '1234')290 @mock.patch('tempest.lib.common.rest_client.RestClient')291 def _test_get_same_role_creds_with_project_scope(self, MockRestClient,292 scope=None):293 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)294 self._mock_list_2_roles()295 self._mock_user_create('1234', 'fake_role_user')296 self._mock_tenant_create('1234', 'fake_role_project')297 with mock.patch.object(self.roles_client.RolesClient,298 'create_user_role_on_project') as user_mock:299 role_creds = creds.get_creds_by_roles(300 roles=['role1', 'role2'], scope=scope)301 calls = user_mock.mock_calls302 # Assert that the role creation is called with the 2 specified roles303 self.assertEqual(len(calls), 2)304 # Fetch the same creds again305 with mock.patch.object(self.roles_client.RolesClient,306 'create_user_role_on_project') as user_mock1:307 role_creds_new = creds.get_creds_by_roles(308 roles=['role1', 'role2'], scope=scope)309 calls = user_mock1.mock_calls310 # Assert that previously created creds are return and no call to311 # role creation.312 self.assertEqual(len(calls), 0)313 # Check if previously created creds are returned.314 self.assertEqual(role_creds, role_creds_new)315 def test_get_same_role_creds_with_project_scope(self):316 self._test_get_same_role_creds_with_project_scope(scope='project')317 def test_get_same_role_creds_with_default_scope(self):318 self._test_get_same_role_creds_with_project_scope()319 @mock.patch('tempest.lib.common.rest_client.RestClient')320 def _test_get_different_role_creds_with_project_scope(321 self, MockRestClient, scope=None):322 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)323 self._mock_list_2_roles()324 self._mock_user_create('1234', 'fake_role_user')325 self._mock_tenant_create('1234', 'fake_role_project')326 with mock.patch.object(self.roles_client.RolesClient,327 'create_user_role_on_project') as user_mock:328 role_creds = creds.get_creds_by_roles(329 roles=['role1', 'role2'], scope=scope)330 calls = user_mock.mock_calls331 # Assert that the role creation is called with the 2 specified roles332 self.assertEqual(len(calls), 2)333 # Fetch the creds with one role different334 with mock.patch.object(self.roles_client.RolesClient,335 'create_user_role_on_project') as user_mock1:336 role_creds_new = creds.get_creds_by_roles(337 roles=['role1'], scope=scope)338 calls = user_mock1.mock_calls339 # Because one role is different, assert that the role creation340 # is called with the 1 specified roles341 self.assertEqual(len(calls), 1)342 # Check new creds is created for new roles.343 self.assertNotEqual(role_creds, role_creds_new)344 def test_get_different_role_creds_with_project_scope(self):345 self._test_get_different_role_creds_with_project_scope(346 scope='project')347 def test_get_different_role_creds_with_default_scope(self):348 self._test_get_different_role_creds_with_project_scope()349 @mock.patch('tempest.lib.common.rest_client.RestClient')350 def test_all_cred_cleanup(self, MockRestClient):351 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)352 self._mock_assign_user_role()353 self._mock_list_role()354 self._mock_tenant_create('1234', 'fake_prim_tenant')355 self._mock_user_create('1234', 'fake_prim_user')356 creds.get_primary_creds()357 self._mock_tenant_create('12345', 'fake_alt_tenant')358 self._mock_user_create('12345', 'fake_alt_user')359 creds.get_alt_creds()360 self._mock_tenant_create('123456', 'fake_admin_tenant')361 self._mock_user_create('123456', 'fake_admin_user')362 self._mock_list_roles('123456', 'admin')363 creds.get_admin_creds()364 user_mock = self.patchobject(self.users_client.UsersClient,365 'delete_user')366 tenant_mock = self.patchobject(self.tenants_client_class,367 self.delete_tenant)368 creds.clear_creds()369 # Verify user delete calls370 calls = user_mock.mock_calls371 self.assertEqual(len(calls), 3)372 args = map(lambda x: x[1][0], calls)373 args = list(args)374 self.assertIn('1234', args)375 self.assertIn('12345', args)376 self.assertIn('123456', args)377 # Verify tenant delete calls378 calls = tenant_mock.mock_calls379 self.assertEqual(len(calls), 3)380 args = map(lambda x: x[1][0], calls)381 args = list(args)382 self.assertIn('1234', args)383 self.assertIn('12345', args)384 self.assertIn('123456', args)385 @mock.patch('tempest.lib.common.rest_client.RestClient')386 def test_alt_creds(self, MockRestClient):387 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)388 self._mock_assign_user_role()389 self._mock_list_role()390 self._mock_user_create('1234', 'fake_alt_user')391 self._mock_tenant_create('1234', 'fake_alt_tenant')392 alt_creds = creds.get_alt_creds()393 self.assertEqual(alt_creds.username, 'fake_alt_user')394 self.assertEqual(alt_creds.tenant_name, 'fake_alt_tenant')395 # Verify IDs396 self.assertEqual(alt_creds.tenant_id, '1234')397 self.assertEqual(alt_creds.user_id, '1234')398 @mock.patch('tempest.lib.common.rest_client.RestClient')399 def test_no_network_creation_with_config_set(self, MockRestClient):400 creds = dynamic_creds.DynamicCredentialProvider(401 neutron_available=True, create_networks=False,402 project_network_cidr='10.100.0.0/16', project_network_mask_bits=28,403 **self.fixed_params)404 self._mock_assign_user_role()405 self._mock_list_role()406 self._mock_user_create('1234', 'fake_prim_user')407 self._mock_tenant_create('1234', 'fake_prim_tenant')408 net = mock.patch.object(creds.networks_admin_client,409 'delete_network')410 net_mock = net.start()411 subnet = mock.patch.object(creds.subnets_admin_client,412 'delete_subnet')413 subnet_mock = subnet.start()414 router = mock.patch.object(creds.routers_admin_client,415 'delete_router')416 router_mock = router.start()417 primary_creds = creds.get_primary_creds()418 self.assertEqual(net_mock.mock_calls, [])419 self.assertEqual(subnet_mock.mock_calls, [])420 self.assertEqual(router_mock.mock_calls, [])421 network = primary_creds.network422 subnet = primary_creds.subnet423 router = primary_creds.router424 self.assertIsNone(network)425 self.assertIsNone(subnet)426 self.assertIsNone(router)427 @mock.patch('tempest.lib.common.rest_client.RestClient')428 def test_network_creation(self, MockRestClient):429 creds = dynamic_creds.DynamicCredentialProvider(430 neutron_available=True,431 project_network_cidr='10.100.0.0/16', project_network_mask_bits=28,432 **self.fixed_params)433 self._mock_assign_user_role()434 self._mock_list_role()435 self._mock_user_create('1234', 'fake_prim_user')436 self._mock_tenant_create('1234', 'fake_prim_tenant')437 self._mock_network_create(creds, '1234', 'fake_net')438 self._mock_subnet_create(creds, '1234', 'fake_subnet')439 self._mock_router_create('1234', 'fake_router')440 router_interface_mock = self.patch(441 'tempest.lib.services.network.routers_client.RoutersClient.'442 'add_router_interface')443 primary_creds = creds.get_primary_creds()444 router_interface_mock.assert_called_once_with('1234', subnet_id='1234')445 network = primary_creds.network446 subnet = primary_creds.subnet447 router = primary_creds.router448 self.assertEqual(network['id'], '1234')449 self.assertEqual(network['name'], 'fake_net')450 self.assertEqual(subnet['id'], '1234')451 self.assertEqual(subnet['name'], 'fake_subnet')452 self.assertEqual(router['id'], '1234')453 self.assertEqual(router['name'], 'fake_router')454 @mock.patch('tempest.lib.common.rest_client.RestClient')455 def test_network_cleanup(self, MockRestClient):456 def side_effect(**args):457 return {"security_groups": [{"tenant_id": args['tenant_id'],458 "name": args['name'],459 "description": args['name'],460 "security_group_rules": [],461 "id": "sg-%s" % args['tenant_id']}]}462 creds = dynamic_creds.DynamicCredentialProvider(463 neutron_available=True,464 project_network_cidr='10.100.0.0/16', project_network_mask_bits=28,465 **self.fixed_params)466 # Create primary tenant and network467 self._mock_assign_user_role()468 self._mock_list_role()469 self._mock_user_create('1234', 'fake_prim_user')470 self._mock_tenant_create('1234', 'fake_prim_tenant')471 self._mock_network_create(creds, '1234', 'fake_net')472 self._mock_subnet_create(creds, '1234', 'fake_subnet')473 self._mock_router_create('1234', 'fake_router')474 router_interface_mock = self.patch(475 'tempest.lib.services.network.routers_client.RoutersClient.'476 'add_router_interface')477 creds.get_primary_creds()478 router_interface_mock.assert_called_once_with('1234', subnet_id='1234')479 router_interface_mock.reset_mock()480 # Create alternate tenant and network481 self._mock_user_create('12345', 'fake_alt_user')482 self._mock_tenant_create('12345', 'fake_alt_tenant')483 self._mock_network_create(creds, '12345', 'fake_alt_net')484 self._mock_subnet_create(creds, '12345', 'fake_alt_subnet')485 self._mock_router_create('12345', 'fake_alt_router')486 creds.get_alt_creds()487 router_interface_mock.assert_called_once_with('12345',488 subnet_id='12345')489 router_interface_mock.reset_mock()490 # Create admin tenant and networks491 self._mock_user_create('123456', 'fake_admin_user')492 self._mock_tenant_create('123456', 'fake_admin_tenant')493 self._mock_network_create(creds, '123456', 'fake_admin_net')494 self._mock_subnet_create(creds, '123456', 'fake_admin_subnet')495 self._mock_router_create('123456', 'fake_admin_router')496 self._mock_list_roles('123456', 'admin')497 creds.get_admin_creds()498 self.patchobject(self.users_client.UsersClient, 'delete_user')499 self.patchobject(self.tenants_client_class, self.delete_tenant)500 net = mock.patch.object(creds.networks_admin_client, 'delete_network')501 net_mock = net.start()502 subnet = mock.patch.object(creds.subnets_admin_client, 'delete_subnet')503 subnet_mock = subnet.start()504 router = mock.patch.object(creds.routers_admin_client, 'delete_router')505 router_mock = router.start()506 remove_router_interface_mock = self.patch(507 'tempest.lib.services.network.routers_client.RoutersClient.'508 'remove_router_interface')509 return_values = ({'status': 200}, {'ports': []})510 port_list_mock = mock.patch.object(creds.ports_admin_client,511 'list_ports',512 return_value=return_values)513 port_list_mock.start()514 secgroup_list_mock = mock.patch.object(515 creds.security_groups_admin_client,516 'list_security_groups',517 side_effect=side_effect)518 secgroup_list_mock.start()519 return_values = fake_http.fake_http_response({}, status=204), ''520 remove_secgroup_mock = self.patch(521 'tempest.lib.services.network.security_groups_client.'522 'SecurityGroupsClient.delete', return_value=return_values)523 creds.clear_creds()524 # Verify default security group delete525 calls = remove_secgroup_mock.mock_calls526 self.assertEqual(len(calls), 3)527 args = map(lambda x: x[1][0], calls)528 args = list(args)529 self.assertIn('v2.0/security-groups/sg-1234', args)530 self.assertIn('v2.0/security-groups/sg-12345', args)531 self.assertIn('v2.0/security-groups/sg-123456', args)532 # Verify remove router interface calls533 calls = remove_router_interface_mock.mock_calls534 self.assertEqual(len(calls), 3)535 args = map(lambda x: (x[1][0], x[2]), calls)536 args = list(args)537 self.assertIn(('1234', {'subnet_id': '1234'}), args)538 self.assertIn(('12345', {'subnet_id': '12345'}), args)539 self.assertIn(('123456', {'subnet_id': '123456'}), args)540 # Verify network delete calls541 calls = net_mock.mock_calls542 self.assertEqual(len(calls), 3)543 args = map(lambda x: x[1][0], calls)544 args = list(args)545 self.assertIn('1234', args)546 self.assertIn('12345', args)547 self.assertIn('123456', args)548 # Verify subnet delete calls549 calls = subnet_mock.mock_calls550 self.assertEqual(len(calls), 3)551 args = map(lambda x: x[1][0], calls)552 args = list(args)553 self.assertIn('1234', args)554 self.assertIn('12345', args)555 self.assertIn('123456', args)556 # Verify router delete calls557 calls = router_mock.mock_calls558 self.assertEqual(len(calls), 3)559 args = map(lambda x: x[1][0], calls)560 args = list(args)561 self.assertIn('1234', args)562 self.assertIn('12345', args)563 self.assertIn('123456', args)564 @mock.patch('tempest.lib.common.rest_client.RestClient')565 def test_network_alt_creation(self, MockRestClient):566 creds = dynamic_creds.DynamicCredentialProvider(567 neutron_available=True,568 project_network_cidr='10.100.0.0/16', project_network_mask_bits=28,569 **self.fixed_params)570 self._mock_assign_user_role()571 self._mock_list_role()572 self._mock_user_create('1234', 'fake_alt_user')573 self._mock_tenant_create('1234', 'fake_alt_tenant')574 self._mock_network_create(creds, '1234', 'fake_alt_net')575 self._mock_subnet_create(creds, '1234', 'fake_alt_subnet')576 self._mock_router_create('1234', 'fake_alt_router')577 router_interface_mock = self.patch(578 'tempest.lib.services.network.routers_client.RoutersClient.'579 'add_router_interface')580 alt_creds = creds.get_alt_creds()581 router_interface_mock.assert_called_once_with('1234', subnet_id='1234')582 network = alt_creds.network583 subnet = alt_creds.subnet584 router = alt_creds.router585 self.assertEqual(network['id'], '1234')586 self.assertEqual(network['name'], 'fake_alt_net')587 self.assertEqual(subnet['id'], '1234')588 self.assertEqual(subnet['name'], 'fake_alt_subnet')589 self.assertEqual(router['id'], '1234')590 self.assertEqual(router['name'], 'fake_alt_router')591 @mock.patch('tempest.lib.common.rest_client.RestClient')592 def test_network_admin_creation(self, MockRestClient):593 creds = dynamic_creds.DynamicCredentialProvider(594 neutron_available=True,595 project_network_cidr='10.100.0.0/16', project_network_mask_bits=28,596 **self.fixed_params)597 self._mock_assign_user_role()598 self._mock_user_create('1234', 'fake_admin_user')599 self._mock_tenant_create('1234', 'fake_admin_tenant')600 self._mock_network_create(creds, '1234', 'fake_admin_net')601 self._mock_subnet_create(creds, '1234', 'fake_admin_subnet')602 self._mock_router_create('1234', 'fake_admin_router')603 router_interface_mock = self.patch(604 'tempest.lib.services.network.routers_client.RoutersClient.'605 'add_router_interface')606 self._mock_list_roles('123456', 'admin')607 admin_creds = creds.get_admin_creds()608 router_interface_mock.assert_called_once_with('1234', subnet_id='1234')609 network = admin_creds.network610 subnet = admin_creds.subnet611 router = admin_creds.router612 self.assertEqual(network['id'], '1234')613 self.assertEqual(network['name'], 'fake_admin_net')614 self.assertEqual(subnet['id'], '1234')615 self.assertEqual(subnet['name'], 'fake_admin_subnet')616 self.assertEqual(router['id'], '1234')617 self.assertEqual(router['name'], 'fake_admin_router')618 @mock.patch('tempest.lib.common.rest_client.RestClient')619 def test_no_network_resources(self, MockRestClient):620 net_dict = {621 'network': False,622 'router': False,623 'subnet': False,624 'dhcp': False,625 }626 creds = dynamic_creds.DynamicCredentialProvider(627 neutron_available=True,628 project_network_cidr='10.100.0.0/16', project_network_mask_bits=28,629 network_resources=net_dict,630 **self.fixed_params)631 self._mock_assign_user_role()632 self._mock_list_role()633 self._mock_user_create('1234', 'fake_prim_user')634 self._mock_tenant_create('1234', 'fake_prim_tenant')635 net = mock.patch.object(creds.networks_admin_client,636 'delete_network')637 net_mock = net.start()638 subnet = mock.patch.object(creds.subnets_admin_client,639 'delete_subnet')640 subnet_mock = subnet.start()641 router = mock.patch.object(creds.routers_admin_client,642 'delete_router')643 router_mock = router.start()644 primary_creds = creds.get_primary_creds()645 self.assertEqual(net_mock.mock_calls, [])646 self.assertEqual(subnet_mock.mock_calls, [])647 self.assertEqual(router_mock.mock_calls, [])648 network = primary_creds.network649 subnet = primary_creds.subnet650 router = primary_creds.router651 self.assertIsNone(network)652 self.assertIsNone(subnet)653 self.assertIsNone(router)654 @mock.patch('tempest.lib.common.rest_client.RestClient')655 def test_router_without_network(self, MockRestClient):656 net_dict = {657 'network': False,658 'router': True,659 'subnet': False,660 'dhcp': False,661 }662 creds = dynamic_creds.DynamicCredentialProvider(663 neutron_available=True,664 project_network_cidr='10.100.0.0/16', project_network_mask_bits=28,665 network_resources=net_dict,666 **self.fixed_params)667 self._mock_assign_user_role()668 self._mock_list_role()669 self._mock_user_create('1234', 'fake_prim_user')670 self._mock_tenant_create('1234', 'fake_prim_tenant')671 self.assertRaises(lib_exc.InvalidConfiguration,672 creds.get_primary_creds)673 @mock.patch('tempest.lib.common.rest_client.RestClient')674 def test_subnet_without_network(self, MockRestClient):675 net_dict = {676 'network': False,677 'router': False,678 'subnet': True,679 'dhcp': False,680 }681 creds = dynamic_creds.DynamicCredentialProvider(682 neutron_available=True,683 project_network_cidr='10.100.0.0/16', project_network_mask_bits=28,684 network_resources=net_dict,685 **self.fixed_params)686 self._mock_assign_user_role()687 self._mock_list_role()688 self._mock_user_create('1234', 'fake_prim_user')689 self._mock_tenant_create('1234', 'fake_prim_tenant')690 self.assertRaises(lib_exc.InvalidConfiguration,691 creds.get_primary_creds)692 @mock.patch('tempest.lib.common.rest_client.RestClient')693 def test_dhcp_without_subnet(self, MockRestClient):694 net_dict = {695 'network': False,696 'router': False,697 'subnet': False,698 'dhcp': True,699 }700 creds = dynamic_creds.DynamicCredentialProvider(701 neutron_available=True,702 project_network_cidr='10.100.0.0/16', project_network_mask_bits=28,703 network_resources=net_dict,704 **self.fixed_params)705 self._mock_assign_user_role()706 self._mock_list_role()707 self._mock_user_create('1234', 'fake_prim_user')708 self._mock_tenant_create('1234', 'fake_prim_tenant')709 self.assertRaises(lib_exc.InvalidConfiguration,710 creds.get_primary_creds)711class TestDynamicCredentialProviderV3(TestDynamicCredentialProvider):712 fixed_params = {'name': 'test class',713 'identity_version': 'v3',714 'admin_role': 'admin',715 'identity_uri': 'fake_uri'}716 token_client = v3_token_client717 iden_client = v3_iden_client718 roles_client = v3_roles_client719 tenants_client = v3_projects_client720 users_client = v3_users_client721 token_client_class = token_client.V3TokenClient722 fake_response = fake_identity._fake_v3_response723 tenants_client_class = tenants_client.ProjectsClient724 delete_tenant = 'delete_project'725 def setUp(self):726 super(TestDynamicCredentialProviderV3, self).setUp()727 self.useFixture(fake_config.ConfigFixture())728 self.useFixture(fixtures.MockPatchObject(729 domains_client.DomainsClient, 'list_domains',730 return_value=dict(domains=[dict(id='default',731 name='Default')])))732 self.patchobject(self.roles_client.RolesClient,733 'create_user_role_on_domain')734 def _mock_list_ec2_credentials(self, user_id, tenant_id):735 pass736 def _mock_tenant_create(self, id, name):737 project_fix = self.useFixture(fixtures.MockPatchObject(738 self.tenants_client.ProjectsClient,739 'create_project',740 return_value=(rest_client.ResponseBody741 (200, {'project': {'id': id, 'name': name}}))))742 return project_fix743 @mock.patch('tempest.lib.common.rest_client.RestClient')744 def test_role_creds_with_system_scope(self, MockRestClient):745 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)746 self._mock_list_2_roles()747 self._mock_user_create('1234', 'fake_role_user')748 with mock.patch.object(self.roles_client.RolesClient,749 'create_user_role_on_system') as user_mock:750 role_creds = creds.get_creds_by_roles(751 roles=['role1', 'role2'], scope='system')752 calls = user_mock.mock_calls753 # Assert that the role creation is called with the 2 specified roles754 self.assertEqual(len(calls), 2)755 args = map(lambda x: x[1], calls)756 args = list(args)757 self.assertIn(('1234', '1234'), args)758 self.assertIn(('1234', '12345'), args)759 self.assertEqual(role_creds.username, 'fake_role_user')760 self.assertEqual(role_creds.user_id, '1234')761 # Verify system scope762 self.assertEqual(role_creds.system, 'all')763 # Verify domain is default764 self.assertEqual(role_creds.domain_id, 'default')765 self.assertEqual(role_creds.domain_name, 'Default')766 @mock.patch('tempest.lib.common.rest_client.RestClient')767 def test_get_same_role_creds_with_system_scope(self, MockRestClient):768 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)769 self._mock_list_2_roles()770 self._mock_user_create('1234', 'fake_role_user')771 with mock.patch.object(self.roles_client.RolesClient,772 'create_user_role_on_system') as user_mock:773 role_creds = creds.get_creds_by_roles(774 roles=['role1', 'role2'], scope='system')775 calls = user_mock.mock_calls776 # Assert that the role creation is called with the 2 specified roles777 self.assertEqual(len(calls), 2)778 # Fetch the same creds again779 with mock.patch.object(self.roles_client.RolesClient,780 'create_user_role_on_system') as user_mock1:781 role_creds_new = creds.get_creds_by_roles(782 roles=['role1', 'role2'], scope='system')783 calls = user_mock1.mock_calls784 # Assert that previously created creds are return and no call to785 # role creation.786 self.assertEqual(len(calls), 0)787 # Verify system scope788 self.assertEqual(role_creds_new.system, 'all')789 # Check if previously created creds are returned.790 self.assertEqual(role_creds, role_creds_new)791 @mock.patch('tempest.lib.common.rest_client.RestClient')792 def test_get_different_role_creds_with_system_scope(self, MockRestClient):793 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)794 self._mock_list_2_roles()795 self._mock_user_create('1234', 'fake_role_user')796 with mock.patch.object(self.roles_client.RolesClient,797 'create_user_role_on_system') as user_mock:798 role_creds = creds.get_creds_by_roles(799 roles=['role1', 'role2'], scope='system')800 calls = user_mock.mock_calls801 # Assert that the role creation is called with the 2 specified roles802 self.assertEqual(len(calls), 2)803 # Verify system scope804 self.assertEqual(role_creds.system, 'all')805 # Fetch the creds with one role different806 with mock.patch.object(self.roles_client.RolesClient,807 'create_user_role_on_system') as user_mock1:808 role_creds_new = creds.get_creds_by_roles(809 roles=['role1'], scope='system')810 calls = user_mock1.mock_calls811 # Because one role is different, assert that the role creation812 # is called with the 1 specified roles813 self.assertEqual(len(calls), 1)814 # Verify Scope815 self.assertEqual(role_creds_new.system, 'all')816 # Check new creds is created for new roles.817 self.assertNotEqual(role_creds, role_creds_new)818 @mock.patch('tempest.lib.common.rest_client.RestClient')819 def test_role_creds_with_domain_scope(self, MockRestClient):820 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)821 self._mock_list_2_roles()822 self._mock_user_create('1234', 'fake_role_user')823 domain = {824 "id": '12',825 "enabled": True,826 "name": "TestDomain"827 }828 self.useFixture(fixtures.MockPatch(829 'tempest.lib.common.cred_client.V3CredsClient.create_domain',830 return_value=domain))831 with mock.patch.object(self.roles_client.RolesClient,832 'create_user_role_on_domain') as user_mock:833 role_creds = creds.get_creds_by_roles(834 roles=['role1', 'role2'], scope='domain')835 calls = user_mock.mock_calls836 # Assert that the role creation is called with the 2 specified roles837 self.assertEqual(len(calls), 2)838 args = map(lambda x: x[1], calls)839 args = list(args)840 self.assertIn((domain['id'], '1234', '1234'), args)841 self.assertIn((domain['id'], '1234', '12345'), args)842 self.assertEqual(role_creds.username, 'fake_role_user')843 self.assertEqual(role_creds.user_id, '1234')844 # Verify creds are under new created domain845 self.assertEqual(role_creds.domain_id, domain['id'])846 self.assertEqual(role_creds.domain_name, domain['name'])847 # Verify that Scope is None848 self.assertIsNone(role_creds.system)849 @mock.patch('tempest.lib.common.rest_client.RestClient')850 def test_get_same_role_creds_with_domain_scope(self, MockRestClient):851 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)852 self._mock_list_2_roles()853 self._mock_user_create('1234', 'fake_role_user')854 domain = {855 "id": '12',856 "enabled": True,857 "name": "TestDomain"858 }859 self.useFixture(fixtures.MockPatch(860 'tempest.lib.common.cred_client.V3CredsClient.create_domain',861 return_value=domain))862 with mock.patch.object(self.roles_client.RolesClient,863 'create_user_role_on_domain') as user_mock:864 role_creds = creds.get_creds_by_roles(865 roles=['role1', 'role2'], scope='domain')866 calls = user_mock.mock_calls867 # Assert that the role creation is called with the 2 specified roles868 self.assertEqual(len(calls), 2)869 self.assertEqual(role_creds.user_id, '1234')870 # Verify Scope871 self.assertIsNone(role_creds.system)872 # Fetch the same creds again873 with mock.patch.object(self.roles_client.RolesClient,874 'create_user_role_on_domain') as user_mock1:875 role_creds_new = creds.get_creds_by_roles(876 roles=['role1', 'role2'], scope='domain')877 calls = user_mock1.mock_calls878 # Assert that previously created creds are return and no call to879 # role creation.880 self.assertEqual(len(calls), 0)881 # Verify Scope882 self.assertIsNone(role_creds_new.system)883 # Check if previously created creds are returned.884 self.assertEqual(role_creds, role_creds_new)885 @mock.patch('tempest.lib.common.rest_client.RestClient')886 def test_get_different_role_creds_with_domain_scope(self, MockRestClient):887 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)888 self._mock_list_2_roles()889 self._mock_user_create('1234', 'fake_role_user')890 domain = {891 "id": '12',892 "enabled": True,893 "name": "TestDomain"894 }895 self.useFixture(fixtures.MockPatch(896 'tempest.lib.common.cred_client.V3CredsClient.create_domain',897 return_value=domain))898 with mock.patch.object(self.roles_client.RolesClient,899 'create_user_role_on_domain') as user_mock:900 role_creds = creds.get_creds_by_roles(901 roles=['role1', 'role2'], scope='domain')902 calls = user_mock.mock_calls903 # Assert that the role creation is called with the 2 specified roles904 self.assertEqual(len(calls), 2)905 self.assertEqual(role_creds.user_id, '1234')906 # Verify Scope907 self.assertIsNone(role_creds.system)908 # Fetch the same creds again909 with mock.patch.object(self.roles_client.RolesClient,910 'create_user_role_on_domain') as user_mock1:911 role_creds_new = creds.get_creds_by_roles(912 roles=['role1'], scope='domain')913 calls = user_mock1.mock_calls914 # Because one role is different, assert that the role creation915 # is called with the 1 specified roles916 self.assertEqual(len(calls), 1)917 # Verify Scope918 self.assertIsNone(role_creds_new.system)919 # Check new creds is created for new roles.920 self.assertNotEqual(role_creds, role_creds_new)921 @mock.patch('tempest.lib.common.rest_client.RestClient')922 def test_get_role_creds_with_different_scope(self, MockRestClient):923 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)924 self._mock_list_2_roles()925 self._mock_user_create('1234', 'fake_role_user')926 self._mock_tenant_create('1234', 'fake_role_project')927 with mock.patch.object(self.roles_client.RolesClient,928 'create_user_role_on_system') as user_mock:929 role_creds = creds.get_creds_by_roles(930 roles=['role1', 'role2'], scope='system')931 calls = user_mock.mock_calls932 # Assert that the role creation is called with the 2 specified roles933 self.assertEqual(len(calls), 2)934 # Verify Scope935 self.assertEqual(role_creds.system, 'all')936 # Fetch the same role creds but with different scope937 with mock.patch.object(self.roles_client.RolesClient,938 'create_user_role_on_project') as user_mock1:...

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