forked from CrazyLegsSteph/ShortCommands
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtests.py
More file actions
178 lines (149 loc) · 5.72 KB
/
Copy pathtests.py
File metadata and controls
178 lines (149 loc) · 5.72 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import unittest
import random
import subprocess
import os
import sys
from pathlib import Path
import urllib.request
import zipfile
import io
import shutil
import shlex
import re
import time
import json
import requests
from typing import Optional, Union, Collection, Set, Callable
from datetime import timedelta
from bootstrap import download_tshock
import tempfile
import asyncio
import hashlib
import threading
OptionalPath = Union[None, Path, str]
def download_map(url=r"https://github.com/TEdit/Terraria-Map-Editor/raw/5c4afae20b/tests/World%20Files%201.4.0.3/SM_Classic.wld", dest: OptionalPath=None):
if dest is None:
dest = "./TShock"
dest: Path = Path(dest)
target_file_name = f'world_{random.randint(0, 2 ** 32)}.wld'
target = Path(dest, target_file_name)
with urllib.request.urlopen(url) as response, target.open('wb') as f:
shutil.copyfileobj(response, f)
return target
def hash_bytes(b: bytes) -> int:
m = hashlib.sha256()
m.update(b)
return int.from_bytes(m.digest(), "big")
class PopenBuffer(threading.Thread):
def __init__(self, popen, *args, **kwargs):
super().__init__(*args, **kwargs)
self.event = threading.Event()
self.popen: subprocess.Popen = popen
self.buff = io.BytesIO()
self.setDaemon(True)
self.start()
def run(self, *args, **kwargs):
while not self.event.is_set():
try:
line = self.popen.stdout.readline()
except:
if self.popen.returncode is not None:
break
else:
raise
else:
self.buff.write(line)
def read(self) -> bytes:
return self.buff.getvalue()
def clear(self):
self.buff = io.BytesIO()
def stop(self):
self.event.set()
def setup_tshock(world_path: Path, dest: OptionalPath=None):
if dest is None:
dest = "./TShock"
dest: Path = Path(dest)
start = time.monotonic()
if not Path(dest, "tshock", "config.json").exists():
p: subprocess.Popen = subprocess.Popen(str(Path(dest, "TerrariaServer.exe").absolute()), cwd=dest, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
pbuff = PopenBuffer(p)
try:
while not Path(dest, "tshock", "config.json").exists():
try:
p.wait(5)
except (TimeoutError, subprocess.TimeoutExpired):
pass
if time.monotonic() - start > timedelta(minutes=2).total_seconds():
raise TimeoutError("unable to get TShock ready")
finally:
pbuff.stop()
p.kill()
json_config_path = Path(dest, "tshock", "config.json")
config = json.loads(json_config_path.read_text())
assert "RestApiEnabled" in config
config["RestApiEnabled"] = True
config["EnableTokenEndpointAuthentication"] = True
config["ApplicationRestTokens"] = {
"TESTTOKEN": {
"Username": "Server",
"UserGroupName": "superadmin"
}}
json_config_path.write_text(json.dumps(config))
shutil.copy2("ShortCommands/bin/Release/ShortCommands.dll", dest / "ServerPlugins")
cmd = f'\"{(dest / "TerrariaServer.exe").absolute()}\" -ip 127.0.0.1 -port 17779 --stats-optout -world \"{world_path.absolute()}\"'
stdout = subprocess.PIPE
p = subprocess.Popen(shlex.split(cmd), bufsize=2**21, cwd=dest, stdout=stdout, stderr=subprocess.STDOUT, stdin=subprocess.PIPE, env=os.environ.copy())
pbuff = PopenBuffer(p)
while b"Server started" not in pbuff.read():
time.sleep(1)
else:
pbuff.clear()
assert p.returncode is None
r = requests.get(f"http://127.0.0.1:{config['RestApiPort']}/tokentest?token=TESTTOKEN")
r.raise_for_status()
print(r.json())
return config, p, pbuff
class IntegrationTest(unittest.TestCase):
world_path: Path
server_config: dict
server_process: subprocess.Popen
tmp_dir: tempfile.TemporaryDirectory
pbuff: PopenBuffer
def setUp(self):
self.tmp_dir = tempfile.TemporaryDirectory()
download_tshock(dest=self.tmp_dir.name)
self.world_path = download_map(dest=self.tmp_dir.name)
self.server_config, self.server_process, self.pbuff = setup_tshock(self.world_path, dest=self.tmp_dir.name)
def tearDown(self):
self.server_process.kill()
self.server_process.wait(15)
self.tmp_dir.cleanup()
self.pbuff.stop()
def wait_for_ouput(self, pattern: Union[bytes, Callable, re.Pattern], timeout=None, buff=b"", clear=True) -> bytes:
def is_a_match(buff):
if isinstance(pattern, re.Pattern):
return re.match(pattern, buff)
if callable(pattern):
return pattern(buff)
return pattern in buff
t = time.monotonic()
while not is_a_match(buff):
if time.monotonic() - t > timeout:
raise TimeoutError()
buff = self.pbuff.read()
if clear:
self.pbuff.clear()
return buff
def test_screload(self):
cmd = '/screload'
r = requests.get(f"http://127.0.0.1:{self.server_config['RestApiPort']}/v3/server/rawcmd", {'token': 'TESTTOKEN', 'cmd': cmd}, timeout=15)
r.raise_for_status()
data = r.json()
self.assertEqual('200', str(data.get('status')))
self.assertEqual(1, len(data.get('response')))
self.assertEqual('Shortcommand config reloaded!', data.get('response')[0])
needle = f'Server executed: {cmd}.'.encode()
haystack = self.wait_for_ouput(needle, timeout=30)
self.assertIn(needle, haystack)
if __name__ == '__main__':
unittest.main()