Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move UTs to separate directory, add pytest to pre-commit #1109

Merged
merged 16 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
default_stages: [commit]
repos:
- repo: https://github.com/pycqa/isort
rev: 5.8.0
Expand Down Expand Up @@ -33,3 +34,12 @@ repos:
hooks:
- id: eslint
args: ["monkey/monkey_island/cc/ui/src/", "--fix", "--max-warnings=0"]
- repo: local
mssalvatore marked this conversation as resolved.
Show resolved Hide resolved
hooks:
- id: pytest
name: pytest
entry: bash -c "cd monkey && pytest"
language: system
files: "monkey/"
exclude: "monkey/monkey_island/cc/ui"
stages: [push]
2 changes: 1 addition & 1 deletion deployment_scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ been run or all issues have not been resolved.

To install and configure pre-commit manually, run `pip install --user
pre-commit`. Next, go to the top level directory of this repository and run
`pre-commit install` Now, pre-commit will automatically run whenever you `git commit`.
`pre-commit install -t pre-commit -t pre-push` Now, pre-commit will automatically run whenever you `git commit`.
2 changes: 1 addition & 1 deletion deployment_scripts/deploy_linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ log_message() {
configure_precommit() {
$1 -m pip install --user pre-commit
pushd "$2"
$HOME/.local/bin/pre-commit install
$HOME/.local/bin/pre-commit install -t pre-commit -t pre-push
popd
}

Expand Down
2 changes: 1 addition & 1 deletion deployment_scripts/deploy_windows.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function Configure-precommit([String] $git_repo_dir)
if ($LastExitCode) {
exit
}
pre-commit install
pre-commit install -t pre-commit -t pre-push
if ($LastExitCode) {
exit
}
Expand Down
2 changes: 1 addition & 1 deletion docs/content/development/setup-development-environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ Pre-commit is a multi-language package manager for pre-commit hooks. It will run

Our CI system runs the same checks when pull requests are submitted. This system may report that the build has failed if the pre-commit hooks have not been run or all issues have not been resolved.

To install and configure pre-commit, run `pip install --user pre-commit`. Next, go to the top level directory of this repository and run `pre-commit install`. Pre-commit will now run automatically whenever you `git commit`.
To install and configure pre-commit, run `pip install --user pre-commit`. Next, go to the top level directory of this repository and run `pre-commit install -t pre-commit -t pre-push`. Pre-commit will now run automatically whenever you `git commit`.
62 changes: 0 additions & 62 deletions monkey/monkey_island/cc/services/reporting/test_report.py

This file was deleted.

7 changes: 0 additions & 7 deletions monkey/pytest.ini

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from unittest import TestCase

from .aws_service import filter_instance_data_from_aws_response
from common.cloud.aws.aws_service import filter_instance_data_from_aws_response

__author__ = "shay.nehmad"

Expand Down
51 changes: 51 additions & 0 deletions monkey/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import os
import sys
from pathlib import Path

import pytest

MONKEY_BASE_PATH = str(Path(__file__).parent.parent)
sys.path.insert(0, MONKEY_BASE_PATH)


print("imported")
shreyamalviya marked this conversation as resolved.
Show resolved Hide resolved


@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")
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase

from .payload_parsing import LimitedSizePayload, Payload
from infection_monkey.exploit.tools.payload_parsing import LimitedSizePayload, Payload


class TestPayload(TestCase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,44 @@
from unittest import TestCase
from unittest.mock import MagicMock, patch

import pytest

from common.utils.exceptions import (
AlreadyRegisteredError,
CredentialsNotRequiredError,
InvalidRegistrationCredentialsError,
RegistrationNotNeededError,
)
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 = 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"
)
STANDARD_ENV = os.path.join(TEST_RESOURCES_DIR, "server_config_standard_env.json")
WITH_CREDENTIALS = None
NO_CREDENTIALS = None
PARTIAL_CREDENTIALS = None
STANDARD_WITH_CREDENTIALS = None
STANDARD_ENV = None


# This fixture is a dirty hack that can be removed once these tests are converted from
# unittest to pytest. Instead, the appropriate fixtures from conftest.py can be used.
@pytest.fixture(scope="module", autouse=True)
def configure_resources(environment_resources_dir):
global WITH_CREDENTIALS
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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,16 @@

from monkey_island.cc.environment.environment_config import EnvironmentConfig
from monkey_island.cc.environment.user_creds import UserCreds
from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR, MONKEY_ISLAND_ABS_PATH

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")
from monkey_island.cc.server_utils.consts import DEFAULT_DATA_DIR


@pytest.fixture
def config_file(tmpdir):
return os.path.join(tmpdir, "test_config.json")


def test_get_with_credentials():
config_dict = EnvironmentConfig(WITH_CREDENTIALS).to_dict()
def test_get_with_credentials(with_credentials):
config_dict = EnvironmentConfig(with_credentials).to_dict()

assert len(config_dict.keys()) == 5
assert config_dict["server_config"] == "password"
Expand All @@ -36,17 +25,17 @@ def test_get_with_credentials():
assert config_dict["data_dir"] == DEFAULT_DATA_DIR


def test_get_with_no_credentials():
config_dict = EnvironmentConfig(NO_CREDENTIALS).to_dict()
def test_get_with_no_credentials(no_credentials):
config_dict = EnvironmentConfig(no_credentials).to_dict()

assert len(config_dict.keys()) == 3
assert config_dict["server_config"] == "password"
assert config_dict["deployment"] == "develop"
assert config_dict["data_dir"] == DEFAULT_DATA_DIR


def test_get_with_partial_credentials():
config_dict = EnvironmentConfig(PARTIAL_CREDENTIALS).to_dict()
def test_get_with_partial_credentials(partial_credentials):
config_dict = EnvironmentConfig(partial_credentials).to_dict()

assert len(config_dict.keys()) == 4
assert config_dict["server_config"] == "password"
Expand All @@ -55,8 +44,8 @@ def test_get_with_partial_credentials():
assert config_dict["data_dir"] == DEFAULT_DATA_DIR


def test_save_to_file(config_file):
shutil.copyfile(STANDARD_WITH_CREDENTIALS, config_file)
def test_save_to_file(config_file, standard_with_credentials):
shutil.copyfile(standard_with_credentials, config_file)

environment_config = EnvironmentConfig(config_file)
environment_config.aws = "test_aws"
Expand All @@ -74,12 +63,12 @@ def test_save_to_file(config_file):
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_password_hash = "fedcba"
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.add_user(new_user_creds)
Expand All @@ -92,8 +81,8 @@ def test_add_user(config_file):
assert from_file["password_hash"] == new_password_hash


def test_get_users():
environment_config = EnvironmentConfig(STANDARD_WITH_CREDENTIALS)
def test_get_users(standard_with_credentials):
environment_config = EnvironmentConfig(standard_with_credentials)
users = environment_config.get_users()

assert len(users) == 1
Expand All @@ -115,17 +104,17 @@ def test_generate_default_file(config_file):
assert environment_config.data_dir == DEFAULT_DATA_DIR


def test_data_dir():
environment_config = EnvironmentConfig(WITH_DATA_DIR)
def test_data_dir(with_data_dir):
environment_config = EnvironmentConfig(with_data_dir)
assert environment_config.data_dir == "/test/data/dir"


def set_home_env(monkeypatch, 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)

config = EnvironmentConfig(WITH_DATA_DIR_HOME)
config = EnvironmentConfig(with_data_dir_home)
assert config.data_dir_abs_path == os.path.join(tmpdir, "data_dir")
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import pytest

from monkey_island.cc.models.monkey import Monkey, MonkeyNotFoundError

from ..test_common.fixtures import FixtureEnum
from .monkey_ttl import MonkeyTtl
from monkey_island.cc.models.monkey_ttl import MonkeyTtl
from monkey_island.cc.test_common.fixtures import FixtureEnum

logger = logging.getLogger(__name__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

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

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
Expand All @@ -17,11 +17,11 @@ def mock_home_env(monkeypatch, 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")
TEST_STRING = "Hello, Monkey!"

json_setup_logging(TEST_LOGGER_CONFIG_PATH)
json_setup_logging(test_logger_config_path)

logger = logging.getLogger("TestLogger")
logger.info(TEST_STRING)
Expand Down
Loading