Best Python code snippet using avocado_python
test_api_networks_create.py
Source:test_api_networks_create.py  
...24        self.objects = dict()25    def tearDown(self):26        try:27            # Removes Vlans of Racks28            self.remove_all_vlans([self.objects['id_envrk_A']])29            self.remove_all_vlans([self.objects['id_envrk_B']])30            self.remove_all_vlans([self.objects['id_envrk_C']])31            self.remove_all_vlans([self.objects['id_envrk_D']])32        except:33            pass34        try:35            # Removes environment of load balancing36            self.client_api.create_api_environment()\37                .delete(self.objects['id_envlb'])38            # Removes environment of racks39            self.client_api.create_api_environment()\40                .delete(self.objects['id_envrk_A'])41            self.client_api.create_api_environment()\42                .delete(self.objects['id_envrk_B'])43            self.client_api.create_api_environment()\44                .delete(self.objects['id_envrk_C'])45            self.client_api.create_api_environment()\46                .delete(self.objects['id_envrk_D'])47        except:48            pass49        # Removes vrfs50        try:51            self.client_api.create_api_vrf().delete(self.objects['id_vrf'])52        except:53            pass54        # Removes Environment DC55        for id_envdc in self.objects['id_envdc']:56            try:57                self.client_api.create_divisao_dc().remover(id_envdc)58            except:59                pass60        # Removes Environment Logic61        for id_envlog in self.objects['id_envlog']:62            try:63                self.client_api.create_ambiente_logico().remover(id_envlog)64            except:65                pass66        # Removes Environment layer 3 for load balancing67        for id_envl3 in self.objects['id_envl3']:68            try:69                self.client_api.create_grupo_l3().remover(id_envl3)70            except:71                pass72        # Removes eqpts73        try:74            self.client_api.create_api_create().delete(self.objects['id_eqpt'])75        except:76            pass77    def test_create_networkv4_by_zero(self):78        """79        Test of integration for create environment, vlan, eqpt networks v4.80        ##################81        Starting test:82            - environment A:83                - eqpt 1, 2, 384                - vrf 185                - starting vlans 1,2,386            - environment B:87                - eqpt 2, 4, 588                - vrf 189                - starting vlans 4, 5, 6, 7, 8, 990            - environment C:91                - EQpt 5, 692                - vrf 293                - startinG vlans 10, 1194            - environment D:95                - eqpt 796                - vrf 197                - starting vlans 198        ##################99        ##################100        Inserting new vlans without numbers:101            - environment A: Expected 10102            - environment B: Expected 12103            - environment C: Expected 1104            - environment C: Expected 2105            - environment C: Expected 3106            - environment C: Expected 13107            - environment B: Expected 14108            - environment B: Expected 15109            - environment B: Expected 16110            - environment A: Expected 11111            - environment A: Expected 13112            - environment A: Expected 17113            - environment A: Expected 18114            - environment B: Expected 19115            - environment C: Expected 17116            - environment D: Expected 1117        ##################118        ##################119        Starting networks:120            environment A:121                Nothing122            environment B:123                10.0.1.0/24124                10.0.2.0/25125                10.0.3.0/24126            environment C:127                Nothing128            environment D:129                Nothing130        ##################131        ##################132        Inserting networks without octs:133            - environment B: Expected 10.0.0.0/24134            - environment C: Expected 10.0.0.0/25135            - environment C using prefix 24: Expected 10.0.1.0/24136            - environment A: Expected 10.0.2.128/25137            - environment A: Expected 10.0.4.0/25138            - environment B: Expected 10.0.5.0/24139            - environment A: Expected 10.0.4.128/25140            - environment D: Expected 10.0.0.0/24141        ##################142        """143        # Creates VRF 1144        id_vrf_a = self.create_vrf('BeTeste-1')145        self.objects['id_vrf'] = [id_vrf_a]146        # Creates VRF 2147        id_vrf_b = self.create_vrf('BeTeste-2')148        self.objects['id_vrf'].append(id_vrf_b)149        # Creates Environment DC and Logic150        id_envdc = self.create_envdc('BE-TESTE')151        id_envlog = self.create_envlog('TESTE')152        self.objects['id_envdc'] = [id_envdc]153        self.objects['id_envlog'] = [id_envlog]154        # Creates environment layer 3 for load balancing155        id_envl3 = self.create_envl3('BALANCEAMENTO-POOL')156        self.objects['id_envl3'] = [id_envl3]157        # Creates environment of load balancing158        id_env = self.create_env(id_envl3, id_envlog, id_envdc,159                                 id_vrf_a, self.configs['env_lb'])160        self.objects['id_envlb'] = [id_env]161        env_list = {'A': id_vrf_a, 'B': id_vrf_a, 'C': id_vrf_b, 'D': id_vrf_a}162        # Creates environments of racks163        for i in env_list:164            # Creates environment layer 3 for racks165            id_envl3 = self.create_envl3('RACK-%s' % i)166            self.objects['id_envl3'].append(id_envl3)167            id_env = self.create_env(id_envl3, id_envlog, id_envdc,168                                     env_list[i], self.configs[i])169            self.objects['id_envrk_' + i] = id_env170        # Creates equipments with relationship environments171        self.create_equipments()172        # Creates vlans with numbers173        self.create_vlans_with_number_envs()174        # Creates vlans with auto numbers175        self.create_vlans_without_number()176        # Creates networks v4 with octs177        self.create_netv4_with_octs()178        # Creates networks with auto octs and prefix179        self.create_netv4_without_octs()180    def test_create_networkv6_by_zero(self):181        """182        Test of integration for create environment, vlan, eqpt networks v6.183        ##################184        Starting test:185            - environment A:186                - eqpt 1, 2, 3187                - vrf 1188                - starting vlans 1,2,3189            - environment B:190                - eqpt 2, 4, 5191                - vrf 1192                - starting vLANS 4, 5, 6, 7, 8, 9193            - environment C:194                - EQpt 5, 6195                - vrf 2196                - startinG VLANS 10, 11197            - environment D:198                - eqpt 7199                - vrf 1200                - starting vlans 1201        ##################202        ##################203        Inserting new vlans:204            - environment A: 10205            - environment B: 12206            - environment C: 1207            - environment C: 2208            - environment C: 3209            - environment C: 13210            - environment B: 14211            - environment B: 15212            - environment B: 16213            - environment A: 11214            - environment A: 13215            - environment A: 17216            - environment A: 18217            - environment B: 19218            - environment C: 17219            - environment D: 1220        ##################221        ##################222        Starting networks:223            environment A:224                Nothing225            environment B:226                fdbe:bebe:bebe:1201:0000:0000:0000:0000/64227                fdbe:bebe:bebe:1202:0000:0000:0000:0000/65228                fdbe:bebe:bebe:1203:0000:0000:0000:0000/64229            environment C:230                Nothing231            environment D:232                Nothing233        ##################234        ##################235        Inserting networks:236            - environment B:fdbe:bebe:bebe:1200:0000:0000:0000:0000/64237            - environment C:fdbe:bebe:bebe:1200:0000:0000:0000:0000/65238            - environment C using prefix 24: fdbe:bebe:bebe:1201:0000:0000:0000:0000/64239            - environment A:fdbe:bebe:bebe:1202:8000:0000:0000:0000/65240            - environment A:fdbe:bebe:bebe:1204:0000:0000:0000:0000/65241            - environment B:fdbe:bebe:bebe:1205:0000:0000:0000:0000/64242            - environment A:fdbe:bebe:bebe:1204:8000:0000:0000:0000/65243            - environment D:fdbe:bebe:bebe:1200:0000:0000:0000:0000/64244        ##################245        """246        # Creates VRF 1247        id_vrf_a = self.create_vrf('BeTeste-1')248        self.objects['id_vrf'] = [id_vrf_a]249        # Creates VRF 2250        id_vrf_b = self.create_vrf('BeTeste-2')251        self.objects['id_vrf'].append(id_vrf_b)252        # Creates Environment DC and Logic253        id_envdc = self.create_envdc('BE-TESTE')254        id_envlog = self.create_envlog('TESTE')255        self.objects['id_envdc'] = [id_envdc]256        self.objects['id_envlog'] = [id_envlog]257        # Creates environment layer 3 for load balancing258        id_envl3 = self.create_envl3('BALANCEAMENTO-POOL')259        self.objects['id_envl3'] = [id_envl3]260        # Creates environment of load balancing261        id_env = self.create_env(id_envl3, id_envlog, id_envdc,262                                 id_vrf_a, self.configs['env_lb'])263        self.objects['id_envlb'] = [id_env]264        env_list = {'A': id_vrf_a, 'B': id_vrf_a, 'C': id_vrf_b, 'D': id_vrf_a}265        # Creates environments of racks266        for i in env_list:267            # Creates environment layer 3 for racks268            id_envl3 = self.create_envl3('RACK-%s' % i)269            self.objects['id_envl3'].append(id_envl3)270            id_env = self.create_env(id_envl3, id_envlog, id_envdc,271                                     env_list[i], self.configs[i])272            self.objects['id_envrk_' + i] = id_env273        # Creates equipments with relationship environments274        self.create_equipments()275        # Creates vlans with numbers276        self.create_vlans_with_number_envs()277        # Creates vlans with auto numbers278        self.create_vlans_without_number()279        # Creates networks with octs280        self.create_netv6_with_octs()281        # Creates networks with auto octs and prefix282        self.create_netv6_without_octs()283    def configs(self):284        self.configs = {285            'env_lb': [{286                'subnet': 'febe:bebe:bebe:8200:0:0:0:0/57',287                'new_prefix': '64',288                'type': 'v6',289                'network_type': 8290            }, {291                'subnet': '10.10.0.0/16',292                'new_prefix': '24',293                'type': 'v4',294                'network_type': 8295            }],296            'A': [{297                'subnet': 'fdbe:bebe:bebe:1200:0000:0000:0000:0000/57',298                'new_prefix': '65',299                'type': 'v6',300                'network_type': 8301            }, {302                'subnet': '10.0.0.0/16',303                'new_prefix': '25',304                'type': 'v4',305                'network_type': 8306            }],307            'B': [{308                'subnet': 'fdbe:bebe:bebe:1200:0000:0000:0000:0000/57',309                'new_prefix': '64',310                'type': 'v6',311                'network_type': 8312            }, {313                'subnet': '10.0.0.0/16',314                'new_prefix': '24',315                'type': 'v4',316                'network_type': 8317            }],318            'C': [{319                'subnet': 'fdbe:bebe:bebe:1200:0000:0000:0000:0000/57',320                'new_prefix': '65',321                'type': 'v6',322                'network_type': 8323            }, {324                'subnet': '10.0.0.0/16',325                'new_prefix': '25',326                'type': 'v4',327                'network_type': 8328            }],329            'D': [{330                'subnet': 'fdbe:bebe:bebe:1200:0000:0000:0000:0000/57',331                'new_prefix': '64',332                'type': 'v6',333                'network_type': 8334            }, {335                'subnet': '10.0.0.0/16',336                'new_prefix': '24',337                'type': 'v4',338                'network_type': 8339            }]340        }341    def create_envdc(self, name):342        """Creates Environment DC"""343        id_envdc = self.client_api.create_divisao_dc()\344            .inserir('%s-%s' % (name, self.sufix))['division_dc']['id']345        return id_envdc346    def create_envlog(self, name):347        """Creates Environment Logic"""348        id_envlog = self.client_api.create_ambiente_logico()\349            .inserir('%s-%s' % (name, self.sufix))['logical_environment']['id']350        return id_envlog351    def create_envl3(self, name):352        """Creates environment layer 3 for load balancing."""353        id_envl3 = self.client_api.create_grupo_l3()\354            .inserir('%s-%s' % (name, self.sufix))['group_l3']['id']355        return id_envl3356    def create_vrf(self, name):357        """Creates VRF."""358        vrf_dict = [{359            'internal_name': '%s-%s' % (name, self.sufix),360            'vrf': '%s-%s' % (name, self.sufix)361        }]362        id_vrf = self.client_api.create_api_vrf()\363            .create(vrf_dict)[0]['id']364        return id_vrf365    def create_env(self, id_envl3, id_envlog, id_envdc, id_vrf, configs):366        """Creates environment."""367        env_dict = [{368            'grupo_l3': int(id_envl3),369            'ambiente_logico': int(id_envlog),370            'divisao_dc': int(id_envdc),371            'filter': 1,372            'default_vrf': id_vrf,373            'min_num_vlan_1': 1,374            'max_num_vlan_1': 500,375            'min_num_vlan_2': 1000,376            'max_num_vlan_2': 1500,377            'configs': configs378        }]379        id_env = self.client_api.create_api_environment()\380            .create(env_dict)[0]['id']381        return id_env382    def create_equipments(self):383        """Creates equipments."""384        eqpt_dict = [{385            'name': 'TESTE-EQUIP-1%s' % self.sufix,386            'maintenance': False,387            'equipment_type': 3,388            'model': 1,389            'environments': [{390                'is_router': False,391                'environment': self.objects['id_envrk_A']392            }]393        }, {394            'name': 'TESTE-EQUIP-2%s' % self.sufix,395            'maintenance': False,396            'equipment_type': 3,397            'model': 1,398            'environments': [{399                'is_router': False,400                'environment': self.objects['id_envrk_A']401            }]402        }, {403            'name': 'TESTE-EQUIP-3%s' % self.sufix,404            'maintenance': False,405            'equipment_type': 3,406            'model': 1,407            'environments': [{408                'is_router': False,409                'environment': self.objects['id_envrk_A']410            }, {411                'is_router': False,412                'environment': self.objects['id_envrk_B']413            }]414        }, {415            'name': 'TESTE-EQUIP-4%s' % self.sufix,416            'maintenance': False,417            'equipment_type': 3,418            'model': 1,419            'environments': [{420                'is_router': False,421                'environment': self.objects['id_envrk_B']422            }]423        }, {424            'name': 'TESTE-EQUIP-5%s' % self.sufix,425            'maintenance': False,426            'equipment_type': 3,427            'model': 1,428            'environments': [{429                'is_router': False,430                'environment': self.objects['id_envrk_B']431            }, {432                'is_router': False,433                'environment': self.objects['id_envrk_C']434            }]435        }, {436            'name': 'TESTE-EQUIP-6%s' % self.sufix,437            'maintenance': False,438            'equipment_type': 3,439            'model': 1,440            'environments': [{441                'is_router': False,442                'environment': self.objects['id_envrk_C']443            }]444        }, {445            'name': 'TESTE-EQUIP-7%s' % self.sufix,446            'maintenance': False,447            'equipment_type': 3,448            'model': 1,449            'environments': [{450                'is_router': False,451                'environment': self.objects['id_envrk_D']452            }]453        }]454        ids = self.client_api.create_api_equipment().create(eqpt_dict)455        self.objects['id_eqpt'] = ids456    def create_vlans_without_number(self):457        """Creates vlans without number."""458        id_env_a = self.objects['id_envrk_A']459        id_env_b = self.objects['id_envrk_B']460        id_env_c = self.objects['id_envrk_C']461        id_env_d = self.objects['id_envrk_D']462        list_envs_alocate_vlans = [463            id_env_a, id_env_b, id_env_c, id_env_c, id_env_c, id_env_c,464            id_env_b, id_env_b, id_env_b, id_env_a, id_env_a, id_env_a,465            id_env_a, id_env_b, id_env_c, id_env_d466        ]467        list_expected_num_vlans = [468            10, 12, 1, 2, 3, 13, 14, 15, 16, 11, 13, 17, 18, 19, 17, 1469        ]470        vlans = []471        for i, id_env in enumerate(list_envs_alocate_vlans):472            vlan = [{473                'name': 'Vlan auto %s - %s' % (i, self.sufix),474                'environment': id_env475            }]476            id_vlan = [self.client_api.create_api_vlan().create(vlan)[0]['id']]477            vlan_obj = self.client_api.create_api_vlan().get(id_vlan)[478                'vlans'][0]479            vlans.append(vlan_obj)480        self.verify_num_vlan(vlans, list_expected_num_vlans)481    def create_vlans_with_number(self, nums_vlan, id_env):482        """Creates vlans with number."""483        vlans = []484        for num_vlan in nums_vlan:485            vlan = [{486                'name': 'Vlan %s' % (num_vlan),487                'environment': id_env,488                'num_vlan': num_vlan489            }]490            id_vlan = [self.client_api.create_api_vlan().create(vlan)[0]['id']]491            vlan_obj = self.client_api.create_api_vlan().get(id_vlan)[492                'vlans'][0]493            vlans.append(vlan_obj)494        return vlans495    def create_vlans_with_number_envs(self):496        """Creates vlans with number for environments A, B and C."""497        id_env_a = self.objects['id_envrk_A']498        id_env_b = self.objects['id_envrk_B']499        id_env_c = self.objects['id_envrk_C']500        # Environment A501        # [1, 2, 3]502        nums_vlan = range(1, 4)503        # Creates Vlans504        id_vlans = self.create_vlans_with_number(nums_vlan, id_env_a)505        ids = [id_vlan['id'] for id_vlan in id_vlans]506        # Get Vlans507        vlans = self.client_api.create_api_vlan().get(ids)['vlans']508        # Verify num vlans was created509        self.verify_num_vlan(vlans, nums_vlan)510        # Environment B511        # [4, 5, 6, 7, 8, 9]512        nums_vlan = range(4, 10)513        # Creates Vlans514        id_vlans = self.create_vlans_with_number(nums_vlan, id_env_b)515        ids = [id_vlan['id'] for id_vlan in id_vlans]516        # Get Vlans517        vlans = self.client_api.create_api_vlan().get(ids)['vlans']518        # Verify num vlans was created519        self.verify_num_vlan(vlans, nums_vlan)520        # Environment C521        # [10, 11]522        nums_vlan = range(10, 12)523        # Creates Vlans524        id_vlans = self.create_vlans_with_number(nums_vlan, id_env_c)525        ids = [id_vlan['id'] for id_vlan in id_vlans]526        # Get Vlans527        vlans = self.client_api.create_api_vlan().get(ids)['vlans']528        # Verify num vlans was created529        self.verify_num_vlan(vlans, nums_vlan)530    def verify_num_vlan(self, objs, nums_vlan):531        for obj, num_vlan in izip(objs, nums_vlan):532            self.assertEqual(533                num_vlan,534                obj.get('num_vlan'),535                'Num vlan should be %s was %s' % (536                    num_vlan, obj.get('num_vlan'))537            )538    def search_all_vlans(self, ids_env):539        search_vlan = {540            'start_record': 0,541            'end_record': 100,542            'asorting_cols': [],543            'searchable_columns': [],544            'extends_search': [545                {'ambiente': id_env} for id_env in ids_env546            ]547        }548        vlans = self.client_api.create_api_vlan()\549            .search(search=search_vlan, fields=['id'])['vlans']550        ids_vlans = [id_vlan['id'] for id_vlan in vlans]551        return ids_vlans552    def remove_all_vlans(self, ids_env):553        ids_vlans = self.search_all_vlans(ids_env)554        if ids_vlans:555            search_net = {556                'start_record': 0,557                'end_record': 100,558                'asorting_cols': [],559                'searchable_columns': [],560                'extends_search': [{'vlan': id_vlan} for id_vlan in ids_vlans]561            }562        networks = self.client_api.create_api_network_ipv4()\563            .search(search=search_net, fields=['id'])['networks']564        ids_networks = [id['id'] for id in networks]565        if ids_networks:566            self.client_api.create_api_network_ipv4().delete(ids_networks)...interfaces.py
Source:interfaces.py  
...183            run_command(cmd, self.host, sudo=True)184            return True185        except Exception as ex:186            raise NWException("Failed to remove VLAN interface: {}".format(ex))187    def remove_all_vlans(self):188        """Remove all VLANs of this interface.189        This method will remove all the VLAN interfaces associated by the190        interface. If it fails, the method will raise a NWException.191        """192        try:193            for v in self.vlans.values():194                cmd = "ip link delete {}".format(v)195                run_command(cmd, self.host, sudo=True)196        except Exception as ex:197            raise NWException("Failed to remove VLAN interface: {}".format(ex))198    def bring_down(self):199        """Shutdown the interface.200        This will shutdown the interface link. Be careful, you might lost201        connection to the host....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!!
