2024-02-24 11:28:55 +00:00
|
|
|
import os
|
2024-03-17 18:12:37 +00:00
|
|
|
import signal
|
2024-02-24 11:28:55 +00:00
|
|
|
import socket
|
2024-03-17 18:12:37 +00:00
|
|
|
import sys
|
2024-02-24 11:28:55 +00:00
|
|
|
import time
|
2024-03-17 18:12:37 +00:00
|
|
|
import traceback
|
2024-02-24 11:28:55 +00:00
|
|
|
from contextlib import closing
|
2024-03-20 05:33:49 +00:00
|
|
|
from subprocess import TimeoutExpired
|
2024-02-24 11:28:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def before_scenario(context, scenario):
|
2024-03-02 21:00:14 +00:00
|
|
|
context.debug = 'DEBUG' in os.environ and os.environ['DEBUG'] == 'ON'
|
|
|
|
if context.debug:
|
2024-03-20 05:33:49 +00:00
|
|
|
print("DEBUG=ON")
|
|
|
|
print(f"\x1b[33;42mStarting new scenario: {scenario.name}!\x1b[0m")
|
2024-02-24 11:28:55 +00:00
|
|
|
port = 8080
|
|
|
|
if 'PORT' in os.environ:
|
|
|
|
port = int(os.environ['PORT'])
|
|
|
|
if is_server_listening("localhost", port):
|
|
|
|
assert False, "Server already started"
|
|
|
|
|
|
|
|
|
|
|
|
def after_scenario(context, scenario):
|
2024-03-17 18:12:37 +00:00
|
|
|
try:
|
|
|
|
if 'server_process' not in context or context.server_process is None:
|
|
|
|
return
|
|
|
|
if scenario.status == "failed":
|
|
|
|
if 'GITHUB_ACTIONS' in os.environ:
|
2024-03-20 05:33:49 +00:00
|
|
|
print(f"\x1b[33;101mSCENARIO FAILED: {scenario.name} server logs:\x1b[0m\n")
|
2024-03-17 18:12:37 +00:00
|
|
|
if os.path.isfile('llama.log'):
|
|
|
|
with closing(open('llama.log', 'r')) as f:
|
|
|
|
for line in f:
|
|
|
|
print(line)
|
|
|
|
if not is_server_listening(context.server_fqdn, context.server_port):
|
2024-03-20 05:33:49 +00:00
|
|
|
print("\x1b[33;101mERROR: Server stopped listening\x1b[0m")
|
2024-03-17 18:12:37 +00:00
|
|
|
|
2024-03-20 05:33:49 +00:00
|
|
|
if context.server_process.poll() is not None:
|
2024-03-17 18:12:37 +00:00
|
|
|
assert False, f"Server not running pid={context.server_process.pid} ..."
|
|
|
|
|
2024-03-20 05:33:49 +00:00
|
|
|
server_graceful_shutdown(context) # SIGINT
|
2024-03-17 18:12:37 +00:00
|
|
|
|
2024-03-20 05:33:49 +00:00
|
|
|
try:
|
|
|
|
context.server_process.wait(0.5)
|
|
|
|
except TimeoutExpired:
|
|
|
|
print(f"server still alive after 500ms, force-killing pid={context.server_process.pid} ...")
|
|
|
|
context.server_process.kill() # SIGKILL
|
|
|
|
context.server_process.wait()
|
2024-03-17 18:12:37 +00:00
|
|
|
|
2024-03-20 05:33:49 +00:00
|
|
|
while is_server_listening(context.server_fqdn, context.server_port):
|
2024-03-17 18:12:37 +00:00
|
|
|
time.sleep(0.1)
|
2024-03-20 05:33:49 +00:00
|
|
|
except Exception:
|
|
|
|
print("ignoring error in after_scenario:")
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
2024-03-10 17:17:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def server_graceful_shutdown(context):
|
2024-03-20 05:33:49 +00:00
|
|
|
print(f"shutting down server pid={context.server_process.pid} ...")
|
2024-03-10 17:17:47 +00:00
|
|
|
if os.name == 'nt':
|
2024-03-20 05:33:49 +00:00
|
|
|
interrupt = signal.CTRL_C_EVENT
|
2024-03-10 17:17:47 +00:00
|
|
|
else:
|
2024-03-20 05:33:49 +00:00
|
|
|
interrupt = signal.SIGINT
|
|
|
|
context.server_process.send_signal(interrupt)
|
2024-02-24 11:28:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_server_listening(server_fqdn, server_port):
|
|
|
|
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
|
|
|
|
result = sock.connect_ex((server_fqdn, server_port))
|
2024-03-10 17:17:47 +00:00
|
|
|
_is_server_listening = result == 0
|
|
|
|
if _is_server_listening:
|
2024-03-20 05:33:49 +00:00
|
|
|
print(f"server is listening on {server_fqdn}:{server_port}...")
|
2024-03-10 17:17:47 +00:00
|
|
|
return _is_server_listening
|