forked from p34709852/monkey
Removed the need to change server_config.json just to run tests.
This commit is contained in:
parent
e69c94ae50
commit
d4dc42adb5
|
@ -53,12 +53,6 @@ install:
|
||||||
# print hugo version (useful for debugging documentation build errors)
|
# print hugo version (useful for debugging documentation build errors)
|
||||||
- hugo version
|
- hugo version
|
||||||
|
|
||||||
before_script:
|
|
||||||
# Set the server config to `testing`. This is required for for the UTs to pass.
|
|
||||||
- pushd /home/travis/build/guardicore/monkey/monkey
|
|
||||||
- python monkey_island/cc/environment/set_server_config.py testing
|
|
||||||
- popd
|
|
||||||
|
|
||||||
script:
|
script:
|
||||||
# Check Python code
|
# Check Python code
|
||||||
## Check syntax errors and fail the build if any are found.
|
## Check syntax errors and fail the build if any are found.
|
||||||
|
|
|
@ -10,13 +10,11 @@ logger = logging.getLogger(__name__)
|
||||||
AWS = 'aws'
|
AWS = 'aws'
|
||||||
STANDARD = 'standard'
|
STANDARD = 'standard'
|
||||||
PASSWORD = 'password'
|
PASSWORD = 'password'
|
||||||
TESTING = 'testing'
|
|
||||||
|
|
||||||
ENV_DICT = {
|
ENV_DICT = {
|
||||||
STANDARD: standard.StandardEnvironment,
|
STANDARD: standard.StandardEnvironment,
|
||||||
AWS: aws.AwsEnvironment,
|
AWS: aws.AwsEnvironment,
|
||||||
PASSWORD: password.PasswordEnvironment,
|
PASSWORD: password.PasswordEnvironment
|
||||||
TESTING: testing.TestingEnvironment
|
|
||||||
}
|
}
|
||||||
|
|
||||||
env = None
|
env = None
|
||||||
|
|
|
@ -42,7 +42,7 @@ def main():
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("server_config", choices=["standard", "testing", "password"])
|
parser.add_argument("server_config", choices=["standard", "password"])
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
return args
|
return args
|
||||||
|
|
||||||
|
|
|
@ -10,11 +10,6 @@ from .monkey import Monkey # noqa: F401
|
||||||
from .monkey_ttl import MonkeyTtl # noqa: F401
|
from .monkey_ttl import MonkeyTtl # noqa: F401
|
||||||
from .pba_results import PbaResults # noqa: F401
|
from .pba_results import PbaResults # noqa: F401
|
||||||
|
|
||||||
# This section sets up the DB connection according to the environment.
|
connect(db=env_singleton.env.mongo_db_name,
|
||||||
# If testing, use mongomock which only emulates mongo. for more information, see
|
host=env_singleton.env.mongo_db_host,
|
||||||
# http://docs.mongoengine.org/guide/mongomock.html .
|
port=env_singleton.env.mongo_db_port)
|
||||||
# Otherwise, use an actual mongod instance with connection parameters supplied by env.
|
|
||||||
if env_singleton.env.testing: # See monkey_island.cc.environment.testing
|
|
||||||
connect('mongoenginetest', host='mongomock://localhost')
|
|
||||||
else:
|
|
||||||
connect(db=env_singleton.env.mongo_db_name, host=env_singleton.env.mongo_db_host, port=env_singleton.env.mongo_db_port)
|
|
||||||
|
|
|
@ -22,9 +22,6 @@ class TestMonkey(IslandTestCase):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def test_is_dead(self):
|
def test_is_dead(self):
|
||||||
self.fail_if_not_testing_env()
|
|
||||||
self.clean_monkey_db()
|
|
||||||
|
|
||||||
# Arrange
|
# Arrange
|
||||||
alive_monkey_ttl = MonkeyTtl.create_ttl_expire_in(30)
|
alive_monkey_ttl = MonkeyTtl.create_ttl_expire_in(30)
|
||||||
alive_monkey_ttl.save()
|
alive_monkey_ttl.save()
|
||||||
|
@ -52,9 +49,6 @@ class TestMonkey(IslandTestCase):
|
||||||
self.assertFalse(alive_monkey.is_dead())
|
self.assertFalse(alive_monkey.is_dead())
|
||||||
|
|
||||||
def test_ttl_renewal(self):
|
def test_ttl_renewal(self):
|
||||||
self.fail_if_not_testing_env()
|
|
||||||
self.clean_monkey_db()
|
|
||||||
|
|
||||||
# Arrange
|
# Arrange
|
||||||
monkey = Monkey(guid=str(uuid.uuid4()))
|
monkey = Monkey(guid=str(uuid.uuid4()))
|
||||||
monkey.save()
|
monkey.save()
|
||||||
|
@ -65,9 +59,6 @@ class TestMonkey(IslandTestCase):
|
||||||
self.assertIsNotNone(monkey.ttl_ref)
|
self.assertIsNotNone(monkey.ttl_ref)
|
||||||
|
|
||||||
def test_get_single_monkey_by_id(self):
|
def test_get_single_monkey_by_id(self):
|
||||||
self.fail_if_not_testing_env()
|
|
||||||
self.clean_monkey_db()
|
|
||||||
|
|
||||||
# Arrange
|
# Arrange
|
||||||
a_monkey = Monkey(guid=str(uuid.uuid4()))
|
a_monkey = Monkey(guid=str(uuid.uuid4()))
|
||||||
a_monkey.save()
|
a_monkey.save()
|
||||||
|
@ -81,9 +72,6 @@ class TestMonkey(IslandTestCase):
|
||||||
_ = Monkey.get_single_monkey_by_id("abcdefabcdefabcdefabcdef")
|
_ = Monkey.get_single_monkey_by_id("abcdefabcdefabcdefabcdef")
|
||||||
|
|
||||||
def test_get_os(self):
|
def test_get_os(self):
|
||||||
self.fail_if_not_testing_env()
|
|
||||||
self.clean_monkey_db()
|
|
||||||
|
|
||||||
linux_monkey = Monkey(guid=str(uuid.uuid4()),
|
linux_monkey = Monkey(guid=str(uuid.uuid4()),
|
||||||
description="Linux shay-Virtual-Machine 4.15.0-50-generic #54-Ubuntu")
|
description="Linux shay-Virtual-Machine 4.15.0-50-generic #54-Ubuntu")
|
||||||
windows_monkey = Monkey(guid=str(uuid.uuid4()),
|
windows_monkey = Monkey(guid=str(uuid.uuid4()),
|
||||||
|
@ -99,9 +87,6 @@ class TestMonkey(IslandTestCase):
|
||||||
self.assertEqual(1, len([m for m in Monkey.objects() if m.get_os() == "unknown"]))
|
self.assertEqual(1, len([m for m in Monkey.objects() if m.get_os() == "unknown"]))
|
||||||
|
|
||||||
def test_get_tunneled_monkeys(self):
|
def test_get_tunneled_monkeys(self):
|
||||||
self.fail_if_not_testing_env()
|
|
||||||
self.clean_monkey_db()
|
|
||||||
|
|
||||||
linux_monkey = Monkey(guid=str(uuid.uuid4()),
|
linux_monkey = Monkey(guid=str(uuid.uuid4()),
|
||||||
description="Linux shay-Virtual-Machine")
|
description="Linux shay-Virtual-Machine")
|
||||||
windows_monkey = Monkey(guid=str(uuid.uuid4()),
|
windows_monkey = Monkey(guid=str(uuid.uuid4()),
|
||||||
|
@ -121,9 +106,6 @@ class TestMonkey(IslandTestCase):
|
||||||
self.assertTrue(test, "Tunneling test")
|
self.assertTrue(test, "Tunneling test")
|
||||||
|
|
||||||
def test_get_label_by_id(self):
|
def test_get_label_by_id(self):
|
||||||
self.fail_if_not_testing_env()
|
|
||||||
self.clean_monkey_db()
|
|
||||||
|
|
||||||
hostname_example = "a_hostname"
|
hostname_example = "a_hostname"
|
||||||
ip_example = "1.1.1.1"
|
ip_example = "1.1.1.1"
|
||||||
linux_monkey = Monkey(guid=str(uuid.uuid4()),
|
linux_monkey = Monkey(guid=str(uuid.uuid4()),
|
||||||
|
@ -169,9 +151,6 @@ class TestMonkey(IslandTestCase):
|
||||||
self.assertEqual(cache_info_after_query_3.misses, 2)
|
self.assertEqual(cache_info_after_query_3.misses, 2)
|
||||||
|
|
||||||
def test_is_monkey(self):
|
def test_is_monkey(self):
|
||||||
self.fail_if_not_testing_env()
|
|
||||||
self.clean_monkey_db()
|
|
||||||
|
|
||||||
a_monkey = Monkey(guid=str(uuid.uuid4()))
|
a_monkey = Monkey(guid=str(uuid.uuid4()))
|
||||||
a_monkey.save()
|
a_monkey.save()
|
||||||
|
|
||||||
|
|
|
@ -7,9 +7,6 @@ from monkey_island.cc.testing.IslandTestCase import IslandTestCase
|
||||||
|
|
||||||
class TestEvent(IslandTestCase):
|
class TestEvent(IslandTestCase):
|
||||||
def test_create_event(self):
|
def test_create_event(self):
|
||||||
self.fail_if_not_testing_env()
|
|
||||||
self.clean_finding_db()
|
|
||||||
|
|
||||||
with self.assertRaises(ValidationError):
|
with self.assertRaises(ValidationError):
|
||||||
_ = Event.create_event(
|
_ = Event.create_event(
|
||||||
title=None, # title required
|
title=None, # title required
|
||||||
|
|
|
@ -16,11 +16,7 @@ class TestFinding(IslandTestCase):
|
||||||
server.json file is found and loaded.
|
server.json file is found and loaded.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@pytest.mark.skip(reason="Broken during ScoutSuite refactoring, need to be fixed")
|
|
||||||
def test_save_finding_validation(self):
|
def test_save_finding_validation(self):
|
||||||
self.fail_if_not_testing_env()
|
|
||||||
self.clean_finding_db()
|
|
||||||
|
|
||||||
with self.assertRaises(ValidationError):
|
with self.assertRaises(ValidationError):
|
||||||
_ = Finding.save_finding(test="bla bla", status=zero_trust_consts.STATUS_FAILED, events=[])
|
_ = Finding.save_finding(test="bla bla", status=zero_trust_consts.STATUS_FAILED, events=[])
|
||||||
|
|
||||||
|
@ -29,9 +25,6 @@ class TestFinding(IslandTestCase):
|
||||||
|
|
||||||
@pytest.mark.skip(reason="Broken during ScoutSuite refactoring, need to be fixed")
|
@pytest.mark.skip(reason="Broken during ScoutSuite refactoring, need to be fixed")
|
||||||
def test_save_finding_sanity(self):
|
def test_save_finding_sanity(self):
|
||||||
self.fail_if_not_testing_env()
|
|
||||||
self.clean_finding_db()
|
|
||||||
|
|
||||||
self.assertEqual(len(Finding.objects(test=zero_trust_consts.TEST_SEGMENTATION)), 0)
|
self.assertEqual(len(Finding.objects(test=zero_trust_consts.TEST_SEGMENTATION)), 0)
|
||||||
|
|
||||||
event_example = Event.create_event(
|
event_example = Event.create_event(
|
||||||
|
|
|
@ -47,7 +47,6 @@ EXPLOIT_DATA_MOCK = [{
|
||||||
|
|
||||||
class TestDisplayedEdgeService(IslandTestCase):
|
class TestDisplayedEdgeService(IslandTestCase):
|
||||||
def test_get_displayed_edges_by_to(self):
|
def test_get_displayed_edges_by_to(self):
|
||||||
self.clean_edge_db()
|
|
||||||
|
|
||||||
dst_id = ObjectId()
|
dst_id = ObjectId()
|
||||||
|
|
||||||
|
|
|
@ -19,9 +19,6 @@ class TestEdgeService(IslandTestCase):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def test_get_or_create_edge(self):
|
def test_get_or_create_edge(self):
|
||||||
self.fail_if_not_testing_env()
|
|
||||||
self.clean_edge_db()
|
|
||||||
|
|
||||||
src_id = ObjectId()
|
src_id = ObjectId()
|
||||||
dst_id = ObjectId()
|
dst_id = ObjectId()
|
||||||
|
|
||||||
|
|
|
@ -7,9 +7,6 @@ from monkey_island.cc.testing.IslandTestCase import IslandTestCase
|
||||||
|
|
||||||
class TestPTHReportServiceGenerateMapNodes(IslandTestCase):
|
class TestPTHReportServiceGenerateMapNodes(IslandTestCase):
|
||||||
def test_generate_map_nodes(self):
|
def test_generate_map_nodes(self):
|
||||||
self.fail_if_not_testing_env()
|
|
||||||
self.clean_monkey_db()
|
|
||||||
|
|
||||||
self.assertEqual(PTHReportService.generate_map_nodes(), [])
|
self.assertEqual(PTHReportService.generate_map_nodes(), [])
|
||||||
|
|
||||||
windows_monkey_with_services = Monkey(
|
windows_monkey_with_services = Monkey(
|
||||||
|
@ -43,9 +40,6 @@ class TestPTHReportServiceGenerateMapNodes(IslandTestCase):
|
||||||
self.assertEqual(2, len(map_nodes))
|
self.assertEqual(2, len(map_nodes))
|
||||||
|
|
||||||
def test_generate_map_nodes_parsing(self):
|
def test_generate_map_nodes_parsing(self):
|
||||||
self.fail_if_not_testing_env()
|
|
||||||
self.clean_monkey_db()
|
|
||||||
|
|
||||||
monkey_id = str(uuid.uuid4())
|
monkey_id = str(uuid.uuid4())
|
||||||
hostname = "A_Windows_PC_1"
|
hostname = "A_Windows_PC_1"
|
||||||
windows_monkey_with_services = Monkey(
|
windows_monkey_with_services = Monkey(
|
||||||
|
|
|
@ -8,9 +8,6 @@ from monkey_island.cc.testing.IslandTestCase import IslandTestCase
|
||||||
|
|
||||||
class TestEnvironmentTelemetryProcessing(IslandTestCase):
|
class TestEnvironmentTelemetryProcessing(IslandTestCase):
|
||||||
def test_process_environment_telemetry(self):
|
def test_process_environment_telemetry(self):
|
||||||
self.fail_if_not_testing_env()
|
|
||||||
self.clean_monkey_db()
|
|
||||||
|
|
||||||
# Arrange
|
# Arrange
|
||||||
monkey_guid = str(uuid.uuid4())
|
monkey_guid = str(uuid.uuid4())
|
||||||
a_monkey = Monkey(guid=monkey_guid)
|
a_monkey = Monkey(guid=monkey_guid)
|
||||||
|
|
|
@ -36,9 +36,6 @@ class SystemInfoTelemetryDispatcherTest(IslandTestCase):
|
||||||
dispatcher.dispatch_collector_results_to_relevant_processors(good_telem_empty_collectors)
|
dispatcher.dispatch_collector_results_to_relevant_processors(good_telem_empty_collectors)
|
||||||
|
|
||||||
def test_dispatch_to_relevant_collector(self):
|
def test_dispatch_to_relevant_collector(self):
|
||||||
self.fail_if_not_testing_env()
|
|
||||||
self.clean_monkey_db()
|
|
||||||
|
|
||||||
a_monkey = Monkey(guid=str(uuid.uuid4()))
|
a_monkey = Monkey(guid=str(uuid.uuid4()))
|
||||||
a_monkey.save()
|
a_monkey.save()
|
||||||
|
|
||||||
|
|
|
@ -16,9 +16,6 @@ THIRD_SUBNET = "3.3.3.3-3.3.3.200"
|
||||||
class TestSegmentationChecks(IslandTestCase):
|
class TestSegmentationChecks(IslandTestCase):
|
||||||
|
|
||||||
def test_create_findings_for_all_done_pairs(self):
|
def test_create_findings_for_all_done_pairs(self):
|
||||||
self.fail_if_not_testing_env()
|
|
||||||
self.clean_finding_db()
|
|
||||||
|
|
||||||
all_subnets = [FIRST_SUBNET, SECOND_SUBNET, THIRD_SUBNET]
|
all_subnets = [FIRST_SUBNET, SECOND_SUBNET, THIRD_SUBNET]
|
||||||
|
|
||||||
monkey = Monkey(
|
monkey = Monkey(
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
import mongoengine
|
||||||
|
|
||||||
import monkey_island.cc.environment.environment_singleton as env_singleton
|
import monkey_island.cc.environment.environment_singleton as env_singleton
|
||||||
from monkey_island.cc.models import Monkey
|
from monkey_island.cc.models import Monkey
|
||||||
from monkey_island.cc.models.edge import Edge
|
from monkey_island.cc.models.edge import Edge
|
||||||
|
@ -7,17 +9,33 @@ from monkey_island.cc.models.zero_trust.finding import Finding
|
||||||
|
|
||||||
|
|
||||||
class IslandTestCase(unittest.TestCase):
|
class IslandTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
def __init__(self, methodName):
|
||||||
|
# Make sure test is working with mongomock
|
||||||
|
if mongoengine.connection.get_connection().server_info()['sysInfo'] != 'Mock':
|
||||||
|
mongoengine.disconnect()
|
||||||
|
mongoengine.connect('mongoenginetest', host='mongomock://localhost')
|
||||||
|
else:
|
||||||
|
IslandTestCase.clean_db()
|
||||||
|
super().__init__(methodName)
|
||||||
|
|
||||||
def fail_if_not_testing_env(self):
|
def fail_if_not_testing_env(self):
|
||||||
self.assertFalse(not env_singleton.env.testing, "Change server_config.json to testing environment.")
|
self.assertFalse(not env_singleton.env.testing, "Change server_config.json to testing environment.")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def clean_monkey_db():
|
def clean_db():
|
||||||
|
IslandTestCase._clean_edge_db()
|
||||||
|
IslandTestCase._clean_monkey_db()
|
||||||
|
IslandTestCase._clean_finding_db()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _clean_monkey_db():
|
||||||
Monkey.objects().delete()
|
Monkey.objects().delete()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def clean_edge_db():
|
def _clean_edge_db():
|
||||||
Edge.objects().delete()
|
Edge.objects().delete()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def clean_finding_db():
|
def _clean_finding_db():
|
||||||
Finding.objects().delete()
|
Finding.objects().delete()
|
||||||
|
|
Loading…
Reference in New Issue