Added functionality for testing with config files.

This commit is contained in:
Jeff Baskin 2022-06-19 00:15:17 -04:00
parent f761cbf56c
commit 8701adbd57
3 changed files with 45 additions and 38 deletions

View File

@ -11,6 +11,7 @@ Feature: Server Start
Scenario: Start using the server on another port
Given a server
And port is changed to something different
And environment variables are used
And it is running
When the home page is accessed
Then the status should be OK

View File

@ -1,6 +1,7 @@
"""Fixture for setting up a single server."""
from asyncio import create_subprocess_exec, get_event_loop, sleep
from json import dumps
from pathlib import Path
from random import choices
from shutil import rmtree
@ -13,44 +14,44 @@ class Server:
def __init__(self):
"""Initialization of a server."""
self.datadir = Path.cwd().joinpath("tests", "data", "".join(choices(ascii_lowercase, k=5)))
self.datadir = Path.cwd().joinpath(
"tests", "data", "".join(choices(ascii_lowercase, k=5))
)
self.datadir.mkdir(parents=True)
self.env = {}
self.settings = {}
self.use_config = True
# self.env = {}
self.process = None
self.loop = get_event_loop()
@property
def address(self):
"""Get the server address."""
def base_url(self):
"""Returns the base URL for the server."""
address = "127.0.0.1"
if "MMT_ADDRESS" in self.env:
address = self.env["MMT_ADDRESS"]
return address
@address.setter
def address(self, addr):
"""Sets the server address."""
self.env["MTT_ADDRESS"] = addr
@property
def port(self):
"""Get the port of the server."""
port = "9090"
if "MTT_PORT" in self.env:
port = self.env["MTT_PORT"]
return port
@port.setter
def port(self, num):
"""Set the port for the server."""
self.env["MTT_PORT"] = str(num)
port = 9090
if "address" in self.settings:
address = self.settings["address"]
if "port" in self.settings:
port = self.settings["port"]
return f"{address}:{port}"
async def __start(self):
"""async start of the server."""
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)
self.process = await create_subprocess_exec(
Path.cwd().joinpath("target", "release", "morethantext_web"),
env=self.env,
cwd=self.datadir
Path.cwd().joinpath("target", "release", "morethantext_web"),
env=env,
cwd=self.datadir,
)
await sleep(1)
@ -75,15 +76,20 @@ class Server:
def set_safe_port(self):
"""Set the server port to something not being used."""
while self.port == "9090":
port = 9090
while port == 9090:
sock = socket()
sock.bind((self.address, 0))
self.port = sock.getsockname()[1]
if "address" in self.settings:
address = self.settings["address"]
else:
address = "127.0.0.1"
sock.bind((address, 0))
port = sock.getsockname()[1]
sock.close()
self.settings["port"] = port
def set_to_host_ip(self):
"""Set the server to use something other than localhost."""
hostname = gethostname()
self.address = gethostbyname(hostname)
print(self.address)
self.settings["address"] = gethostbyname(hostname)
self.set_safe_port()

View File

@ -10,10 +10,10 @@ def create_server():
"""Set up a server."""
@given("it is not using localhost")
def set_server_address(server):
"""Sets tthe server to not use localhost."""
server.set_to_host_ip()
@given("environment variables are used")
def set_use_environment(server):
"""Sets up the process for environment variables."""
server.use_config = False
@given("port is changed to something different")
@ -31,7 +31,7 @@ def start_server(server):
@when("the home page is accessed")
def access_home_page(server, page):
"""Access the home page."""
url = f"http://{server.address}:{server.port}/"
url = f"http://{server.base_url}/"
page.request_url(url)