add ready checks to reversecan and sessions tests

This commit is contained in:
florian 2023-10-30 14:02:59 +01:00
parent fa628cb7d6
commit cd773b6e80
No known key found for this signature in database
GPG key ID: 93EE47CC3D061500
4 changed files with 52 additions and 18 deletions

View file

@ -21,7 +21,7 @@ from pathlib import Path
from shutil import copy, rmtree from shutil import copy, rmtree
from signal import SIGINT, SIGTERM, signal, SIGHUP from signal import SIGINT, SIGTERM, signal, SIGHUP
from stat import S_IEXEC from stat import S_IEXEC
from subprocess import run as subprocess_run, DEVNULL, STDOUT from subprocess import run as subprocess_run, DEVNULL, STDOUT, PIPE
from sys import path as sys_path from sys import path as sys_path
from tarfile import open as tar_open from tarfile import open as tar_open
from threading import Thread from threading import Thread
@ -532,12 +532,13 @@ if __name__ == "__main__":
stderr=STDOUT, stderr=STDOUT,
env=env.copy(), env=env.copy(),
check=False, check=False,
stdout=PIPE
) )
if proc.returncode == 0: if proc.returncode == 0:
logger.info("Successfully sent reload signal to nginx") logger.info("Successfully sent reload signal to nginx")
else: else:
logger.error( logger.error(
f"Error while reloading nginx - returncode: {proc.returncode} - error: {proc.stderr.decode('utf-8') if proc.stderr else 'Missing stderr'}", f"Error while reloading nginx - returncode: {proc.returncode} - error: {proc.stdout.decode('utf-8') if proc.stdout else 'no output'}",
) )
# # Stop temp nginx # # Stop temp nginx
# logger.info("Stopping temp nginx ...") # logger.info("Stopping temp nginx ...")

View file

@ -179,6 +179,8 @@ try:
global_values = session.query(Global_values).all() global_values = session.query(Global_values).all()
for global_value in global_values: for global_value in global_values:
if global_value.setting_id == "API_LISTEN_IP":
continue
if global_value.setting_id in global_settings: if global_value.setting_id in global_settings:
if global_value.value != global_settings[global_value.setting_id]["value"]: if global_value.value != global_settings[global_value.setting_id]["value"]:
print( print(

View file

@ -6,6 +6,8 @@ from requests import get
from multiprocessing import Process from multiprocessing import Process
from traceback import format_exc from traceback import format_exc
from uvicorn import run from uvicorn import run
from contextlib import suppress
from requests.exceptions import RequestException
fastapi_proc = None fastapi_proc = None
@ -17,6 +19,28 @@ if getenv("TEST_TYPE", "docker") == "docker":
sleep(1) sleep(1)
try: try:
ready = False
retries = 0
while not ready:
with suppress(RequestException):
resp = get("http://www.example.com/ready", headers={"Host": "www.example.com"})
status_code = resp.status_code
text = resp.text
if status_code >= 500:
print("❌ An error occurred with the server, exiting ...", flush=True)
exit(1)
ready = status_code < 400 or status_code == 403 and text == "ready"
if retries > 10:
print("❌ The service took too long to be ready, exiting ...", flush=True)
exit(1)
elif not ready:
retries += 1
print("⚠️ Waiting for the service to be ready, retrying in 5s ...", flush=True)
sleep(5)
use_reverse_scan = getenv("USE_REVERSE_SCAN", "yes") == "yes" use_reverse_scan = getenv("USE_REVERSE_SCAN", "yes") == "yes"
reverse_scan_ports = getenv("REVERSE_SCAN_PORTS", "80") reverse_scan_ports = getenv("REVERSE_SCAN_PORTS", "80")

View file

@ -13,13 +13,15 @@ try:
retries = 0 retries = 0
while not ready: while not ready:
with suppress(RequestException): with suppress(RequestException):
status_code = get("http://www.example.com", headers={"Host": "www.example.com"}).status_code resp = get("http://www.example.com/ready", headers={"Host": "www.example.com"})
status_code = resp.status_code
text = resp.text
if status_code >= 500: if status_code >= 500:
print("❌ An error occurred with the server, exiting ...", flush=True) print("❌ An error occurred with the server, exiting ...", flush=True)
exit(1) exit(1)
ready = status_code < 400 ready = status_code < 400 and text == "ready"
if retries > 10: if retries > 10:
print("❌ The service took too long to be ready, exiting ...", flush=True) print("❌ The service took too long to be ready, exiting ...", flush=True)
@ -76,22 +78,27 @@ try:
print("❌ An error occurred when restarting BunkerWeb, exiting ...", flush=True) print("❌ An error occurred when restarting BunkerWeb, exiting ...", flush=True)
exit(1) exit(1)
ready = False
retries = 0 retries = 0
while ( while not ready:
b"BunkerWeb is ready" with suppress(RequestException):
not in run( resp = get("http://www.example.com/ready", headers={"Host": "www.example.com"})
["sudo", "tail", "-n", "1", "/var/log/bunkerweb/error.log"], status_code = resp.status_code
stdout=PIPE, text = resp.text
check=True,
).stdout
) and retries < 10:
retries += 1
print(" Waiting for BunkerWeb to be ready, retrying in 5s ...")
sleep(5)
if retries >= 10: if status_code >= 500:
print("❌ An error occurred with the server, exiting ...", flush=True)
exit(1)
ready = status_code < 400 and text == "ready"
if retries > 10:
print("❌ BunkerWeb took too long to be ready, exiting ...", flush=True) print("❌ BunkerWeb took too long to be ready, exiting ...", flush=True)
exit(1) exit(1)
elif not ready:
retries += 1
print("⚠️ Waiting for BunkerWeb to be ready, retrying in 5s ...", flush=True)
sleep(5)
print(" Starting Firefox again ...", flush=True) print(" Starting Firefox again ...", flush=True)
with webdriver.Firefox(options=firefox_options) as driver: with webdriver.Firefox(options=firefox_options) as driver: