How to use _mock_list_roles method in tempest

Best Python code snippet using tempest_python

test_tenant_isolation.py

Source:test_tenant_isolation.py Github

copy

Full Screen

...53 'create_tenant',54 return_value=({'status': 200},55 {'id': id, 'name': name})))56 return tenant_fix57 def _mock_list_roles(self, id, name):58 roles_fix = self.useFixture(mockpatch.PatchObject(59 json_iden_client.IdentityClientJSON,60 'list_roles',61 return_value=({'status': 200},62 [{'id': id, 'name': name},63 {'id': '1', 'name': 'FakeRole'}])))64 return roles_fix65 def _mock_assign_user_role(self):66 tenant_fix = self.useFixture(mockpatch.PatchObject(67 json_iden_client.IdentityClientJSON,68 'assign_user_role',69 return_value=({'status': 200},70 {})))71 return tenant_fix72 def _mock_list_role(self):73 roles_fix = self.useFixture(mockpatch.PatchObject(74 json_iden_client.IdentityClientJSON,75 'list_roles',76 return_value=({'status': 200},77 [{'id': '1', 'name': 'FakeRole'}])))78 return roles_fix79 def _mock_network_create(self, iso_creds, id, name):80 net_fix = self.useFixture(mockpatch.PatchObject(81 iso_creds.network_admin_client,82 'create_network',83 return_value={'network': {'id': id, 'name': name}}))84 return net_fix85 def _mock_subnet_create(self, iso_creds, id, name):86 subnet_fix = self.useFixture(mockpatch.PatchObject(87 iso_creds.network_admin_client,88 'create_subnet',89 return_value={'subnet': {'id': id, 'name': name}}))90 return subnet_fix91 def _mock_router_create(self, id, name):92 router_fix = self.useFixture(mockpatch.PatchObject(93 json_network_client.NetworkClientJSON,94 'create_router',95 return_value={'router': {'id': id, 'name': name}}))96 return router_fix97 @mock.patch('tempest.common.rest_client.RestClient')98 def test_primary_creds(self, MockRestClient):99 cfg.CONF.set_default('neutron', False, 'service_available')100 iso_creds = isolated_creds.IsolatedCreds('test class',101 password='fake_password')102 self._mock_assign_user_role()103 self._mock_list_role()104 self._mock_tenant_create('1234', 'fake_prim_tenant')105 self._mock_user_create('1234', 'fake_prim_user')106 primary_creds = iso_creds.get_primary_creds()107 self.assertEqual(primary_creds.username, 'fake_prim_user')108 self.assertEqual(primary_creds.tenant_name, 'fake_prim_tenant')109 # Verify IDs110 self.assertEqual(primary_creds.tenant_id, '1234')111 self.assertEqual(primary_creds.user_id, '1234')112 @mock.patch('tempest.common.rest_client.RestClient')113 def test_admin_creds(self, MockRestClient):114 cfg.CONF.set_default('neutron', False, 'service_available')115 iso_creds = isolated_creds.IsolatedCreds('test class',116 password='fake_password')117 self._mock_list_roles('1234', 'admin')118 self._mock_user_create('1234', 'fake_admin_user')119 self._mock_tenant_create('1234', 'fake_admin_tenant')120 user_mock = mock.patch.object(json_iden_client.IdentityClientJSON,121 'assign_user_role')122 user_mock.start()123 self.addCleanup(user_mock.stop)124 with mock.patch.object(json_iden_client.IdentityClientJSON,125 'assign_user_role') as user_mock:126 admin_creds = iso_creds.get_admin_creds()127 user_mock.assert_has_calls([128 mock.call('1234', '1234', '1'),129 mock.call('1234', '1234', '1234')])130 self.assertEqual(admin_creds.username, 'fake_admin_user')131 self.assertEqual(admin_creds.tenant_name, 'fake_admin_tenant')132 # Verify IDs133 self.assertEqual(admin_creds.tenant_id, '1234')134 self.assertEqual(admin_creds.user_id, '1234')135 @mock.patch('tempest.common.rest_client.RestClient')136 def test_all_cred_cleanup(self, MockRestClient):137 cfg.CONF.set_default('neutron', False, 'service_available')138 iso_creds = isolated_creds.IsolatedCreds('test class',139 password='fake_password')140 self._mock_assign_user_role()141 roles_fix = self._mock_list_role()142 tenant_fix = self._mock_tenant_create('1234', 'fake_prim_tenant')143 user_fix = self._mock_user_create('1234', 'fake_prim_user')144 iso_creds.get_primary_creds()145 tenant_fix.cleanUp()146 user_fix.cleanUp()147 tenant_fix = self._mock_tenant_create('12345', 'fake_alt_tenant')148 user_fix = self._mock_user_create('12345', 'fake_alt_user')149 iso_creds.get_alt_creds()150 tenant_fix.cleanUp()151 user_fix.cleanUp()152 roles_fix.cleanUp()153 tenant_fix = self._mock_tenant_create('123456', 'fake_admin_tenant')154 user_fix = self._mock_user_create('123456', 'fake_admin_user')155 self._mock_list_roles('123456', 'admin')156 iso_creds.get_admin_creds()157 user_mock = self.patch(158 'tempest.services.identity.json.identity_client.'159 'IdentityClientJSON.delete_user')160 tenant_mock = self.patch(161 'tempest.services.identity.json.identity_client.'162 'IdentityClientJSON.delete_tenant')163 iso_creds.clear_isolated_creds()164 # Verify user delete calls165 calls = user_mock.mock_calls166 self.assertEqual(len(calls), 3)167 args = map(lambda x: x[1][0], calls)168 self.assertIn('1234', args)169 self.assertIn('12345', args)170 self.assertIn('123456', args)171 # Verify tenant delete calls172 calls = tenant_mock.mock_calls173 self.assertEqual(len(calls), 3)174 args = map(lambda x: x[1][0], calls)175 self.assertIn('1234', args)176 self.assertIn('12345', args)177 self.assertIn('123456', args)178 @mock.patch('tempest.common.rest_client.RestClient')179 def test_alt_creds(self, MockRestClient):180 cfg.CONF.set_default('neutron', False, 'service_available')181 iso_creds = isolated_creds.IsolatedCreds('test class',182 password='fake_password')183 self._mock_assign_user_role()184 self._mock_list_role()185 self._mock_user_create('1234', 'fake_alt_user')186 self._mock_tenant_create('1234', 'fake_alt_tenant')187 alt_creds = iso_creds.get_alt_creds()188 self.assertEqual(alt_creds.username, 'fake_alt_user')189 self.assertEqual(alt_creds.tenant_name, 'fake_alt_tenant')190 # Verify IDs191 self.assertEqual(alt_creds.tenant_id, '1234')192 self.assertEqual(alt_creds.user_id, '1234')193 @mock.patch('tempest.common.rest_client.RestClient')194 def test_network_creation(self, MockRestClient):195 iso_creds = isolated_creds.IsolatedCreds('test class',196 password='fake_password')197 self._mock_assign_user_role()198 self._mock_list_role()199 self._mock_user_create('1234', 'fake_prim_user')200 self._mock_tenant_create('1234', 'fake_prim_tenant')201 self._mock_network_create(iso_creds, '1234', 'fake_net')202 self._mock_subnet_create(iso_creds, '1234', 'fake_subnet')203 self._mock_router_create('1234', 'fake_router')204 router_interface_mock = self.patch(205 'tempest.services.network.json.network_client.NetworkClientJSON.'206 'add_router_interface_with_subnet_id')207 iso_creds.get_primary_creds()208 router_interface_mock.called_once_with('1234', '1234')209 network = iso_creds.get_primary_network()210 subnet = iso_creds.get_primary_subnet()211 router = iso_creds.get_primary_router()212 self.assertEqual(network['id'], '1234')213 self.assertEqual(network['name'], 'fake_net')214 self.assertEqual(subnet['id'], '1234')215 self.assertEqual(subnet['name'], 'fake_subnet')216 self.assertEqual(router['id'], '1234')217 self.assertEqual(router['name'], 'fake_router')218 @mock.patch('tempest.common.rest_client.RestClient')219 def test_network_cleanup(self, MockRestClient):220 def side_effect(**args):221 return {"security_groups": [{"tenant_id": args['tenant_id'],222 "name": args['name'],223 "description": args['name'],224 "security_group_rules": [],225 "id": "sg-%s" % args['tenant_id']}]}226 iso_creds = isolated_creds.IsolatedCreds('test class',227 password='fake_password')228 # Create primary tenant and network229 self._mock_assign_user_role()230 roles_fix = self._mock_list_role()231 user_fix = self._mock_user_create('1234', 'fake_prim_user')232 tenant_fix = self._mock_tenant_create('1234', 'fake_prim_tenant')233 net_fix = self._mock_network_create(iso_creds, '1234', 'fake_net')234 subnet_fix = self._mock_subnet_create(iso_creds, '1234', 'fake_subnet')235 router_fix = self._mock_router_create('1234', 'fake_router')236 router_interface_mock = self.patch(237 'tempest.services.network.json.network_client.NetworkClientJSON.'238 'add_router_interface_with_subnet_id')239 iso_creds.get_primary_creds()240 router_interface_mock.called_once_with('1234', '1234')241 router_interface_mock.reset_mock()242 tenant_fix.cleanUp()243 user_fix.cleanUp()244 net_fix.cleanUp()245 subnet_fix.cleanUp()246 router_fix.cleanUp()247 # Create alternate tenant and network248 user_fix = self._mock_user_create('12345', 'fake_alt_user')249 tenant_fix = self._mock_tenant_create('12345', 'fake_alt_tenant')250 net_fix = self._mock_network_create(iso_creds, '12345', 'fake_alt_net')251 subnet_fix = self._mock_subnet_create(iso_creds, '12345',252 'fake_alt_subnet')253 router_fix = self._mock_router_create('12345', 'fake_alt_router')254 iso_creds.get_alt_creds()255 router_interface_mock.called_once_with('12345', '12345')256 router_interface_mock.reset_mock()257 tenant_fix.cleanUp()258 user_fix.cleanUp()259 net_fix.cleanUp()260 subnet_fix.cleanUp()261 router_fix.cleanUp()262 roles_fix.cleanUp()263 # Create admin tenant and networks264 user_fix = self._mock_user_create('123456', 'fake_admin_user')265 tenant_fix = self._mock_tenant_create('123456', 'fake_admin_tenant')266 net_fix = self._mock_network_create(iso_creds, '123456',267 'fake_admin_net')268 subnet_fix = self._mock_subnet_create(iso_creds, '123456',269 'fake_admin_subnet')270 router_fix = self._mock_router_create('123456', 'fake_admin_router')271 self._mock_list_roles('123456', 'admin')272 iso_creds.get_admin_creds()273 self.patch('tempest.services.identity.json.identity_client.'274 'IdentityClientJSON.delete_user')275 self.patch('tempest.services.identity.json.identity_client.'276 'IdentityClientJSON.delete_tenant')277 net = mock.patch.object(iso_creds.network_admin_client,278 'delete_network')279 net_mock = net.start()280 subnet = mock.patch.object(iso_creds.network_admin_client,281 'delete_subnet')282 subnet_mock = subnet.start()283 router = mock.patch.object(iso_creds.network_admin_client,284 'delete_router')285 router_mock = router.start()286 remove_router_interface_mock = self.patch(287 'tempest.services.network.json.network_client.NetworkClientJSON.'288 'remove_router_interface_with_subnet_id')289 return_values = ({'status': 200}, {'ports': []})290 port_list_mock = mock.patch.object(iso_creds.network_admin_client,291 'list_ports',292 return_value=return_values)293 port_list_mock.start()294 secgroup_list_mock = mock.patch.object(iso_creds.network_admin_client,295 'list_security_groups',296 side_effect=side_effect)297 secgroup_list_mock.start()298 return_values = (fake_http.fake_httplib({}, status=204), {})299 remove_secgroup_mock = self.patch(300 'tempest.services.network.json.network_client.'301 'NetworkClientJSON.delete', return_value=return_values)302 iso_creds.clear_isolated_creds()303 # Verify default security group delete304 calls = remove_secgroup_mock.mock_calls305 self.assertEqual(len(calls), 3)306 args = map(lambda x: x[1][0], calls)307 self.assertIn('v2.0/security-groups/sg-1234', args)308 self.assertIn('v2.0/security-groups/sg-12345', args)309 self.assertIn('v2.0/security-groups/sg-123456', args)310 # Verify remove router interface calls311 calls = remove_router_interface_mock.mock_calls312 self.assertEqual(len(calls), 3)313 args = map(lambda x: x[1], calls)314 self.assertIn(('1234', '1234'), args)315 self.assertIn(('12345', '12345'), args)316 self.assertIn(('123456', '123456'), args)317 # Verify network delete calls318 calls = net_mock.mock_calls319 self.assertEqual(len(calls), 3)320 args = map(lambda x: x[1][0], calls)321 self.assertIn('1234', args)322 self.assertIn('12345', args)323 self.assertIn('123456', args)324 # Verify subnet delete calls325 calls = subnet_mock.mock_calls326 self.assertEqual(len(calls), 3)327 args = map(lambda x: x[1][0], calls)328 self.assertIn('1234', args)329 self.assertIn('12345', args)330 self.assertIn('123456', args)331 # Verify router delete calls332 calls = router_mock.mock_calls333 self.assertEqual(len(calls), 3)334 args = map(lambda x: x[1][0], calls)335 self.assertIn('1234', args)336 self.assertIn('12345', args)337 self.assertIn('123456', args)338 @mock.patch('tempest.common.rest_client.RestClient')339 def test_network_alt_creation(self, MockRestClient):340 iso_creds = isolated_creds.IsolatedCreds('test class',341 password='fake_password')342 self._mock_assign_user_role()343 self._mock_list_role()344 self._mock_user_create('1234', 'fake_alt_user')345 self._mock_tenant_create('1234', 'fake_alt_tenant')346 self._mock_network_create(iso_creds, '1234', 'fake_alt_net')347 self._mock_subnet_create(iso_creds, '1234', 'fake_alt_subnet')348 self._mock_router_create('1234', 'fake_alt_router')349 router_interface_mock = self.patch(350 'tempest.services.network.json.network_client.NetworkClientJSON.'351 'add_router_interface_with_subnet_id')352 iso_creds.get_alt_creds()353 router_interface_mock.called_once_with('1234', '1234')354 network = iso_creds.get_alt_network()355 subnet = iso_creds.get_alt_subnet()356 router = iso_creds.get_alt_router()357 self.assertEqual(network['id'], '1234')358 self.assertEqual(network['name'], 'fake_alt_net')359 self.assertEqual(subnet['id'], '1234')360 self.assertEqual(subnet['name'], 'fake_alt_subnet')361 self.assertEqual(router['id'], '1234')362 self.assertEqual(router['name'], 'fake_alt_router')363 @mock.patch('tempest.common.rest_client.RestClient')364 def test_network_admin_creation(self, MockRestClient):365 iso_creds = isolated_creds.IsolatedCreds('test class',366 password='fake_password')367 self._mock_assign_user_role()368 self._mock_user_create('1234', 'fake_admin_user')369 self._mock_tenant_create('1234', 'fake_admin_tenant')370 self._mock_network_create(iso_creds, '1234', 'fake_admin_net')371 self._mock_subnet_create(iso_creds, '1234', 'fake_admin_subnet')372 self._mock_router_create('1234', 'fake_admin_router')373 router_interface_mock = self.patch(374 'tempest.services.network.json.network_client.NetworkClientJSON.'375 'add_router_interface_with_subnet_id')376 self._mock_list_roles('123456', 'admin')377 iso_creds.get_admin_creds()378 router_interface_mock.called_once_with('1234', '1234')379 network = iso_creds.get_admin_network()380 subnet = iso_creds.get_admin_subnet()381 router = iso_creds.get_admin_router()382 self.assertEqual(network['id'], '1234')383 self.assertEqual(network['name'], 'fake_admin_net')384 self.assertEqual(subnet['id'], '1234')385 self.assertEqual(subnet['name'], 'fake_admin_subnet')386 self.assertEqual(router['id'], '1234')387 self.assertEqual(router['name'], 'fake_admin_router')388 @mock.patch('tempest.common.rest_client.RestClient')389 def test_no_network_resources(self, MockRestClient):390 net_dict = {...

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