Added functionality for testing with config files.
This commit is contained in:
parent
f761cbf56c
commit
8701adbd57
@ -11,6 +11,7 @@ Feature: Server Start
|
|||||||
Scenario: Start using the server on another port
|
Scenario: Start using the server on another port
|
||||||
Given a server
|
Given a server
|
||||||
And port is changed to something different
|
And port is changed to something different
|
||||||
|
And environment variables are used
|
||||||
And it is running
|
And it is running
|
||||||
When the home page is accessed
|
When the home page is accessed
|
||||||
Then the status should be OK
|
Then the status should be OK
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""Fixture for setting up a single server."""
|
"""Fixture for setting up a single server."""
|
||||||
|
|
||||||
from asyncio import create_subprocess_exec, get_event_loop, sleep
|
from asyncio import create_subprocess_exec, get_event_loop, sleep
|
||||||
|
from json import dumps
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from random import choices
|
from random import choices
|
||||||
from shutil import rmtree
|
from shutil import rmtree
|
||||||
@ -13,44 +14,44 @@ class Server:
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""Initialization of a server."""
|
"""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.datadir.mkdir(parents=True)
|
||||||
self.env = {}
|
self.settings = {}
|
||||||
|
self.use_config = True
|
||||||
|
# self.env = {}
|
||||||
self.process = None
|
self.process = None
|
||||||
self.loop = get_event_loop()
|
self.loop = get_event_loop()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def address(self):
|
def base_url(self):
|
||||||
"""Get the server address."""
|
"""Returns the base URL for the server."""
|
||||||
address = "127.0.0.1"
|
address = "127.0.0.1"
|
||||||
if "MMT_ADDRESS" in self.env:
|
port = 9090
|
||||||
address = self.env["MMT_ADDRESS"]
|
if "address" in self.settings:
|
||||||
return address
|
address = self.settings["address"]
|
||||||
|
if "port" in self.settings:
|
||||||
@address.setter
|
port = self.settings["port"]
|
||||||
def address(self, addr):
|
return f"{address}:{port}"
|
||||||
"""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)
|
|
||||||
|
|
||||||
async def __start(self):
|
async def __start(self):
|
||||||
"""async start of the server."""
|
"""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(
|
self.process = await create_subprocess_exec(
|
||||||
Path.cwd().joinpath("target", "release", "morethantext_web"),
|
Path.cwd().joinpath("target", "release", "morethantext_web"),
|
||||||
env=self.env,
|
env=env,
|
||||||
cwd=self.datadir
|
cwd=self.datadir,
|
||||||
)
|
)
|
||||||
await sleep(1)
|
await sleep(1)
|
||||||
|
|
||||||
@ -75,15 +76,20 @@ class Server:
|
|||||||
|
|
||||||
def set_safe_port(self):
|
def set_safe_port(self):
|
||||||
"""Set the server port to something not being used."""
|
"""Set the server port to something not being used."""
|
||||||
while self.port == "9090":
|
port = 9090
|
||||||
|
while port == 9090:
|
||||||
sock = socket()
|
sock = socket()
|
||||||
sock.bind((self.address, 0))
|
if "address" in self.settings:
|
||||||
self.port = sock.getsockname()[1]
|
address = self.settings["address"]
|
||||||
|
else:
|
||||||
|
address = "127.0.0.1"
|
||||||
|
sock.bind((address, 0))
|
||||||
|
port = sock.getsockname()[1]
|
||||||
sock.close()
|
sock.close()
|
||||||
|
self.settings["port"] = port
|
||||||
|
|
||||||
def set_to_host_ip(self):
|
def set_to_host_ip(self):
|
||||||
"""Set the server to use something other than localhost."""
|
"""Set the server to use something other than localhost."""
|
||||||
hostname = gethostname()
|
hostname = gethostname()
|
||||||
self.address = gethostbyname(hostname)
|
self.settings["address"] = gethostbyname(hostname)
|
||||||
print(self.address)
|
|
||||||
self.set_safe_port()
|
self.set_safe_port()
|
||||||
|
@ -10,10 +10,10 @@ def create_server():
|
|||||||
"""Set up a server."""
|
"""Set up a server."""
|
||||||
|
|
||||||
|
|
||||||
@given("it is not using localhost")
|
@given("environment variables are used")
|
||||||
def set_server_address(server):
|
def set_use_environment(server):
|
||||||
"""Sets tthe server to not use localhost."""
|
"""Sets up the process for environment variables."""
|
||||||
server.set_to_host_ip()
|
server.use_config = False
|
||||||
|
|
||||||
|
|
||||||
@given("port is changed to something different")
|
@given("port is changed to something different")
|
||||||
@ -31,7 +31,7 @@ def start_server(server):
|
|||||||
@when("the home page is accessed")
|
@when("the home page is accessed")
|
||||||
def access_home_page(server, page):
|
def access_home_page(server, page):
|
||||||
"""Access the home page."""
|
"""Access the home page."""
|
||||||
url = f"http://{server.address}:{server.port}/"
|
url = f"http://{server.base_url}/"
|
||||||
page.request_url(url)
|
page.request_url(url)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user