Exemplo n.º 1
0
class EnableNetworkInterfaceHost(TestStepBase):
    """
    Implements a Test Step to enable a network interface on the host pc
    """

    def run(self, context):
        """
        Enable a network interface on the host pc

        :type context: TestStepContext
        :param context: test case context
        """
        TestStepBase.run(self, context)

        # Get parameters
        self._hostpc = EquipmentManager().get_computer(eqt_name="COMPUTER1")
        self._timeout = self._pars.dhcp_timeout_sec
        self._interface = self._pars.interface

        # Bring up the specified network interface
        if 'Linux' in self._hostpc.get_os():
            primaryAddress =  str(self._hostpc.get_eqt_dict().get_param_value("IP", ""))
            username =  str(self._hostpc.get_eqt_dict().get_param_value("username", ""))

            if os.system('ssh {0}@{1} ifconfig {2} up'.format(username, primaryAddress, self._interface)) != 0:
                msg = "Failed to bring the {0} network interface up".format(self._interface)
                raise DeviceException(DeviceException.OPERATION_FAILED, msg)

            # Configure the tethered network interface on the host using DHCP
            network_interface_start_time = time.time()
            network_interface_available = False
            while (time.time() - network_interface_start_time < self._timeout) and network_interface_available == False:
                time.sleep(1)

                try:
                    self._hostpc.dhclient(self._interface)
                    network_interface_available = True
                except Exception, e:
                    network_interface_available = False
                    msg = "Failed to allocate IP address to {0} : {1}".format(self._interface, e)
                    self._logger.info(msg)

            if network_interface_available == False:
                msg = "DHCP time out occurred while allocating IP address to {0}. Aborting TestStep execution".format(self._interface)
                raise DeviceException(DeviceException.OPERATION_FAILED, msg)
        else:
Exemplo n.º 2
0
    def check_and_recover_usb_tether(self,
                                     path='www.google.com',
                                     numRetries=5):
        """
        If the USB tether has gone down, attempt to recover it before continuing

        :type path: str
        :param path: Remote path to be used for connection test

        :type numRetries: int
        :param numRetries: the number of times to retry

        :return: None
        """
        hostpc = EquipmentManager().get_computer(eqt_name="COMPUTER1")
        try:
            time.sleep(1)
            urllib.urlopen(path).read()
        except Exception:
            for i in range(1, numRetries + 1):
                try:
                    self._logger.debug(
                        "USB tether connection went down. Attempting to recover..."
                    )
                    time.sleep(1)
                    self._netAPI.start_usb_tethering(unplug=True)
                    if 'linux' in os.sys.platform:
                        usbInterface = 'usb0'
                        hostpc.dhclient(usbInterface)
                except Exception, e:
                    if i == numRetries:
                        msg = "Failed to recover the USB tether: {0}".format(e)
                        raise DeviceException(DeviceException.OPERATION_FAILED,
                                              msg)
                    else:
                        self._logger.debug(
                            'Attempt %d to recover USB tether failed. Trying again...'
                            .format(i))