app_test.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from __future__ import unicode_literals
  2. import glob
  3. import json
  4. import os
  5. import re
  6. import threading
  7. import ttfw_idf
  8. from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
  9. from tiny_test_fw import Utility
  10. class IDEWSProtocol(WebSocket):
  11. def handleMessage(self):
  12. try:
  13. j = json.loads(self.data)
  14. except Exception as e:
  15. Utility.console_log('Server ignores error: {}'.format(e), 'orange')
  16. return
  17. event = j.get('event')
  18. if event and 'prog' in j and ((event == 'gdb_stub' and 'port' in j) or
  19. (event == 'coredump' and 'file' in j)):
  20. payload = {'event': 'debug_finished'}
  21. self.sendMessage(json.dumps(payload))
  22. Utility.console_log('Server sent: {}'.format(payload))
  23. else:
  24. Utility.console_log('Server received: {}'.format(j), 'orange')
  25. def handleConnected(self):
  26. Utility.console_log('{} connected to server'.format(self.address))
  27. def handleClose(self):
  28. Utility.console_log('{} closed the connection'.format(self.address))
  29. class WebSocketServer(object):
  30. HOST = '127.0.0.1'
  31. PORT = 1123
  32. def run(self):
  33. server = SimpleWebSocketServer(self.HOST, self.PORT, IDEWSProtocol)
  34. while not self.exit_event.is_set():
  35. server.serveonce()
  36. def __init__(self):
  37. self.exit_event = threading.Event()
  38. self.thread = threading.Thread(target=self.run)
  39. self.thread.start()
  40. def __enter__(self):
  41. return self
  42. def __exit__(self, exc_type, exc_value, traceback):
  43. self.exit_event.set()
  44. self.thread.join(10)
  45. if self.thread.is_alive():
  46. Utility.console_log('Thread cannot be joined', 'orange')
  47. @ttfw_idf.idf_custom_test(env_tag='test_jtag_arm', group='test-apps')
  48. def test_monitor_ide_integration(env, extra_data):
  49. config_files = glob.glob(os.path.join(os.path.dirname(__file__), 'sdkconfig.ci.*'))
  50. config_names = [os.path.basename(s).replace('sdkconfig.ci.', '') for s in config_files]
  51. rel_proj_path = 'tools/test_apps/system/monitor_ide_integration'
  52. for name in config_names:
  53. Utility.console_log('Checking config "{}"... '.format(name), 'green', end='')
  54. dut = env.get_dut('panic', rel_proj_path, app_config_name=name)
  55. monitor_path = os.path.join(dut.app.idf_path, 'tools/idf_monitor.py')
  56. elf_path = os.path.join(dut.app.binary_path, 'panic.elf')
  57. dut.start_app()
  58. # Closing the DUT because we will reconnect with IDF Monitor
  59. env.close_dut(dut.name)
  60. with WebSocketServer(), ttfw_idf.CustomProcess(' '.join([monitor_path,
  61. elf_path,
  62. '--port', str(dut.port),
  63. '--ws', 'ws://{}:{}'.format(WebSocketServer.HOST,
  64. WebSocketServer.PORT)]),
  65. logfile='monitor_{}.log'.format(name)) as p:
  66. p.pexpect_proc.expect(re.compile(r'Guru Meditation Error'), timeout=10)
  67. p.pexpect_proc.expect_exact('Communicating through WebSocket', timeout=5)
  68. # "u?" is for Python 2 only in the following regular expressions.
  69. # The elements of dictionary can be printed in different order depending on the Python version.
  70. p.pexpect_proc.expect(re.compile(r"WebSocket sent: \{u?.*'event': u?'" + name + "'"), timeout=5)
  71. p.pexpect_proc.expect_exact('Waiting for debug finished event', timeout=5)
  72. p.pexpect_proc.expect(re.compile(r"WebSocket received: \{u?'event': u?'debug_finished'\}"), timeout=5)
  73. p.pexpect_proc.expect_exact('Communications through WebSocket is finished', timeout=5)
  74. if __name__ == '__main__':
  75. test_monitor_ide_integration()