Best Python code snippet using lisa_python
nic.py
Source:nic.py
...196 self._node.get_pure_path(f"{str(bind_path)}"),197 sudo=True,198 )199 nic.bound_driver = nic.driver_sysfs_path.name200 def load_interface_info(self, nic_name: Optional[str] = None) -> None:201 command = "/sbin/ip addr show"202 if nic_name:203 command += f" {nic_name}"204 result = self._node.execute(205 command,206 shell=True,207 expected_exit_code=0,208 expected_exit_code_failure_message=(209 f"Could not run {command} on node {self._node.name}"210 ),211 )212 entries = find_groups_in_lines(213 result.stdout, self.__ip_addr_show_regex, single_line=False214 )215 found_nics = []216 for entry in entries:217 self._node.log.debug(f"Found nic info: {entry}")218 nic_name = entry["name"]219 mac = entry["mac"]220 ip_addr = entry["ip_addr"]221 if nic_name in self.get_upper_nics():222 nic_entry = self.nics[nic_name]223 nic_entry.ip_addr = ip_addr224 nic_entry.mac_addr = mac225 found_nics.append(nic_name)226 if not nic_name:227 assert_that(sorted(found_nics)).described_as(228 f"Could not locate nic info for all nics. "229 f"Nic set was {self.nics.keys()} and only found info for {found_nics}"230 ).is_equal_to(sorted(self.nics.keys()))231 def reload(self) -> None:232 self.nics.clear()233 self._initialize()234 @retry(tries=15, delay=3, backoff=1.15)235 def wait_for_sriov_enabled(self) -> None:236 lspci = self._node.tools[Lspci]237 # check for VFs on the guest238 vfs = lspci.get_devices_by_type(constants.DEVICE_TYPE_SRIOV, force_run=True)239 assert_that(len(vfs)).described_as(240 "Could not identify any SRIOV NICs on the test node."241 ).is_not_zero()242 # check if the NIC driver has finished setting up the243 # failsafe pair, reload if not244 if not self.get_lower_nics():245 self.reload()246 if not self.get_lower_nics():247 assert_that(self.get_lower_nics()).described_as(248 "Did not detect any upper/lower sriov paired nics!: "249 f"upper: {self.get_upper_nics()} "250 f"lower: {self.get_lower_nics()} "251 f"unpaired: {self.get_unpaired_devices()}"252 f"vfs: {','.join([str(pci) for pci in vfs])}"253 ).is_not_empty()254 def _initialize(self, *args: Any, **kwargs: Any) -> None:255 self._node.log.debug("loading nic information...")256 self.nic_names = self._get_nic_names()257 self._get_node_nic_info()258 self._get_default_nic()259 self.load_interface_info()260 self._get_nic_uuids()261 for nic in self.get_upper_nics():262 self.get_nic_driver(nic)263 def _get_nic_names(self) -> List[str]:264 # identify all of the nics on the device, excluding tunnels and loopbacks etc.265 all_nics = self._node.execute(266 "ls /sys/class/net/",267 shell=True,268 sudo=True,269 ).stdout.split()270 virtual_nics = self._node.execute(271 "ls /sys/devices/virtual/net",272 shell=True,273 sudo=True,...
netinterface.py
Source:netinterface.py
...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!!