-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtesting_server.py
100 lines (79 loc) · 2.75 KB
/
testing_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/python3
"""
This file runs the environment that the Playwright tests expect.
It creates a clean var dir in /dev/shm/ every run
"""
import builtins
import os
import shutil
import kaithem
# test module setting spec moves the module here
if os.path.exists("/dev/shm/test_ext_module_location"):
shutil.rmtree("/dev/shm/test_ext_module_location")
# Clean up the test environment.
if os.path.exists("/dev/shm/kaithem_test_env/"):
shutil.rmtree("/dev/shm/kaithem_test_env/")
# Only load this here, not in the pytest unit tests,
# for those we specifically want to run in a clean environment
os.makedirs("/dev/shm/kaithem_test_env/modules/data", exist_ok=True)
shutil.copytree(
os.path.join(
os.path.dirname(__file__),
"kaithem/data/testing/TestingServerModule",
),
"/dev/shm/kaithem_test_env/modules/data/TestingServerModule",
)
with open(
"/dev/shm/kaithem_test_env/modules/data/test_preloaded_ext_module.location",
"w",
) as f:
f.write(
os.path.join(
os.path.dirname(__file__),
"kaithem/data/testing/TestingServerModule",
),
)
# Ensure tests don't do anything outside the sandbox
old_open = open
a = "/dev/shm/kaithem_test_env/assets/"
os.makedirs(a, exist_ok=True)
shutil.copy(
os.path.join(
os.path.dirname(__file__),
"kaithem/data/static/sounds/320181__dland__hint.opus",
),
"/dev/shm/kaithem_test_env/assets/alert.ogg",
)
def open2(path, mode: str = "r", *args, **kwargs):
if not (str(path).startswith("/dev/shm/")) and not path == "/dev/null":
if "w" in mode or "a" in mode:
if "__pycache__" not in str(path) and "/.cache/" not in str(path):
raise RuntimeError(
"Unit testing is not allowed to write outside of /dev/shm: "
+ str(path)
)
return old_open(path, mode, *args, **kwargs)
builtins.open = open2
cfg = {
"ssl_dir": "/dev/shm/kaithem_test_env/ssl",
"site_data_dir": "/dev/shm/kaithem_test_env",
# Prevent it from getting IP geolocation every time
"location": "0.123,0.456",
"log_format": "normal",
"local_access_only": True,
}
# This object is the same as the "kaithem" object in pages and events
api = kaithem.initialize_app(cfg)
# This causes problems if imported before the app is initialized
from kaithem.src import auth, directories # noqa
# Fake map tile to make sure we are reading from the cache
os.makedirs(
"/dev/shm/kaithem_test_env/maptiles/openstreetmap/0/0/", exist_ok=True
)
shutil.copy(
"kaithem/data/static/img/1x1.png",
"/dev/shm/kaithem_test_env/maptiles/openstreetmap/0/0/0.png",
)
auth.add_user("admin", "test-admin-password")
auth.add_user_to_group("admin", "Administrators")
kaithem.start_server()