testNIC.py 4.89 KB
Newer Older
manuel's avatar
manuel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#!/usr/bin/env python3
# @file testNIC.py
#
# @brief Check the correct operation of the Network Interface Core
#
# @author Manuel Castilla
# @date 16/04/2019
#
###########################################################################

from ssh import SshCmd,SshCmdException
from vuart import Vuart
import configparser

def configurationIpInterface(hostName,userName,password,busId,interfaceName,ip):
    """
    Set ip of white rabbit interface

    Args:
        hostName: ip remote host
        username: user name
        password: password
        busId: number PCIe bus
        interfaceName : interface name
        ip : ip set interface
    Raises:
        error function
    """
    try:
        sshCmd = SshCmd()
31 32 33 34 35 36 37

        #check network manager
        command = "sudo service network-manager status | grep running"
        value = sshCmd.exec(hostName,command,userName,password)
        if ("running" in value):
            print("\nWarning: The network manager is running on host %s, the test may not work correctly\n" % ip)

manuel's avatar
manuel committed
38 39 40 41 42
        command = "sudo ifconfig " + interfaceName + " " + ip
        sshCmd.exec(hostName,command,userName,password)
        command = "ifconfig | grep " + ip + " | wc -l"
        value = sshCmd.exec(hostName,command,userName,password)
        if (int(value) == 1):
43 44 45
            print("Configuration ip interface: ok on host:",hostName)
        else:
            raise
manuel's avatar
manuel committed
46 47
    except SshCmdException as e:
        sshCmd.printSshErrorCommand(e.strError,command)
48
        print("Configuration ip interface: error on host:",hostName)
manuel's avatar
manuel committed
49 50
        raise
    except:
51
        print("Configuration ip interface: error on host:",hostName)
manuel's avatar
manuel committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        raise


def testIperf(hostName,userName,password,ip,hostName2,userName2,password2):
    """
    Test iperf by white rabbit interfaces

    Args:
        hostName: ip remote host client
        username: user name client
        password: password
        ip : ip server
        hostName2: ip remote host server
        username2: user name server
        password2: password
    Raises:
        error function
    """
    try:
        sshCmdServer = SshCmd()
        sshCmdClient = SshCmd()
        command = "iperf -s"
        sshCmdServer.exec(hostName2,command,userName2,password2,withResponse=False)
        command = "iperf -t 4 -c " + str(ip)
        value = sshCmdClient.exec(hostName,command,userName,password)

        if ("failed" in value):
            print("Test iperf: error")
            raise
        else:
            print(value)

        print("Test iperf: ok")
    except SshCmdException as e:
        print("Test iperf: error")
        print(e.strError)
        raise


#--------------------------------------------------#
#--------------------------------------------------#
# MAIN
#--------------------------------------------------#
#--------------------------------------------------#

def main(configuration):

    hostName1 = configuration["hostName1"]
    userName1 = configuration["userName1"]
    password1 = configuration["password1"]
    busIdSpec1 = configuration["busIdSpec1"]
    ipWR1 = configuration["ipWR1"]
    interfaceName1 = configuration["interfaceName1"]
    hostName2 = configuration["hostName2"]
    userName2 = configuration["userName2"]
    password2 = configuration["password2"]
    busIdSpec2 = configuration["busIdSpec2"]
    ipWR2 = configuration["ipWR2"]
    interfaceName2 = configuration["interfaceName2"]

    try:
113 114
        print("\nExecuting NIC test")
        print("This test needs the network manager stopped")
manuel's avatar
manuel committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
        configurationIpInterface(hostName1,userName1,password1,busIdSpec1,interfaceName1,ipWR1)
        configurationIpInterface(hostName2,userName2,password2,busIdSpec2,interfaceName2,ipWR2)
        testIperf(hostName1,userName1,password1,ipWR2,hostName2,userName2,password2)
        print("Test NIC: successful")
    except:
        print("Test NIC: error")


if __name__ == '__main__':
    try:
        configuration = {}
        nameConfig = "configuration.cfg"

        config = configparser.ConfigParser()
        config.readfp(open(nameConfig))

        configuration["hostName1"] = config.get('pc1',"hostName")
        configuration["userName1"] = config.get('pc1',"userName")
        configuration["password1"] = config.get('pc1',"password")
        configuration["busIdSpec1"] = config.get('pc1',"busIdSpec")
        configuration["ipWR1"] = config.get('pc1',"ipWR")
        configuration["interfaceName1"] = config.get('pc1',"interfaceName")
        configuration["hostName2"] = config.get('pc2',"hostName")
        configuration["userName2"] = config.get('pc2',"userName")
        configuration["password2"] = config.get('pc2',"password")
        configuration["busIdSpec2"] = config.get('pc2',"busIdSpec")
        configuration["ipWR2"] = config.get('pc2',"ipWR")
        configuration["interfaceName2"] = config.get('pc2',"interfaceName")

        main(configuration)
    except configparser.Error as e:
        print("Error load configuration:",e.message)