Best Python code snippet using lisa_python
netinterface.py
Source:netinterface.py  
...41        priority=1,42        requirement=simple_requirement(network_interface=Synthetic()),43    )44    def validate_netvsc_reload(self, node: Node) -> None:45        self._validate_netvsc_built_in(node)46        network_interface_feature = node.features[NetworkInterface]47        # Test loading and unloading netvsc driver48        test_count = 049        while test_count < self.NETVSC_RELOAD_TEST_COUNT:50            test_count += 151            # Unload and load hv_netvsc52            network_interface_feature.reload_module()53    @TestCaseMetadata(54        description="""55            This test case verifies if synthetic network interface can be56            brought up and brought down gracefully via ip link set commands.57            Steps:58            1. Validate netvsc isn't built-in already. If it is then skip the test.59            2. Ensure netvsc module is loaded.60            3. Change nic state to up and down multiple times using ifup-ifdown commands61                Each time after "up" state, verify ip address is assigned to nic62                and internet is accessible via nic.63        """,64        priority=1,65        requirement=simple_requirement(66            network_interface=schema.NetworkInterfaceOptionSettings(67                data_path=schema.NetworkDataPath.Synthetic,68            ),69        ),70    )71    def validate_network_interface_reload_via_ip_link(72        self, node: Node, log: Logger73    ) -> None:74        self._validate_netvsc_built_in(node)75        network_interface_feature = node.features[NetworkInterface]76        # Ensure netvsc module is loaded77        network_interface_feature.reload_module()78        node_nic_info = Nics(node)79        node_nic_info.initialize()80        default_nic = node_nic_info.default_nic81        default_route = node_nic_info.default_nic_route82        assert_that(default_nic).is_not_none()83        assert_that(default_route).is_not_none()84        test_count = 085        ip = node.tools[Ip]86        while test_count < self.NET_INTERFACE_RELOAD_TEST_COUNT:87            test_count += 188            ip.restart_device(default_nic)89            if not node_nic_info.default_nic:90                # Add default route if missing after running ip link down/up91                node.execute(f"ip route add {default_route}", shell=True, sudo=True)92            if not node_nic_info.nics[default_nic].ip_addr:93                node.execute("kill $(pidof dhclient)", shell=True, sudo=True)94                dhclient = node.tools[Dhclient]95                dhclient.renew()96            timer = perf_timer.Timer()97            while timer.elapsed(stop=False) < self.DHCLIENT_TIMEOUT:98                node_nic_info.load_interface_info(default_nic)99                if node_nic_info.nics[default_nic].ip_addr:100                    break101                time.sleep(1)102            wget_tool = node.tools[Wget]103            if not wget_tool.verify_internet_access():104                raise LisaException(105                    "Cannot access internet from inside VM after test run."106                )107    @TestCaseMetadata(108        description="""109            This test case verifies if the second network interface can be brought up110             after setting static MAC address.111            Steps:112            1. Validate the second nic has IP address.113            2. Bring down the second nic.114            3. Set a random MAC address to the second nic.115            4. Bring up the second nic.116        """,117        priority=3,118        requirement=simple_requirement(119            network_interface=schema.NetworkInterfaceOptionSettings(120                nic_count=2,121            ),122        ),123    )124    def validate_set_static_mac(self, node: Node, log: Logger) -> None:125        ip = node.tools[Ip]126        node_nic_info = Nics(node)127        node_nic_info.initialize()128        origin_nic_count = len(node_nic_info)129        # attach one more nic for testing if only 1 nic by default130        if 1 == origin_nic_count:131            network_interface_feature = node.features[NetworkInterface]132            network_interface_feature.attach_nics(133                extra_nic_count=1, enable_accelerated_networking=True134            )135            node_nic_info = Nics(node)136            node_nic_info.initialize()137        # get one nic which is not eth0 for setting new mac address138        current_nic_count = len(node_nic_info)139        for index in range(0, current_nic_count):140            test_nic = node_nic_info.get_nic_by_index(index)141            test_nic_name = test_nic.upper142            if "eth0" == test_nic_name:143                continue144        assert_that(test_nic).is_not_none()145        assert_that(test_nic.ip_addr).is_not_none()146        assert_that(test_nic.mac_addr).is_not_none()147        origin_mac_address = test_nic.mac_addr148        try:149            random_mac_address = str(RandMac())150            ip.set_mac_address(test_nic_name, random_mac_address)151            node_nic_info.load_interface_info(test_nic_name)152            assert_that(test_nic.mac_addr).described_as(153                f"fail to set network interface {test_nic_name}'s mac "154                f"address into {random_mac_address}"155            ).is_equal_to(random_mac_address)156        finally:157            # restore the test nic state back to origin state158            ip.set_mac_address(test_nic_name, origin_mac_address)159            node_nic_info.load_interface_info(test_nic_name)160            assert_that(test_nic.mac_addr).described_as(161                f"fail to set network interface {test_nic}'s mac "162                f"address back into {origin_mac_address}"163            ).is_equal_to(origin_mac_address)164            # restore vm nics status if 1 extra nic attached165            if 1 == origin_nic_count:166                restore_extra_nics_per_node(node)167                node_nic_info = Nics(node)168                node_nic_info.initialize()169    def _validate_netvsc_built_in(self, node: Node) -> None:170        if node.tools[KernelConfig].is_built_in("CONFIG_HYPERV_NET"):...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!!
