Best Python code snippet using tempest_python
test_subnets.py
Source:test_subnets.py  
...61        cls.name = cls.network['name']62        cls.subnet = cls._create_subnet_with_last_subnet_block(cls.network)63        cls.cidr = cls.subnet['cidr']64        cls._subnet_data = {6: {'gateway':65                                str(cls._get_gateway_from_tempest_conf(6)),66                                'allocation_pools':67                                cls._get_allocation_pools_from_gateway(6),68                                'dns_nameservers': ['2001:4860:4860::8844',69                                                    '2001:4860:4860::8888'],70                                'host_routes': [{'destination': '2001::/64',71                                                 'nexthop': '2003::1'}],72                                'new_host_routes': [{'destination':73                                                     '2001::/64',74                                                     'nexthop': '2005::1'}],75                                'new_dns_nameservers':76                                ['2001:4860:4860::7744',77                                 '2001:4860:4860::7888']},78                            4: {'gateway':79                                str(cls._get_gateway_from_tempest_conf(4)),80                                'allocation_pools':81                                cls._get_allocation_pools_from_gateway(4),82                                'dns_nameservers': ['8.8.4.4', '8.8.8.8'],83                                'host_routes': [{'destination': '10.20.0.0/32',84                                                 'nexthop': '10.100.1.1'}],85                                'new_host_routes': [{'destination':86                                                     '10.20.0.0/32',87                                                     'nexthop':88                                                     '10.100.1.2'}],89                                'new_dns_nameservers': ['7.8.8.8', '7.8.4.4']}}90    @classmethod91    def _create_subnet_with_last_subnet_block(cls, network, ip_version=4):92        """Derive last subnet CIDR block from tenant CIDR and93           create the subnet with that derived CIDR94        """95        if ip_version == 4:96            cidr = netaddr.IPNetwork(CONF.network.project_network_cidr)97            mask_bits = CONF.network.project_network_mask_bits98        elif ip_version == 6:99            cidr = netaddr.IPNetwork(CONF.network.project_network_v6_cidr)100            mask_bits = CONF.network.project_network_v6_mask_bits101        subnet_cidr = list(cidr.subnet(mask_bits))[-1]102        gateway_ip = str(netaddr.IPAddress(subnet_cidr) + 1)103        body = cls.create_subnet(network, gateway=gateway_ip,104                                 cidr=subnet_cidr, mask_bits=mask_bits)105        return body['subnet']106    @classmethod107    def _get_gateway_from_tempest_conf(cls, ip_version):108        """Return first subnet gateway for configured CIDR."""109        if ip_version == 4:110            cidr = netaddr.IPNetwork(CONF.network.project_network_cidr)111            mask_bits = CONF.network.project_network_mask_bits112        elif ip_version == 6:113            cidr = netaddr.IPNetwork(CONF.network.project_network_v6_cidr)114            mask_bits = CONF.network.project_network_v6_mask_bits115        if mask_bits >= cidr.prefixlen:116            return netaddr.IPAddress(cidr) + 1117        else:118            for subnet in cidr.subnet(mask_bits):119                return netaddr.IPAddress(subnet) + 1120    @classmethod121    def _get_allocation_pools_from_gateway(cls, ip_version):122        """Return allocation range for subnet of given gateway."""123        gateway = cls._get_gateway_from_tempest_conf(ip_version)124        return [{'start': str(gateway + 2), 'end': str(gateway + 3)}]125    def subnet_dict(self, include_keys):126        """Return a subnet dict which has include_keys and their corresponding127           value from self._subnet_data128        """129        return dict((key, self._subnet_data[self._ip_version][key])130                    for key in include_keys)131    def _create_network(self, _auto_clean_up=True, network_name=None,132                        **kwargs):133        network_name = network_name or data_utils.rand_name('adm-netwk')134        post_body = {'name': network_name}135        post_body.update(kwargs)136        LOG.debug("create ADM network: %s", str(post_body))137        body = self.create_network(client=self.admin_networks_client,...test_networks.py
Source:test_networks.py  
...52        cls.name = cls.network['name']53        cls.subnet = cls.create_subnet(cls.network)54        cls.cidr = cls.subnet['cidr']55        cls._subnet_data = {6: {'gateway':56                                str(cls._get_gateway_from_tempest_conf(6)),57                                'allocation_pools':58                                cls._get_allocation_pools_from_gateway(6),59                                'dns_nameservers': ['2001:4860:4860::8844',60                                                    '2001:4860:4860::8888'],61                                'host_routes': [{'destination': '2001::/64',62                                                 'nexthop': '2003::1'}],63                                'new_host_routes': [{'destination':64                                                     '2001::/64',65                                                     'nexthop': '2005::1'}],66                                'new_dns_nameservers':67                                ['2001:4860:4860::7744',68                                 '2001:4860:4860::7888']},69                            4: {'gateway':70                                str(cls._get_gateway_from_tempest_conf(4)),71                                'allocation_pools':72                                cls._get_allocation_pools_from_gateway(4),73                                'dns_nameservers': ['8.8.4.4', '8.8.8.8'],74                                'host_routes': [{'destination': '10.20.0.0/32',75                                                 'nexthop': '10.100.1.1'}],76                                'new_host_routes': [{'destination':77                                                     '10.20.0.0/32',78                                                     'nexthop':79                                                     '10.100.1.2'}],80                                'new_dns_nameservers': ['7.8.8.8', '7.8.4.4']}}81    @classmethod82    def _get_gateway_from_tempest_conf(cls, ip_version):83        """Return first subnet gateway for configured CIDR """84        if ip_version == 4:85            cidr = netaddr.IPNetwork(CONF.network.tenant_network_cidr)86            mask_bits = CONF.network.tenant_network_mask_bits87        elif ip_version == 6:88            cidr = netaddr.IPNetwork(CONF.network.tenant_network_v6_cidr)89            mask_bits = CONF.network.tenant_network_v6_mask_bits90        if mask_bits >= cidr.prefixlen:91            return netaddr.IPAddress(cidr) + 192        else:93            for subnet in cidr.subnet(mask_bits):94                return netaddr.IPAddress(subnet) + 195    @classmethod96    def _get_allocation_pools_from_gateway(cls, ip_version):97        """Return allocation range for subnet of given gateway"""98        gateway = cls._get_gateway_from_tempest_conf(ip_version)99        return [{'start': str(gateway + 2), 'end': str(gateway + 3)}]100    def subnet_dict(self, include_keys):101        """Return a subnet dict which has include_keys and their corresponding102           value from self._subnet_data103        """104        return dict((key, self._subnet_data[self._ip_version][key])105                    for key in include_keys)106    def _compare_resource_attrs(self, actual, expected):107        exclude_keys = set(actual).symmetric_difference(expected)108        self.assertThat(actual, custom_matchers.MatchesDictExceptForKeys(109                        expected, exclude_keys))110    def _create_verify_delete_subnet(self, cidr=None, mask_bits=None,111                                     **kwargs):112        network = self.create_network()...test_vlan_network_action.py
Source:test_vlan_network_action.py  
...21        cls.name = cls.network['name']22        cls.subnet = cls._create_subnet_with_last_subnet_block(cls.network,23                                                               cls._ip_version)24        cls._subnet_data = {6: {'gateway':25                                str(cls._get_gateway_from_tempest_conf(6)),26                                'allocation_pools':27                                cls._get_allocation_pools_from_gateway(6),28                                'dns_nameservers': ['2001:4860:4860::8844',29                                                    '2001:4860:4860::8888'],30                                'host_routes': [{'destination': '2001::/64',31                                                 'nexthop': '2003::1'}],32                                'new_host_routes': [{'destination':33                                                     '2001::/64',34                                                     'nexthop': '2005::1'}],35                                'new_dns_nameservers':36                                ['2001:4860:4860::7744',37                                 '2001:4860:4860::7888']},38                            4: {'gateway':39                                str(cls._get_gateway_from_tempest_conf(4)),40                                'allocation_pools':41                                cls._get_allocation_pools_from_gateway(4),42                                'dns_nameservers': ['8.8.4.4', '8.8.8.8'],43                                'host_routes': [{'destination': '10.20.0.0/32',44                                                 'nexthop': '10.100.1.1'}],45                                'new_host_routes': [{'destination':46                                                     '10.20.0.0/32',47                                                     'nexthop':48                                                     '10.100.1.2'}],49                                'new_dns_nameservers': ['7.8.8.8', '7.8.4.4']}}50#    @classmethod51#    def resource_cleanup(cls):52#        " do not delete network "53#        pass 54    def test_a_create_update_delete_network_subnet(self):55        " Create a network "56        name = data_utils.rand_name('network-')       57        network = self.create_network(network_name=name)58        self.addCleanup(self._delete_network, network)59        net_id = network['id']60        self.net_id = net_id61        self.assertEqual('ACTIVE', network['status'])62        "network update"63        new_name = data_utils.rand_name('new-network-')64        body = self.networks_client.update_network(net_id, name=new_name)65        updated_net = body['network']66        self.assertEqual(updated_net['name'], new_name)67        "Find a cidr that is not in use yet and create a subnet with it"68        subnet = self.create_subnet(network)69        subnet_id = subnet['id']70        "subnet update"71        new_name = "New_subnet"72        body = self.subnets_client.update_subnet(subnet_id, name=new_name)73        updated_subnet = body['subnet']74        self.assertEqual(updated_subnet['name'], new_name)75        76    def test_b_show_network(self):77        "show network"78        body = self.networks_client.show_network(self.network['id'])79        network = body['network']80        for key in ['id', 'name']:81            self.assertEqual(network[key], self.network[key])82            print network83    def test_c_show_network_fields(self):84        fields = ['id', 'name']85        body = self.networks_client.show_network(self.network['id'],86                                                 fields=fields)87        network = body['network']88        self.assertEqual(sorted(network.keys()), sorted(fields))89        for field_name in fields:90            self.assertEqual(network[field_name], self.network[field_name])91            print ">>>>", network92    def test_d_list_networks(self):93        body = self.networks_client.list_networks()94        networks = [network['id'] for network in body['networks']95                    if network['id'] == self.network['id']]96        self.assertNotEmpty(networks, "Created network not found in the list")97        98    def test_e_delete_network(self):99        "delete network"100        body = self.networks_client.delete_network(self.network['id'])101        self.assertEqual(204, body.response.status)102    @classmethod103    def _create_subnet_with_last_subnet_block(cls, network, ip_version):104        if ip_version == 4:105            cidr = netaddr.IPNetwork(CONF.network.project_network_cidr)106            mask_bits = CONF.network.project_network_mask_bits107        elif ip_version == 6:108            cidr = netaddr.IPNetwork(CONF.network.project_network_v6_cidr)109            mask_bits = CONF.network.project_network_v6_mask_bits110        subnet_cidr = list(cidr.subnet(mask_bits))[-1]111        gateway_ip = str(netaddr.IPAddress(subnet_cidr) + 1)112        return cls.create_subnet(network, gateway=gateway_ip,113                                 cidr=subnet_cidr, mask_bits=mask_bits)114    @classmethod115    def _get_gateway_from_tempest_conf(cls, ip_version):116        """Return first subnet gateway for configured CIDR """117        if ip_version == 4:118            cidr = netaddr.IPNetwork(CONF.network.project_network_cidr)119            mask_bits = CONF.network.project_network_mask_bits120        elif ip_version == 6:121            cidr = netaddr.IPNetwork(CONF.network.project_network_v6_cidr)122            mask_bits = CONF.network.project_network_v6_mask_bits123        if mask_bits >= cidr.prefixlen:124            return netaddr.IPAddress(cidr) + 1125        else:126            for subnet in cidr.subnet(mask_bits):127                return netaddr.IPAddress(subnet) + 1128    @classmethod129    def _get_allocation_pools_from_gateway(cls, ip_version):130        """Return allocation range for subnet of given gateway"""131        gateway = cls._get_gateway_from_tempest_conf(ip_version)132        return [{'start': str(gateway + 2), 'end': str(gateway + 3)}]133#    def subnet_dict(self, include_keys):134#        return dict((key, self._subnet_data[self._ip_version][key])135#                    for key in include_keys)136#137#    def _compare_resource_attrs(self, actual, expected):138#        exclude_keys = set(actual).symmetric_difference(expected)139#        self.assertThat(actual, custom_matchers.MatchesDictExceptForKeys(140#                        expected, exclude_keys))141    def _delete_network(self, network):142        self.networks_client.delete_network(network['id'])143        if network in self.networks:144            self.networks.remove(network)145        for subnet in self.subnets:...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!!
