Best Python code snippet using tempest_python
test_networks.py
Source:test_networks.py  
...186        # is actually deleted here - this will create and issue, hence remove187        # it from the list.188        self.subnets.pop()189    @test.attr(type='smoke')190    def test_create_delete_subnet_with_gw(self):191        gateway = '10.100.0.13'192        name = data_utils.rand_name('network-')193        resp, body = self.client.create_network(name=name)194        self.assertEqual('201', resp['status'])195        network = body['network']196        net_id = network['id']197        subnet = self.create_subnet(network, gateway)198        # Verifies Subnet GW in IPv4199        self.assertEqual(subnet['gateway_ip'], gateway)200        # Delete network and subnet201        resp, body = self.client.delete_network(net_id)202        self.assertEqual('204', resp['status'])203        self.subnets.pop()204    @test.attr(type='smoke')205    def test_create_delete_subnet_without_gw(self):206        net = netaddr.IPNetwork(CONF.network.tenant_network_cidr)207        gateway_ip = str(netaddr.IPAddress(net.first + 1))208        name = data_utils.rand_name('network-')209        resp, body = self.client.create_network(name=name)210        self.assertEqual('201', resp['status'])211        network = body['network']212        net_id = network['id']213        subnet = self.create_subnet(network)214        # Verifies Subnet GW in IPv4215        self.assertEqual(subnet['gateway_ip'], gateway_ip)216        # Delete network and subnet217        resp, body = self.client.delete_network(net_id)218        self.assertEqual('204', resp['status'])219        self.subnets.pop()220class NetworksTestXML(NetworksTestJSON):221    _interface = 'xml'222class BulkNetworkOpsTestJSON(base.BaseNetworkTest):223    _interface = 'json'224    """225    Tests the following operations in the Neutron API using the REST client for226    Neutron:227        bulk network creation228        bulk subnet creation229        bulk port creation230        list tenant's networks231    v2.0 of the Neutron API is assumed. It is also assumed that the following232    options are defined in the [network] section of etc/tempest.conf:233        tenant_network_cidr with a block of cidr's from which smaller blocks234        can be allocated for tenant networks235        tenant_network_mask_bits with the mask bits to be used to partition the236        block defined by tenant-network_cidr237    """238    @classmethod239    @test.safe_setup240    def setUpClass(cls):241        super(BulkNetworkOpsTestJSON, cls).setUpClass()242        cls.network1 = cls.create_network()243        cls.network2 = cls.create_network()244    def _delete_networks(self, created_networks):245        for n in created_networks:246            resp, body = self.client.delete_network(n['id'])247            self.assertEqual(204, resp.status)248        # Asserting that the networks are not found in the list after deletion249        resp, body = self.client.list_networks()250        networks_list = [network['id'] for network in body['networks']]251        for n in created_networks:252            self.assertNotIn(n['id'], networks_list)253    def _delete_subnets(self, created_subnets):254        for n in created_subnets:255            resp, body = self.client.delete_subnet(n['id'])256            self.assertEqual(204, resp.status)257        # Asserting that the subnets are not found in the list after deletion258        resp, body = self.client.list_subnets()259        subnets_list = [subnet['id'] for subnet in body['subnets']]260        for n in created_subnets:261            self.assertNotIn(n['id'], subnets_list)262    def _delete_ports(self, created_ports):263        for n in created_ports:264            resp, body = self.client.delete_port(n['id'])265            self.assertEqual(204, resp.status)266        # Asserting that the ports are not found in the list after deletion267        resp, body = self.client.list_ports()268        ports_list = [port['id'] for port in body['ports']]269        for n in created_ports:270            self.assertNotIn(n['id'], ports_list)271    @test.attr(type='smoke')272    def test_bulk_create_delete_network(self):273        # Creates 2 networks in one request274        network_names = [data_utils.rand_name('network-'),275                         data_utils.rand_name('network-')]276        resp, body = self.client.create_bulk_network(2, network_names)277        created_networks = body['networks']278        self.assertEqual('201', resp['status'])279        self.addCleanup(self._delete_networks, created_networks)280        # Asserting that the networks are found in the list after creation281        resp, body = self.client.list_networks()282        networks_list = [network['id'] for network in body['networks']]283        for n in created_networks:284            self.assertIsNotNone(n['id'])285            self.assertIn(n['id'], networks_list)286    @test.attr(type='smoke')287    def test_bulk_create_delete_subnet(self):288        # Creates 2 subnets in one request289        cidr = netaddr.IPNetwork(CONF.network.tenant_network_cidr)290        mask_bits = CONF.network.tenant_network_mask_bits291        cidrs = [subnet_cidr for subnet_cidr in cidr.subnet(mask_bits)]292        networks = [self.network1['id'], self.network2['id']]293        names = [data_utils.rand_name('subnet-') for i in range(len(networks))]294        subnets_list = []295        # TODO(raies): "for IPv6, version list [4, 6] will be used.296        # and cidr for IPv6 will be of IPv6"297        ip_version = [4, 4]298        for i in range(len(names)):299            p1 = {300                'network_id': networks[i],301                'cidr': str(cidrs[(i)]),302                'name': names[i],303                'ip_version': ip_version[i]304            }305            subnets_list.append(p1)306        del subnets_list[1]['name']307        resp, body = self.client.create_bulk_subnet(subnets_list)308        created_subnets = body['subnets']309        self.addCleanup(self._delete_subnets, created_subnets)310        self.assertEqual('201', resp['status'])311        # Asserting that the subnets are found in the list after creation312        resp, body = self.client.list_subnets()313        subnets_list = [subnet['id'] for subnet in body['subnets']]314        for n in created_subnets:315            self.assertIsNotNone(n['id'])316            self.assertIn(n['id'], subnets_list)317    @test.attr(type='smoke')318    def test_bulk_create_delete_port(self):319        # Creates 2 ports in one request320        networks = [self.network1['id'], self.network2['id']]321        names = [data_utils.rand_name('port-') for i in range(len(networks))]322        port_list = []323        state = [True, False]324        for i in range(len(names)):325            p1 = {326                'network_id': networks[i],327                'name': names[i],328                'admin_state_up': state[i],329            }330            port_list.append(p1)331        del port_list[1]['name']332        resp, body = self.client.create_bulk_port(port_list)333        created_ports = body['ports']334        self.addCleanup(self._delete_ports, created_ports)335        self.assertEqual('201', resp['status'])336        # Asserting that the ports are found in the list after creation337        resp, body = self.client.list_ports()338        ports_list = [port['id'] for port in body['ports']]339        for n in created_ports:340            self.assertIsNotNone(n['id'])341            self.assertIn(n['id'], ports_list)342class BulkNetworkOpsTestXML(BulkNetworkOpsTestJSON):343    _interface = 'xml'344class NetworksIpV6TestJSON(NetworksTestJSON):345    _ip_version = 6346    @classmethod347    def setUpClass(cls):348        if not CONF.network_feature_enabled.ipv6:349            skip_msg = "IPv6 Tests are disabled."350            raise cls.skipException(skip_msg)351        super(NetworksIpV6TestJSON, cls).setUpClass()352    @test.attr(type='smoke')353    def test_create_delete_subnet_with_gw(self):354        gateway = '2003::2'355        name = data_utils.rand_name('network-')356        resp, body = self.client.create_network(name=name)357        self.assertEqual('201', resp['status'])358        network = body['network']359        net_id = network['id']360        subnet = self.create_subnet(network, gateway)361        # Verifies Subnet GW in IPv6362        self.assertEqual(subnet['gateway_ip'], gateway)363        # Delete network and subnet364        resp, body = self.client.delete_network(net_id)365        self.assertEqual('204', resp['status'])366        self.subnets.pop()367    @test.attr(type='smoke')...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!!
