Skip to content

Commit

Permalink
Merge pull request #1178 from guardicore/test_common_refactoring
Browse files Browse the repository at this point in the history
Test common refactoring
  • Loading branch information
VakarisZ authored May 20, 2021
2 parents 832453f + e978823 commit b02c006
Show file tree
Hide file tree
Showing 85 changed files with 27 additions and 43 deletions.
4 changes: 0 additions & 4 deletions monkey/monkey_island/cc/test_common/fixtures/__init__.py

This file was deleted.

2 changes: 0 additions & 2 deletions monkey/monkey_island/cc/test_common/fixtures/fixture_enum.py

This file was deleted.

File renamed without changes.
3 changes: 0 additions & 3 deletions monkey/tests/monkey_island/cc/conftest.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Profiling island

To profile specific methods on island a `@profile(sort_args=['cumulative'], print_args=[100])`
decorator can be used.
To profile specific methods on island a `@profile(sort_args=['cumulative'], print_args=[100])`
decorator can be used.
Use it as a parameterised decorator(`@profile()`). After decorated method is used, a file will appear in a
directory provided in `profiler_decorator.py`. Filename describes the path of
the method that was profiled. For example if method `monkey_island/cc/resources/netmap.get`
was profiled, then the results of this profiling will appear in
was profiled, then the results of this profiling will appear in
`monkey_island_cc_resources_netmap_get`.
Empty file.
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions monkey/tests/unit_tests/monkey_island/cc/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Without these imports pytests can't use fixtures,
# because they are not found
from tests.unit_tests.monkey_island.cc.mongomock_fixtures import * # noqa: F401,F403
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

from monkey_island.cc.models.monkey import Monkey, MonkeyNotFoundError
from monkey_island.cc.models.monkey_ttl import MonkeyTtl
from monkey_island.cc.test_common.fixtures import FixtureEnum

logger = logging.getLogger(__name__)


class TestMonkey:
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_is_dead(self):
# Arrange
alive_monkey_ttl = MonkeyTtl.create_ttl_expire_in(30)
Expand All @@ -38,7 +37,7 @@ def test_is_dead(self):
assert mia_monkey.is_dead()
assert not alive_monkey.is_dead()

@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_ttl_renewal(self):
# Arrange
monkey = Monkey(guid=str(uuid.uuid4()))
Expand All @@ -49,7 +48,7 @@ def test_ttl_renewal(self):
monkey.renew_ttl()
assert monkey.ttl_ref

@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_get_single_monkey_by_id(self):
# Arrange
a_monkey = Monkey(guid=str(uuid.uuid4()))
Expand All @@ -63,7 +62,7 @@ def test_get_single_monkey_by_id(self):
with pytest.raises(MonkeyNotFoundError) as _:
_ = Monkey.get_single_monkey_by_id("abcdefabcdefabcdefabcdef")

@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_get_os(self):
linux_monkey = Monkey(
guid=str(uuid.uuid4()),
Expand All @@ -79,7 +78,7 @@ def test_get_os(self):
assert 1 == len([m for m in Monkey.objects() if m.get_os() == "linux"])
assert 1 == len([m for m in Monkey.objects() if m.get_os() == "unknown"])

@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_get_tunneled_monkeys(self):
linux_monkey = Monkey(guid=str(uuid.uuid4()), description="Linux shay-Virtual-Machine")
windows_monkey = Monkey(
Expand All @@ -100,7 +99,7 @@ def test_get_tunneled_monkeys(self):
)
assert test

@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_get_label_by_id(self):
hostname_example = "a_hostname"
ip_example = "1.1.1.1"
Expand Down Expand Up @@ -148,7 +147,7 @@ def test_get_label_by_id(self):
assert cache_info_after_query_3.hits == 1
assert cache_info_after_query_3.misses == 2

@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_is_monkey(self):
a_monkey = Monkey(guid=str(uuid.uuid4()))
a_monkey.save()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
from monkey_island.cc.models.zero_trust.finding import Finding
from monkey_island.cc.models.zero_trust.monkey_finding import MonkeyFinding
from monkey_island.cc.models.zero_trust.monkey_finding_details import MonkeyFindingDetails
from monkey_island.cc.test_common.fixtures import FixtureEnum

MONKEY_FINDING_DETAIL_MOCK = MonkeyFindingDetails()
MONKEY_FINDING_DETAIL_MOCK.events = ["mock1", "mock2"]


class TestMonkeyFinding:
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_save_finding_validation(self):
with pytest.raises(ValidationError):
_ = MonkeyFinding.save_finding(
Expand All @@ -22,7 +21,7 @@ def test_save_finding_validation(self):
detail_ref=MONKEY_FINDING_DETAIL_MOCK,
)

@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_save_finding_sanity(self):
assert len(Finding.objects(test=zero_trust_consts.TEST_SEGMENTATION)) == 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from monkey_island.cc.models.zero_trust.scoutsuite_finding import ScoutSuiteFinding
from monkey_island.cc.models.zero_trust.scoutsuite_finding_details import ScoutSuiteFindingDetails
from monkey_island.cc.services.zero_trust.test_common.scoutsuite_finding_data import RULES
from monkey_island.cc.test_common.fixtures import FixtureEnum

MONKEY_FINDING_DETAIL_MOCK = MonkeyFindingDetails()
MONKEY_FINDING_DETAIL_MOCK.events = ["mock1", "mock2"]
Expand All @@ -16,7 +15,7 @@


class TestScoutSuiteFinding:
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_save_finding_validation(self):
with pytest.raises(ValidationError):
_ = ScoutSuiteFinding.save_finding(
Expand All @@ -25,7 +24,7 @@ def test_save_finding_validation(self):
detail_ref=SCOUTSUITE_FINDING_DETAIL_MOCK,
)

@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_save_finding_sanity(self):
assert len(Finding.objects(test=zero_trust_consts.TEST_SEGMENTATION)) == 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from monkey_island.cc.models.zero_trust.finding import Finding


@pytest.fixture(scope="session", autouse=True)
@pytest.fixture(scope="module", autouse=True)
def change_to_mongo_mock():
# Make sure tests are working with mongomock
mongoengine.disconnect()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@

from monkey_island.cc.models.edge import Edge
from monkey_island.cc.services.edge.edge import EdgeService
from monkey_island.cc.test_common.fixtures import FixtureEnum

logger = logging.getLogger(__name__)


class TestEdgeService:
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_get_or_create_edge(self):
src_id = ObjectId()
dst_id = ObjectId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from monkey_island.cc.services.zero_trust.monkey_findings.monkey_zt_finding_service import (
MonkeyZTFindingService,
)
from monkey_island.cc.test_common.fixtures import FixtureEnum

EVENTS = [
Event.create_event(
Expand Down Expand Up @@ -40,7 +39,7 @@


class TestMonkeyZTFindingService:
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_create_or_add_to_existing_creation(self):
# Create new finding
MonkeyZTFindingService.create_or_add_to_existing(
Expand All @@ -55,7 +54,7 @@ def test_create_or_add_to_existing_creation(self):
assert len(finding_details.events) == 1
assert finding_details.events[0].message == EVENTS[0].message

@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_create_or_add_to_existing_addition(self):
# Create new finding
MonkeyZTFindingService.create_or_add_to_existing(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
from monkey_island.cc.services.zero_trust.scoutsuite.scoutsuite_auth_service import (
is_aws_keys_setup,
)
from monkey_island.cc.test_common.fixtures import FixtureEnum


class MockObject:
pass


@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_is_aws_keys_setup(tmp_path):
# Mock default configuration
ConfigService.init_default_config()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
RULES,
SCOUTSUITE_FINDINGS,
)
from monkey_island.cc.test_common.fixtures import FixtureEnum


class TestScoutSuiteZTFindingService:
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_process_rule(self):
# Creates new PermissiveFirewallRules finding with a rule
ScoutSuiteZTFindingService.process_rule(SCOUTSUITE_FINDINGS[0], RULES[0])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@
EnrichedFinding,
FindingService,
)
from monkey_island.cc.test_common.fixtures.fixture_enum import FixtureEnum


@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_get_all_findings():
get_scoutsuite_finding_dto().save()
get_monkey_finding_dto().save()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
from monkey_island.cc.services.zero_trust.zero_trust_report.test_common.example_finding_data import ( # noqa: E501
save_example_findings,
)
from monkey_island.cc.test_common.fixtures import FixtureEnum


@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_get_pillars_grades():
save_example_findings()
expected_grades = _get_expected_pillar_grades()
Expand Down Expand Up @@ -97,7 +96,7 @@ def _get_cnt_of_tests_in_pillar(pillar: str):
return len(tests_in_pillar)


@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_get_pillars_to_statuses():
# Test empty database
expected = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from monkey_island.cc.services.zero_trust.zero_trust_report.principle_service import (
PrincipleService,
)
from monkey_island.cc.test_common.fixtures import FixtureEnum

EXPECTED_DICT = {
"test_pillar1": [
Expand Down Expand Up @@ -39,7 +38,7 @@
}


@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
@pytest.mark.usefixtures("uses_database")
def test_get_principles_status():
TEST_PILLAR1 = "test_pillar1"
TEST_PILLAR2 = "test_pillar2"
Expand Down
File renamed without changes.

0 comments on commit b02c006

Please sign in to comment.