Files
copyparty/scripts/test/smoketest.py

210 lines
5.2 KiB
Python
Raw Normal View History

2021-06-11 01:45:28 +02:00
import os
import sys
import time
2021-06-12 01:39:14 +02:00
import shlex
2021-06-11 01:45:28 +02:00
import shutil
2021-06-12 01:39:14 +02:00
import signal
2021-06-11 01:45:28 +02:00
import tempfile
import requests
import threading
import subprocess as sp
2021-06-12 01:39:14 +02:00
CPP = []
2021-06-11 01:45:28 +02:00
class Cpp(object):
def __init__(self, args):
2021-06-12 01:39:14 +02:00
args = [sys.executable, "-m", "copyparty"] + args
print(" ".join([shlex.quote(x) for x in args]))
2021-06-11 23:01:13 +02:00
self.ls_pre = set(list(os.listdir()))
2021-06-12 01:39:14 +02:00
self.p = sp.Popen(args)
2021-06-11 01:45:28 +02:00
# , stdout=sp.PIPE, stderr=sp.PIPE)
self.t = threading.Thread(target=self._run)
self.t.daemon = True
self.t.start()
def _run(self):
self.so, self.se = self.p.communicate()
def stop(self, wait):
if wait:
2021-06-12 01:39:14 +02:00
os.kill(self.p.pid, signal.SIGINT)
self.t.join(timeout=2)
else:
self.p.kill() # macos py3.8
2021-06-11 01:45:28 +02:00
2021-06-11 23:01:13 +02:00
def clean(self):
t = os.listdir()
for f in t:
if f not in self.ls_pre and f.startswith("up."):
2021-06-11 18:12:06 +02:00
os.unlink(f)
2021-06-12 01:39:14 +02:00
def await_idle(self, ub, timeout):
req = ["scanning</td><td>False", "hash-q</td><td>0", "tag-q</td><td>0"]
2021-06-12 01:55:45 +02:00
lim = int(timeout * 10)
2021-06-12 01:39:14 +02:00
u = ub + "?h"
2021-06-12 01:55:45 +02:00
for n in range(lim):
2021-06-12 01:39:14 +02:00
try:
time.sleep(0.1)
r = requests.get(u, timeout=0.1)
for x in req:
if x not in r.text:
2021-06-12 01:55:45 +02:00
print("ST: {}/{} miss {}".format(n, lim, x))
2021-06-12 01:39:14 +02:00
raise Exception()
print("ST: idle")
return
except:
pass
2021-06-11 18:12:06 +02:00
2021-06-11 23:01:13 +02:00
def tc1():
2021-06-11 01:45:28 +02:00
ub = "http://127.0.0.1:4321/"
td = os.path.join("srv", "smoketest")
try:
shutil.rmtree(td)
except:
2021-06-11 23:01:13 +02:00
if os.path.exists(td):
raise
for _ in range(10):
try:
os.mkdir(td)
except:
time.sleep(0.1) # win10
2021-06-11 01:45:28 +02:00
2021-06-11 23:01:13 +02:00
assert os.path.exists(td)
2021-06-11 01:45:28 +02:00
vidp = os.path.join(tempfile.gettempdir(), "smoketest.h264")
if not os.path.exists(vidp):
cmd = "ffmpeg -f lavfi -i testsrc=48x32:3 -t 1 -c:v libx264 -tune animation -preset veryslow -crf 69"
sp.check_call(cmd.split(" ") + [vidp])
with open(vidp, "rb") as f:
ovid = f.read()
2021-06-11 23:01:13 +02:00
args = [
2021-06-12 01:39:14 +02:00
"-p4321",
2021-06-11 23:01:13 +02:00
"-e2dsa",
"-e2tsr",
2021-06-12 01:39:14 +02:00
"--no-mutagen",
2021-06-11 23:01:13 +02:00
"--th-ff-jpg",
"--hist",
os.path.join(td, "dbm"),
]
2021-06-11 01:45:28 +02:00
pdirs = []
2021-06-11 23:01:13 +02:00
hpaths = {}
2021-06-11 01:45:28 +02:00
for d1 in ["r", "w", "a"]:
pdirs.append("{}/{}".format(td, d1))
2021-06-11 03:05:42 +02:00
pdirs.append("{}/{}/j".format(td, d1))
2021-06-11 01:45:28 +02:00
for d2 in ["r", "w", "a"]:
d = os.path.join(td, d1, "j", d2)
2021-06-11 18:12:06 +02:00
pdirs.append(d)
2021-06-11 01:45:28 +02:00
os.makedirs(d)
2021-06-11 18:12:06 +02:00
pdirs = [x.replace("\\", "/") for x in pdirs]
2021-06-11 01:45:28 +02:00
udirs = [x.split("/", 2)[2] for x in pdirs]
2021-06-11 03:05:42 +02:00
perms = [x.rstrip("j/")[-1] for x in pdirs]
for pd, ud, p in zip(pdirs, udirs, perms):
2021-06-11 23:01:13 +02:00
if ud[-1] == "j":
continue
hp = None
if pd.endswith("st/a"):
2021-06-12 01:39:14 +02:00
hp = hpaths[ud] = os.path.join(td, "db1")
2021-06-11 23:01:13 +02:00
elif pd[:-1].endswith("a/j/"):
2021-06-12 01:39:14 +02:00
hpaths[ud] = os.path.join(td, "dbm")
hp = None
2021-06-11 23:01:13 +02:00
else:
hp = "-"
2021-06-12 01:39:14 +02:00
hpaths[ud] = os.path.join(pd, ".hist")
arg = "{}:{}:{}".format(pd, ud, p, hp)
if hp:
arg += ":chist=" + hp
2021-06-11 23:01:13 +02:00
2021-06-12 01:39:14 +02:00
args += ["-v", arg]
2021-06-11 01:45:28 +02:00
2021-06-11 18:12:06 +02:00
# return
2021-06-11 01:45:28 +02:00
cpp = Cpp(args)
2021-06-12 01:39:14 +02:00
CPP.append(cpp)
cpp.await_idle(ub, 3)
2021-06-11 01:45:28 +02:00
for d in udirs:
vid = ovid + "\n{}".format(d).encode("utf-8")
2021-06-11 18:12:06 +02:00
try:
requests.post(ub + d, data={"act": "bput"}, files={"f": ("a.h264", vid)})
except:
pass
2021-06-11 03:05:42 +02:00
2021-06-11 23:01:13 +02:00
cpp.clean()
# GET permission
2021-06-11 03:05:42 +02:00
for d, p in zip(udirs, perms):
u = "{}{}/a.h264".format(ub, d)
r = requests.get(u)
ok = bool(r)
if ok != (p in ["a"]):
raise Exception("get {} with perm {} at {}".format(ok, p, u))
2021-06-11 23:01:13 +02:00
# stat filesystem
2021-06-11 03:05:42 +02:00
for d, p in zip(pdirs, perms):
u = "{}/a.h264".format(d)
ok = os.path.exists(u)
if ok != (p in ["a", "w"]):
raise Exception("stat {} with perm {} at {}".format(ok, p, u))
2021-06-11 23:01:13 +02:00
# GET thumbnail, vreify contents
2021-06-11 03:05:42 +02:00
for d, p in zip(udirs, perms):
u = "{}{}/a.h264?th=j".format(ub, d)
r = requests.get(u)
2021-06-11 03:10:33 +02:00
ok = bool(r and r.content[:3] == b"\xff\xd8\xff")
2021-06-11 03:05:42 +02:00
if ok != (p in ["a"]):
raise Exception("thumb {} with perm {} at {}".format(ok, p, u))
2021-06-11 23:01:13 +02:00
# check tags
2021-06-12 01:39:14 +02:00
cpp.await_idle(ub, 5)
2021-06-11 23:01:13 +02:00
for d, p in zip(udirs, perms):
u = "{}{}?ls".format(ub, d)
r = requests.get(u)
j = r.json() if r else False
tag = None
if j:
for f in j["files"]:
tag = tag or f["tags"].get("res")
r_ok = bool(j)
w_ok = bool(r_ok and j.get("files"))
if not r_ok or w_ok != (p in ["a"]):
raise Exception("ls {} with perm {} at {}".format(ok, p, u))
if (tag and p != "a") or (not tag and p == "a"):
2021-06-12 01:39:14 +02:00
raise Exception("tag {} with perm {} at {}".format(tag, p, u))
2021-06-11 23:01:13 +02:00
if tag is not None and tag != "48x32":
raise Exception("tag [{}] at {}".format(tag, u))
2021-06-11 01:45:28 +02:00
cpp.stop(True)
2021-06-12 01:39:14 +02:00
def run(tc):
try:
tc()
finally:
try:
CPP[0].stop(False)
except:
pass
2021-06-11 23:01:13 +02:00
def main():
2021-06-12 01:39:14 +02:00
run(tc1)
2021-06-11 23:01:13 +02:00
2021-06-11 01:45:28 +02:00
if __name__ == "__main__":
main()