Skip to content

Commit

Permalink
start work on tests for RedisConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
bgunnar5 committed May 23, 2024
1 parent 812459f commit e1f667d
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 21 deletions.
84 changes: 67 additions & 17 deletions tests/fixtures/server.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
"""
Fixtures specifically for help testing the modules in the server/ directory.
"""
import os
import pytest
import shutil
from typing import Dict

@pytest.fixture(scope="class")
def server_container_config_data(temp_output_dir: str):
def server_container_config_data(temp_output_dir: str) -> Dict[str, 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
:returns: A dict containing the necessary key/values for the ContainerConfig object
"""
return {
"format": "docker",
Expand All @@ -25,9 +26,11 @@ def server_container_config_data(temp_output_dir: str):
}

@pytest.fixture(scope="class")
def server_container_format_config_data():
def server_container_format_config_data() -> Dict[str, str]:
"""
Fixture to provide sample data for ContainerFormatConfig tests
:returns: A dict containing the necessary key/values for the ContainerFormatConfig object
"""
return {
"command": "docker",
Expand All @@ -37,9 +40,11 @@ def server_container_format_config_data():
}

@pytest.fixture(scope="class")
def server_process_config_data():
def server_process_config_data() -> Dict[str, str]:
"""
Fixture to provide sample data for ProcessConfig tests
:returns: A dict containing the necessary key/values for the ProcessConfig object
"""
return {
"status": "status {pid}",
Expand All @@ -51,13 +56,14 @@ 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],
):
) -> Dict[str, 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
:returns: A dictionary containing each of the configuration dicts we'll need
"""
return {
"container": server_container_config_data,
Expand All @@ -66,19 +72,63 @@ def server_server_config(
}


@pytest.fixture(scope="class")
def server_redis_conf_file(temp_output_dir: str):
@pytest.fixture(scope="session")
def server_testing_dir(temp_output_dir: str) -> 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
Fixture to create a temporary output directory for tests related to the server functionality.
:param temp_output_dir: The path to the temporary output directory we'll be using for this test run
:returns: The path to the temporary testing directory for server tests
"""
testing_dir = f"{temp_output_dir}/server_testing/"
if not os.path.exists(testing_dir):
os.mkdir(testing_dir)

return testing_dir


@pytest.fixture(scope="session")
def server_redis_conf_file(server_testing_dir: str) -> 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.
If a test will modify this file with a file write, you should make a copy of
this file to modify instead.
:param server_testing_dir: A pytest fixture that defines a path to the the output directory we'll write to
:returns: The path to the redis configuration file we'll use for testing
"""
# 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
redis_conf_file = f"{server_testing_dir}/redis.conf"
file_contents = """
# ip address
bind 127.0.0.1
# port
port 6379
# password
requirepass merlin_password
# directory
dir ./
# snapshot
save 300 100
# db file
dbfilename dump.rdb
# append mode
appendfsync everysec
# append file
appendfilename appendonly.aof
# dummy trailing comment
""".strip().replace(" ", "")

with open(redis_conf_file, "w") as rcf:
rcf.write(file_contents)

return redis_conf_file
62 changes: 58 additions & 4 deletions tests/unit/server/test_server_util.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""
Tests for the `server_util.py` module.
"""
import filecmp
import os
import pytest
import shutil
from typing import Callable, Dict, Union

from merlin.server.server_util import (
Expand Down Expand Up @@ -288,8 +290,60 @@ def test_init_with_missing_data(self, server_process_config_data: Dict[str, str]
assert config.container_format is None


# class TestRedisConfig:
# """Tests for the RedisConfig class."""
class TestRedisConfig:
"""Tests for the RedisConfig class."""

def test_initialization(self, server_redis_conf_file: str):
"""
Using a dummy redis configuration file, test that the initialization
of the RedisConfig class behaves as expected.
:param server_redis_conf_file: The path to a dummy redis configuration file
"""
expected_entries = {
"bind": "127.0.0.1",
"port": "6379",
"requirepass": "merlin_password",
"dir": "./",
"save": "300 100",
"dbfilename": "dump.rdb",
"appendfsync": "everysec",
"appendfilename": "appendonly.aof",
}
expected_comments = {
"bind": "# ip address\n",
"port": "\n# port\n",
"requirepass": "\n# password\n",
"dir": "\n# directory\n",
"save": "\n# snapshot\n",
"dbfilename": "\n# db file\n",
"appendfsync": "\n# append mode\n",
"appendfilename": "\n# append file\n",
}
expected_trailing_comment = "\n# dummy trailing comment"
expected_entry_order = list(expected_entries.keys())
redis_config = RedisConfig(server_redis_conf_file)
assert redis_config.filename == server_redis_conf_file
assert not redis_config.changed
assert redis_config.entries == expected_entries
assert redis_config.entry_order == expected_entry_order
assert redis_config.comments == expected_comments
assert redis_config.trailing_comments == expected_trailing_comment

def test_write(self, server_redis_conf_file: str, server_testing_dir: str):
"""
"""
copy_redis_conf_file = f"{server_testing_dir}/redis_copy.conf"

# Create a RedisConf object with the basic redis conf file
redis_config = RedisConfig(server_redis_conf_file)

# Change the filepath of the redis config file to be the copy that we'll write to
redis_config.filename = copy_redis_conf_file

# Run the test
redis_config.write()

# Check that the contents of the copied file match the contents of the basic file
assert filecmp.cmp(server_redis_conf_file, copy_redis_conf_file)

# def test_parse(self, server_redis_conf_file):
# raise ValueError

0 comments on commit e1f667d

Please sign in to comment.