Best Python code snippet using tempest_python
test_routers.py
Source:test_routers.py  
...65    def test_create_router_with_default_snat_value(self):66        # Create a router with default snat rule67        router = self._create_router(68            external_network_id=CONF.network.public_network_id)69        self._verify_router_gateway(70            router['id'], {'network_id': CONF.network.public_network_id,71                           'enable_snat': True})72    @decorators.idempotent_id('ea74068d-09e9-4fd7-8995-9b6a1ace920f')73    @utils.requires_ext(extension='ext-gw-mode', service='network')74    @testtools.skipUnless(CONF.network.public_network_id,75                          'The public_network_id option must be specified.')76    def test_create_router_with_snat_explicit(self):77        name = data_utils.rand_name('snat-router')78        # Create a router enabling snat attributes79        enable_snat_states = [False, True]80        for enable_snat in enable_snat_states:81            external_gateway_info = {82                'network_id': CONF.network.public_network_id,83                'enable_snat': enable_snat}84            create_body = self.admin_routers_client.create_router(85                name=name, external_gateway_info=external_gateway_info)86            self.addCleanup(test_utils.call_and_ignore_notfound_exc,87                            self.admin_routers_client.delete_router,88                            create_body['router']['id'])89            # Verify snat attributes after router creation90            self._verify_router_gateway(create_body['router']['id'],91                                        exp_ext_gw_info=external_gateway_info)92    def _verify_router_gateway(self, router_id, exp_ext_gw_info=None):93        show_body = self.admin_routers_client.show_router(router_id)94        actual_ext_gw_info = show_body['router']['external_gateway_info']95        if exp_ext_gw_info is None:96            self.assertIsNone(actual_ext_gw_info)97            return98        # Verify only keys passed in exp_ext_gw_info99        for k, v in exp_ext_gw_info.items():100            self.assertEqual(v, actual_ext_gw_info[k])101    def _verify_gateway_port(self, router_id):102        list_body = self.admin_ports_client.list_ports(103            network_id=CONF.network.public_network_id,104            device_id=router_id,105            device_owner="network:router_gateway")106        self.assertEqual(len(list_body['ports']), 1)107        gw_port = list_body['ports'][0]108        fixed_ips = gw_port['fixed_ips']109        self.assertNotEmpty(fixed_ips)110        # Assert that all of the IPs from the router gateway port111        # are allocated from a valid public subnet.112        public_net_body = self.admin_networks_client.show_network(113            CONF.network.public_network_id)114        public_subnet_ids = public_net_body['network']['subnets']115        for fixed_ip in fixed_ips:116            subnet_id = fixed_ip['subnet_id']117            self.assertIn(subnet_id, public_subnet_ids)118    @decorators.idempotent_id('6cc285d8-46bf-4f36-9b1a-783e3008ba79')119    @testtools.skipUnless(CONF.network.public_network_id,120                          'The public_network_id option must be specified.')121    def test_update_router_set_gateway(self):122        router = self._create_router()123        self.routers_client.update_router(124            router['id'],125            external_gateway_info={126                'network_id': CONF.network.public_network_id})127        # Verify operation - router128        self._verify_router_gateway(129            router['id'],130            {'network_id': CONF.network.public_network_id})131        self._verify_gateway_port(router['id'])132    @decorators.idempotent_id('b386c111-3b21-466d-880c-5e72b01e1a33')133    @utils.requires_ext(extension='ext-gw-mode', service='network')134    @testtools.skipUnless(CONF.network.public_network_id,135                          'The public_network_id option must be specified.')136    def test_update_router_set_gateway_with_snat_explicit(self):137        router = self._create_router()138        self.admin_routers_client.update_router(139            router['id'],140            external_gateway_info={141                'network_id': CONF.network.public_network_id,142                'enable_snat': True})143        self._verify_router_gateway(144            router['id'],145            {'network_id': CONF.network.public_network_id,146             'enable_snat': True})147        self._verify_gateway_port(router['id'])148    @decorators.idempotent_id('96536bc7-8262-4fb2-9967-5c46940fa279')149    @utils.requires_ext(extension='ext-gw-mode', service='network')150    @testtools.skipUnless(CONF.network.public_network_id,151                          'The public_network_id option must be specified.')152    def test_update_router_set_gateway_without_snat(self):153        router = self._create_router()154        self.admin_routers_client.update_router(155            router['id'],156            external_gateway_info={157                'network_id': CONF.network.public_network_id,158                'enable_snat': False})159        self._verify_router_gateway(160            router['id'],161            {'network_id': CONF.network.public_network_id,162             'enable_snat': False})163        self._verify_gateway_port(router['id'])164    @decorators.idempotent_id('ad81b7ee-4f81-407b-a19c-17e623f763e8')165    @testtools.skipUnless(CONF.network.public_network_id,166                          'The public_network_id option must be specified.')167    def test_update_router_unset_gateway(self):168        router = self._create_router(169            external_network_id=CONF.network.public_network_id)170        self.routers_client.update_router(router['id'],171                                          external_gateway_info={})172        self._verify_router_gateway(router['id'])173        # No gateway port expected174        list_body = self.admin_ports_client.list_ports(175            network_id=CONF.network.public_network_id,176            device_id=router['id'])177        self.assertFalse(list_body['ports'])178    @decorators.idempotent_id('f2faf994-97f4-410b-a831-9bc977b64374')179    @utils.requires_ext(extension='ext-gw-mode', service='network')180    @testtools.skipUnless(CONF.network.public_network_id,181                          'The public_network_id option must be specified.')182    def test_update_router_reset_gateway_without_snat(self):183        router = self._create_router(184            external_network_id=CONF.network.public_network_id)185        self.admin_routers_client.update_router(186            router['id'],187            external_gateway_info={188                'network_id': CONF.network.public_network_id,189                'enable_snat': False})190        self._verify_router_gateway(191            router['id'],192            {'network_id': CONF.network.public_network_id,193             'enable_snat': False})194        self._verify_gateway_port(router['id'])195class RoutersIpV6AdminTest(RoutersAdminTest):...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!!
