test_devices.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python2
  2. #
  3. # Show/check devices
  4. # Copyright (c) 2016, Tieto Corporation
  5. #
  6. # This software may be distributed under the terms of the BSD license.
  7. # See README for more details.
  8. import time
  9. import traceback
  10. import config
  11. import os
  12. import sys
  13. import getopt
  14. import re
  15. import logging
  16. logger = logging.getLogger()
  17. import utils
  18. from remotehost import Host
  19. from wpasupplicant import WpaSupplicant
  20. import hostapd
  21. def show_devices(devices, setup_params):
  22. """Show/check available devices"""
  23. print "Devices:"
  24. for device in devices:
  25. dev = config.get_device(devices, device['name'])
  26. host = Host(host = dev['hostname'], ifname = dev['ifname'],
  27. port = dev['port'], name = dev['name'])
  28. # simple check if authorized_keys works correctly
  29. status, buf = host.execute(["id"])
  30. if status != 0:
  31. print "[" + host.name + "] - ssh communication: FAILED"
  32. continue
  33. else:
  34. print "[" + host.name + "] - ssh communication: OK"
  35. # check setup_hw works correctly
  36. try:
  37. setup_hw = setup_params['setup_hw']
  38. try:
  39. restart_device = setup_params['restart_device']
  40. except:
  41. restart_device = "0"
  42. host.execute([setup_hw, "-I", host.ifname, "-R", restart_device])
  43. except:
  44. pass
  45. # show uname
  46. status, buf = host.execute(["uname", "-s", "-n", "-r", "-m", "-o"])
  47. print "\t" + buf
  48. # show ifconfig
  49. status, buf = host.execute(["ifconfig", host.ifname])
  50. if status != 0:
  51. print "\t" + host.ifname + " failed\n"
  52. continue
  53. lines = buf.splitlines()
  54. for line in lines:
  55. print "\t" + line
  56. # check hostapd, wpa_supplicant, iperf exist
  57. status, buf = host.execute([setup_params['wpa_supplicant'], "-v"])
  58. if status != 0:
  59. print "\t" + setup_params['wpa_supplicant'] + " not find\n"
  60. continue
  61. lines = buf.splitlines()
  62. for line in lines:
  63. print "\t" + line
  64. print ""
  65. status, buf = host.execute([setup_params['hostapd'], "-v"])
  66. if status != 1:
  67. print "\t" + setup_params['hostapd'] + " not find\n"
  68. continue
  69. lines = buf.splitlines()
  70. for line in lines:
  71. print "\t" + line
  72. print ""
  73. status, buf = host.execute([setup_params['iperf'], "-v"])
  74. if status != 0 and status != 1:
  75. print "\t" + setup_params['iperf'] + " not find\n"
  76. continue
  77. lines = buf.splitlines()
  78. for line in lines:
  79. print "\t" + line
  80. print ""
  81. def check_device(devices, setup_params, dev_name, monitor=False):
  82. dev = config.get_device(devices, dev_name)
  83. host = Host(host = dev['hostname'], ifname = dev['ifname'],
  84. port = dev['port'], name = dev['name'])
  85. # simple check if authorized_keys works correctly
  86. status, buf = host.execute(["id"])
  87. if status != 0:
  88. raise Exception(dev_name + " - ssh communication FAILED: " + buf)
  89. ifaces = re.split('; | |, ', host.ifname)
  90. # try to setup host/ifaces
  91. for iface in ifaces:
  92. try:
  93. setup_hw = setup_params['setup_hw']
  94. try:
  95. restart_device = setup_params['restart_device']
  96. except:
  97. restart_device = "0"
  98. host.execute(setup_hw + " -I " + iface + " -R " + restart_device)
  99. except:
  100. pass
  101. # check interfaces (multi for monitor)
  102. for iface in ifaces:
  103. status, buf = host.execute(["ifconfig", iface])
  104. if status != 0:
  105. raise Exception(dev_name + " ifconfig " + iface + " failed: " + buf)
  106. # monitor doesn't need wpa_supplicant/hostapd ...
  107. if monitor == True:
  108. return
  109. status, buf = host.execute(["ls", "-l", setup_params['wpa_supplicant']])
  110. if status != 0:
  111. raise Exception(dev_name + " - wpa_supplicant: " + buf)
  112. status, buf = host.execute(["ls", "-l", setup_params['hostapd']])
  113. if status != 0:
  114. raise Exception(dev_name + " - hostapd: " + buf)
  115. status, buf = host.execute(["which", setup_params['iperf']])
  116. if status != 0:
  117. raise Exception(dev_name + " - hostapd: " + buf)
  118. status, buf = host.execute(["which", "tshark"])
  119. if status != 0:
  120. logger.debug(dev_name + " - hostapd: " + buf)
  121. def check_devices(devices, setup_params, refs, duts, monitors):
  122. """Check duts/refs/monitors devices"""
  123. for dut in duts:
  124. check_device(devices, setup_params, dut)
  125. for ref in refs:
  126. check_device(devices, setup_params, ref)
  127. for monitor in monitors:
  128. if monitor == "all":
  129. continue
  130. check_device(devices, setup_params, monitor, monitor=True)