Best Python code snippet using lisa_python
ethtool.py
Source:ethtool.py  
...626        change_result.assert_exit_code(627            message=f" Couldn't change device {interface} ring buffer settings."628        )629        return self.get_device_ring_buffer_settings(interface, force_run=True)630    def get_device_rss_hash_key(631        self, interface: str, force_run: bool = False632    ) -> DeviceRssHashKey:633        device = self._get_or_create_device_setting(interface)634        if not force_run and device.device_rss_hash_key:635            return device.device_rss_hash_key636        result = self.run(f"-x {interface}", force_run=force_run)637        if (result.exit_code != 0) and ("Operation not supported" in result.stdout):638            raise UnsupportedOperationException(639                f"ethtool -x {interface} operation not supported."640            )641        result.assert_exit_code(642            message=f"Couldn't get device {interface} ring buffer settings."643        )644        device.device_rss_hash_key = DeviceRssHashKey(interface, result.stdout)645        return device.device_rss_hash_key646    def change_device_rss_hash_key(647        self, interface: str, hash_key: str648    ) -> DeviceRssHashKey:649        result = self.run(f"-X {interface} hkey {hash_key}", sudo=True, force_run=True)650        if (result.exit_code != 0) and ("Operation not supported" in result.stdout):651            raise UnsupportedOperationException(652                f"Changing RSS hash key with 'ethtool -X {interface}' not supported."653            )654        result.assert_exit_code(655            message=f" Couldn't change device {interface} hash key."656        )657        return self.get_device_rss_hash_key(interface, force_run=True)658    def get_device_rx_hash_level(659        self, interface: str, protocol: str, force_run: bool = False660    ) -> DeviceRxHashLevel:661        device = self._get_or_create_device_setting(interface)662        if (663            not force_run664            and device.device_rx_hash_level665            and (protocol in device.device_rx_hash_level.protocol_hash_map.keys())666        ):667            return device.device_rx_hash_level668        result = self.run(669            f"-n {interface} rx-flow-hash {protocol}", force_run=force_run670        )671        if "Operation not supported" in result.stdout:672            raise UnsupportedOperationException(673                f"ethtool -n {interface} operation not supported."674            )675        result.assert_exit_code(676            message=f"Couldn't get device {interface} RX flow hash level for"677            f" protocol {protocol}."678        )679        if device.device_rx_hash_level:680            device.device_rx_hash_level._parse_rx_hash_level(681                interface, protocol, result.stdout682            )683            device_rx_hash_level = device.device_rx_hash_level684        else:685            device_rx_hash_level = DeviceRxHashLevel(interface, protocol, result.stdout)686        device.device_rx_hash_level = device_rx_hash_level687        return device_rx_hash_level688    def change_device_rx_hash_level(689        self, interface: str, protocol: str, enable: bool690    ) -> DeviceRxHashLevel:691        param = "sd"692        if enable:693            param = "sdfn"694        result = self.run(695            f"-N {interface} rx-flow-hash {protocol} {param}",696            sudo=True,697            force_run=True,698        )699        if "Operation not supported" in result.stdout:700            raise UnsupportedOperationException(701                f"ethtool -N {interface} rx-flow-hash {protocol} {param}"702                " operation not supported."703            )704        result.assert_exit_code(705            message=f" Couldn't change device {interface} hash level for {protocol}."706        )707        return self.get_device_rx_hash_level(interface, protocol, force_run=True)708    def get_device_sg_settings(709        self, interface: str, force_run: bool = False710    ) -> DeviceSgSettings:711        device = self._get_or_create_device_setting(interface)712        if not force_run and device.device_sg_settings:713            return device.device_sg_settings714        result = self.run(f"-k {interface}", force_run=force_run)715        result.assert_exit_code()716        device.device_sg_settings = DeviceSgSettings(interface, result.stdout)717        return device.device_sg_settings718    def change_device_sg_settings(719        self, interface: str, sg_setting: bool720    ) -> DeviceSgSettings:721        sg = "on" if sg_setting else "off"722        change_result = self.run(723            f"-K {interface} sg {sg}",724            sudo=True,725            force_run=True,726        )727        change_result.assert_exit_code(728            message=f" Couldn't change device {interface} scatter-gather settings."729        )730        return self.get_device_sg_settings(interface, force_run=True)731    def get_device_statistics(732        self, interface: str, force_run: bool = False733    ) -> DeviceStatistics:734        device = self._get_or_create_device_setting(interface)735        if not force_run and device.device_statistics:736            return device.device_statistics737        result = self.run(f"-S {interface}", force_run=True)738        if (result.exit_code != 0) and (739            "Operation not supported" in result.stdout740            or "no stats available" in result.stdout741        ):742            raise UnsupportedOperationException(743                f"ethtool -S {interface} operation not supported."744            )745        result.assert_exit_code(message=f"Couldn't get device {interface} statistics.")746        device.device_statistics = DeviceStatistics(interface, result.stdout)747        return device.device_statistics748    def get_device_statistics_delta(749        self, interface: str, previous_statistics: Dict[str, int]750    ) -> Dict[str, int]:751        """752        use this method to get the delta of an operation.753        """754        new_statistics = self.get_device_statistics(755            interface=interface, force_run=True756        ).counters757        for key, value in previous_statistics.items():758            new_statistics[key] = new_statistics.get(key, 0) - value759        self._log.debug(f"none-zero delta statistics on {interface}:")760        self._log.debug(761            {key: value for key, value in new_statistics.items() if value != 0}762        )763        return new_statistics764    def get_device_firmware_version(765        self, interface: str, force_run: bool = False766    ) -> str:767        device = self._get_or_create_device_setting(interface)768        if not force_run and device.device_firmware_version:769            return device.device_firmware_version770        result = self.run(f"-i {interface}", force_run=force_run)771        if (result.exit_code != 0) and ("Operation not supported" in result.stdout):772            raise UnsupportedOperationException(773                f"ethtool -i {interface} operation not supported."774            )775        result.assert_exit_code(776            message=f"Couldn't get device {interface} firmware version info."777        )778        firmware_version_pattern = self._firmware_version_pattern.search(result.stdout)779        if not firmware_version_pattern:780            raise LisaException(781                f"Cannot get {interface} device firmware version information"782            )783        firmware_version = firmware_version_pattern.group("value")784        device.device_firmware_version = firmware_version785        return firmware_version786    def get_all_device_channels_info(self) -> List[DeviceChannel]:787        devices_channel_list = []788        devices = self.get_device_list()789        for device in devices:790            devices_channel_list.append(self.get_device_channels_info(device))791        return devices_channel_list792    def get_all_device_enabled_features(793        self, force_run: bool = False794    ) -> List[DeviceFeatures]:795        devices_features_list = []796        devices = self.get_device_list(force_run)797        for device in devices:798            devices_features_list.append(799                self.get_device_enabled_features(device, force_run)800            )801        return devices_features_list802    def get_all_device_gro_lro_settings(self) -> List[DeviceGroLroSettings]:803        devices_gro_lro_settings = []804        devices = self.get_device_list()805        for device in devices:806            devices_gro_lro_settings.append(self.get_device_gro_lro_settings(device))807        return devices_gro_lro_settings808    def get_all_device_link_settings(self) -> List[DeviceLinkSettings]:809        devices_link_settings_list = []810        devices = self.get_device_list()811        for device in devices:812            devices_link_settings_list.append(self.get_device_link_settings(device))813        return devices_link_settings_list814    def get_all_device_msg_level(self) -> List[DeviceMessageLevel]:815        devices_msg_level_list = []816        devices = self.get_device_list()817        for device in devices:818            devices_msg_level_list.append(self.get_device_msg_level(device))819        return devices_msg_level_list820    def get_all_device_ring_buffer_settings(self) -> List[DeviceRingBufferSettings]:821        devices_ring_buffer_settings_list = []822        devices = self.get_device_list()823        for device in devices:824            devices_ring_buffer_settings_list.append(825                self.get_device_ring_buffer_settings(device)826            )827        return devices_ring_buffer_settings_list828    def get_all_device_rss_hash_key(self) -> List[DeviceRssHashKey]:829        devices_rss_hash_keys = []830        devices = self.get_device_list()831        for device in devices:832            devices_rss_hash_keys.append(self.get_device_rss_hash_key(device))833        return devices_rss_hash_keys834    def get_all_device_rx_hash_level(self, protocol: str) -> List[DeviceRxHashLevel]:835        devices_rx_hash_level = []836        devices = self.get_device_list()837        for device in devices:838            devices_rx_hash_level.append(839                self.get_device_rx_hash_level(device, protocol)840            )841        return devices_rx_hash_level842    def get_all_device_statistics(self) -> List[DeviceStatistics]:843        devices_statistics = []844        devices = self.get_device_list()845        for device in devices:846            devices_statistics.append(...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!!
