environment.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import os
  2. import signal
  3. import socket
  4. import sys
  5. import time
  6. import traceback
  7. from contextlib import closing
  8. import psutil
  9. def before_scenario(context, scenario):
  10. context.debug = 'DEBUG' in os.environ and os.environ['DEBUG'] == 'ON'
  11. if context.debug:
  12. print("DEBUG=ON\n")
  13. print(f"\x1b[33;42mStarting new scenario: {scenario.name}!\x1b[0m\n")
  14. port = 8080
  15. if 'PORT' in os.environ:
  16. port = int(os.environ['PORT'])
  17. if is_server_listening("localhost", port):
  18. assert False, "Server already started"
  19. def after_scenario(context, scenario):
  20. try:
  21. if 'server_process' not in context or context.server_process is None:
  22. return
  23. if scenario.status == "failed":
  24. if 'GITHUB_ACTIONS' in os.environ:
  25. print(f"\x1b[33;101mSCENARIO FAILED: {scenario.name} server logs:\x1b[0m\n\n")
  26. if os.path.isfile('llama.log'):
  27. with closing(open('llama.log', 'r')) as f:
  28. for line in f:
  29. print(line)
  30. if not is_server_listening(context.server_fqdn, context.server_port):
  31. print("\x1b[33;101mERROR: Server stopped listening\x1b[0m\n")
  32. if not pid_exists(context.server_process.pid):
  33. assert False, f"Server not running pid={context.server_process.pid} ..."
  34. server_graceful_shutdown(context)
  35. # Wait few for socket to free up
  36. time.sleep(0.05)
  37. attempts = 0
  38. while pid_exists(context.server_process.pid) or is_server_listening(context.server_fqdn, context.server_port):
  39. server_kill(context)
  40. time.sleep(0.1)
  41. attempts += 1
  42. if attempts > 5:
  43. server_kill_hard(context)
  44. except:
  45. exc = sys.exception()
  46. print("error in after scenario: \n")
  47. print(exc)
  48. print("*** print_tb: \n")
  49. traceback.print_tb(exc.__traceback__, file=sys.stdout)
  50. def server_graceful_shutdown(context):
  51. print(f"shutting down server pid={context.server_process.pid} ...\n")
  52. if os.name == 'nt':
  53. os.kill(context.server_process.pid, signal.CTRL_C_EVENT)
  54. else:
  55. os.kill(context.server_process.pid, signal.SIGINT)
  56. def server_kill(context):
  57. print(f"killing server pid={context.server_process.pid} ...\n")
  58. context.server_process.kill()
  59. def server_kill_hard(context):
  60. pid = context.server_process.pid
  61. path = context.server_path
  62. print(f"Server dangling exits, hard killing force {pid}={path}...\n")
  63. try:
  64. psutil.Process(pid).kill()
  65. except psutil.NoSuchProcess:
  66. return False
  67. return True
  68. def is_server_listening(server_fqdn, server_port):
  69. with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
  70. result = sock.connect_ex((server_fqdn, server_port))
  71. _is_server_listening = result == 0
  72. if _is_server_listening:
  73. print(f"server is listening on {server_fqdn}:{server_port}...\n")
  74. return _is_server_listening
  75. def pid_exists(pid):
  76. try:
  77. psutil.Process(pid)
  78. except psutil.NoSuchProcess:
  79. return False
  80. return True