Skip to content

Commit e48fe32

Browse files
committed
begin work on server tests and modular fixtures
1 parent b8185cc commit e48fe32

File tree

4 files changed

+411
-7
lines changed

4 files changed

+411
-7
lines changed

merlin/server/server_util.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def valid_ipv4(ip: str) -> bool: # pylint: disable=C0103
6060
return False
6161

6262
for i in arr:
63-
if int(i) < 0 and int(i) > 255:
63+
if int(i) < 0 or int(i) > 255:
6464
return False
6565

6666
return True
@@ -121,6 +121,15 @@ def __init__(self, data: dict) -> None:
121121
self.pass_file = data["pass_file"] if "pass_file" in data else self.PASSWORD_FILE
122122
self.user_file = data["user_file"] if "user_file" in data else self.USERS_FILE
123123

124+
def __eq__(self, other: "ContainerFormatConfig"):
125+
"""
126+
Equality magic method used for testing this class
127+
128+
:param other: Another ContainerFormatConfig object to check if they're the same
129+
"""
130+
variables = ("format", "image_type", "image", "url", "config", "config_dir", "pfile", "pass_file", "user_file")
131+
return all(getattr(self, attr) == getattr(other, attr) for attr in variables)
132+
124133
def get_format(self) -> str:
125134
"""Getter method to get the container format"""
126135
return self.format
@@ -208,6 +217,15 @@ def __init__(self, data: dict) -> None:
208217
self.stop_command = data["stop_command"] if "stop_command" in data else self.STOP_COMMAND
209218
self.pull_command = data["pull_command"] if "pull_command" in data else self.PULL_COMMAND
210219

220+
def __eq__(self, other: "ContainerFormatConfig"):
221+
"""
222+
Equality magic method used for testing this class
223+
224+
:param other: Another ContainerFormatConfig object to check if they're the same
225+
"""
226+
variables = ("command", "run_command", "stop_command", "pull_command")
227+
return all(getattr(self, attr) == getattr(other, attr) for attr in variables)
228+
211229
def get_command(self) -> str:
212230
"""Getter method to get the container command"""
213231
return self.command
@@ -242,6 +260,15 @@ def __init__(self, data: dict) -> None:
242260
self.status = data["status"] if "status" in data else self.STATUS_COMMAND
243261
self.kill = data["kill"] if "kill" in data else self.KILL_COMMAND
244262

263+
def __eq__(self, other: "ProcessConfig"):
264+
"""
265+
Equality magic method used for testing this class
266+
267+
:param other: Another ProcessConfig object to check if they're the same
268+
"""
269+
variables = ("status", "kill")
270+
return all(getattr(self, attr) == getattr(other, attr) for attr in variables)
271+
245272
def get_status_command(self) -> str:
246273
"""Getter method to get the status command"""
247274
return self.status
@@ -264,12 +291,10 @@ class ServerConfig: # pylint: disable=R0903
264291
container_format: ContainerFormatConfig = None
265292

266293
def __init__(self, data: dict) -> None:
267-
if "container" in data:
268-
self.container = ContainerConfig(data["container"])
269-
if "process" in data:
270-
self.process = ProcessConfig(data["process"])
271-
if self.container.get_format() in data:
272-
self.container_format = ContainerFormatConfig(data[self.container.get_format()])
294+
self.container = ContainerConfig(data["container"]) if "container" in data else None
295+
self.process = ProcessConfig(data["process"]) if "process" in data else None
296+
container_format_data = data.get(self.container.get_format() if self.container else None)
297+
self.container_format = ContainerFormatConfig(container_format_data) if container_format_data else None
273298

274299

275300
class RedisConfig:

tests/fixtures/server.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"""
2+
Fixtures specifically for help testing the modules in the server/ directory.
3+
"""
4+
import pytest
5+
import shutil
6+
from typing import Dict
7+
8+
@pytest.fixture(scope="class")
9+
def server_container_config_data(temp_output_dir: str):
10+
"""
11+
Fixture to provide sample data for ContainerConfig tests
12+
13+
:param temp_output_dir: The path to the temporary output directory we'll be using for this test run
14+
"""
15+
return {
16+
"format": "docker",
17+
"image_type": "postgres",
18+
"image": "postgres:latest",
19+
"url": "postgres://localhost",
20+
"config": "postgres.conf",
21+
"config_dir": "/path/to/config",
22+
"pfile": "merlin_server_postgres.pf",
23+
"pass_file": f"{temp_output_dir}/postgres.pass",
24+
"user_file": "postgres.users",
25+
}
26+
27+
@pytest.fixture(scope="class")
28+
def server_container_format_config_data():
29+
"""
30+
Fixture to provide sample data for ContainerFormatConfig tests
31+
"""
32+
return {
33+
"command": "docker",
34+
"run_command": "{command} run --name {name} -d {image}",
35+
"stop_command": "{command} stop {name}",
36+
"pull_command": "{command} pull {url}",
37+
}
38+
39+
@pytest.fixture(scope="class")
40+
def server_process_config_data():
41+
"""
42+
Fixture to provide sample data for ProcessConfig tests
43+
"""
44+
return {
45+
"status": "status {pid}",
46+
"kill": "terminate {pid}",
47+
}
48+
49+
@pytest.fixture(scope="class")
50+
def server_server_config(
51+
server_container_config_data: Dict[str, str],
52+
server_process_config_data: Dict[str, str],
53+
server_container_format_config_data: Dict[str, str],
54+
):
55+
"""
56+
Fixture to provide sample data for ServerConfig tests
57+
58+
:param server_container_config_data: A pytest fixture of test data to pass to the ContainerConfig class
59+
:param server_process_config_data: A pytest fixture of test data to pass to the ProcessConfig class
60+
:param server_container_format_config_data: A pytest fixture of test data to pass to the ContainerFormatConfig class
61+
"""
62+
return {
63+
"container": server_container_config_data,
64+
"process": server_process_config_data,
65+
"docker": server_container_format_config_data,
66+
}
67+
68+
69+
@pytest.fixture(scope="class")
70+
def server_redis_conf_file(temp_output_dir: str):
71+
"""
72+
Fixture to copy the redis.conf file from the merlin/server/ directory to the
73+
temporary output directory and provide the path to the copied file
74+
75+
:param temp_output_dir: The path to the temporary output directory we'll be using for this test run
76+
"""
77+
# TODO
78+
# - will probably have to do more than just copying over the conf file
79+
# - likely want to create our own test conf file with the settings that
80+
# can be modified by RedisConf instead
81+
path_to_redis_conf = f"{os.path.dirname(os.path.abspath(__file__))}/../../merlin/server/redis.conf"
82+
path_to_copied_redis = f"{temp_output_dir}/redis.conf"
83+
shutil.copy(path_to_redis_conf, path_to_copied_redis)
84+
return path_to_copied_redis

tests/unit/server/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)