Best Python code snippet using avocado_python
multiport_stress.py
Source:multiport_stress.py  
...66        self.remotehost_public = RemoteHost(self.peer_public_ip,67                                            self.peer_user,68                                            password=self.peer_password)69        for peer_ip in self.peer_ips:70            peer_interface = self.remotehost.get_interface_by_ipaddr(peer_ip).name71            peer_networkinterface = NetworkInterface(peer_interface,72                                                     self.remotehost)73            if peer_networkinterface.set_mtu(self.mtu) is not None:74                self.cancel("Failed to set mtu in peer")75        for host_interface in self.host_interfaces:76            self.networkinterface = NetworkInterface(host_interface, self.local)77            if self.networkinterface.set_mtu(self.mtu) is not None:78                self.cancel("Failed to set mtu in host")79    def multiport_ping(self, ping_option):80        '''81        Ping to multiple peers parallely82        '''83        parallel_procs = []84        for host, peer in zip(self.host_interfaces, self.peer_ips):85            self.log.info('Starting Ping test')86            cmd = "ping -I %s %s -c %s %s" % (host, peer, self.count,87                                              ping_option)88            obj = process.SubProcess(cmd, verbose=False, shell=True)89            obj.start()90            parallel_procs.append(obj)91        self.log.info('Wait for background processes to finish'92                      ' before proceeding')93        for proc in parallel_procs:94            proc.wait()95        errors = []96        for proc in parallel_procs:97            out_buf = proc.get_stdout()98            out_buf += proc.get_stderr()99            for val in out_buf.decode("utf-8").splitlines():100                if 'packet loss' in val and ', 0% packet loss,' not in val:101                    errors.append(out_buf)102                    break103        if errors:104            self.fail(b"\n".join(errors))105    def test_multiport_ping(self):106        self.multiport_ping('')107    def test_multiport_floodping(self):108        self.multiport_ping('-f')109    def tearDown(self):110        '''111        unset ip for host interface112        '''113        for host_interface in self.host_interfaces:114            networkinterface = NetworkInterface(host_interface, self.local)115            if networkinterface.set_mtu("1500") is not None:116                self.cancel("Failed to set mtu in host")117        for peer_ip in self.peer_ips:118            peer_interface = self.remotehost.get_interface_by_ipaddr(peer_ip).name119            try:120                peer_networkinterface = NetworkInterface(peer_interface,121                                                         self.remotehost)122                peer_networkinterface.set_mtu("1500")123            except Exception:124                peer_public_networkinterface = NetworkInterface(peer_interface,125                                                                self.remotehost_public)126                peer_public_networkinterface.set_mtu("1500")127        for ipaddr, interface in zip(self.ipaddr, self.host_interfaces):128            networkinterface = NetworkInterface(interface, self.local)129            networkinterface.remove_ipaddr(ipaddr, self.netmask)130            try:131                networkinterface.restore_from_backup()132            except Exception:...hosts.py
Source:hosts.py  
...45            names = run_command(cmd, self).split()46        except Exception as ex:47            raise NWException("Failed to get interfaces: {}".format(ex))48        return [NetworkInterface(if_name=name, host=self) for name in names]49    def get_interface_by_ipaddr(self, ipaddr):50        """Return an interface that has a specific ipaddr."""51        for interface in self.interfaces:52            if ipaddr in interface.get_ipaddrs():53                return interface54    def get_default_route_interface(self):55        """Get a list of default routes interfaces56        :return: list of interface names57        """58        cmd = "ip -j route list default"59        output = run_command(cmd, self)60        try:61            result = json.loads(output)62            return [str(item['dev']) for item in result]63        except Exception as ex:...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!!
