2022-06-03 11:52:18 -04:00
|
|
|
"""Fixture for setting up a single server."""
|
|
|
|
|
|
|
|
from asyncio import create_subprocess_exec, get_event_loop, sleep
|
2022-06-19 00:15:17 -04:00
|
|
|
from json import dumps
|
2022-06-03 11:52:18 -04:00
|
|
|
from pathlib import Path
|
2022-06-18 22:15:20 -04:00
|
|
|
from random import choices
|
|
|
|
from shutil import rmtree
|
2022-06-18 21:24:11 -04:00
|
|
|
from socket import gethostname, gethostbyname, socket
|
2022-06-18 22:15:20 -04:00
|
|
|
from string import ascii_lowercase
|
2022-06-11 18:10:23 -04:00
|
|
|
|
2022-06-03 11:52:18 -04:00
|
|
|
|
|
|
|
class Server:
|
|
|
|
"""Setup a single server."""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Initialization of a server."""
|
2022-06-19 00:15:17 -04:00
|
|
|
self.datadir = Path.cwd().joinpath(
|
|
|
|
"tests", "data", "".join(choices(ascii_lowercase, k=5))
|
|
|
|
)
|
2022-06-18 22:15:20 -04:00
|
|
|
self.datadir.mkdir(parents=True)
|
2022-06-19 00:15:17 -04:00
|
|
|
self.settings = {}
|
|
|
|
self.use_config = True
|
|
|
|
# self.env = {}
|
2022-06-03 11:52:18 -04:00
|
|
|
self.process = None
|
|
|
|
self.loop = get_event_loop()
|
|
|
|
|
2022-06-07 13:53:13 -04:00
|
|
|
@property
|
2022-06-19 00:15:17 -04:00
|
|
|
def base_url(self):
|
|
|
|
"""Returns the base URL for the server."""
|
2022-06-07 13:53:13 -04:00
|
|
|
address = "127.0.0.1"
|
2022-06-19 00:15:17 -04:00
|
|
|
port = 9090
|
|
|
|
if "address" in self.settings:
|
|
|
|
address = self.settings["address"]
|
|
|
|
if "port" in self.settings:
|
|
|
|
port = self.settings["port"]
|
|
|
|
return f"{address}:{port}"
|
2022-06-07 13:53:13 -04:00
|
|
|
|
2022-06-03 11:52:18 -04:00
|
|
|
async def __start(self):
|
|
|
|
"""async start of the server."""
|
2022-06-19 00:15:17 -04:00
|
|
|
env = None
|
|
|
|
if self.settings:
|
|
|
|
if self.use_config:
|
|
|
|
with open(
|
|
|
|
self.datadir.joinpath("morethantext.json"), "w", encoding="utf-8"
|
|
|
|
) as cfg:
|
|
|
|
cfg.write(dumps(self.settings))
|
|
|
|
else:
|
|
|
|
env = {}
|
|
|
|
for key, value in self.settings.items():
|
|
|
|
env[f"MTT_{key.upper()}"] = str(value)
|
2022-06-03 11:52:18 -04:00
|
|
|
self.process = await create_subprocess_exec(
|
2022-06-19 00:15:17 -04:00
|
|
|
Path.cwd().joinpath("target", "release", "morethantext_web"),
|
|
|
|
env=env,
|
|
|
|
cwd=self.datadir,
|
2022-06-11 18:10:23 -04:00
|
|
|
)
|
2022-06-03 11:52:18 -04:00
|
|
|
await sleep(1)
|
|
|
|
|
|
|
|
async def __stop(self):
|
|
|
|
"""async stop of the server."""
|
|
|
|
if self.process is not None and self.process.returncode is None:
|
|
|
|
self.process.terminate()
|
|
|
|
await self.process.wait()
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
"""Start the server."""
|
|
|
|
self.loop.run_until_complete(self.__start())
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
"""Stop the server."""
|
|
|
|
self.loop.run_until_complete(self.__stop())
|
|
|
|
|
|
|
|
def destroy(self):
|
|
|
|
"""Removes the server instance."""
|
|
|
|
self.stop()
|
2022-06-18 22:15:20 -04:00
|
|
|
rmtree(self.datadir, ignore_errors=True)
|
2022-06-11 18:10:23 -04:00
|
|
|
|
|
|
|
def set_safe_port(self):
|
|
|
|
"""Set the server port to something not being used."""
|
2022-06-19 00:15:17 -04:00
|
|
|
port = 9090
|
|
|
|
while port == 9090:
|
2022-06-12 09:07:06 -04:00
|
|
|
sock = socket()
|
2022-06-19 00:15:17 -04:00
|
|
|
if "address" in self.settings:
|
|
|
|
address = self.settings["address"]
|
|
|
|
else:
|
|
|
|
address = "127.0.0.1"
|
|
|
|
sock.bind((address, 0))
|
|
|
|
port = sock.getsockname()[1]
|
2022-06-12 09:07:06 -04:00
|
|
|
sock.close()
|
2022-06-19 00:15:17 -04:00
|
|
|
self.settings["port"] = port
|
2022-06-18 21:24:11 -04:00
|
|
|
|
|
|
|
def set_to_host_ip(self):
|
|
|
|
"""Set the server to use something other than localhost."""
|
|
|
|
hostname = gethostname()
|
2022-06-19 00:15:17 -04:00
|
|
|
self.settings["address"] = gethostbyname(hostname)
|
2022-06-18 21:24:11 -04:00
|
|
|
self.set_safe_port()
|