Best Python code snippet using lisa_python
test_functional_network.py
Source:test_functional_network.py  
...25        """26        # 1. Attach max NICs and check all can get IP27        count = self.vm.nic_count - 128        self.log.info("Step 1: Attach %s NICs." % count)29        self.vm.attach_nics(count, wait=True)30        self.assertEqual(len(self.vm.query_nics()), count + 1,31                         "Total NICs number is not %d" % (count + 1))32        if self.cloud.cloud_provider == "alibaba":33            guest_cmd = """34primary_nic=$(ifconfig | grep "flags=.*\<UP\>" | cut -d: -f1 | \35grep -e eth -e ens | head -n 1)36device_name=$(echo $primary_nic | tr -d '[:digit:]')37device_numb=$(echo $primary_nic | tr -d '[:alpha:]')38[ "${device_name}" = "ens" ] && return    # ens* will up automatically39spath="/etc/sysconfig/network-scripts"40for offset in $(seq 1 %s); do41    device=${device_name}$((${device_numb}+${offset}))42    echo "STEP1: Create configure file ifcfg-${device}..."43    echo DEVICE=${device} | sudo tee $spath/ifcfg-${device}44    echo BOOTPROTO=dhcp   | sudo tee -a $spath/ifcfg-${device}45    echo ONBOOT=yes       | sudo tee -a $spath/ifcfg-${device}46    echo DEFROUTE=no      | sudo tee -a $spath/ifcfg-${device}47    echo "STEP2: 'ifup' this device..."48    sudo ifup ${device}49    sleep 2s50done51""" % count52            self.session.cmd_output(guest_cmd, timeout=180)53        else:54            self.session.cmd_output(55                "for i in {1..%s};do sudo cp "56                "/etc/sysconfig/network-scripts/ifcfg-eth0 "57                "/etc/sysconfig/network-scripts/ifcfg-eth$i; "58                "sudo sed -i -e \"s/eth0/eth$i/g\" "59                "-e '$a\DEFROUTE=no' -e '/HWADDR/d' "60                "/etc/sysconfig/network-scripts/ifcfg-eth$i; "61                "sudo ifup eth$i;sleep 2; done" % count,62                timeout=180)63        time.sleep(10)64        outside_ips = [65            str(self.vm.get_private_ip_address(nic))66            for nic in self.vm.query_nics()67        ]68        inside_ips = self.session.cmd_output("ip addr")69        for outside_ip in outside_ips:70            self.assertIn(71                outside_ip, inside_ips, "Some of NICs are not available. "72                "Outside IP: %s Inside IPs:\n %s" % (outside_ip, inside_ips))73        # 2. Add 1 more NIC. Should not be added74        self.log.info("Step 2: Add 1 more NIC, should not be added.")75        self.vm.attach_nics(1)76        self.assertEqual(77            len(self.vm.query_nics()), count + 1,78            "NICs number should not greater than %d" % (count + 1))79        # 3. Detach all NICs. NICs should be removed inside guest80        self.log.info("Step 3: Detach all NICs")81        if self.cloud.cloud_provider == "alibaba":82            guest_cmd = """83primary_nic=$(ifconfig | grep "flags=.*\<UP\>" | cut -d: -f1 | \84grep -e eth -e ens | head -n 1)85device_name=$(echo $primary_nic | tr -d '[:digit:]')86dev_list=$(ifconfig | grep "flags=.*\<UP\>" | cut -d: -f1 | \87grep $device_name | grep -v $primary_nic)88for dev in $dev_list; do89    echo "'ifdown' device $dev..."90    sudo ifdown $dev91    sleep 2s92done93"""94            self.session.cmd_output(guest_cmd)95        else:96            self.session.cmd_output(97                "for i in {1..%s};do sudo ifdown eth$i;done" % count)98        nic_ids = [99            self.vm.get_nic_id(nic) for nic in self.vm.query_nics()100            if self.vm.get_nic_id(nic) != self.primary_nic_id101        ]102        self.vm.detach_nics(nic_ids, wait=True)103        self.assertEqual(len(self.vm.query_nics()), 1,104                         "Fail to remove all NICs outside guest")105        time.sleep(5)106        if self.cloud.cloud_provider == "alibaba":107            self.assertEqual(108                self.session.cmd_output(109                    "ip addr | grep -e 'eth.*mtu' -e 'ens.*mtu' | wc -l"), "1",110                "Fail to remove all NICs inside guest")111        else:112            self.assertEqual(113                self.session.cmd_output("ip addr|grep 'eth.*mtu'|wc -l"), "1",114                "Fail to remove all NICs inside guest")115        self.log.info("Detach all NICs successfully")116    def test_coldplug_nics(self):117        """118        1. Stop VM. Attach max NICs. Start VM and check all can get IP119        2. Stop VM. Add 1 more NIC. Should not be added120        3. Stop VM. Detach all NICs. Device should be removed inside guest121        """122        # 1. Attach max NICs and check all can get IP123        count = self.vm.nic_count - 1124        self.log.info("Step 1: Attach %s NICs." % count)125        self.vm.attach_nics(count, wait=True)126        self.assertEqual(len(self.vm.query_nics()), count + 1,127                         "Total NICs number is not %d" % (count + 1))128        self.vm.start(wait=True)129        self.session.connect(timeout=180)130        if self.cloud.cloud_provider == "alibaba":131            guest_cmd = """132primary_nic=$(ifconfig | grep "flags=.*\<UP\>" | cut -d: -f1 | \133grep -e eth -e ens | head -n 1)134device_name=$(echo $primary_nic | tr -d '[:digit:]')135device_numb=$(echo $primary_nic | tr -d '[:alpha:]')136[ "${device_name}" = "ens" ] && return    # ens* will up automatically137spath="/etc/sysconfig/network-scripts"138for offset in $(seq 1 %s); do139    device=${device_name}$((${device_numb}+${offset}))140    echo "STEP1: Create configure file ifcfg-${device}..."141    echo DEVICE=${device} | sudo tee $spath/ifcfg-${device}142    echo BOOTPROTO=dhcp   | sudo tee -a $spath/ifcfg-${device}143    echo ONBOOT=yes       | sudo tee -a $spath/ifcfg-${device}144    echo DEFROUTE=no      | sudo tee -a $spath/ifcfg-${device}145    echo "STEP2: 'ifup' this device..."146    sudo ifup ${device}147    sleep 2s148done149""" % count150            self.session.cmd_output(guest_cmd, timeout=180)151        else:152            self.session.cmd_output(153                "for i in {1..%s};do sudo cp "154                "/etc/sysconfig/network-scripts/ifcfg-eth0 "155                "/etc/sysconfig/network-scripts/ifcfg-eth$i; "156                "sudo sed -i -e \"s/eth0/eth$i/g\" "157                "-e '$a\DEFROUTE=no' -e '/HWADDR/d' "158                "/etc/sysconfig/network-scripts/ifcfg-eth$i; "159                "sudo ifup eth$i;sleep 2; done" % count,160                timeout=180)161        time.sleep(10)162        outside_ips = [163            self.vm.get_private_ip_address(nic)164            for nic in self.vm.query_nics()165        ]166        inside_ips = self.session.cmd_output("ip addr")167        for outside_ip in outside_ips:168            self.assertIn(169                outside_ip, inside_ips,170                "Some of NICs are not available. Inside IPs: %s" % inside_ips)171        # 2. Add 1 more NIC. Should not be added172        self.log.info("Step 2: Add 1 more NIC, should not be added.")173        self.vm.stop(wait=True)174        self.assertTrue(self.vm.is_stopped(), "Fail to stop VM")175        self.vm.attach_nics(1)176        self.assertEqual(177            len(self.vm.query_nics()), count + 1,178            "NICs number should not greater than %d" % (count + 1))179        # 3. Detach all NICs. NICs should be removed inside guest180        self.log.info("Step 3: Detach all NICs.")181        nic_ids = [182            self.vm.get_nic_id(nic) for nic in self.vm.query_nics()183            if self.vm.get_nic_id(nic) != self.primary_nic_id184        ]185        self.vm.detach_nics(nic_ids, wait=True)186        self.assertEqual(len(self.vm.query_nics()), 1,187                         "Fail to remove all NICs outside guest")188        self.vm.start(wait=True)189        self.assertTrue(self.vm.is_started(), "Fail to start VM")...__init__.py
Source:__init__.py  
1# =========================================================================2# Copyright 2012-present Yunify, Inc.3# -------------------------------------------------------------------------4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this work except in compliance with the License.6# You may obtain a copy of the License in the LICENSE file, or at:7#8#  http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.15# =========================================================================16from .attach_nics import AttachNicsAction17from .create_nics import CreateNicsAction18from .delete_nics import DeleteNicsAction19from .describe_nics import DescribeNicsAction20from .detach_nics import DetachNicsAction21from .modify_nic_attributes import ModifyNicAttributesAction22__all__ = [23    AttachNicsAction, CreateNicsAction, DeleteNicsAction,24    DescribeNicsAction, DetachNicsAction,25    ModifyNicAttributesAction,...routes.py
Source:routes.py  
...4  from app.macs import register_routes as attach_macs5  from app.jobs import register_routes as attach_jobs6  from app.logs import register_routes as attach_logs7  attach_switch(api, app)8  attach_nics(api, app)9  attach_macs(api, app)10  attach_jobs(api, app)...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!!
