Skip to content

Commit

Permalink
begin work on server tests and modular fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
bgunnar5 committed May 7, 2024
1 parent b8185cc commit e48fe32
Show file tree
Hide file tree
Showing 4 changed files with 411 additions and 7 deletions.
39 changes: 32 additions & 7 deletions merlin/server/server_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def valid_ipv4(ip: str) -> bool: # pylint: disable=C0103
return False

for i in arr:
if int(i) < 0 and int(i) > 255:
if int(i) < 0 or int(i) > 255:
return False

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

def __eq__(self, other: "ContainerFormatConfig"):
"""
Equality magic method used for testing this class
:param other: Another ContainerFormatConfig object to check if they're the same
"""
variables = ("format", "image_type", "image", "url", "config", "config_dir", "pfile", "pass_file", "user_file")
return all(getattr(self, attr) == getattr(other, attr) for attr in variables)

def get_format(self) -> str:
"""Getter method to get the container format"""
return self.format
Expand Down Expand Up @@ -208,6 +217,15 @@ def __init__(self, data: dict) -> None:
self.stop_command = data["stop_command"] if "stop_command" in data else self.STOP_COMMAND
self.pull_command = data["pull_command"] if "pull_command" in data else self.PULL_COMMAND

def __eq__(self, other: "ContainerFormatConfig"):
"""
Equality magic method used for testing this class
:param other: Another ContainerFormatConfig object to check if they're the same
"""
variables = ("command", "run_command", "stop_command", "pull_command")
return all(getattr(self, attr) == getattr(other, attr) for attr in variables)

def get_command(self) -> str:
"""Getter method to get the container command"""
return self.command
Expand Down Expand Up @@ -242,6 +260,15 @@ def __init__(self, data: dict) -> None:
self.status = data["status"] if "status" in data else self.STATUS_COMMAND
self.kill = data["kill"] if "kill" in data else self.KILL_COMMAND

def __eq__(self, other: "ProcessConfig"):
"""
Equality magic method used for testing this class
:param other: Another ProcessConfig object to check if they're the same
"""
variables = ("status", "kill")
return all(getattr(self, attr) == getattr(other, attr) for attr in variables)

def get_status_command(self) -> str:
"""Getter method to get the status command"""
return self.status
Expand All @@ -264,12 +291,10 @@ class ServerConfig: # pylint: disable=R0903
container_format: ContainerFormatConfig = None

def __init__(self, data: dict) -> None:
if "container" in data:
self.container = ContainerConfig(data["container"])
if "process" in data:
self.process = ProcessConfig(data["process"])
if self.container.get_format() in data:
self.container_format = ContainerFormatConfig(data[self.container.get_format()])
self.container = ContainerConfig(data["container"]) if "container" in data else None
self.process = ProcessConfig(data["process"]) if "process" in data else None
container_format_data = data.get(self.container.get_format() if self.container else None)
self.container_format = ContainerFormatConfig(container_format_data) if container_format_data else None


class RedisConfig:
Expand Down
84 changes: 84 additions & 0 deletions tests/fixtures/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""
Fixtures specifically for help testing the modules in the server/ directory.
"""
import pytest
import shutil
from typing import Dict

@pytest.fixture(scope="class")
def server_container_config_data(temp_output_dir: str):
"""
Fixture to provide sample data for ContainerConfig tests
:param temp_output_dir: The path to the temporary output directory we'll be using for this test run
"""
return {
"format": "docker",
"image_type": "postgres",
"image": "postgres:latest",
"url": "postgres://localhost",
"config": "postgres.conf",
"config_dir": "/path/to/config",
"pfile": "merlin_server_postgres.pf",
"pass_file": f"{temp_output_dir}/postgres.pass",
"user_file": "postgres.users",
}

@pytest.fixture(scope="class")
def server_container_format_config_data():
"""
Fixture to provide sample data for ContainerFormatConfig tests
"""
return {
"command": "docker",
"run_command": "{command} run --name {name} -d {image}",
"stop_command": "{command} stop {name}",
"pull_command": "{command} pull {url}",
}

@pytest.fixture(scope="class")
def server_process_config_data():
"""
Fixture to provide sample data for ProcessConfig tests
"""
return {
"status": "status {pid}",
"kill": "terminate {pid}",
}

@pytest.fixture(scope="class")
def server_server_config(
server_container_config_data: Dict[str, str],
server_process_config_data: Dict[str, str],
server_container_format_config_data: Dict[str, str],
):
"""
Fixture to provide sample data for ServerConfig tests
:param server_container_config_data: A pytest fixture of test data to pass to the ContainerConfig class
:param server_process_config_data: A pytest fixture of test data to pass to the ProcessConfig class
:param server_container_format_config_data: A pytest fixture of test data to pass to the ContainerFormatConfig class
"""
return {
"container": server_container_config_data,
"process": server_process_config_data,
"docker": server_container_format_config_data,
}


@pytest.fixture(scope="class")
def server_redis_conf_file(temp_output_dir: str):
"""
Fixture to copy the redis.conf file from the merlin/server/ directory to the
temporary output directory and provide the path to the copied file
:param temp_output_dir: The path to the temporary output directory we'll be using for this test run
"""
# TODO
# - will probably have to do more than just copying over the conf file
# - likely want to create our own test conf file with the settings that
# can be modified by RedisConf instead
path_to_redis_conf = f"{os.path.dirname(os.path.abspath(__file__))}/../../merlin/server/redis.conf"
path_to_copied_redis = f"{temp_output_dir}/redis.conf"
shutil.copy(path_to_redis_conf, path_to_copied_redis)
return path_to_copied_redis
Empty file added tests/unit/server/__init__.py
Empty file.
Loading

0 comments on commit e48fe32

Please sign in to comment.