cc: Move config files and other testing resources to tests/ dir

This commit is contained in:
Mike Salvatore 2021-04-20 11:27:30 -04:00
parent 88a3ab5bf1
commit 753c0586c1
16 changed files with 112 additions and 51 deletions

View File

@ -1,5 +1,51 @@
import os
import sys import sys
from pathlib import Path from pathlib import Path
import pytest
MONKEY_BASE_PATH = str(Path(__file__).parent.parent) MONKEY_BASE_PATH = str(Path(__file__).parent.parent)
sys.path.insert(0, MONKEY_BASE_PATH) sys.path.insert(0, MONKEY_BASE_PATH)
print("imported")
@pytest.fixture(scope="session")
def resources_dir(pytestconfig):
return os.path.join(pytestconfig.rootdir, "monkey", "tests", "resources")
@pytest.fixture(scope="session")
def environment_resources_dir(resources_dir):
return os.path.join(resources_dir, "environment")
@pytest.fixture(scope="session")
def with_credentials(environment_resources_dir):
return os.path.join(environment_resources_dir, "server_config_with_credentials.json")
@pytest.fixture(scope="session")
def no_credentials(environment_resources_dir):
return os.path.join(environment_resources_dir, "server_config_no_credentials.json")
@pytest.fixture(scope="session")
def partial_credentials(environment_resources_dir):
return os.path.join(environment_resources_dir, "server_config_partial_credentials.json")
@pytest.fixture(scope="session")
def standard_with_credentials(environment_resources_dir):
return os.path.join(environment_resources_dir, "server_config_standard_with_credentials.json")
@pytest.fixture(scope="session")
def with_data_dir(environment_resources_dir):
return os.path.join(environment_resources_dir, "server_config_with_data_dir.json")
@pytest.fixture(scope="session")
def with_data_dir_home(environment_resources_dir):
return os.path.join(environment_resources_dir, "server_config_with_data_dir_home.json")

View File

@ -4,6 +4,8 @@ from typing import Dict
from unittest import TestCase from unittest import TestCase
from unittest.mock import MagicMock, patch from unittest.mock import MagicMock, patch
import pytest
from common.utils.exceptions import ( from common.utils.exceptions import (
AlreadyRegisteredError, AlreadyRegisteredError,
CredentialsNotRequiredError, CredentialsNotRequiredError,
@ -11,17 +13,35 @@ from common.utils.exceptions import (
RegistrationNotNeededError, RegistrationNotNeededError,
) )
from monkey_island.cc.environment import Environment, EnvironmentConfig, UserCreds from monkey_island.cc.environment import Environment, EnvironmentConfig, UserCreds
from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH
TEST_RESOURCES_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "testing", "environment") WITH_CREDENTIALS = None
NO_CREDENTIALS = None
PARTIAL_CREDENTIALS = None
STANDARD_WITH_CREDENTIALS = None
STANDARD_ENV = None
WITH_CREDENTIALS = os.path.join(TEST_RESOURCES_DIR, "server_config_with_credentials.json")
NO_CREDENTIALS = os.path.join(TEST_RESOURCES_DIR, "server_config_no_credentials.json") # This fixture is a dirty hack that can be removed once these tests are converted from
PARTIAL_CREDENTIALS = os.path.join(TEST_RESOURCES_DIR, "server_config_partial_credentials.json") # unittest to pytest. Instead, the appropriate fixtures from conftest.py can be used.
STANDARD_WITH_CREDENTIALS = os.path.join( @pytest.fixture(scope="module", autouse=True)
TEST_RESOURCES_DIR, "server_config_standard_with_credentials.json" def configure_resources(environment_resources_dir):
) global WITH_CREDENTIALS
STANDARD_ENV = os.path.join(TEST_RESOURCES_DIR, "server_config_standard_env.json") global NO_CREDENTIALS
global PARTIAL_CREDENTIALS
global STANDARD_WITH_CREDENTIALS
global STANDARD_ENV
WITH_CREDENTIALS = os.path.join(
environment_resources_dir, "server_config_with_credentials.json"
)
NO_CREDENTIALS = os.path.join(environment_resources_dir, "server_config_no_credentials.json")
PARTIAL_CREDENTIALS = os.path.join(
environment_resources_dir, "server_config_partial_credentials.json"
)
STANDARD_WITH_CREDENTIALS = os.path.join(
environment_resources_dir, "server_config_standard_with_credentials.json"
)
STANDARD_ENV = os.path.join(environment_resources_dir, "server_config_standard_env.json")
def get_tmp_file(): def get_tmp_file():

View File

@ -6,18 +6,7 @@ import pytest
from monkey_island.cc.environment.environment_config import EnvironmentConfig from monkey_island.cc.environment.environment_config import EnvironmentConfig
from monkey_island.cc.environment.user_creds import UserCreds from monkey_island.cc.environment.user_creds import UserCreds
from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR, MONKEY_ISLAND_ABS_PATH from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR
TEST_RESOURCES_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "testing", "environment")
WITH_CREDENTIALS = os.path.join(TEST_RESOURCES_DIR, "server_config_with_credentials.json")
NO_CREDENTIALS = os.path.join(TEST_RESOURCES_DIR, "server_config_no_credentials.json")
PARTIAL_CREDENTIALS = os.path.join(TEST_RESOURCES_DIR, "server_config_partial_credentials.json")
STANDARD_WITH_CREDENTIALS = os.path.join(
TEST_RESOURCES_DIR, "server_config_standard_with_credentials.json"
)
WITH_DATA_DIR = os.path.join(TEST_RESOURCES_DIR, "server_config_with_data_dir.json")
WITH_DATA_DIR_HOME = os.path.join(TEST_RESOURCES_DIR, "server_config_with_data_dir_home.json")
@pytest.fixture @pytest.fixture
@ -25,8 +14,8 @@ def config_file(tmpdir):
return os.path.join(tmpdir, "test_config.json") return os.path.join(tmpdir, "test_config.json")
def test_get_with_credentials(): def test_get_with_credentials(with_credentials):
config_dict = EnvironmentConfig(WITH_CREDENTIALS).to_dict() config_dict = EnvironmentConfig(with_credentials).to_dict()
assert len(config_dict.keys()) == 5 assert len(config_dict.keys()) == 5
assert config_dict["server_config"] == "password" assert config_dict["server_config"] == "password"
@ -36,8 +25,8 @@ def test_get_with_credentials():
assert config_dict["data_dir"] == DEFAULT_DATA_DIR assert config_dict["data_dir"] == DEFAULT_DATA_DIR
def test_get_with_no_credentials(): def test_get_with_no_credentials(no_credentials):
config_dict = EnvironmentConfig(NO_CREDENTIALS).to_dict() config_dict = EnvironmentConfig(no_credentials).to_dict()
assert len(config_dict.keys()) == 3 assert len(config_dict.keys()) == 3
assert config_dict["server_config"] == "password" assert config_dict["server_config"] == "password"
@ -45,8 +34,8 @@ def test_get_with_no_credentials():
assert config_dict["data_dir"] == DEFAULT_DATA_DIR assert config_dict["data_dir"] == DEFAULT_DATA_DIR
def test_get_with_partial_credentials(): def test_get_with_partial_credentials(partial_credentials):
config_dict = EnvironmentConfig(PARTIAL_CREDENTIALS).to_dict() config_dict = EnvironmentConfig(partial_credentials).to_dict()
assert len(config_dict.keys()) == 4 assert len(config_dict.keys()) == 4
assert config_dict["server_config"] == "password" assert config_dict["server_config"] == "password"
@ -55,8 +44,8 @@ def test_get_with_partial_credentials():
assert config_dict["data_dir"] == DEFAULT_DATA_DIR assert config_dict["data_dir"] == DEFAULT_DATA_DIR
def test_save_to_file(config_file): def test_save_to_file(config_file, standard_with_credentials):
shutil.copyfile(STANDARD_WITH_CREDENTIALS, config_file) shutil.copyfile(standard_with_credentials, config_file)
environment_config = EnvironmentConfig(config_file) environment_config = EnvironmentConfig(config_file)
environment_config.aws = "test_aws" environment_config.aws = "test_aws"
@ -74,12 +63,12 @@ def test_save_to_file(config_file):
assert from_file["data_dir"] == DEFAULT_DATA_DIR assert from_file["data_dir"] == DEFAULT_DATA_DIR
def test_add_user(config_file): def test_add_user(config_file, standard_with_credentials):
new_user = "new_user" new_user = "new_user"
new_password_hash = "fedcba" new_password_hash = "fedcba"
new_user_creds = UserCreds(new_user, new_password_hash) new_user_creds = UserCreds(new_user, new_password_hash)
shutil.copyfile(STANDARD_WITH_CREDENTIALS, config_file) shutil.copyfile(standard_with_credentials, config_file)
environment_config = EnvironmentConfig(config_file) environment_config = EnvironmentConfig(config_file)
environment_config.add_user(new_user_creds) environment_config.add_user(new_user_creds)
@ -92,8 +81,8 @@ def test_add_user(config_file):
assert from_file["password_hash"] == new_password_hash assert from_file["password_hash"] == new_password_hash
def test_get_users(): def test_get_users(standard_with_credentials):
environment_config = EnvironmentConfig(STANDARD_WITH_CREDENTIALS) environment_config = EnvironmentConfig(standard_with_credentials)
users = environment_config.get_users() users = environment_config.get_users()
assert len(users) == 1 assert len(users) == 1
@ -115,8 +104,8 @@ def test_generate_default_file(config_file):
assert environment_config.data_dir == DEFAULT_DATA_DIR assert environment_config.data_dir == DEFAULT_DATA_DIR
def test_data_dir(): def test_data_dir(with_data_dir):
environment_config = EnvironmentConfig(WITH_DATA_DIR) environment_config = EnvironmentConfig(with_data_dir)
assert environment_config.data_dir == "/test/data/dir" assert environment_config.data_dir == "/test/data/dir"
@ -124,8 +113,8 @@ def set_home_env(monkeypatch, tmpdir):
monkeypatch.setenv("HOME", str(tmpdir)) monkeypatch.setenv("HOME", str(tmpdir))
def test_data_dir_abs_path_from_file(monkeypatch, tmpdir): def test_data_dir_abs_path_from_file(monkeypatch, tmpdir, with_data_dir_home):
set_home_env(monkeypatch, tmpdir) set_home_env(monkeypatch, tmpdir)
config = EnvironmentConfig(WITH_DATA_DIR_HOME) config = EnvironmentConfig(with_data_dir_home)
assert config.data_dir_abs_path == os.path.join(tmpdir, "data_dir") assert config.data_dir_abs_path == os.path.join(tmpdir, "data_dir")

View File

@ -3,12 +3,12 @@ import os
import pytest import pytest
from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH
from monkey_island.cc.server_utils.island_logger import json_setup_logging from monkey_island.cc.server_utils.island_logger import json_setup_logging
TEST_LOGGER_CONFIG_PATH = os.path.join(
MONKEY_ISLAND_ABS_PATH, "cc", "testing", "logger_config.json" @pytest.fixture()
) def test_logger_config_path(resources_dir):
return os.path.join(resources_dir, "logger_config.json")
# TODO move into monkey/monkey_island/cc/test_common/fixtures after rebase/backmerge # TODO move into monkey/monkey_island/cc/test_common/fixtures after rebase/backmerge
@ -17,11 +17,11 @@ def mock_home_env(monkeypatch, tmpdir):
monkeypatch.setenv("HOME", str(tmpdir)) monkeypatch.setenv("HOME", str(tmpdir))
def test_expanduser_filename(mock_home_env, tmpdir): def test_expanduser_filename(mock_home_env, tmpdir, test_logger_config_path):
INFO_LOG = os.path.join(tmpdir, "info.log") INFO_LOG = os.path.join(tmpdir, "info.log")
TEST_STRING = "Hello, Monkey!" TEST_STRING = "Hello, Monkey!"
json_setup_logging(TEST_LOGGER_CONFIG_PATH) json_setup_logging(test_logger_config_path)
logger = logging.getLogger("TestLogger") logger = logging.getLogger("TestLogger")
logger.info(TEST_STRING) logger.info(TEST_STRING)

View File

@ -1,29 +1,27 @@
import os import os
from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH
from monkey_island.cc.server_utils.encryptor import get_encryptor, initialize_encryptor from monkey_island.cc.server_utils.encryptor import get_encryptor, initialize_encryptor
TEST_DATA_DIR = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", "testing")
PASSWORD_FILENAME = "mongo_key.bin" PASSWORD_FILENAME = "mongo_key.bin"
PLAINTEXT = "Hello, Monkey!" PLAINTEXT = "Hello, Monkey!"
CYPHERTEXT = "vKgvD6SjRyIh1dh2AM/rnTa0NI/vjfwnbZLbMocWtE4e42WJmSUz2ordtbQrH1Fq" CYPHERTEXT = "vKgvD6SjRyIh1dh2AM/rnTa0NI/vjfwnbZLbMocWtE4e42WJmSUz2ordtbQrH1Fq"
def test_aes_cbc_encryption(): def test_aes_cbc_encryption(resources_dir):
initialize_encryptor(TEST_DATA_DIR) initialize_encryptor(resources_dir)
assert get_encryptor().enc(PLAINTEXT) != PLAINTEXT assert get_encryptor().enc(PLAINTEXT) != PLAINTEXT
def test_aes_cbc_decryption(): def test_aes_cbc_decryption(resources_dir):
initialize_encryptor(TEST_DATA_DIR) initialize_encryptor(resources_dir)
assert get_encryptor().dec(CYPHERTEXT) == PLAINTEXT assert get_encryptor().dec(CYPHERTEXT) == PLAINTEXT
def test_aes_cbc_enc_dec(): def test_aes_cbc_enc_dec(resources_dir):
initialize_encryptor(TEST_DATA_DIR) initialize_encryptor(resources_dir)
assert get_encryptor().dec(get_encryptor().enc(PLAINTEXT)) == PLAINTEXT assert get_encryptor().dec(get_encryptor().enc(PLAINTEXT)) == PLAINTEXT

View File

@ -0,0 +1,4 @@
{
"server_config": "password",
"deployment": "develop"
}

View File

@ -0,0 +1,4 @@
{
"server_config": "password",
"deployment": "develop"
}