Files
copyparty/scripts/test/smoketest.py

222 lines
5.9 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
def tc1(vflags):
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)
2022-06-16 01:07:15 +02:00
if os.path.exists(td):
break
2021-06-11 23:01:13 +02:00
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))
for d2 in ["r", "w", "a", "c"]:
2021-06-11 01:45:28 +02:00
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]
perms = [x.rstrip("cj/")[-1] for x in pdirs]
2021-08-14 22:45:33 +02:00
perms = ["rw" if x == "a" else x for x in perms]
2021-06-11 03:05:42 +02:00
for pd, ud, p in zip(pdirs, udirs, perms):
if ud[-1] == "j" or ud[-1] == "c":
2021-06-11 23:01:13 +02:00
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)
2021-06-12 01:39:14 +02:00
if hp:
2021-08-06 00:50:29 +02:00
arg += ":c,hist=" + hp
2021-06-11 23:01:13 +02:00
args += ["-v", arg + vflags]
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, p in zip(udirs, perms):
2021-06-11 01:45:28 +02:00
vid = ovid + "\n{}".format(d).encode("utf-8")
r = requests.post(
ub + d,
data={"act": "bput"},
files={"f": (d.replace("/", "") + ".h264", vid)},
)
c = r.status_code
if c == 200 and p not in ["w", "rw"]:
raise Exception("post {} with perm {} at {}".format(c, p, d))
elif c == 403 and p not in ["r"]:
raise Exception("post {} with perm {} at {}".format(c, p, d))
elif c not in [200, 403]:
raise Exception("post {} with perm {} at {}".format(c, p, d))
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 = "{}{}/{}.h264".format(ub, d, d.replace("/", ""))
2021-06-11 03:05:42 +02:00
r = requests.get(u)
ok = bool(r)
2021-08-14 22:45:33 +02:00
if ok != (p in ["rw"]):
2021-06-11 03:05:42 +02:00
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 = "{}/{}.h264".format(d, d.split("test/")[-1].replace("/", ""))
2021-06-11 03:05:42 +02:00
ok = os.path.exists(u)
2021-08-14 22:45:33 +02:00
if ok != (p in ["rw", "w"]):
2021-06-11 03:05:42 +02:00
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 = "{}{}/{}.h264?th=j".format(ub, d, d.replace("/", ""))
2021-06-11 03:05:42 +02:00
r = requests.get(u)
2021-06-11 03:10:33 +02:00
ok = bool(r and r.content[:3] == b"\xff\xd8\xff")
2021-08-14 22:45:33 +02:00
if ok != (p in ["rw"]):
2021-06-11 03:05:42 +02:00
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"))
2021-08-14 22:45:33 +02:00
if not r_ok or w_ok != (p in ["rw"]):
2021-06-11 23:01:13 +02:00
raise Exception("ls {} with perm {} at {}".format(ok, p, u))
2021-08-14 22:45:33 +02:00
if (tag and p != "rw") or (not tag and p == "rw"):
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)
def run(tc, *a):
2021-06-12 01:39:14 +02:00
try:
tc(*a)
2021-06-12 01:39:14 +02:00
finally:
try:
CPP[0].stop(False)
except:
pass
2021-06-11 23:01:13 +02:00
def main():
run(tc1, "")
run(tc1, ":c,fk")
2021-06-11 23:01:13 +02:00
2021-06-11 01:45:28 +02:00
if __name__ == "__main__":
main()