Exemple #1
0
class NetworkTest(Test):
    '''
    To test different types of pings
    '''
    def setUp(self):
        '''
        To check and install dependencies for the test
        '''
        smm = SoftwareManager()
        pkgs = ["ethtool", "net-tools"]
        detected_distro = distro.detect()
        if detected_distro.name == "Ubuntu":
            pkgs.extend(["openssh-client", "iputils-ping"])
        elif detected_distro.name == "SuSE":
            pkgs.extend(["openssh", "iputils"])
        else:
            pkgs.extend(["openssh-clients", "iputils"])
        for pkg in pkgs:
            if not smm.check_installed(pkg) and not smm.install(pkg):
                self.cancel("%s package is need to test" % pkg)
        interfaces = netifaces.interfaces()
        interface = self.params.get("interface")
        if interface not in interfaces:
            self.cancel("%s interface is not available" % interface)
        self.iface = interface
        self.ipaddr = self.params.get("host_ip", default="")
        self.netmask = self.params.get("netmask", default="")
        self.ip_config = self.params.get("ip_config", default=True)
        local = LocalHost()
        self.networkinterface = NetworkInterface(self.iface, local)
        if self.ip_config:
            try:
                self.networkinterface.add_ipaddr(self.ipaddr, self.netmask)
                self.networkinterface.save(self.ipaddr, self.netmask)
            except Exception:
                self.networkinterface.save(self.ipaddr, self.netmask)
            self.networkinterface.bring_up()
        if not wait.wait_for(self.networkinterface.is_link_up, timeout=120):
            self.fail("Link up of interface is taking longer than 120 seconds")
        self.peer = self.params.get("peer_ip")
        if not self.peer:
            self.cancel("No peer provided")
        self.mtu = self.params.get("mtu", default=1500)
        self.peer_public_ip = self.params.get("peer_public_ip", default="")
        self.peer_user = self.params.get("peer_user", default="root")
        self.peer_password = self.params.get("peer_password",
                                             '*',
                                             default=None)
        if 'scp' or 'ssh' in str(self.name.name):
            self.session = Session(self.peer,
                                   user=self.peer_user,
                                   password=self.peer_password)
            if not self.session.connect():
                self.cancel("failed connecting to peer")
        self.remotehost = RemoteHost(self.peer,
                                     self.peer_user,
                                     password=self.peer_password)
        self.peer_interface = self.remotehost.get_interface_by_ipaddr(
            self.peer).name
        self.peer_networkinterface = NetworkInterface(self.peer_interface,
                                                      self.remotehost)
        self.remotehost_public = RemoteHost(self.peer_public_ip,
                                            self.peer_user,
                                            password=self.peer_password)
        self.peer_public_networkinterface = NetworkInterface(
            self.peer_interface, self.remotehost_public)
        self.mtu = self.params.get("mtu", default=1500)
        self.mtu_set()
        if self.networkinterface.ping_check(self.peer, count=5) is not None:
            self.cancel("No connection to peer")

    def mtu_set(self):
        '''
        set mtu size
        '''
        if self.peer_networkinterface.set_mtu(self.mtu) is not None:
            self.cancel("Failed to set mtu in peer")
        if self.networkinterface.set_mtu(self.mtu) is not None:
            self.cancel("Failed to set mtu in host")

    def test_gro(self):
        '''
        Test GRO
        '''
        ro_type = "gro"
        ro_type_full = "generic-receive-offload"
        if not self.offload_state(ro_type_full):
            self.fail("Could not get state of %s" % ro_type)
        if self.offload_state(ro_type_full) == 'fixed':
            self.fail("Can not change the state of %s" % ro_type)
        self.offload_toggle_test(ro_type, ro_type_full)

    def test_gso(self):
        '''
        Test GSO
        '''
        ro_type = "gso"
        ro_type_full = "generic-segmentation-offload"
        if not self.offload_state(ro_type_full):
            self.fail("Could not get state of %s" % ro_type)
        if self.offload_state(ro_type_full) == 'fixed':
            self.fail("Can not change the state of %s" % ro_type)
        self.offload_toggle_test(ro_type, ro_type_full)

    def test_lro(self):
        '''
        Test LRO
        '''
        ro_type = "lro"
        ro_type_full = "large-receive-offload"
        path = '/sys/class/net/%s/device/uevent' % self.iface
        if os.path.exists(path):
            output = open(path, 'r').read()
            for line in output.splitlines():
                if "OF_NAME" in line:
                    if 'vnic' in line.split('=')[-1]:
                        self.cancel("Unsupported on vNIC")
        if not self.offload_state(ro_type_full):
            self.fail("Could not get state of %s" % ro_type)
        if self.offload_state(ro_type_full) == 'fixed':
            self.fail("Can not change the state of %s" % ro_type)
        self.offload_toggle_test(ro_type, ro_type_full)

    def test_tso(self):
        '''
        Test TSO
        '''
        ro_type = "tso"
        ro_type_full = "tcp-segmentation-offload"
        if not self.offload_state(ro_type_full):
            self.fail("Could not get state of %s" % ro_type)
        if self.offload_state(ro_type_full) == 'fixed':
            self.fail("Can not change the state of %s" % ro_type)
        self.offload_toggle_test(ro_type, ro_type_full)

    def test_ping(self):
        '''
        ping to peer machine
        '''
        if self.networkinterface.ping_check(self.peer, count=10) is not None:
            self.fail("ping test failed")

    def test_floodping(self):
        '''
        Flood ping to peer machine
        '''
        if self.networkinterface.ping_check(self.peer,
                                            count=500000,
                                            options='-f') is not None:
            self.fail("flood ping test failed")

    def test_ssh(self):
        '''
        Test ssh
        '''
        cmd = "echo hi"
        output = self.session.cmd(cmd)
        if not output.exit_status == 0:
            self.fail("unable to ssh into peer machine")

    def test_scp(self):
        '''
        Test scp
        '''
        process.run("dd if=/dev/zero of=/tmp/tempfile bs=1024000000 count=1",
                    shell=True)
        md_val1 = hashlib.md5(open('/tmp/tempfile', 'rb').read()).hexdigest()
        destination = "%s:/tmp" % self.peer
        output = self.session.copy_files('/tmp/tempfile', destination)
        if not output:
            self.fail("unable to copy into peer machine")

        source = "%s:/tmp/tempfile" % self.peer
        output = self.session.copy_files(source, '/tmp')
        if not output:
            self.fail("unable to copy from peer machine")

        md_val2 = hashlib.md5(open('/tmp/tempfile', 'rb').read()).hexdigest()
        if md_val1 != md_val2:
            self.fail("Test Failed")

    def test_jumbo_frame(self):
        '''
        Test jumbo frames
        '''
        if self.networkinterface.ping_check(self.peer,
                                            count=30,
                                            options='-i 0.1 -s %d' %
                                            (int(self.mtu) - 28)) is not None:
            self.fail("jumbo frame test failed")

    def test_statistics(self):
        '''
        Test Statistics
        '''
        rx_file = "/sys/class/net/%s/statistics/rx_packets" % self.iface
        tx_file = "/sys/class/net/%s/statistics/tx_packets" % self.iface
        rx_before = genio.read_file(rx_file)
        tx_before = genio.read_file(tx_file)
        self.networkinterface.ping_check(self.peer, count=500000, options='-f')
        rx_after = genio.read_file(rx_file)
        tx_after = genio.read_file(tx_file)
        if (rx_after <= rx_before) or (tx_after <= tx_before):
            self.log.debug("Before\nrx: %s tx: %s" % (rx_before, tx_before))
            self.log.debug("After\nrx: %s tx: %s" % (rx_after, tx_after))
            self.fail("Statistics not incremented properly")

    def mtu_set_back(self):
        '''
        Test set mtu back to 1500
        '''
        try:
            self.peer_networkinterface.set_mtu('1500')
        except Exception:
            self.peer_public_networkinterface.set_mtu('1500')
        if self.networkinterface.set_mtu('1500') is not None:
            self.cancel("Failed to set mtu in host")

    def offload_toggle_test(self, ro_type, ro_type_full):
        '''
        Check to toggle the LRO / GRO / GSO / TSO
        '''
        for state in ["off", "on"]:
            if not self.offload_state_change(ro_type, ro_type_full, state):
                self.fail("%s %s failed" % (ro_type, state))
            if self.networkinterface.ping_check(self.peer,
                                                count=500000,
                                                options='-f') is not None:
                self.fail("ping failed in %s %s" % (ro_type, state))

    def offload_state_change(self, ro_type, ro_type_full, state):
        '''
        Change the state of LRO / GRO / GSO / TSO to specified state
        '''
        cmd = "ethtool -K %s %s %s" % (self.iface, ro_type, state)
        if process.system(cmd, shell=True, ignore_status=True) != 0:
            return False
        if self.offload_state(ro_type_full) != state:
            return False
        return True

    def offload_state(self, ro_type_full):
        '''
        Return the state of LRO / GRO / GSO / TSO.
        If the state can not be changed, we return 'fixed'.
        If any other error, we return ''.
        '''
        cmd = "ethtool -k %s" % self.iface
        output = process.system_output(cmd, shell=True,
                                       ignore_status=True).decode("utf-8")
        for line in output.splitlines():
            if ro_type_full in line:
                if 'fixed' in line.split()[-1]:
                    return 'fixed'
                return line.split()[-1]
        return ''

    def test_promisc(self):
        '''
        promisc mode testing
        '''
        cmd = "ip link set %s promisc on" % self.iface
        if process.system(cmd, shell=True, ignore_status=True) != 0:
            self.fail("failed to enable promisc mode")
        self.networkinterface.ping_check(self.peer, count=100000, options='-f')
        cmd = "ip link set %s promisc off" % self.iface
        if process.system(cmd, shell=True, ignore_status=True) != 0:
            self.fail("failed to disable promisc mode")
        self.networkinterface.ping_check(self.peer, count=5)

    def tearDown(self):
        '''
        Remove the files created
        '''
        self.mtu_set_back()
        if 'scp' in str(self.name.name):
            process.run("rm -rf /tmp/tempfile")
            cmd = "rm -rf /tmp/tempfile"
            self.session.cmd(cmd)
        if self.ip_config:
            self.networkinterface.remove_ipaddr(self.ipaddr, self.netmask)
            try:
                self.networkinterface.restore_from_backup()
            except Exception:
                self.log.info(
                    "backup file not availbale, could not restore file.")
        self.remotehost.remote_session.quit()
        self.remotehost_public.remote_session.quit()
        if 'scp' or 'ssh' in str(self.name.name):
            self.session.quit()
Exemple #2
0
class Uperf(Test):
    """
    Uperf Test
    """

    def setUp(self):
        """
        To check and install dependencies for the test
        """
        self.peer_ip = self.params.get("peer_ip", default="")
        self.peer_public_ip = self.params.get("peer_public_ip", default="")
        self.peer_user = self.params.get("peer_user_name", default="root")
        self.peer_password = self.params.get("peer_password", '*',
                                             default="None")
        interfaces = netifaces.interfaces()
        self.iface = self.params.get("interface", default="")
        if self.iface not in interfaces:
            self.cancel("%s interface is not available" % self.iface)
        self.ipaddr = self.params.get("host_ip", default="")
        self.netmask = self.params.get("netmask", default="")
        local = LocalHost()
        self.networkinterface = NetworkInterface(self.iface, local)
        try:
            self.networkinterface.add_ipaddr(self.ipaddr, self.netmask)
            self.networkinterface.save(self.ipaddr, self.netmask)
        except Exception:
            self.networkinterface.save(self.ipaddr, self.netmask)
        self.networkinterface.bring_up()
        self.session = Session(self.peer_ip, user=self.peer_user,
                               password=self.peer_password)
        if not self.session.connect():
            self.cancel("failed connecting to peer")
        smm = SoftwareManager()
        detected_distro = distro.detect()
        pkgs = ["gcc", "autoconf", "perl", "m4", "git-core", "automake"]
        if detected_distro.name == "Ubuntu":
            pkgs.extend(["libsctp1", "libsctp-dev", "lksctp-tools"])
        else:
            pkgs.extend(["lksctp-tools", "lksctp-tools-devel"])
        for pkg in pkgs:
            if not smm.check_installed(pkg) and not smm.install(pkg):
                self.cancel("%s package is need to test" % pkg)
            cmd = "%s install %s" % (smm.backend.base_command, pkg)
            output = self.session.cmd(cmd)
            if not output.exit_status == 0:
                self.cancel("unable to install the package %s on peer machine "
                            % pkg)
        if self.peer_ip == "":
            self.cancel("%s peer machine is not available" % self.peer_ip)
        self.mtu = self.params.get("mtu", default=1500)
        self.remotehost = RemoteHost(self.peer_ip, self.peer_user,
                                     password=self.peer_password)
        self.peer_interface = self.remotehost.get_interface_by_ipaddr(self.peer_ip).name
        self.peer_networkinterface = NetworkInterface(self.peer_interface,
                                                      self.remotehost)
        self.remotehost_public = RemoteHost(self.peer_public_ip, self.peer_user,
                                            password=self.peer_password)
        self.peer_public_networkinterface = NetworkInterface(self.peer_interface,
                                                             self.remotehost_public)
        if self.peer_networkinterface.set_mtu(self.mtu) is not None:
            self.cancel("Failed to set mtu in peer")
        if self.networkinterface.set_mtu(self.mtu) is not None:
            self.cancel("Failed to set mtu in host")
        uperf_download = self.params.get("uperf_download", default="https:"
                                         "//github.com/uperf/uperf/"
                                         "archive/master.zip")
        tarball = self.fetch_asset("uperf.zip", locations=[uperf_download],
                                   expire='7d')
        archive.extract(tarball, self.teststmpdir)
        self.uperf_dir = os.path.join(self.teststmpdir, "uperf-master")
        destination = "%s:/tmp" % self.peer_ip
        output = self.session.copy_files(self.uperf_dir, destination,
                                         recursive=True)
        if not output:
            self.cancel("unable to copy the uperf into peer machine")
        cmd = "cd /tmp/uperf-master;autoreconf -fi;./configure ppc64le;make"
        output = self.session.cmd(cmd)
        if not output.exit_status == 0:
            self.cancel("Unable to compile Uperf into peer machine")
        self.uperf_run = str(self.params.get("UPERF_SERVER_RUN", default=0))
        if self.uperf_run == '1':
            cmd = "/tmp/uperf-master/src/uperf -s &"
            cmd = self.session.get_raw_ssh_command(cmd)
            self.obj = SubProcess(cmd)
            self.obj.start()
        os.chdir(self.uperf_dir)
        process.system('autoreconf -fi', shell=True)
        process.system('./configure ppc64le', shell=True)
        build.make(self.uperf_dir)
        self.expected_tp = self.params.get("EXPECTED_THROUGHPUT", default="85")

    def test(self):
        """
        Test run is a One way throughput test. In this test, we have one host
        transmitting (or receiving) data from a client. This transmit large
        messages using multiple threads or processes.
        """
        speed = int(read_file("/sys/class/net/%s/speed" % self.iface))
        cmd = "h=%s proto=tcp ./src/uperf -m manual/throughput.xml -a" \
            % self.peer_ip
        result = process.run(cmd, shell=True, ignore_status=True)
        if result.exit_status:
            self.fail("FAIL: Uperf Run failed")
        for line in result.stdout.decode("utf-8").splitlines():
            if self.peer_ip in line:
                if 'Mb/s' in line:
                    tput = int(line.split()[3].split('.')[0])
                else:
                    # Converting the throughput calculated in Gb to Mb
                    tput = int(line.split()[3].split('.')[0]) * 1000
                if tput < (int(self.expected_tp) * speed) / 100:
                    self.fail("FAIL: Throughput Actual - %s%%, Expected - %s%%"
                              ", Throughput Actual value - %s "
                              % ((tput*100)/speed, self.expected_tp,
                                 str(tput)+'Mb/sec'))
        if 'WARNING' in result.stdout.decode("utf-8"):
            self.log.warn('Test completed with warning')

    def tearDown(self):
        """
        Killing Uperf process in peer machine
        """
        self.obj.stop()
        cmd = "pkill uperf; rm -rf /tmp/uperf-master"
        output = self.session.cmd(cmd)
        if not output.exit_status == 0:
            self.fail("Either the ssh to peer machine machine\
                       failed or uperf process was not killed")
        if self.networkinterface.set_mtu('1500') is not None:
            self.cancel("Failed to set mtu in host")
        try:
            self.peer_networkinterface.set_mtu('1500')
        except Exception:
            self.peer_public_networkinterface.set_mtu('1500')
        self.networkinterface.remove_ipaddr(self.ipaddr, self.netmask)
        try:
            self.networkinterface.restore_from_backup()
        except Exception:
            self.log.info("backup file not availbale, could not restore file.")
        self.remotehost.remote_session.quit()
        self.remotehost_public.remote_session.quit()
        self.session.quit()
Exemple #3
0
class Iperf(Test):
    """
    Iperf Test
    """
    def setUp(self):
        """
        To check and install dependencies for the test
        """
        self.peer_user = self.params.get("peer_user", default="root")
        self.peer_ip = self.params.get("peer_ip", default="")
        self.peer_public_ip = self.params.get("peer_public_ip", default="")
        self.peer_password = self.params.get("peer_password",
                                             '*',
                                             default=None)
        interfaces = netifaces.interfaces()
        self.iface = self.params.get("interface", default="")
        if self.iface not in interfaces:
            self.cancel("%s interface is not available" % self.iface)
        self.ipaddr = self.params.get("host_ip", default="")
        self.netmask = self.params.get("netmask", default="")
        self.hbond = self.params.get("hbond", default=False)
        localhost = LocalHost()
        if self.hbond:
            self.networkinterface = NetworkInterface(self.iface,
                                                     localhost,
                                                     if_type='Bond')
        else:
            self.networkinterface = NetworkInterface(self.iface, localhost)
        try:
            self.networkinterface.add_ipaddr(self.ipaddr, self.netmask)
            self.networkinterface.save(self.ipaddr, self.netmask)
        except Exception:
            self.networkinterface.save(self.ipaddr, self.netmask)
        self.networkinterface.bring_up()
        self.session = Session(self.peer_ip,
                               user=self.peer_user,
                               password=self.peer_password)
        if not self.session.connect():
            self.cancel("failed connecting to peer")
        smm = SoftwareManager()
        for pkg in ["gcc", "autoconf", "perl", "m4", "libtool", "gcc-c++"]:
            if not smm.check_installed(pkg) and not smm.install(pkg):
                self.cancel("%s package is need to test" % pkg)
            cmd = "%s install %s" % (smm.backend.base_command, pkg)
            output = self.session.cmd(cmd)
            if not output.exit_status == 0:
                self.cancel(
                    "unable to install the package %s on peer machine " % pkg)

        detected_distro = distro.detect()
        pkg = "nmap"
        if detected_distro.name == 'rhel':
            if not smm.check_installed(pkg) and not smm.install(pkg):
                self.cancel("%s package Can not install" % pkg)
        if detected_distro.name == "SuSE":
            self.nmap = os.path.join(self.teststmpdir, 'nmap')
            nmap_download = self.params.get("nmap_download",
                                            default="https:"
                                            "//nmap.org/dist/"
                                            "nmap-7.80.tar.bz2")
            tarball = self.fetch_asset(nmap_download)
            self.version = os.path.basename(tarball.split('.tar')[0])
            self.n_map = os.path.join(self.nmap, self.version)
            archive.extract(tarball, self.nmap)
            os.chdir(self.n_map)
            process.system('./configure ppc64le', shell=True)
            build.make(self.n_map)
            process.system('./nping/nping -h', shell=True)

        if self.peer_ip == "":
            self.cancel("%s peer machine is not available" % self.peer_ip)
        self.mtu = self.params.get("mtu", default=1500)
        self.remotehost = RemoteHost(self.peer_ip,
                                     self.peer_user,
                                     password=self.peer_password)
        self.peer_interface = self.remotehost.get_interface_by_ipaddr(
            self.peer_ip).name
        self.peer_networkinterface = NetworkInterface(self.peer_interface,
                                                      self.remotehost)
        self.remotehost_public = RemoteHost(self.peer_public_ip,
                                            self.peer_user,
                                            password=self.peer_password)
        self.peer_public_networkinterface = NetworkInterface(
            self.peer_interface, self.remotehost_public)
        if self.peer_networkinterface.set_mtu(self.mtu) is not None:
            self.cancel("Failed to set mtu in peer")
        if self.networkinterface.set_mtu(self.mtu) is not None:
            self.cancel("Failed to set mtu in host")
        self.iperf = os.path.join(self.teststmpdir, 'iperf')
        iperf_download = self.params.get("iperf_download",
                                         default="https:"
                                         "//sourceforge.net/projects/iperf2/"
                                         "files/iperf-2.0.13.tar.gz")
        tarball = self.fetch_asset(iperf_download, expire='7d')
        archive.extract(tarball, self.iperf)
        self.version = os.path.basename(tarball.split('.tar')[0])
        self.iperf_dir = os.path.join(self.iperf, self.version)
        destination = "%s:/tmp" % self.peer_ip
        output = self.session.copy_files(self.iperf_dir,
                                         destination,
                                         recursive=True)
        if not output:
            self.cancel("unable to copy the iperf into peer machine")
        cmd = "cd /tmp/%s;./configure ppc64le;make" % self.version
        output = self.session.cmd(cmd)
        if not output.exit_status == 0:
            self.cancel("Unable to compile Iperf into peer machine")
        self.iperf_run = str(self.params.get("IPERF_SERVER_RUN", default=0))
        if self.iperf_run == '1':
            cmd = "/tmp/%s/src/iperf -s" % self.version
            cmd = self.session.get_raw_ssh_command(cmd)
            self.obj = SubProcess(cmd)
            self.obj.start()
        os.chdir(self.iperf_dir)
        process.system('./configure', shell=True)
        build.make(self.iperf_dir)
        self.iperf = os.path.join(self.iperf_dir, 'src')
        self.expected_tp = self.params.get("EXPECTED_THROUGHPUT", default="85")

    def nping(self):
        """
        Run nping test with tcp packets
        """
        detected_distro = distro.detect()
        if detected_distro.name == "SuSE":
            os.chdir(self.n_map)
            cmd = "./nping/nping --tcp %s -c 10" % self.peer_ip
            return process.run(cmd, verbose=False, shell=True)
        else:
            cmd = "nping --tcp %s -c 10" % self.peer_ip
            return process.run(cmd, verbose=False, shell=True)

    def test(self):
        """
        Test run is a One way throughput test. In this test, we have one host
        transmitting (or receiving) data from a client. This transmit large
        messages using multiple threads or processes.
        """
        speed = int(read_file("/sys/class/net/%s/speed" % self.iface))
        os.chdir(self.iperf)
        cmd = "./iperf -c %s" % self.peer_ip
        result = process.run(cmd, shell=True, ignore_status=True)
        nping_result = self.nping()
        if result.exit_status:
            self.fail("FAIL: Iperf Run failed")
        for line in result.stdout.decode("utf-8").splitlines():
            if 'local {}'.format(self.ipaddr) in line:
                id = line[3]
        for line in result.stdout.decode("utf-8").splitlines():
            if id in line and 'Mbits/sec' in line:
                tput = int(line.split()[6])
            elif id in line and 'Gbits/sec' in line:
                tput = int(float(line.split()[6])) * 1000
        if tput < (int(self.expected_tp) * speed) / 100:
            self.fail(
                "FAIL: Throughput Actual - %s%%, Expected - %s%%"
                ", Throughput Actual value - %s " %
                ((tput * 100) / speed, self.expected_tp, str(tput) + 'Mb/sec'))
        for line in nping_result.stdout.decode("utf-8").splitlines():
            if 'Raw packets' in line:
                lost = int(line.split("|")[2].split(" ")[2]) * 10
                if lost > 60:
                    self.fail("FAIL: Ping fails after iperf test")

    def tearDown(self):
        """
        Killing Iperf process in peer machine
        """
        cmd = "pkill iperf; rm -rf /tmp/%s" % self.version
        output = self.session.cmd(cmd)
        if not output.exit_status == 0:
            self.fail("Either the ssh to peer machine machine\
                       failed or iperf process was not killed")
        self.obj.stop()
        if self.networkinterface.set_mtu('1500') is not None:
            self.cancel("Failed to set mtu in host")
        try:
            self.peer_networkinterface.set_mtu('1500')
        except Exception:
            self.peer_public_networkinterface.set_mtu('1500')
        self.networkinterface.remove_ipaddr(self.ipaddr, self.netmask)
        try:
            self.networkinterface.restore_from_backup()
        except Exception:
            self.networkinterface.remove_cfg_file()
            self.log.info("backup file not availbale, could not restore file.")
        if self.hbond:
            self.networkinterface.restore_slave_cfg_file()
        self.remotehost.remote_session.quit()
        self.remotehost_public.remote_session.quit()
        self.session.quit()
class NVMfTest(Test):
    """
    NVM-Express over Fabrics using nvme-cli.
    """
    def setUp(self):
        """
        Sets up NVMf configuration
        """
        self.nss = self.params.get('namespaces', default='')
        self.peer_ips = self.params.get('peer_ips', default='')
        if not self.nss or not self.peer_ips:
            self.cancel("No inputs provided")
        self.peer_user = self.params.get("peer_user", default="root")
        self.peer_password = self.params.get("peer_password", default=None)
        self.nss = self.nss.split(' ')
        self.peer_ips = self.peer_ips.split(' ')
        self.ids = range(1, len(self.peer_ips) + 1)
        if len(self.nss) != len(self.peer_ips):
            self.cancel("Count of namespace and peer ips mismatch")
        smm = SoftwareManager()
        if not smm.check_installed("nvme-cli") and not \
                smm.install("nvme-cli"):
            self.cancel('nvme-cli is needed for the test to be run')
        try:
            if not linux_modules.module_is_loaded("nvme-rdma"):
                linux_modules.load_module("nvme-rdma")
        except CmdError:
            self.cancel("nvme-rdma module not loadable")
        self.cfg_tmpl = self.get_data("nvmf_template.cfg")
        dirname = os.path.dirname(os.path.abspath(self.cfg_tmpl))
        self.cfg_file = os.path.join(dirname, "nvmf.cfg")
        self.nvmf_discovery_file = "/etc/nvme/discovery.conf"

    def create_cfg_file(self):
        """
        Creates the config file for nvmetcli to use in the target
        """
        with open(self.cfg_tmpl, "r") as cfg_fp:
            cfg = yaml.safe_load(cfg_fp)

        subsys_template = list(cfg["subsystems"])
        ports_template = list(cfg["ports"])
        del cfg["subsystems"][0]
        del cfg["ports"][0]
        for i in range(len(self.ids)):
            cfg["subsystems"].append(copy.deepcopy(subsys_template[0]))
            cfg["ports"].append(copy.deepcopy(ports_template[0]))

            cfg_namespace = cfg["subsystems"][i]["namespaces"][0]
            cfg_namespace["device"]["nguid"] = str(i + 1).zfill(32)
            cfg_namespace["device"]["path"] = self.nss[i]
            cfg_namespace["nsid"] = str(i + 1)

            cfg["subsystems"][i]["nqn"] = "mysubsys%s" % str(i + 1)
            cfg["ports"][i]["addr"]["traddr"] = self.peer_ips[i]
            cfg["ports"][i]["subsystems"][0] = "mysubsys%s" % str(i + 1)
            cfg["ports"][i]["portid"] = str(i + 1)

        with open(self.cfg_file, "w") as cfg_fp:
            json.dump(cfg, cfg_fp, indent=2)

    @staticmethod
    def nvme_devs_count():
        """
        Returns count of nvme devices in the system
        """
        cmd = "nvme list"
        output = process.system_output(cmd, shell=True, ignore_status=True)
        count = max(len(output.splitlines()) - 2, 0)
        return count

    def test_targetconfig(self):
        """
        Configures the peer NVMf.
        """
        self.session = Session(self.peer_ips[0],
                               user=self.peer_user,
                               password=self.peer_password)
        if not self.session.connect():
            self.fail("failed connecting to peer")
        self.create_cfg_file()
        destination = "%s:/tmp/" % self.peer_ips[0]
        output = self.session.copy_files(self.cfg_file, destination)
        if not output:
            self.cancel("unable to copy the NVMf cfg file into peer machine")
        for mdl in ["nvmet", "nvmet-rdma"]:
            cmd = "modprobe %s" % mdl
            output = self.session.cmd(cmd)
            if output.exit_status:
                self.cancel("%s is not loadable on the peer" % mdl)
        msg = "which nvmetcli"
        output = self.session.cmd(msg)
        if output.exit_status:
            self.cancel("nvmetcli is not installed on the peer")
        msg = "nvmetcli restore /tmp/nvmf.cfg"
        output = self.session.cmd(msg)
        if output.exit_status:
            self.fail("nvmetcli setup config fails on peer")

    def test_nvmfdiscover(self):
        """
        Discovers NVMf subsystems on the initiator
        """
        for i in range(len(self.ids)):
            cmd = "nvme discover -t rdma -a %s -s 4420 -q mysubsys%s" % (
                self.peer_ips[i], str(i + 1))
            if process.system(cmd, shell=True, ignore_status=True) != 0:
                self.fail("Discover of mysubsys%s fails" % str(i + 1))

    def test_nvmfconnect(self):
        """
        Connects to NVMf subsystems on the initiator
        """
        pre_count = self.nvme_devs_count()
        for i in range(len(self.ids)):
            cmd = "nvme connect -t rdma -n mysubsys%s -a %s -s 4420" % (
                str(i + 1), self.peer_ips[i])
            if process.system(cmd, shell=True, ignore_status=True) != 0:
                self.fail("Connect to mysubsys%s fails" % str(i + 1))
            # Time needed to populate the device in nvme list command
            time.sleep(1)
        if (self.nvme_devs_count() - pre_count) != len(self.ids):
            self.fail("%d new nvme devices not added" % len(self.ids))

    def test_nvmfdisconnect(self):
        """
        Disconnects to NVMf subsystems on the initiator
        """
        pre_count = self.nvme_devs_count()
        for i in range(len(self.ids)):
            cmd = "nvme disconnect -n mysubsys%s" % str(i + 1)
            if process.system(cmd, shell=True, ignore_status=True) != 0:
                self.fail("Disconnect to mysubsys%s fails" % str(i + 1))
        if (pre_count - self.nvme_devs_count()) != len(self.ids):
            self.fail("%d new nvme devices not removed" % len(self.ids))

    def test_nvmfconnectcfg(self):
        """
        Connects to allNVMf subsystems in /etc/nvme/discovery.conf
        """
        if not os.path.exists(os.path.dirname(self.nvmf_discovery_file)):
            os.makedirs(os.path.dirname(self.nvmf_discovery_file))
        msg = []
        for i in range(len(self.ids)):
            msg.append("-t rdma -a %s -s 4420 -q mysubsys%s" %
                       (self.peer_ips[i], str(i + 1)))
        genio.write_file(self.nvmf_discovery_file, "\n".join(msg))
        process.system("cat %s" % self.nvmf_discovery_file)
        pre_count = self.nvme_devs_count()
        cmd = "nvme connect-all"
        if process.system(cmd, shell=True, ignore_status=True) != 0:
            self.fail("connect-all fails")
        if (self.nvme_devs_count() - pre_count) != len(self.ids):
            self.fail("%d new nvme devices not added" % len(self.ids))

    def test_nvmfdisconnectcfg(self):
        """
        Disconnects to NVMf subsystems on the initiator
        """
        pre_count = self.nvme_devs_count()
        for i in range(len(self.ids)):
            cmd = "nvme disconnect -n mysubsys%s" % str(i + 1)
            if process.system(cmd, shell=True, ignore_status=True) != 0:
                self.fail("Disconnect to mysubsys%s fails" % str(i + 1))
        genio.write_file(self.nvmf_discovery_file, "")
        if (pre_count - self.nvme_devs_count()) != len(self.ids):
            self.fail("%d new nvme devices not removed" % len(self.ids))

    def test_cleartargetconfig(self):
        """
        Clears the peer NVMf
        """
        self.session = Session(self.peer_ips[0],
                               user=self.peer_user,
                               password=self.peer_password)
        if not self.session.connect():
            self.fail("failed connecting to peer")
        msg = "nvmetcli clear"
        output = self.session.cmd(msg)
        if output.exit_status:
            self.fail("nvmetcli clear config remove on peer")
        msg = "rm -rf /tmp/nvmf.cfg"
        output = self.session.cmd(msg)
        if output.exit_status:
            self.log.warn("removing config file on peer failed")
class Netperf(Test):
    """
    Netperf Test
    """
    def setUp(self):
        """
        To check and install dependencies for the test
        """
        self.peer_user = self.params.get("peer_user", default="root")
        self.peer_public_ip = self.params.get("peer_public_ip", default="")
        self.peer_ip = self.params.get("peer_ip", default="")
        self.peer_password = self.params.get("peer_password",
                                             '*',
                                             default="None")
        interfaces = netifaces.interfaces()
        self.iface = self.params.get("interface", default="")
        if self.iface not in interfaces:
            self.cancel("%s interface is not available" % self.iface)
        self.ipaddr = self.params.get("host_ip", default="")
        self.netmask = self.params.get("netmask", default="")
        local = LocalHost()
        self.networkinterface = NetworkInterface(self.iface, local)
        try:
            self.networkinterface.add_ipaddr(self.ipaddr, self.netmask)
            self.networkinterface.save(self.ipaddr, self.netmask)
        except Exception:
            self.networkinterface.save(self.ipaddr, self.netmask)
        self.networkinterface.bring_up()
        self.session = Session(self.peer_ip,
                               user=self.peer_user,
                               password=self.peer_password)
        if not self.session.connect():
            self.cancel("failed connecting to peer")
        smm = SoftwareManager()
        detected_distro = distro.detect()
        pkgs = ['gcc']
        if detected_distro.name == "Ubuntu":
            pkgs.append('openssh-client')
        elif detected_distro.name == "SuSE":
            pkgs.append('openssh')
        else:
            pkgs.append('openssh-clients')
        for pkg in pkgs:
            if not smm.check_installed(pkg) and not smm.install(pkg):
                self.cancel("%s package is need to test" % pkg)
            cmd = "%s install %s" % (smm.backend.base_command, pkg)
            output = self.session.cmd(cmd)
            if not output.exit_status == 0:
                self.cancel(
                    "unable to install the package %s on peer machine " % pkg)
        if self.peer_ip == "":
            self.cancel("%s peer machine is not available" % self.peer_ip)
        self.timeout = self.params.get("TIMEOUT", default="600")
        self.mtu = self.params.get("mtu", default=1500)
        self.remotehost = RemoteHost(self.peer_ip,
                                     self.peer_user,
                                     password=self.peer_password)
        self.peer_interface = self.remotehost.get_interface_by_ipaddr(
            self.peer_ip).name
        self.peer_networkinterface = NetworkInterface(self.peer_interface,
                                                      self.remotehost)
        self.remotehost_public = RemoteHost(self.peer_public_ip,
                                            self.peer_user,
                                            password=self.peer_password)
        self.peer_public_networkinterface = NetworkInterface(
            self.peer_interface, self.remotehost_public)
        if self.peer_networkinterface.set_mtu(self.mtu) is not None:
            self.cancel("Failed to set mtu in peer")
        if self.networkinterface.set_mtu(self.mtu) is not None:
            self.cancel("Failed to set mtu in host")
        self.netperf_run = str(self.params.get("NETSERVER_RUN", default=0))
        self.netperf = os.path.join(self.teststmpdir, 'netperf')
        netperf_download = self.params.get("netperf_download",
                                           default="https:"
                                           "//github.com/HewlettPackard/"
                                           "netperf/archive/netperf-2.7.0.zip")
        tarball = self.fetch_asset(netperf_download, expire='7d')
        archive.extract(tarball, self.netperf)
        self.version = "%s-%s" % ("netperf",
                                  os.path.basename(tarball.split('.zip')[0]))
        self.neperf = os.path.join(self.netperf, self.version)
        destination = "%s:/tmp" % self.peer_ip
        output = self.session.copy_files(self.neperf,
                                         destination,
                                         recursive=True)
        if not output:
            self.cancel("unable to copy the netperf into peer machine")
        cmd = "cd /tmp/%s;./configure --build=powerpc64le;make" % self.version
        output = self.session.cmd(cmd)
        if not output.exit_status == 0:
            self.fail("test failed because command failed in peer machine")
        os.chdir(self.neperf)
        process.system('./configure --build=powerpc64le', shell=True)
        build.make(self.neperf)
        self.perf = os.path.join(self.neperf, 'src', 'netperf')
        self.expected_tp = self.params.get("EXPECTED_THROUGHPUT", default="90")
        self.duration = self.params.get("duration", default="300")
        self.min = self.params.get("minimum_iterations", default="1")
        self.max = self.params.get("maximum_iterations", default="15")
        self.option = self.params.get("option", default='')

    def test(self):
        """
        netperf test
        """
        if self.netperf_run == '1':
            cmd = "chmod 777 /tmp/%s/src" % self.version
            output = self.session.cmd(cmd)
            if not output.exit_status == 0:
                self.fail("test failed because netserver not available")
            cmd = "/tmp/%s/src/netserver" % self.version
            output = self.session.cmd(cmd)
            if not output.exit_status == 0:
                self.fail("test failed because netserver not available")
        speed = int(read_file("/sys/class/net/%s/speed" % self.iface))
        cmd = "timeout %s %s -H %s" % (self.timeout, self.perf, self.peer_ip)
        if self.option != "":
            if "TCP_STREAM" in self.option:
                socket_size = process.run("cat /proc/sys/net/ipv4/tcp_rmem",
                                          shell=True,
                                          ignore_status=True)
                cmd = "%s %s -m %s" % (
                    cmd, self.option,
                    socket_size.stdout.decode("utf-8").split('\t')[1])
            elif "UDP_STREAM" in self.option:
                socket_size = process.run("cat /proc/sys/net/ipv4/udp_mem",
                                          shell=True,
                                          ignore_status=True)
                cmd = "%s %s -m %s" % (
                    cmd, self.option,
                    socket_size.stdout.decode("utf-8").split('\t')[1])
            else:
                cmd = "%s -t %s" % (cmd, self.option)
        cmd = "%s -l %s -i %s,%s" % (cmd, self.duration, self.max, self.min)
        result = process.run(cmd, shell=True, ignore_status=True)
        if result.exit_status != 0:
            self.fail("FAIL: Run failed")
        for line in result.stdout.decode("utf-8").splitlines():
            if line and 'Throughput' in line.split()[-1]:
                tput = int(
                    result.stdout.decode("utf-8").split()[-1].split('.')[0])
                if tput < (int(self.expected_tp) * speed) / 100:
                    self.fail("FAIL: Throughput Actual - %s%%, Expected - %s%%"
                              ", Throughput Actual value - %s " %
                              ((tput * 100) / speed, self.expected_tp,
                               str(tput) + 'Mb/sec'))

        if 'WARNING' in result.stdout.decode("utf-8"):
            self.log.warn('Test completed with warning')

    def tearDown(self):
        """
        removing the data in peer machine
        """
        cmd = "pkill netserver; rm -rf /tmp/%s" % self.version
        output = self.session.cmd(cmd)
        if not output.exit_status == 0:
            self.fail("test failed because peer sys not connected")
        if self.networkinterface.set_mtu('1500') is not None:
            self.cancel("Failed to set mtu in host")
        try:
            self.peer_networkinterface.set_mtu('1500')
        except Exception:
            self.peer_public_networkinterface.set_mtu('1500')
        self.networkinterface.remove_ipaddr(self.ipaddr, self.netmask)
        try:
            self.networkinterface.restore_from_backup()
        except Exception:
            self.log.info("backup file not availbale, could not restore file.")
        self.remotehost.remote_session.quit()
        self.remotehost_public.remote_session.quit()
        self.session.quit()