hwsim_wrapper.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Hwsim wrapper
  2. # Copyright (c) 2016, Tieto Corporation
  3. #
  4. # This software may be distributed under the terms of the BSD license.
  5. # See README for more details.
  6. import remotehost
  7. from wpasupplicant import WpaSupplicant
  8. import hostapd
  9. import config
  10. import rutils
  11. import monitor
  12. import traceback
  13. import logging
  14. logger = logging.getLogger()
  15. def run_hwsim_test(devices, setup_params, refs, duts, monitors, hwsim_test):
  16. try:
  17. ref_hosts = []
  18. dut_hosts = []
  19. dev = []
  20. apdev = []
  21. # get hosts
  22. for ref in refs:
  23. ref_host = rutils.get_host(devices, ref)
  24. ref_hosts.append(ref_host)
  25. for dut in duts:
  26. dut_host = rutils.get_host(devices, dut)
  27. dut_hosts.append(dut_host)
  28. # setup log dir
  29. local_log_dir = setup_params['local_log_dir']
  30. # setup hw before test
  31. rutils.setup_hw(ref_hosts, setup_params)
  32. rutils.setup_hw(dut_hosts, setup_params)
  33. # run monitors if requested/possible
  34. for ref_host in ref_hosts:
  35. monitor.add(ref_host, monitors)
  36. monitor.run(ref_host, setup_params)
  37. for dut_host in dut_hosts:
  38. monitor.add(dut_host, monitors)
  39. monitor.run(dut_host, setup_params)
  40. # run hostapd/wpa_supplicant
  41. for ref_host in ref_hosts:
  42. rutils.run_wpasupplicant(ref_host, setup_params)
  43. wpas = WpaSupplicant(hostname=ref_host.host, global_iface="udp",
  44. global_port=ref_host.port)
  45. wpas.interface_add(ref_host.ifname)
  46. dev.append(wpas)
  47. for dut_host in dut_hosts:
  48. rutils.run_hostapd(dut_host, setup_params)
  49. dut_host.dev['bssid'] = rutils.get_mac_addr(dut_host)
  50. apdev.append(dut_host.dev)
  51. # run hwsim test/currently only 2 params tests
  52. if hwsim_test.func_code.co_argcount == 1:
  53. hwsim_test(dev)
  54. elif hwsim_test.func_code.co_argcount == 2:
  55. hwsim_test(dev, apdev)
  56. else:
  57. raise Exception("more than 2 arguments required")
  58. # hostapd/wpa_supplicant cleanup
  59. for wpas in dev:
  60. wpas.interface_remove(wpas.host.ifname)
  61. wpas.terminate()
  62. dev = []
  63. # remove monitors
  64. for ref_host in ref_hosts:
  65. monitor.remove(ref_host)
  66. for dut_host in dut_hosts:
  67. monitor.remove(dut_host)
  68. for ref_host in ref_hosts:
  69. ref_host.execute(["killall", "wpa_supplicant"])
  70. ref_host.get_logs(local_log_dir)
  71. for dut_host in dut_hosts:
  72. dut_host.execute(["killall", "hostapd"])
  73. dut_host.get_logs(local_log_dir)
  74. return ""
  75. except:
  76. logger.info(traceback.format_exc())
  77. for wpas in dev:
  78. try:
  79. wpas.interface_remove(wpas.host.ifname)
  80. wpas.terminate()
  81. except:
  82. pass
  83. for ref_host in ref_hosts:
  84. monitor.remove(ref_host)
  85. for dut_host in dut_hosts:
  86. monitor.remove(dut_host)
  87. for ref_host in ref_hosts:
  88. ref_host.execute(["killall", "wpa_supplicant"])
  89. ref_host.get_logs(local_log_dir)
  90. for dut_host in dut_hosts:
  91. dut_host.execute(["killall", "hostapd"])
  92. dut_host.get_logs(local_log_dir)
  93. raise