Update bwcli core tests to add backup plugin custom commands test

This commit is contained in:
Théophile Diot 2024-04-04 14:13:24 +01:00
parent 121f091389
commit 39c9e612e3
No known key found for this signature in database
GPG key ID: 248FEA4BAE400D06
3 changed files with 432 additions and 36 deletions

View file

@ -34,6 +34,8 @@ services:
depends_on:
- bw
- bw-docker
labels:
- "bunkerweb.SCHEDULER=yes"
environment:
DOCKER_HOST: "tcp://bw-docker:2375"
LOG_LEVEL: "info"

View file

@ -1,3 +1,5 @@
from datetime import datetime
from pathlib import Path
from subprocess import PIPE, Popen
from traceback import format_exc
@ -12,13 +14,13 @@ try:
stderr=PIPE,
stdout=PIPE,
)
_, err = result.communicate()
stdout, stderr = result.communicate()
if result.returncode != 0:
print(f'❌ Command "ban" failed, exiting ...\noutput: {err.decode()}\nexit_code: {result.returncode}')
print(f'❌ Command "ban" failed, exiting ...\noutput: {stderr.decode()}\nexit_code: {result.returncode}')
exit(1)
print(err.decode(), flush=True)
print(stderr.decode(), flush=True)
print(
' Executing the command "bwcli bans" and checking the result ...',
@ -26,20 +28,20 @@ try:
)
result = Popen(["bwcli", "bans"], stderr=PIPE, stdout=PIPE)
_, err = result.communicate()
stdout, stderr = result.communicate()
if result.returncode != 0:
print(f'❌ Command "bans" failed, exiting ...\noutput: {err.decode()}\nexit_code: {result.returncode}')
print(f'❌ Command "bans" failed, exiting ...\noutput: {stderr.decode()}\nexit_code: {result.returncode}')
exit(1)
if b"- 127.0.0.1" not in err:
print(f'❌ IP 127.0.0.1 not found in the output of "bans", exiting ...\noutput: {err.decode()}')
if b"- 127.0.0.1" not in stderr:
print(f'❌ IP 127.0.0.1 not found in the output of "bans", exiting ...\noutput: {stderr.decode()}')
exit(1)
elif b"List of bans for redis:" not in err:
print(f'❌ Redis ban list not found in the output of "bans", exiting ...\noutput: {err.decode()}')
elif b"List of bans for redis:" not in stderr:
print(f'❌ Redis ban list not found in the output of "bans", exiting ...\noutput: {stderr.decode()}')
exit(1)
elif b"1 hour" not in err and b"59 minutes" not in err:
print(f"❌ Ban duration isn't 1 hour, exiting ...\noutput: {err.decode()}")
elif b"1 hour" not in stderr and b"59 minutes" not in stderr:
print(f"❌ Ban duration isn't 1 hour, exiting ...\noutput: {stderr.decode()}")
exit(1)
print(
@ -48,13 +50,13 @@ try:
)
result = Popen(["bwcli", "unban", "127.0.0.1"], stderr=PIPE, stdout=PIPE)
_, err = result.communicate()
stdout, stderr = result.communicate()
if result.returncode != 0:
print(f'❌ Command "unban" failed, exiting ...\noutput: {err.decode()}\nexit_code: {result.returncode}')
print(f'❌ Command "unban" failed, exiting ...\noutput: {stderr.decode()}\nexit_code: {result.returncode}')
exit(1)
print(err.decode(), flush=True)
print(stderr.decode(), flush=True)
print(
' Executing the command "bwcli bans" to check if the IP was unbanned ...',
@ -62,25 +64,230 @@ try:
)
result = Popen(["bwcli", "bans"], stderr=PIPE, stdout=PIPE)
_, err = result.communicate()
stdout, stderr = result.communicate()
if result.returncode != 0:
print(f'❌ Command "bans" failed, exiting ...\noutput: {err.decode()}\nexit_code: {result.returncode}')
print(f'❌ Command "bans" failed, exiting ...\noutput: {stderr.decode()}\nexit_code: {result.returncode}')
exit(1)
found = 0
for line in err.splitlines():
for line in stderr.splitlines():
if b"No ban found" in line:
found += 1
if found < 2:
print(
f"❌ IP 127.0.0.1 was not unbanned from both redis and the local ban list, exiting ...\noutput: {err.decode()}",
f"❌ IP 127.0.0.1 was not unbanned from both redis and the local ban list, exiting ...\noutput: {stderr.decode()}",
flush=True,
)
exit(1)
print(err.decode(), flush=True)
print(stderr.decode(), flush=True)
print(" Setting integration metadata to Unknown ...", flush=True)
result = Popen(["sqlite3", "/var/lib/bunkerweb/db.sqlite3"], stdin=PIPE, stderr=PIPE, stdout=PIPE)
stdout, stderr = result.communicate(b"UPDATE bw_metadata SET integration = 'Unknown';")
if result.returncode != 0:
print(f"❌ Command failed, exiting ...\nexit_code: {result.returncode}\nstdout: {stdout.decode()}\nstderr: {stderr.decode()}", flush=True)
exit(1)
print(" Checking if the metadata was set correctly ...", flush=True)
result = Popen(["sqlite3", "/var/lib/bunkerweb/db.sqlite3"], stdin=PIPE, stderr=PIPE, stdout=PIPE)
stdout, stderr = result.communicate(b"SELECT integration FROM bw_metadata;")
if result.returncode != 0:
print(f"❌ Command failed, exiting ...\nexit_code: {result.returncode}\nstdout: {stdout.decode()}\nstderr: {stderr.decode()}", flush=True)
exit(1)
if b"Unknown" not in stdout:
print(f"❌ Metadata was not set correctly, exiting ...\nstdout: {stdout.decode()}\nstderr: {stderr.decode()}", flush=True)
exit(1)
print(" Metadata was set correctly", flush=True)
print(' Executing the command "bwcli plugin backup save" ...', flush=True)
result = Popen(["bwcli", "plugin", "backup", "save"], stderr=PIPE, stdout=PIPE)
stdout, stderr = result.communicate()
if result.returncode != 0:
print(
f'❌ Command "plugin backup save" failed, exiting ...\nexit_code: {result.returncode}\nstdout: {stdout.decode()}\nstderr: {stderr.decode()}',
flush=True,
)
exit(1)
file = b""
for line in stdout.splitlines():
if b"created successfully" in line:
file = line[line.find(b"Backup") + 7 :].split(b" ")[0].strip() # noqa: E203
break
if not file:
print(f"❌ Backup was not created successfully, exiting ...\nstdout: {stdout.decode()}\nstderr: {stderr.decode()}", flush=True)
exit(1)
print(stderr.decode(), flush=True)
print(f" Checking if the backup file {file.decode()} exists ...", flush=True)
result = Popen(["bwcli", "plugin", "backup", "list"], stderr=PIPE, stdout=PIPE)
stdout, stderr = result.communicate()
if result.returncode != 0:
print(
f"❌ Command failed, exiting ...\nexit_code: {result.returncode}\nstdout: {stdout.decode()}\nstderr: {stderr.decode()}",
flush=True,
)
exit(1)
date = datetime.strptime("-".join(Path(file.decode()).stem.split("-")[2:]), "%Y-%m-%d_%H-%M-%S").strftime("%d/%m/%Y %H:%M:%S").encode()
found = False
for line in stdout.splitlines():
if date in line:
found = True
break
if not found:
print(f"❌ Backup {file.decode()} does not exist, exiting ...\nstdout: {stdout.decode()}\nstderr: {stderr.decode()}", flush=True)
exit(1)
print(f" Backup {file.decode()} created successfully", flush=True)
print(" Setting integration metadata to Windows ...", flush=True)
result = Popen(["sqlite3", "/var/lib/bunkerweb/db.sqlite3"], stdin=PIPE, stderr=PIPE, stdout=PIPE)
stdout, stderr = result.communicate(b"UPDATE bw_metadata SET integration = 'Windows';")
if result.returncode != 0:
print(f"❌ Command failed, exiting ...\nexit_code: {result.returncode}\nstdout: {stdout.decode()}\nstderr: {stderr.decode()}", flush=True)
exit(1)
print(" Checking if the metadata was set correctly ...", flush=True)
result = Popen(["sqlite3", "/var/lib/bunkerweb/db.sqlite3"], stdin=PIPE, stderr=PIPE, stdout=PIPE)
stdout, stderr = result.communicate(b"SELECT integration FROM bw_metadata;")
if result.returncode != 0:
print(f"❌ Command failed, exiting ...\nexit_code: {result.returncode}\nstdout: {stdout.decode()}\nstderr: {stderr.decode()}", flush=True)
exit(1)
if b"Windows" not in stdout:
print(f"❌ Metadata was not set correctly, exiting ...\nstdout: {stdout.decode()}\nstderr: {stderr.decode()}", flush=True)
exit(1)
print(" Metadata was set correctly", flush=True)
print(' Executing the command "bwcli plugin backup restore" ...', flush=True)
result = Popen(["bwcli", "plugin", "backup", "restore", f"/var/lib/bunkerweb/backups/{file.decode()}"], stderr=PIPE, stdout=PIPE)
stdout, stderr = result.communicate()
if result.returncode != 0:
print(
f'❌ Command "plugin backup restore" failed, exiting ...\nexit_code: {result.returncode}\nstdout: {stdout.decode()}\nstderr: {stderr.decode()}',
flush=True,
)
exit(1)
backup_file = b""
found = False
for line in stdout.splitlines():
if b"created successfully" in line:
backup_file = line[line.find(b"Backup") + 7 :].split(b" ")[0].strip() # noqa: E203
if b"Database restored successfully from /var/lib/bunkerweb/backups/" + file in line:
found = True
break
if not backup_file:
print(f"❌ Temporary backup was not created successfully, exiting ...\nstout: {stdout.decode()}\nstderr: {stderr.decode()}", flush=True)
exit(1)
elif not found:
print(f"❌ Database was not restored successfully, exiting ...\nstdout: {stdout.decode()}\nstderr: {stderr.decode()}", flush=True)
exit(1)
print(stderr.decode(), flush=True)
print(" Checking if the temporary backup file was created ...", flush=True)
result = Popen(["ls", "/tmp/bunkerweb/backups"], stderr=PIPE, stdout=PIPE)
stdout, stderr = result.communicate()
if result.returncode != 0:
print(
f"❌ Temporary backup {backup_file.decode()} does not exist, exiting ...\nexit_code: {result.returncode}\nstout: {stdout.decode()}\nstderr: {stderr.decode()}",
flush=True,
)
exit(1)
found = False
for line in stdout.splitlines():
if backup_file in line:
found = True
break
if not found:
print(f"❌ Temporary backup {backup_file.decode()} does not exist, exiting ...\nstdout: {stdout.decode()}\nstderr: {stderr.decode()}", flush=True)
exit(1)
print(f" Temporary backup {backup_file.decode()} created successfully", flush=True)
print(" Checking if the metadata was restored correctly ...", flush=True)
result = Popen(["sqlite3", "/var/lib/bunkerweb/db.sqlite3"], stdin=PIPE, stderr=PIPE, stdout=PIPE)
stdout, stderr = result.communicate(b"SELECT integration FROM bw_metadata;")
if result.returncode != 0:
print(f"❌ Command failed, exiting ...\nexit_code: {result.returncode}\nstdout: {stdout.decode()}\nstderr: {stderr.decode()}", flush=True)
exit(1)
if b"Unknown" not in stdout:
print(f"❌ Metadata was not restored correctly, exiting ...\nstdout: {stdout.decode()}\nstderr: {stderr.decode()}", flush=True)
exit(1)
print(" Metadata was restored correctly", flush=True)
print(" Trying to restore the temporary backup ...", flush=True)
result = Popen(["bwcli", "plugin", "backup", "restore", f"/tmp/bunkerweb/backups/{backup_file.decode()}"], stderr=PIPE, stdout=PIPE)
stdout, stderr = result.communicate()
if result.returncode != 0:
print(
f'❌ Command "plugin backup restore" failed, exiting ...\nexit_code: {result.returncode}\nstdout: {stdout.decode()}\nstderr: {stderr.decode()}',
flush=True,
)
exit(1)
found = False
for line in stdout.splitlines():
if b"Database restored successfully from /tmp/bunkerweb/backups/" + backup_file in line:
found = True
break
if not found:
print(f"❌ Database was not restored successfully, exiting ...\nstdout: {stdout.decode()}\nstderr: {stderr.decode()}", flush=True)
exit(1)
print(stderr.decode(), flush=True)
print(" Checking if the metadata was restored correctly ...", flush=True)
result = Popen(["sqlite3", "/var/lib/bunkerweb/db.sqlite3"], stdin=PIPE, stderr=PIPE, stdout=PIPE)
stdout, stderr = result.communicate(b"SELECT integration FROM bw_metadata;")
if result.returncode != 0:
print(f"❌ Command failed, exiting ...\nexit_code: {result.returncode}\nstdout: {stdout.decode()}\nstderr: {stderr.decode()}", flush=True)
exit(1)
if b"Windows" not in stdout:
print(f"❌ Metadata was not restored correctly, exiting ...\nstdout: {stdout.decode()}\nstderr: {stderr.decode()}", flush=True)
exit(1)
print(" Metadata was restored correctly", flush=True)
except SystemExit:
exit(1)
except:

View file

@ -1,4 +1,6 @@
from datetime import datetime
from os import getenv
from pathlib import Path
from traceback import format_exc
from docker import DockerClient
from docker.models.containers import Container
@ -15,10 +17,7 @@ try:
bw_instance: Container = bw_instances[0]
print(
' Executing the command "bwcli ban 127.0.0.1 -exp 3600" inside the BW container ...',
flush=True,
)
print(' Executing the command "bwcli ban 127.0.0.1 -exp 3600" inside the BW container ...', flush=True)
result = bw_instance.exec_run("bwcli ban 127.0.0.1 -exp 3600")
@ -28,10 +27,7 @@ try:
print(result.output.decode(), flush=True)
print(
' Executing the command "bwcli bans" inside the BW container and checking the result ...',
flush=True,
)
print(' Executing the command "bwcli bans" inside the BW container and checking the result ...', flush=True)
result = bw_instance.exec_run("bwcli bans")
@ -51,10 +47,7 @@ try:
print(result.output.decode(), flush=True)
print(
' Executing the command "bwcli unban 127.0.0.1" inside the BW container ...',
flush=True,
)
print(' Executing the command "bwcli unban 127.0.0.1" inside the BW container ...', flush=True)
result = bw_instance.exec_run("bwcli unban 127.0.0.1")
@ -64,10 +57,7 @@ try:
print(result.output.decode(), flush=True)
print(
' Executing the command "bwcli bans" inside the BW container to check if the IP was unbanned ...',
flush=True,
)
print(' Executing the command "bwcli bans" inside the BW container to check if the IP was unbanned ...', flush=True)
result = bw_instance.exec_run("bwcli bans")
@ -81,13 +71,210 @@ try:
found += 1
if found < 2:
print(f"❌ IP 127.0.0.1 was not unbanned from both redis and the local ban list, exiting ...\noutput: {result.output.decode()}", flush=True)
exit(1)
print(result.output.decode(), flush=True)
print(' Executing the command "bwcli plugin backup save" inside the BW container ...', flush=True)
result = bw_instance.exec_run("bwcli plugin backup save")
if result.exit_code == 0:
print(f"❌ Command 'plugin backup save' should have failed, exiting ...\noutput: {result.output.decode()}\nexit_code: {result.exit_code}", flush=True)
exit(1)
print(" Command 'plugin backup save' failed as expected", flush=True)
scheduler_instances = docker_client.containers.list(filters={"label": "bunkerweb.SCHEDULER"})
if not scheduler_instances:
print("❌ BunkerWeb instance not found ...", flush=True)
exit(1)
scheduler_instance: Container = scheduler_instances[0]
print(" Setting integration metadata to Unknown ...", flush=True)
result = scheduler_instance.exec_run("sqlite3 /var/lib/bunkerweb/db.sqlite3 \"UPDATE bw_metadata SET integration = 'Unknown';\"")
if result.exit_code != 0:
print(f"❌ Command failed, exiting ...\noutput: {result.output.decode()}\nexit_code: {result.exit_code}", flush=True)
exit(1)
print(" Checking if the metadata was set correctly ...", flush=True)
result = scheduler_instance.exec_run('sqlite3 /var/lib/bunkerweb/db.sqlite3 "SELECT integration FROM bw_metadata;"')
if result.exit_code != 0:
print(f"❌ Command failed, exiting ...\noutput: {result.output.decode()}\nexit_code: {result.exit_code}", flush=True)
exit(1)
if b"Unknown" not in result.output:
print(f"❌ Metadata was not set correctly, exiting ...\noutput: {result.output.decode()}", flush=True)
exit(1)
print(" Metadata was set correctly", flush=True)
print(' Executing the command "bwcli plugin backup save" inside the Scheduler container ...', flush=True)
result = scheduler_instance.exec_run("bwcli plugin backup save")
if result.exit_code != 0:
print(f'❌ Command "plugin backup save" failed, exiting ...\noutput: {result.output.decode()}\nexit_code: {result.exit_code}')
exit(1)
file = b""
for line in result.output.splitlines():
if b"created successfully" in line:
file = line[line.find(b"Backup") + 7 :].split(b" ")[0].strip() # noqa: E203
break
if not file:
print(f"❌ Backup was not created successfully, exiting ...\noutput: {result.output.decode()}", flush=True)
exit(1)
print(result.output.decode(), flush=True)
print(f" Checking if the backup file {file.decode()} exists ...", flush=True)
result = scheduler_instance.exec_run("bwcli plugin backup list")
if result.exit_code != 0:
print(f"❌ Command failed, exiting ...\noutput: {result.output.decode()}\nexit_code: {result.exit_code}", flush=True)
exit(1)
date = datetime.strptime("-".join(Path(file.decode()).stem.split("-")[2:]), "%Y-%m-%d_%H-%M-%S").strftime("%d/%m/%Y %H:%M:%S").encode()
found = False
for line in result.output.splitlines():
if date in line:
found = True
break
if not found:
print(f"❌ Backup {file.decode()} does not exist, exiting ...\noutput: {result.output.decode()}", flush=True)
exit(1)
print(f" Backup {file.decode()} created successfully", flush=True)
print(" Setting integration metadata to Windows ...", flush=True)
result = scheduler_instance.exec_run("sqlite3 /var/lib/bunkerweb/db.sqlite3 \"UPDATE bw_metadata SET integration = 'Windows';\"")
if result.exit_code != 0:
print(f"❌ Command failed, exiting ...\noutput: {result.output.decode()}\nexit_code: {result.exit_code}", flush=True)
exit(1)
print(" Checking if the metadata was set correctly ...", flush=True)
result = scheduler_instance.exec_run('sqlite3 /var/lib/bunkerweb/db.sqlite3 "SELECT integration FROM bw_metadata;"')
if result.exit_code != 0:
print(f"❌ Command failed, exiting ...\noutput: {result.output.decode()}\nexit_code: {result.exit_code}", flush=True)
exit(1)
if b"Windows" not in result.output:
print(f"❌ Metadata was not set correctly, exiting ...\noutput: {result.output.decode()}", flush=True)
exit(1)
print(" Metadata was set correctly", flush=True)
print(' Executing the command "bwcli plugin backup restore" inside the Scheduler container ...', flush=True)
result = scheduler_instance.exec_run("bwcli plugin backup restore")
if result.exit_code != 0:
print(f'❌ Command "plugin backup restore" failed, exiting ...\noutput: {result.output.decode()}\nexit_code: {result.exit_code}')
exit(1)
backup_file = b""
found = False
for line in result.output.splitlines():
if b"created successfully" in line:
backup_file = line[line.find(b"Backup") + 7 :].split(b" ")[0].strip() # noqa: E203
if b"Database restored successfully from /var/lib/bunkerweb/backups/" + file in line:
found = True
break
if not backup_file:
print(f"❌ Temporary backup was not created successfully, exiting ...\noutput: {result.output.decode()}", flush=True)
exit(1)
elif not found:
print(f"❌ Database was not restored successfully, exiting ...\noutput: {result.output.decode()}", flush=True)
exit(1)
print(result.output.decode(), flush=True)
print(" Checking if the temporary backup file was created ...", flush=True)
result = scheduler_instance.exec_run("ls /tmp/bunkerweb/backups")
if result.exit_code != 0:
print(
f"❌ IP 127.0.0.1 was not unbanned from both redis and the local ban list, exiting ...\noutput: {result.output.decode()}",
f"Temporary backup {backup_file.decode()} does not exist, exiting ...\noutput: {result.output.decode()}\nexit_code: {result.exit_code}",
flush=True,
)
exit(1)
found = False
for line in result.output.splitlines():
if backup_file in line:
found = True
break
if not found:
print(f"❌ Temporary backup {backup_file.decode()} does not exist, exiting ...\noutput: {result.output.decode()}", flush=True)
exit(1)
print(f" Temporary backup {backup_file.decode()} created successfully", flush=True)
print(" Checking if the metadata was restored correctly ...", flush=True)
result = scheduler_instance.exec_run('sqlite3 /var/lib/bunkerweb/db.sqlite3 "SELECT integration FROM bw_metadata;"')
if result.exit_code != 0:
print(f"❌ Command failed, exiting ...\noutput: {result.output.decode()}\nexit_code: {result.exit_code}", flush=True)
exit(1)
if b"Unknown" not in result.output:
print(f"❌ Metadata was not restored correctly, exiting ...\noutput: {result.output.decode()}", flush=True)
exit(1)
print(" Metadata was restored correctly", flush=True)
print(" Trying to restore the temporary backup ...", flush=True)
result = scheduler_instance.exec_run(f"bwcli plugin backup restore /tmp/bunkerweb/backups/{backup_file.decode()}")
if result.exit_code != 0:
print(f'❌ Command "plugin backup restore" failed, exiting ...\noutput: {result.output.decode()}\nexit_code: {result.exit_code}')
exit(1)
found = False
for line in result.output.splitlines():
if b"Database restored successfully from /tmp/bunkerweb/backups/" + backup_file in line:
found = True
break
if not found:
print(f"❌ Database was not restored successfully, exiting ...\noutput: {result.output.decode()}", flush=True)
exit(1)
print(result.output.decode(), flush=True)
print(" Checking if the metadata was restored correctly ...", flush=True)
result = scheduler_instance.exec_run('sqlite3 /var/lib/bunkerweb/db.sqlite3 "SELECT integration FROM bw_metadata;"')
if result.exit_code != 0:
print(f"❌ Command failed, exiting ...\noutput: {result.output.decode()}\nexit_code: {result.exit_code}", flush=True)
exit(1)
if b"Windows" not in result.output:
print(f"❌ Metadata was not restored correctly, exiting ...\noutput: {result.output.decode()}", flush=True)
exit(1)
print(" Metadata was restored correctly", flush=True)
except SystemExit:
exit(1)
except: