Best Python code snippet using lisa_python
nic.py
Source:nic.py
...121 def append(self, next_node: NicInfo) -> None:122 self.nics[next_node.upper] = next_node123 def is_empty(self) -> bool:124 return len(self.nics) == 0125 def get_unpaired_devices(self) -> List[str]:126 return [x.upper for x in self.nics.values() if not x.lower]127 def get_upper_nics(self) -> List[str]:128 return list(self.nics.keys())129 def get_lower_nics(self) -> List[str]:130 return [x.lower for x in self.nics.values() if x.lower]131 def get_device_slots(self) -> List[str]:132 return [x.pci_slot for x in self.nics.values() if x.pci_slot]133 # update the current nic driver in the NicInfo instance134 # grabs the driver short name and the driver sysfs path135 def get_nic_driver(self, nic_name: str) -> str:136 # get the current driver for the nic from the node137 # sysfs provides a link to the driver entry at device/driver138 nic = self.get_nic(nic_name)139 cmd = f"readlink -f /sys/class/net/{nic_name}/device/driver"140 # ex return value:141 # /sys/bus/vmbus/drivers/hv_netvsc142 found_link = self._node.execute(cmd, expected_exit_code=0).stdout143 assert_that(found_link).described_as(144 f"sysfs check for NIC device {nic_name} driver returned no output"145 ).is_not_equal_to("")146 nic.driver_sysfs_path = PurePosixPath(found_link)147 driver_name = nic.driver_sysfs_path.name148 assert_that(driver_name).described_as(149 f"sysfs entry contained no filename for device driver: {found_link}"150 ).is_not_equal_to("")151 nic.bound_driver = driver_name152 return driver_name153 def get_nic(self, nic_name: str) -> NicInfo:154 return self.nics[nic_name]155 def get_nic_by_index(self, index: int = -1) -> NicInfo:156 # get nic by index, default is -1 to give a non-primary nic157 # when there are more than one nic on the system158 number_of_nics = len(self.get_upper_nics())159 assert_that(number_of_nics).is_greater_than(0)160 try:161 nic_name = self.get_upper_nics()[index]162 except IndexError:163 raise LisaException(164 f"Attempted get_upper_nics()[{index}], only "165 f"{number_of_nics} nics are registered in node.nics. "166 f"Had upper interfaces: {self.get_upper_nics()}"167 )168 try:169 nic = self.nics[nic_name]170 except KeyError:171 raise LisaException(172 f"NicInfo for interface {nic_name} not found! "173 f"Had upper interfaces: {self.get_upper_nics()}"174 )175 return nic176 def nic_info_is_present(self, nic_name: str) -> bool:177 return nic_name in self.get_upper_nics() or nic_name in self.get_lower_nics()178 def unbind(self, nic: NicInfo) -> None:179 # unbind nic from current driver and return the old sysfs path180 echo = self._node.tools[Echo]181 # if sysfs path is not set, fetch the current driver182 if not nic.driver_sysfs_path:183 self.get_nic_driver(nic.upper)184 unbind_path = nic.driver_sysfs_path.joinpath("unbind")185 echo.write_to_file(186 nic.dev_uuid,187 unbind_path,188 sudo=True,189 )190 def bind(self, nic: NicInfo, driver_module_path: str) -> None:191 echo = self._node.tools[Echo]192 nic.driver_sysfs_path = PurePosixPath(driver_module_path)193 bind_path = nic.driver_sysfs_path.joinpath("bind")194 echo.write_to_file(195 nic.dev_uuid,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(...
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!!