Best Python code snippet using tempest_python
test_dynamic_creds.py
Source:test_dynamic_creds.py
...67 'create_user',68 return_value=(service_client.ResponseBody69 (200, {'user': {'id': id, 'name': name}}))))70 return user_fix71 def _mock_tenant_create(self, id, name):72 tenant_fix = self.useFixture(mockpatch.PatchObject(73 json_tenants_client.TenantsClient,74 'create_tenant',75 return_value=(service_client.ResponseBody76 (200, {'tenant': {'id': id, 'name': name}}))))77 return tenant_fix78 def _mock_list_roles(self, id, name):79 roles_fix = self.useFixture(mockpatch.PatchObject(80 json_roles_client.RolesClient,81 'list_roles',82 return_value=(service_client.ResponseBody83 (200,84 {'roles': [{'id': id, 'name': name},85 {'id': '1', 'name': 'FakeRole'},86 {'id': '2', 'name': 'Member'}]}))))87 return roles_fix88 def _mock_list_2_roles(self):89 roles_fix = self.useFixture(mockpatch.PatchObject(90 json_roles_client.RolesClient,91 'list_roles',92 return_value=(service_client.ResponseBody93 (200,94 {'roles': [{'id': '1234', 'name': 'role1'},95 {'id': '1', 'name': 'FakeRole'},96 {'id': '12345', 'name': 'role2'}]}))))97 return roles_fix98 def _mock_assign_user_role(self):99 tenant_fix = self.useFixture(mockpatch.PatchObject(100 json_roles_client.RolesClient,101 'assign_user_role',102 return_value=(service_client.ResponseBody103 (200, {}))))104 return tenant_fix105 def _mock_list_role(self):106 roles_fix = self.useFixture(mockpatch.PatchObject(107 json_roles_client.RolesClient,108 'list_roles',109 return_value=(service_client.ResponseBody110 (200, {'roles': [{'id': '1',111 'name': 'FakeRole'}]}))))112 return roles_fix113 def _mock_list_ec2_credentials(self, user_id, tenant_id):114 ec2_creds_fix = self.useFixture(mockpatch.PatchObject(115 json_users_client.UsersClient,116 'list_user_ec2_credentials',117 return_value=(service_client.ResponseBody118 (200, {'credentials': [{119 'access': 'fake_access',120 'secret': 'fake_secret',121 'tenant_id': tenant_id,122 'user_id': user_id,123 'trust_id': None}]}))))124 return ec2_creds_fix125 def _mock_network_create(self, iso_creds, id, name):126 net_fix = self.useFixture(mockpatch.PatchObject(127 iso_creds.networks_admin_client,128 'create_network',129 return_value={'network': {'id': id, 'name': name}}))130 return net_fix131 def _mock_subnet_create(self, iso_creds, id, name):132 subnet_fix = self.useFixture(mockpatch.PatchObject(133 iso_creds.subnets_admin_client,134 'create_subnet',135 return_value={'subnet': {'id': id, 'name': name}}))136 return subnet_fix137 def _mock_router_create(self, id, name):138 router_fix = self.useFixture(mockpatch.PatchObject(139 json_network_client.NetworkClient,140 'create_router',141 return_value={'router': {'id': id, 'name': name}}))142 return router_fix143 @mock.patch('tempest_lib.common.rest_client.RestClient')144 def test_primary_creds(self, MockRestClient):145 cfg.CONF.set_default('neutron', False, 'service_available')146 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)147 self._mock_assign_user_role()148 self._mock_list_role()149 self._mock_tenant_create('1234', 'fake_prim_tenant')150 self._mock_user_create('1234', 'fake_prim_user')151 primary_creds = creds.get_primary_creds()152 self.assertEqual(primary_creds.username, 'fake_prim_user')153 self.assertEqual(primary_creds.tenant_name, 'fake_prim_tenant')154 # Verify IDs155 self.assertEqual(primary_creds.tenant_id, '1234')156 self.assertEqual(primary_creds.user_id, '1234')157 @mock.patch('tempest_lib.common.rest_client.RestClient')158 def test_admin_creds(self, MockRestClient):159 cfg.CONF.set_default('neutron', False, 'service_available')160 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)161 self._mock_list_roles('1234', 'admin')162 self._mock_user_create('1234', 'fake_admin_user')163 self._mock_tenant_create('1234', 'fake_admin_tenant')164 user_mock = mock.patch.object(json_roles_client.RolesClient,165 'assign_user_role')166 user_mock.start()167 self.addCleanup(user_mock.stop)168 with mock.patch.object(json_roles_client.RolesClient,169 'assign_user_role') as user_mock:170 admin_creds = creds.get_admin_creds()171 user_mock.assert_has_calls([172 mock.call('1234', '1234', '1234')])173 self.assertEqual(admin_creds.username, 'fake_admin_user')174 self.assertEqual(admin_creds.tenant_name, 'fake_admin_tenant')175 # Verify IDs176 self.assertEqual(admin_creds.tenant_id, '1234')177 self.assertEqual(admin_creds.user_id, '1234')178 @mock.patch('tempest_lib.common.rest_client.RestClient')179 def test_role_creds(self, MockRestClient):180 cfg.CONF.set_default('neutron', False, 'service_available')181 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)182 self._mock_list_2_roles()183 self._mock_user_create('1234', 'fake_role_user')184 self._mock_tenant_create('1234', 'fake_role_tenant')185 user_mock = mock.patch.object(json_roles_client.RolesClient,186 'assign_user_role')187 user_mock.start()188 self.addCleanup(user_mock.stop)189 with mock.patch.object(json_roles_client.RolesClient,190 'assign_user_role') as user_mock:191 role_creds = creds.get_creds_by_roles(192 roles=['role1', 'role2'])193 calls = user_mock.mock_calls194 # Assert that the role creation is called with the 2 specified roles195 self.assertEqual(len(calls), 2)196 args = map(lambda x: x[1], calls)197 args = list(args)198 self.assertIn(('1234', '1234', '1234'), args)199 self.assertIn(('1234', '1234', '12345'), args)200 self.assertEqual(role_creds.username, 'fake_role_user')201 self.assertEqual(role_creds.tenant_name, 'fake_role_tenant')202 # Verify IDs203 self.assertEqual(role_creds.tenant_id, '1234')204 self.assertEqual(role_creds.user_id, '1234')205 @mock.patch('tempest_lib.common.rest_client.RestClient')206 def test_all_cred_cleanup(self, MockRestClient):207 cfg.CONF.set_default('neutron', False, 'service_available')208 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)209 self._mock_assign_user_role()210 self._mock_list_role()211 self._mock_tenant_create('1234', 'fake_prim_tenant')212 self._mock_user_create('1234', 'fake_prim_user')213 creds.get_primary_creds()214 self._mock_tenant_create('12345', 'fake_alt_tenant')215 self._mock_user_create('12345', 'fake_alt_user')216 creds.get_alt_creds()217 self._mock_tenant_create('123456', 'fake_admin_tenant')218 self._mock_user_create('123456', 'fake_admin_user')219 self._mock_list_roles('123456', 'admin')220 creds.get_admin_creds()221 user_mock = self.patch(222 'tempest.services.identity.v2.json.users_client.'223 'UsersClient.delete_user')224 tenant_mock = self.patch(225 'tempest.services.identity.v2.json.tenants_client.'226 'TenantsClient.delete_tenant')227 creds.clear_creds()228 # Verify user delete calls229 calls = user_mock.mock_calls230 self.assertEqual(len(calls), 3)231 args = map(lambda x: x[1][0], calls)232 args = list(args)233 self.assertIn('1234', args)234 self.assertIn('12345', args)235 self.assertIn('123456', args)236 # Verify tenant delete calls237 calls = tenant_mock.mock_calls238 self.assertEqual(len(calls), 3)239 args = map(lambda x: x[1][0], calls)240 args = list(args)241 self.assertIn('1234', args)242 self.assertIn('12345', args)243 self.assertIn('123456', args)244 @mock.patch('tempest_lib.common.rest_client.RestClient')245 def test_alt_creds(self, MockRestClient):246 cfg.CONF.set_default('neutron', False, 'service_available')247 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)248 self._mock_assign_user_role()249 self._mock_list_role()250 self._mock_user_create('1234', 'fake_alt_user')251 self._mock_tenant_create('1234', 'fake_alt_tenant')252 alt_creds = creds.get_alt_creds()253 self.assertEqual(alt_creds.username, 'fake_alt_user')254 self.assertEqual(alt_creds.tenant_name, 'fake_alt_tenant')255 # Verify IDs256 self.assertEqual(alt_creds.tenant_id, '1234')257 self.assertEqual(alt_creds.user_id, '1234')258 @mock.patch('tempest_lib.common.rest_client.RestClient')259 def test_no_network_creation_with_config_set(self, MockRestClient):260 cfg.CONF.set_default('create_isolated_networks', False, group='auth')261 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)262 self._mock_assign_user_role()263 self._mock_list_role()264 self._mock_user_create('1234', 'fake_prim_user')265 self._mock_tenant_create('1234', 'fake_prim_tenant')266 net = mock.patch.object(creds.networks_admin_client,267 'delete_network')268 net_mock = net.start()269 subnet = mock.patch.object(creds.subnets_admin_client,270 'delete_subnet')271 subnet_mock = subnet.start()272 router = mock.patch.object(creds.network_admin_client,273 'delete_router')274 router_mock = router.start()275 primary_creds = creds.get_primary_creds()276 self.assertEqual(net_mock.mock_calls, [])277 self.assertEqual(subnet_mock.mock_calls, [])278 self.assertEqual(router_mock.mock_calls, [])279 network = primary_creds.network280 subnet = primary_creds.subnet281 router = primary_creds.router282 self.assertIsNone(network)283 self.assertIsNone(subnet)284 self.assertIsNone(router)285 @mock.patch('tempest_lib.common.rest_client.RestClient')286 def test_network_creation(self, MockRestClient):287 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)288 self._mock_assign_user_role()289 self._mock_list_role()290 self._mock_user_create('1234', 'fake_prim_user')291 self._mock_tenant_create('1234', 'fake_prim_tenant')292 self._mock_network_create(creds, '1234', 'fake_net')293 self._mock_subnet_create(creds, '1234', 'fake_subnet')294 self._mock_router_create('1234', 'fake_router')295 router_interface_mock = self.patch(296 'tempest.services.network.json.network_client.NetworkClient.'297 'add_router_interface_with_subnet_id')298 primary_creds = creds.get_primary_creds()299 router_interface_mock.assert_called_once_with('1234', '1234')300 network = primary_creds.network301 subnet = primary_creds.subnet302 router = primary_creds.router303 self.assertEqual(network['id'], '1234')304 self.assertEqual(network['name'], 'fake_net')305 self.assertEqual(subnet['id'], '1234')306 self.assertEqual(subnet['name'], 'fake_subnet')307 self.assertEqual(router['id'], '1234')308 self.assertEqual(router['name'], 'fake_router')309 @mock.patch('tempest_lib.common.rest_client.RestClient')310 def test_network_cleanup(self, MockRestClient):311 def side_effect(**args):312 return {"security_groups": [{"tenant_id": args['tenant_id'],313 "name": args['name'],314 "description": args['name'],315 "security_group_rules": [],316 "id": "sg-%s" % args['tenant_id']}]}317 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)318 # Create primary tenant and network319 self._mock_assign_user_role()320 self._mock_list_role()321 self._mock_user_create('1234', 'fake_prim_user')322 self._mock_tenant_create('1234', 'fake_prim_tenant')323 self._mock_network_create(creds, '1234', 'fake_net')324 self._mock_subnet_create(creds, '1234', 'fake_subnet')325 self._mock_router_create('1234', 'fake_router')326 router_interface_mock = self.patch(327 'tempest.services.network.json.network_client.NetworkClient.'328 'add_router_interface_with_subnet_id')329 creds.get_primary_creds()330 router_interface_mock.assert_called_once_with('1234', '1234')331 router_interface_mock.reset_mock()332 # Create alternate tenant and network333 self._mock_user_create('12345', 'fake_alt_user')334 self._mock_tenant_create('12345', 'fake_alt_tenant')335 self._mock_network_create(creds, '12345', 'fake_alt_net')336 self._mock_subnet_create(creds, '12345', 'fake_alt_subnet')337 self._mock_router_create('12345', 'fake_alt_router')338 creds.get_alt_creds()339 router_interface_mock.assert_called_once_with('12345', '12345')340 router_interface_mock.reset_mock()341 # Create admin tenant and networks342 self._mock_user_create('123456', 'fake_admin_user')343 self._mock_tenant_create('123456', 'fake_admin_tenant')344 self._mock_network_create(creds, '123456', 'fake_admin_net')345 self._mock_subnet_create(creds, '123456', 'fake_admin_subnet')346 self._mock_router_create('123456', 'fake_admin_router')347 self._mock_list_roles('123456', 'admin')348 creds.get_admin_creds()349 self.patch('tempest.services.identity.v2.json.users_client.'350 'UsersClient.delete_user')351 self.patch('tempest.services.identity.v2.json.tenants_client.'352 'TenantsClient.delete_tenant')353 net = mock.patch.object(creds.networks_admin_client,354 'delete_network')355 net_mock = net.start()356 subnet = mock.patch.object(creds.subnets_admin_client,357 'delete_subnet')358 subnet_mock = subnet.start()359 router = mock.patch.object(creds.network_admin_client,360 'delete_router')361 router_mock = router.start()362 remove_router_interface_mock = self.patch(363 'tempest.services.network.json.network_client.NetworkClient.'364 'remove_router_interface_with_subnet_id')365 return_values = ({'status': 200}, {'ports': []})366 port_list_mock = mock.patch.object(creds.ports_admin_client,367 'list_ports',368 return_value=return_values)369 port_list_mock.start()370 secgroup_list_mock = mock.patch.object(371 creds.security_groups_admin_client,372 'list_security_groups',373 side_effect=side_effect)374 secgroup_list_mock.start()375 return_values = (fake_http.fake_httplib({}, status=204), {})376 remove_secgroup_mock = self.patch(377 'tempest.services.network.json.security_groups_client.'378 'SecurityGroupsClient.delete', return_value=return_values)379 creds.clear_creds()380 # Verify default security group delete381 calls = remove_secgroup_mock.mock_calls382 self.assertEqual(len(calls), 3)383 args = map(lambda x: x[1][0], calls)384 args = list(args)385 self.assertIn('v2.0/security-groups/sg-1234', args)386 self.assertIn('v2.0/security-groups/sg-12345', args)387 self.assertIn('v2.0/security-groups/sg-123456', args)388 # Verify remove router interface calls389 calls = remove_router_interface_mock.mock_calls390 self.assertEqual(len(calls), 3)391 args = map(lambda x: x[1], calls)392 args = list(args)393 self.assertIn(('1234', '1234'), args)394 self.assertIn(('12345', '12345'), args)395 self.assertIn(('123456', '123456'), args)396 # Verify network delete calls397 calls = net_mock.mock_calls398 self.assertEqual(len(calls), 3)399 args = map(lambda x: x[1][0], calls)400 args = list(args)401 self.assertIn('1234', args)402 self.assertIn('12345', args)403 self.assertIn('123456', args)404 # Verify subnet delete calls405 calls = subnet_mock.mock_calls406 self.assertEqual(len(calls), 3)407 args = map(lambda x: x[1][0], calls)408 args = list(args)409 self.assertIn('1234', args)410 self.assertIn('12345', args)411 self.assertIn('123456', args)412 # Verify router delete calls413 calls = router_mock.mock_calls414 self.assertEqual(len(calls), 3)415 args = map(lambda x: x[1][0], calls)416 args = list(args)417 self.assertIn('1234', args)418 self.assertIn('12345', args)419 self.assertIn('123456', args)420 @mock.patch('tempest_lib.common.rest_client.RestClient')421 def test_network_alt_creation(self, MockRestClient):422 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)423 self._mock_assign_user_role()424 self._mock_list_role()425 self._mock_user_create('1234', 'fake_alt_user')426 self._mock_tenant_create('1234', 'fake_alt_tenant')427 self._mock_network_create(creds, '1234', 'fake_alt_net')428 self._mock_subnet_create(creds, '1234', 'fake_alt_subnet')429 self._mock_router_create('1234', 'fake_alt_router')430 router_interface_mock = self.patch(431 'tempest.services.network.json.network_client.NetworkClient.'432 'add_router_interface_with_subnet_id')433 alt_creds = creds.get_alt_creds()434 router_interface_mock.assert_called_once_with('1234', '1234')435 network = alt_creds.network436 subnet = alt_creds.subnet437 router = alt_creds.router438 self.assertEqual(network['id'], '1234')439 self.assertEqual(network['name'], 'fake_alt_net')440 self.assertEqual(subnet['id'], '1234')441 self.assertEqual(subnet['name'], 'fake_alt_subnet')442 self.assertEqual(router['id'], '1234')443 self.assertEqual(router['name'], 'fake_alt_router')444 @mock.patch('tempest_lib.common.rest_client.RestClient')445 def test_network_admin_creation(self, MockRestClient):446 creds = dynamic_creds.DynamicCredentialProvider(**self.fixed_params)447 self._mock_assign_user_role()448 self._mock_user_create('1234', 'fake_admin_user')449 self._mock_tenant_create('1234', 'fake_admin_tenant')450 self._mock_network_create(creds, '1234', 'fake_admin_net')451 self._mock_subnet_create(creds, '1234', 'fake_admin_subnet')452 self._mock_router_create('1234', 'fake_admin_router')453 router_interface_mock = self.patch(454 'tempest.services.network.json.network_client.NetworkClient.'455 'add_router_interface_with_subnet_id')456 self._mock_list_roles('123456', 'admin')457 admin_creds = creds.get_admin_creds()458 router_interface_mock.assert_called_once_with('1234', '1234')459 network = admin_creds.network460 subnet = admin_creds.subnet461 router = admin_creds.router462 self.assertEqual(network['id'], '1234')463 self.assertEqual(network['name'], 'fake_admin_net')464 self.assertEqual(subnet['id'], '1234')465 self.assertEqual(subnet['name'], 'fake_admin_subnet')466 self.assertEqual(router['id'], '1234')467 self.assertEqual(router['name'], 'fake_admin_router')468 @mock.patch('tempest_lib.common.rest_client.RestClient')469 def test_no_network_resources(self, MockRestClient):470 net_dict = {471 'network': False,472 'router': False,473 'subnet': False,474 'dhcp': False,475 }476 creds = dynamic_creds.DynamicCredentialProvider(477 network_resources=net_dict,478 **self.fixed_params)479 self._mock_assign_user_role()480 self._mock_list_role()481 self._mock_user_create('1234', 'fake_prim_user')482 self._mock_tenant_create('1234', 'fake_prim_tenant')483 net = mock.patch.object(creds.networks_admin_client,484 'delete_network')485 net_mock = net.start()486 subnet = mock.patch.object(creds.subnets_admin_client,487 'delete_subnet')488 subnet_mock = subnet.start()489 router = mock.patch.object(creds.network_admin_client,490 'delete_router')491 router_mock = router.start()492 primary_creds = creds.get_primary_creds()493 self.assertEqual(net_mock.mock_calls, [])494 self.assertEqual(subnet_mock.mock_calls, [])495 self.assertEqual(router_mock.mock_calls, [])496 network = primary_creds.network497 subnet = primary_creds.subnet498 router = primary_creds.router499 self.assertIsNone(network)500 self.assertIsNone(subnet)501 self.assertIsNone(router)502 @mock.patch('tempest_lib.common.rest_client.RestClient')503 def test_router_without_network(self, MockRestClient):504 net_dict = {505 'network': False,506 'router': True,507 'subnet': False,508 'dhcp': False,509 }510 creds = dynamic_creds.DynamicCredentialProvider(511 network_resources=net_dict,512 **self.fixed_params)513 self._mock_assign_user_role()514 self._mock_list_role()515 self._mock_user_create('1234', 'fake_prim_user')516 self._mock_tenant_create('1234', 'fake_prim_tenant')517 self.assertRaises(exceptions.InvalidConfiguration,518 creds.get_primary_creds)519 @mock.patch('tempest_lib.common.rest_client.RestClient')520 def test_subnet_without_network(self, MockRestClient):521 net_dict = {522 'network': False,523 'router': False,524 'subnet': True,525 'dhcp': False,526 }527 creds = dynamic_creds.DynamicCredentialProvider(528 network_resources=net_dict,529 **self.fixed_params)530 self._mock_assign_user_role()531 self._mock_list_role()532 self._mock_user_create('1234', 'fake_prim_user')533 self._mock_tenant_create('1234', 'fake_prim_tenant')534 self.assertRaises(exceptions.InvalidConfiguration,535 creds.get_primary_creds)536 @mock.patch('tempest_lib.common.rest_client.RestClient')537 def test_dhcp_without_subnet(self, MockRestClient):538 net_dict = {539 'network': False,540 'router': False,541 'subnet': False,542 'dhcp': True,543 }544 creds = dynamic_creds.DynamicCredentialProvider(545 network_resources=net_dict,546 **self.fixed_params)547 self._mock_assign_user_role()548 self._mock_list_role()549 self._mock_user_create('1234', 'fake_prim_user')550 self._mock_tenant_create('1234', 'fake_prim_tenant')551 self.assertRaises(exceptions.InvalidConfiguration,...
test_tenant_isolation.py
Source:test_tenant_isolation.py
...49 'create_user',50 return_value=(service_client.ResponseBody51 (200, {'id': id, 'name': name}))))52 return user_fix53 def _mock_tenant_create(self, id, name):54 tenant_fix = self.useFixture(mockpatch.PatchObject(55 json_iden_client.IdentityClient,56 'create_tenant',57 return_value=(service_client.ResponseBody58 (200, {'id': id, 'name': name}))))59 return tenant_fix60 def _mock_list_roles(self, id, name):61 roles_fix = self.useFixture(mockpatch.PatchObject(62 json_iden_client.IdentityClient,63 'list_roles',64 return_value=(service_client.ResponseBodyList65 (200,66 [{'id': id, 'name': name},67 {'id': '1', 'name': 'FakeRole'}]))))68 return roles_fix69 def _mock_list_2_roles(self):70 roles_fix = self.useFixture(mockpatch.PatchObject(71 json_iden_client.IdentityClient,72 'list_roles',73 return_value=(service_client.ResponseBodyList74 (200,75 [{'id': '1234', 'name': 'role1'},76 {'id': '1', 'name': 'FakeRole'},77 {'id': '12345', 'name': 'role2'}]))))78 return roles_fix79 def _mock_assign_user_role(self):80 tenant_fix = self.useFixture(mockpatch.PatchObject(81 json_iden_client.IdentityClient,82 'assign_user_role',83 return_value=(service_client.ResponseBody84 (200, {}))))85 return tenant_fix86 def _mock_list_role(self):87 roles_fix = self.useFixture(mockpatch.PatchObject(88 json_iden_client.IdentityClient,89 'list_roles',90 return_value=(service_client.ResponseBodyList91 (200, [{'id': '1', 'name': 'FakeRole'}]))))92 return roles_fix93 def _mock_list_ec2_credentials(self, user_id, tenant_id):94 ec2_creds_fix = self.useFixture(mockpatch.PatchObject(95 json_iden_client.IdentityClient,96 'list_user_ec2_credentials',97 return_value=(service_client.ResponseBodyList98 (200, [{'access': 'fake_access',99 'secret': 'fake_secret',100 'tenant_id': tenant_id,101 'user_id': user_id,102 'trust_id': None}]))))103 return ec2_creds_fix104 def _mock_network_create(self, iso_creds, id, name):105 net_fix = self.useFixture(mockpatch.PatchObject(106 iso_creds.network_admin_client,107 'create_network',108 return_value={'network': {'id': id, 'name': name}}))109 return net_fix110 def _mock_subnet_create(self, iso_creds, id, name):111 subnet_fix = self.useFixture(mockpatch.PatchObject(112 iso_creds.network_admin_client,113 'create_subnet',114 return_value={'subnet': {'id': id, 'name': name}}))115 return subnet_fix116 def _mock_router_create(self, id, name):117 router_fix = self.useFixture(mockpatch.PatchObject(118 json_network_client.NetworkClient,119 'create_router',120 return_value={'router': {'id': id, 'name': name}}))121 return router_fix122 @mock.patch('tempest_lib.common.rest_client.RestClient')123 def test_primary_creds(self, MockRestClient):124 cfg.CONF.set_default('neutron', False, 'service_available')125 iso_creds = isolated_creds.IsolatedCreds(name='test class')126 self._mock_assign_user_role()127 self._mock_list_role()128 self._mock_tenant_create('1234', 'fake_prim_tenant')129 self._mock_user_create('1234', 'fake_prim_user')130 primary_creds = iso_creds.get_primary_creds()131 self.assertEqual(primary_creds.username, 'fake_prim_user')132 self.assertEqual(primary_creds.tenant_name, 'fake_prim_tenant')133 # Verify IDs134 self.assertEqual(primary_creds.tenant_id, '1234')135 self.assertEqual(primary_creds.user_id, '1234')136 @mock.patch('tempest_lib.common.rest_client.RestClient')137 def test_admin_creds(self, MockRestClient):138 cfg.CONF.set_default('neutron', False, 'service_available')139 iso_creds = isolated_creds.IsolatedCreds(name='test class')140 self._mock_list_roles('1234', 'admin')141 self._mock_user_create('1234', 'fake_admin_user')142 self._mock_tenant_create('1234', 'fake_admin_tenant')143 user_mock = mock.patch.object(json_iden_client.IdentityClient,144 'assign_user_role')145 user_mock.start()146 self.addCleanup(user_mock.stop)147 with mock.patch.object(json_iden_client.IdentityClient,148 'assign_user_role') as user_mock:149 admin_creds = iso_creds.get_admin_creds()150 user_mock.assert_has_calls([151 mock.call('1234', '1234', '1234')])152 self.assertEqual(admin_creds.username, 'fake_admin_user')153 self.assertEqual(admin_creds.tenant_name, 'fake_admin_tenant')154 # Verify IDs155 self.assertEqual(admin_creds.tenant_id, '1234')156 self.assertEqual(admin_creds.user_id, '1234')157 @mock.patch('tempest_lib.common.rest_client.RestClient')158 def test_role_creds(self, MockRestClient):159 cfg.CONF.set_default('neutron', False, 'service_available')160 iso_creds = isolated_creds.IsolatedCreds('v2', 'test class')161 self._mock_list_2_roles()162 self._mock_user_create('1234', 'fake_role_user')163 self._mock_tenant_create('1234', 'fake_role_tenant')164 user_mock = mock.patch.object(json_iden_client.IdentityClient,165 'assign_user_role')166 user_mock.start()167 self.addCleanup(user_mock.stop)168 with mock.patch.object(json_iden_client.IdentityClient,169 'assign_user_role') as user_mock:170 role_creds = iso_creds.get_creds_by_roles(roles=['role1', 'role2'])171 calls = user_mock.mock_calls172 # Assert that the role creation is called with the 2 specified roles173 self.assertEqual(len(calls), 2)174 args = map(lambda x: x[1], calls)175 args = list(args)176 self.assertIn(('1234', '1234', '1234'), args)177 self.assertIn(('1234', '1234', '12345'), args)178 self.assertEqual(role_creds.username, 'fake_role_user')179 self.assertEqual(role_creds.tenant_name, 'fake_role_tenant')180 # Verify IDs181 self.assertEqual(role_creds.tenant_id, '1234')182 self.assertEqual(role_creds.user_id, '1234')183 @mock.patch('tempest_lib.common.rest_client.RestClient')184 def test_all_cred_cleanup(self, MockRestClient):185 cfg.CONF.set_default('neutron', False, 'service_available')186 iso_creds = isolated_creds.IsolatedCreds(name='test class')187 self._mock_assign_user_role()188 self._mock_list_role()189 self._mock_tenant_create('1234', 'fake_prim_tenant')190 self._mock_user_create('1234', 'fake_prim_user')191 iso_creds.get_primary_creds()192 self._mock_tenant_create('12345', 'fake_alt_tenant')193 self._mock_user_create('12345', 'fake_alt_user')194 iso_creds.get_alt_creds()195 self._mock_tenant_create('123456', 'fake_admin_tenant')196 self._mock_user_create('123456', 'fake_admin_user')197 self._mock_list_roles('123456', 'admin')198 iso_creds.get_admin_creds()199 user_mock = self.patch(200 'tempest.services.identity.v2.json.identity_client.'201 'IdentityClient.delete_user')202 tenant_mock = self.patch(203 'tempest.services.identity.v2.json.identity_client.'204 'IdentityClient.delete_tenant')205 iso_creds.clear_isolated_creds()206 # Verify user delete calls207 calls = user_mock.mock_calls208 self.assertEqual(len(calls), 3)209 args = map(lambda x: x[1][0], calls)210 args = list(args)211 self.assertIn('1234', args)212 self.assertIn('12345', args)213 self.assertIn('123456', args)214 # Verify tenant delete calls215 calls = tenant_mock.mock_calls216 self.assertEqual(len(calls), 3)217 args = map(lambda x: x[1][0], calls)218 args = list(args)219 self.assertIn('1234', args)220 self.assertIn('12345', args)221 self.assertIn('123456', args)222 @mock.patch('tempest_lib.common.rest_client.RestClient')223 def test_alt_creds(self, MockRestClient):224 cfg.CONF.set_default('neutron', False, 'service_available')225 iso_creds = isolated_creds.IsolatedCreds(name='test class')226 self._mock_assign_user_role()227 self._mock_list_role()228 self._mock_user_create('1234', 'fake_alt_user')229 self._mock_tenant_create('1234', 'fake_alt_tenant')230 alt_creds = iso_creds.get_alt_creds()231 self.assertEqual(alt_creds.username, 'fake_alt_user')232 self.assertEqual(alt_creds.tenant_name, 'fake_alt_tenant')233 # Verify IDs234 self.assertEqual(alt_creds.tenant_id, '1234')235 self.assertEqual(alt_creds.user_id, '1234')236 @mock.patch('tempest_lib.common.rest_client.RestClient')237 def test_no_network_creation_with_config_set(self, MockRestClient):238 cfg.CONF.set_default('create_isolated_networks', False, group='auth')239 iso_creds = isolated_creds.IsolatedCreds(name='test class')240 self._mock_assign_user_role()241 self._mock_list_role()242 self._mock_user_create('1234', 'fake_prim_user')243 self._mock_tenant_create('1234', 'fake_prim_tenant')244 net = mock.patch.object(iso_creds.network_admin_client,245 'delete_network')246 net_mock = net.start()247 subnet = mock.patch.object(iso_creds.network_admin_client,248 'delete_subnet')249 subnet_mock = subnet.start()250 router = mock.patch.object(iso_creds.network_admin_client,251 'delete_router')252 router_mock = router.start()253 primary_creds = iso_creds.get_primary_creds()254 self.assertEqual(net_mock.mock_calls, [])255 self.assertEqual(subnet_mock.mock_calls, [])256 self.assertEqual(router_mock.mock_calls, [])257 network = primary_creds.network258 subnet = primary_creds.subnet259 router = primary_creds.router260 self.assertIsNone(network)261 self.assertIsNone(subnet)262 self.assertIsNone(router)263 @mock.patch('tempest_lib.common.rest_client.RestClient')264 def test_network_creation(self, MockRestClient):265 iso_creds = isolated_creds.IsolatedCreds(name='test class')266 self._mock_assign_user_role()267 self._mock_list_role()268 self._mock_user_create('1234', 'fake_prim_user')269 self._mock_tenant_create('1234', 'fake_prim_tenant')270 self._mock_network_create(iso_creds, '1234', 'fake_net')271 self._mock_subnet_create(iso_creds, '1234', 'fake_subnet')272 self._mock_router_create('1234', 'fake_router')273 router_interface_mock = self.patch(274 'tempest.services.network.json.network_client.NetworkClient.'275 'add_router_interface_with_subnet_id')276 primary_creds = iso_creds.get_primary_creds()277 router_interface_mock.called_once_with('1234', '1234')278 network = primary_creds.network279 subnet = primary_creds.subnet280 router = primary_creds.router281 self.assertEqual(network['id'], '1234')282 self.assertEqual(network['name'], 'fake_net')283 self.assertEqual(subnet['id'], '1234')284 self.assertEqual(subnet['name'], 'fake_subnet')285 self.assertEqual(router['id'], '1234')286 self.assertEqual(router['name'], 'fake_router')287 @mock.patch('tempest_lib.common.rest_client.RestClient')288 def test_network_cleanup(self, MockRestClient):289 def side_effect(**args):290 return {"security_groups": [{"tenant_id": args['tenant_id'],291 "name": args['name'],292 "description": args['name'],293 "security_group_rules": [],294 "id": "sg-%s" % args['tenant_id']}]}295 iso_creds = isolated_creds.IsolatedCreds(name='test class')296 # Create primary tenant and network297 self._mock_assign_user_role()298 self._mock_list_role()299 self._mock_user_create('1234', 'fake_prim_user')300 self._mock_tenant_create('1234', 'fake_prim_tenant')301 self._mock_network_create(iso_creds, '1234', 'fake_net')302 self._mock_subnet_create(iso_creds, '1234', 'fake_subnet')303 self._mock_router_create('1234', 'fake_router')304 router_interface_mock = self.patch(305 'tempest.services.network.json.network_client.NetworkClient.'306 'add_router_interface_with_subnet_id')307 iso_creds.get_primary_creds()308 router_interface_mock.called_once_with('1234', '1234')309 router_interface_mock.reset_mock()310 # Create alternate tenant and network311 self._mock_user_create('12345', 'fake_alt_user')312 self._mock_tenant_create('12345', 'fake_alt_tenant')313 self._mock_network_create(iso_creds, '12345', 'fake_alt_net')314 self._mock_subnet_create(iso_creds, '12345',315 'fake_alt_subnet')316 self._mock_router_create('12345', 'fake_alt_router')317 iso_creds.get_alt_creds()318 router_interface_mock.called_once_with('12345', '12345')319 router_interface_mock.reset_mock()320 # Create admin tenant and networks321 self._mock_user_create('123456', 'fake_admin_user')322 self._mock_tenant_create('123456', 'fake_admin_tenant')323 self._mock_network_create(iso_creds, '123456',324 'fake_admin_net')325 self._mock_subnet_create(iso_creds, '123456',326 'fake_admin_subnet')327 self._mock_router_create('123456', 'fake_admin_router')328 self._mock_list_roles('123456', 'admin')329 iso_creds.get_admin_creds()330 self.patch('tempest.services.identity.v2.json.identity_client.'331 'IdentityClient.delete_user')332 self.patch('tempest.services.identity.v2.json.identity_client.'333 'IdentityClient.delete_tenant')334 net = mock.patch.object(iso_creds.network_admin_client,335 'delete_network')336 net_mock = net.start()337 subnet = mock.patch.object(iso_creds.network_admin_client,338 'delete_subnet')339 subnet_mock = subnet.start()340 router = mock.patch.object(iso_creds.network_admin_client,341 'delete_router')342 router_mock = router.start()343 remove_router_interface_mock = self.patch(344 'tempest.services.network.json.network_client.NetworkClient.'345 'remove_router_interface_with_subnet_id')346 return_values = ({'status': 200}, {'ports': []})347 port_list_mock = mock.patch.object(iso_creds.network_admin_client,348 'list_ports',349 return_value=return_values)350 port_list_mock.start()351 secgroup_list_mock = mock.patch.object(iso_creds.network_admin_client,352 'list_security_groups',353 side_effect=side_effect)354 secgroup_list_mock.start()355 return_values = (fake_http.fake_httplib({}, status=204), {})356 remove_secgroup_mock = self.patch(357 'tempest.services.network.json.network_client.'358 'NetworkClient.delete', return_value=return_values)359 iso_creds.clear_isolated_creds()360 # Verify default security group delete361 calls = remove_secgroup_mock.mock_calls362 self.assertEqual(len(calls), 3)363 args = map(lambda x: x[1][0], calls)364 args = list(args)365 self.assertIn('v2.0/security-groups/sg-1234', args)366 self.assertIn('v2.0/security-groups/sg-12345', args)367 self.assertIn('v2.0/security-groups/sg-123456', args)368 # Verify remove router interface calls369 calls = remove_router_interface_mock.mock_calls370 self.assertEqual(len(calls), 3)371 args = map(lambda x: x[1], calls)372 args = list(args)373 self.assertIn(('1234', '1234'), args)374 self.assertIn(('12345', '12345'), args)375 self.assertIn(('123456', '123456'), args)376 # Verify network delete calls377 calls = net_mock.mock_calls378 self.assertEqual(len(calls), 3)379 args = map(lambda x: x[1][0], calls)380 args = list(args)381 self.assertIn('1234', args)382 self.assertIn('12345', args)383 self.assertIn('123456', args)384 # Verify subnet delete calls385 calls = subnet_mock.mock_calls386 self.assertEqual(len(calls), 3)387 args = map(lambda x: x[1][0], calls)388 args = list(args)389 self.assertIn('1234', args)390 self.assertIn('12345', args)391 self.assertIn('123456', args)392 # Verify router delete calls393 calls = router_mock.mock_calls394 self.assertEqual(len(calls), 3)395 args = map(lambda x: x[1][0], calls)396 args = list(args)397 self.assertIn('1234', args)398 self.assertIn('12345', args)399 self.assertIn('123456', args)400 @mock.patch('tempest_lib.common.rest_client.RestClient')401 def test_network_alt_creation(self, MockRestClient):402 iso_creds = isolated_creds.IsolatedCreds(name='test class')403 self._mock_assign_user_role()404 self._mock_list_role()405 self._mock_user_create('1234', 'fake_alt_user')406 self._mock_tenant_create('1234', 'fake_alt_tenant')407 self._mock_network_create(iso_creds, '1234', 'fake_alt_net')408 self._mock_subnet_create(iso_creds, '1234', 'fake_alt_subnet')409 self._mock_router_create('1234', 'fake_alt_router')410 router_interface_mock = self.patch(411 'tempest.services.network.json.network_client.NetworkClient.'412 'add_router_interface_with_subnet_id')413 alt_creds = iso_creds.get_alt_creds()414 router_interface_mock.called_once_with('1234', '1234')415 network = alt_creds.network416 subnet = alt_creds.subnet417 router = alt_creds.router418 self.assertEqual(network['id'], '1234')419 self.assertEqual(network['name'], 'fake_alt_net')420 self.assertEqual(subnet['id'], '1234')421 self.assertEqual(subnet['name'], 'fake_alt_subnet')422 self.assertEqual(router['id'], '1234')423 self.assertEqual(router['name'], 'fake_alt_router')424 @mock.patch('tempest_lib.common.rest_client.RestClient')425 def test_network_admin_creation(self, MockRestClient):426 iso_creds = isolated_creds.IsolatedCreds(name='test class')427 self._mock_assign_user_role()428 self._mock_user_create('1234', 'fake_admin_user')429 self._mock_tenant_create('1234', 'fake_admin_tenant')430 self._mock_network_create(iso_creds, '1234', 'fake_admin_net')431 self._mock_subnet_create(iso_creds, '1234', 'fake_admin_subnet')432 self._mock_router_create('1234', 'fake_admin_router')433 router_interface_mock = self.patch(434 'tempest.services.network.json.network_client.NetworkClient.'435 'add_router_interface_with_subnet_id')436 self._mock_list_roles('123456', 'admin')437 admin_creds = iso_creds.get_admin_creds()438 router_interface_mock.called_once_with('1234', '1234')439 network = admin_creds.network440 subnet = admin_creds.subnet441 router = admin_creds.router442 self.assertEqual(network['id'], '1234')443 self.assertEqual(network['name'], 'fake_admin_net')444 self.assertEqual(subnet['id'], '1234')445 self.assertEqual(subnet['name'], 'fake_admin_subnet')446 self.assertEqual(router['id'], '1234')447 self.assertEqual(router['name'], 'fake_admin_router')448 @mock.patch('tempest_lib.common.rest_client.RestClient')449 def test_no_network_resources(self, MockRestClient):450 net_dict = {451 'network': False,452 'router': False,453 'subnet': False,454 'dhcp': False,455 }456 iso_creds = isolated_creds.IsolatedCreds(name='test class',457 network_resources=net_dict)458 self._mock_assign_user_role()459 self._mock_list_role()460 self._mock_user_create('1234', 'fake_prim_user')461 self._mock_tenant_create('1234', 'fake_prim_tenant')462 net = mock.patch.object(iso_creds.network_admin_client,463 'delete_network')464 net_mock = net.start()465 subnet = mock.patch.object(iso_creds.network_admin_client,466 'delete_subnet')467 subnet_mock = subnet.start()468 router = mock.patch.object(iso_creds.network_admin_client,469 'delete_router')470 router_mock = router.start()471 primary_creds = iso_creds.get_primary_creds()472 self.assertEqual(net_mock.mock_calls, [])473 self.assertEqual(subnet_mock.mock_calls, [])474 self.assertEqual(router_mock.mock_calls, [])475 network = primary_creds.network476 subnet = primary_creds.subnet477 router = primary_creds.router478 self.assertIsNone(network)479 self.assertIsNone(subnet)480 self.assertIsNone(router)481 @mock.patch('tempest_lib.common.rest_client.RestClient')482 def test_router_without_network(self, MockRestClient):483 net_dict = {484 'network': False,485 'router': True,486 'subnet': False,487 'dhcp': False,488 }489 iso_creds = isolated_creds.IsolatedCreds(name='test class',490 network_resources=net_dict)491 self._mock_assign_user_role()492 self._mock_list_role()493 self._mock_user_create('1234', 'fake_prim_user')494 self._mock_tenant_create('1234', 'fake_prim_tenant')495 self.assertRaises(exceptions.InvalidConfiguration,496 iso_creds.get_primary_creds)497 @mock.patch('tempest_lib.common.rest_client.RestClient')498 def test_subnet_without_network(self, MockRestClient):499 net_dict = {500 'network': False,501 'router': False,502 'subnet': True,503 'dhcp': False,504 }505 iso_creds = isolated_creds.IsolatedCreds(name='test class',506 network_resources=net_dict)507 self._mock_assign_user_role()508 self._mock_list_role()509 self._mock_user_create('1234', 'fake_prim_user')510 self._mock_tenant_create('1234', 'fake_prim_tenant')511 self.assertRaises(exceptions.InvalidConfiguration,512 iso_creds.get_primary_creds)513 @mock.patch('tempest_lib.common.rest_client.RestClient')514 def test_dhcp_without_subnet(self, MockRestClient):515 net_dict = {516 'network': False,517 'router': False,518 'subnet': False,519 'dhcp': True,520 }521 iso_creds = isolated_creds.IsolatedCreds(name='test class',522 network_resources=net_dict)523 self._mock_assign_user_role()524 self._mock_list_role()525 self._mock_user_create('1234', 'fake_prim_user')526 self._mock_tenant_create('1234', 'fake_prim_tenant')527 self.assertRaises(exceptions.InvalidConfiguration,...
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!!