hwsim_wrapper.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 wlantest
  14. import logging
  15. logger = logging.getLogger()
  16. def run_hwsim_test(devices, setup_params, refs, duts, monitors, hwsim_test):
  17. try:
  18. ref_hosts = []
  19. dut_hosts = []
  20. dev = []
  21. apdev = []
  22. # get hosts
  23. for ref in refs:
  24. ref_host = rutils.get_host(devices, ref)
  25. ref_hosts.append(ref_host)
  26. for dut in duts:
  27. dut_host = rutils.get_host(devices, dut)
  28. dut_hosts.append(dut_host)
  29. # setup log dir
  30. local_log_dir = setup_params['local_log_dir']
  31. # setup hw before test
  32. rutils.setup_hw(ref_hosts, setup_params)
  33. rutils.setup_hw(dut_hosts, setup_params)
  34. # run monitors if requested/possible
  35. for ref_host in ref_hosts:
  36. monitor.add(ref_host, monitors)
  37. monitor.run(ref_host, setup_params)
  38. for dut_host in dut_hosts:
  39. monitor.add(dut_host, monitors)
  40. monitor.run(dut_host, setup_params)
  41. monitor_hosts = monitor.create(devices, setup_params, refs, duts,
  42. monitors)
  43. mon = None
  44. if len(monitor_hosts) > 0:
  45. mon = monitor_hosts[0]
  46. wlantest.Wlantest.reset_remote_wlantest()
  47. wlantest.Wlantest.register_remote_wlantest(mon, setup_params,
  48. monitor)
  49. # run hostapd/wpa_supplicant
  50. for ref_host in ref_hosts:
  51. rutils.run_wpasupplicant(ref_host, setup_params)
  52. wpas = WpaSupplicant(hostname=ref_host.host, global_iface="udp",
  53. global_port=ref_host.port)
  54. wpas.interface_add(ref_host.ifname)
  55. dev.append(wpas)
  56. for dut_host in dut_hosts:
  57. rutils.run_hostapd(dut_host, setup_params)
  58. dut_host.dev['bssid'] = rutils.get_mac_addr(dut_host)
  59. apdev.append(dut_host.dev)
  60. # run hwsim test/currently only 2 params tests
  61. if hwsim_test.func_code.co_argcount == 1:
  62. hwsim_test(dev)
  63. elif hwsim_test.func_code.co_argcount == 2:
  64. hwsim_test(dev, apdev)
  65. else:
  66. raise Exception("more than 2 arguments required")
  67. # hostapd/wpa_supplicant cleanup
  68. for wpas in dev:
  69. wpas.interface_remove(wpas.host.ifname)
  70. wpas.terminate()
  71. dev = []
  72. # remove monitors
  73. for ref_host in ref_hosts:
  74. monitor.remove(ref_host)
  75. for dut_host in dut_hosts:
  76. monitor.remove(dut_host)
  77. for ref_host in ref_hosts:
  78. ref_host.execute(["killall", "wpa_supplicant"])
  79. ref_host.get_logs(local_log_dir)
  80. for dut_host in dut_hosts:
  81. dut_host.execute(["killall", "hostapd"])
  82. dut_host.get_logs(local_log_dir)
  83. if mon is not None:
  84. wlantest.Wlantest.reset_remote_wlantest()
  85. mon.get_logs(local_log_dir)
  86. return ""
  87. except:
  88. logger.info(traceback.format_exc())
  89. for wpas in dev:
  90. try:
  91. wpas.interface_remove(wpas.host.ifname)
  92. wpas.terminate()
  93. except:
  94. pass
  95. for ref_host in ref_hosts:
  96. monitor.remove(ref_host)
  97. for dut_host in dut_hosts:
  98. monitor.remove(dut_host)
  99. for ref_host in ref_hosts:
  100. ref_host.execute(["killall", "wpa_supplicant"])
  101. ref_host.get_logs(local_log_dir)
  102. for dut_host in dut_hosts:
  103. dut_host.execute(["killall", "hostapd"])
  104. dut_host.get_logs(local_log_dir)
  105. if mon is not None:
  106. wlantest.Wlantest.reset_remote_wlantest()
  107. mon.get_logs(local_log_dir)
  108. raise