test_example.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Example test case
  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 logging
  13. logger = logging.getLogger()
  14. def test_example(devices, setup_params, refs, duts, monitors):
  15. """TC example - simple connect and ping test"""
  16. try:
  17. sta = None
  18. ap = None
  19. hapd = None
  20. wpas = None
  21. mon = None
  22. # get hosts based on name
  23. sta = rutils.get_host(devices, duts[0])
  24. ap = rutils.get_host(devices, refs[0])
  25. # setup log dir
  26. local_log_dir = setup_params['local_log_dir']
  27. # setup hw before test
  28. rutils.setup_hw([sta, ap], setup_params)
  29. # run traces if requested
  30. rutils.trace_start([sta], setup_params)
  31. # run perf if requested
  32. rutils.perf_start([sta], setup_params)
  33. # run hostapd/wpa_supplicant
  34. rutils.run_wpasupplicant(sta, setup_params)
  35. rutils.run_hostapd(ap, setup_params)
  36. # get ap_params
  37. ap_params = rutils.get_ap_params(channel="1", bw="HT20", country="US",
  38. security="open")
  39. # Add monitors if requested
  40. monitor_hosts = monitor.create(devices, setup_params, refs, duts,
  41. monitors)
  42. if len(monitor_hosts) > 0:
  43. mon = monitor_hosts[0]
  44. monitor.add(sta, monitors)
  45. monitor.add(ap, monitors)
  46. # connect to hostapd/wpa_supplicant UDP CTRL iface
  47. hapd = hostapd.add_ap(ap.dev, ap_params)
  48. freq = hapd.get_status_field("freq")
  49. wpas = WpaSupplicant(hostname = sta.host, global_iface="udp",
  50. global_port = sta.port)
  51. wpas.interface_add(sta.ifname)
  52. # setup standalone monitor based on hapd; could be multi interface
  53. # monitor
  54. monitor_param = monitor.get_monitor_params(hapd)
  55. monitor.setup(mon, [monitor_param])
  56. # run monitors
  57. monitor.run(sta, setup_params)
  58. monitor.run(ap, setup_params)
  59. monitor.run(mon, setup_params)
  60. # connect wpa_supplicant to hostapd
  61. wpas.connect(ap_params['ssid'], key_mgmt="NONE", scan_freq=freq)
  62. # run ping test
  63. ap_sta, sta_ap = rutils.check_connectivity(ap, sta, "ipv6")
  64. # remove/destroy monitors
  65. monitor.remove(sta)
  66. monitor.remove(ap)
  67. monitor.destroy(devices, monitor_hosts)
  68. # hostapd/wpa_supplicant cleanup
  69. wpas.interface_remove(sta.ifname)
  70. wpas.terminate()
  71. hapd.close_ctrl()
  72. hostapd.remove_bss(ap.dev)
  73. hostapd.terminate(ap.dev)
  74. # stop perf
  75. rutils.perf_stop([sta], setup_params)
  76. # stop traces
  77. rutils.trace_stop([sta], setup_params)
  78. # get wpa_supplicant/hostapd/tshark logs
  79. sta.get_logs(local_log_dir)
  80. ap.get_logs(local_log_dir)
  81. if mon:
  82. mon.get_logs(local_log_dir)
  83. return "packet_loss: " + ap_sta + ", " + sta_ap
  84. except:
  85. rutils.perf_stop([sta], setup_params)
  86. rutils.trace_stop([sta], setup_params)
  87. if wpas:
  88. try:
  89. wpas.interface_remove(sta.ifname)
  90. wpas.terminate()
  91. except:
  92. pass
  93. if hapd:
  94. try:
  95. hapd.close_ctrl()
  96. hostapd.remove_bss(ap.dev)
  97. hostapd.terminate(ap.dev)
  98. except:
  99. pass
  100. if mon:
  101. monitor.destroy(devices, monitor_hosts)
  102. mon.get_logs(local_log_dir)
  103. if sta:
  104. monitor.remove(sta)
  105. dmesg = setup_params['log_dir'] + setup_params['tc_name'] + "_" + sta.name + "_" + sta.ifname + ".dmesg"
  106. sta.execute(["dmesg", "-c", ">", dmesg])
  107. sta.add_log(dmesg)
  108. sta.get_logs(local_log_dir)
  109. sta.execute(["ifconfig", sta.ifname, "down"])
  110. if ap:
  111. monitor.remove(ap)
  112. dmesg = setup_params['log_dir'] + setup_params['tc_name'] + "_" + ap.name + "_" + ap.ifname + ".dmesg"
  113. ap.execute(["dmesg", "-c", ">", dmesg])
  114. ap.add_log(dmesg)
  115. ap.get_logs(local_log_dir)
  116. ap.execute(["ifconfig", ap.ifname, " down"])
  117. raise