From afbc13a06b1d263fc65758a18bbc457bdfc894f9 Mon Sep 17 00:00:00 2001 From: Shay Nehmad Date: Tue, 25 Feb 2020 14:57:50 +0200 Subject: [PATCH] CR fixes --- .../analyzers/performance_analyzer.py | 17 +++++++++++++++-- .../island_client/monkey_island_client.py | 19 +++++++++++-------- envs/monkey_zoo/blackbox/test_blackbox.py | 5 ++--- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/envs/monkey_zoo/blackbox/analyzers/performance_analyzer.py b/envs/monkey_zoo/blackbox/analyzers/performance_analyzer.py index d6b3e23f0..8007f8cdd 100644 --- a/envs/monkey_zoo/blackbox/analyzers/performance_analyzer.py +++ b/envs/monkey_zoo/blackbox/analyzers/performance_analyzer.py @@ -7,6 +7,14 @@ from envs.monkey_zoo.blackbox.island_client.monkey_island_client import MonkeyIs MAX_ALLOWED_SINGLE_PAGE_TIME = timedelta(seconds=2) MAX_ALLOWED_TOTAL_TIME = timedelta(seconds=5) +REPORT_URLS = [ + "api/report/security", + "api/attack/report", + "api/report/zero_trust/findings", + "api/report/zero_trust/principles", + "api/report/zero_trust/pillars" +] + logger = logging.getLogger(__name__) @@ -18,12 +26,17 @@ class PerformanceAnalyzer(Analyzer): def analyze_test_results(self) -> bool: if not self.island_client.is_all_monkeys_dead(): - logger.info("Can't test report times since not all Monkeys have died.") - return False + raise RuntimeError("Can't test report times since not all Monkeys have died.") total_time = timedelta() self.island_client.clear_caches() + + report_resource_to_response_time = {} + + for url in REPORT_URLS: + report_resource_to_response_time[url] = self.island_client.get_elapsed_for_get_request(url) + timings = self.island_client.time_all_report_pages() single_page_time_less_then_max = True diff --git a/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py b/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py index 193f1473c..2f838cec6 100644 --- a/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py +++ b/envs/monkey_zoo/blackbox/island_client/monkey_island_client.py @@ -113,13 +113,16 @@ class MonkeyIslandClient(object): report_resource_to_response_time = {} for url in REPORT_URLS: - response = self.requests.get(url) - if response.ok: - LOGGER.debug(f"Got ok for {url} content peek:\n{response.content[:120].strip()}") - report_resource_to_response_time[url] = response.elapsed - else: - LOGGER.error(f"Trying to get {url} but got unexpected {str(response)}") - # instead of raising for status, mark failed responses as maxtime - report_resource_to_response_time[url] = timedelta.max() + report_resource_to_response_time[url] = self.get_elapsed_for_get_request(url) return report_resource_to_response_time + + def get_elapsed_for_get_request(self, url): + response = self.requests.get(url) + if response.ok: + LOGGER.debug(f"Got ok for {url} content peek:\n{response.content[:120].strip()}") + return response.elapsed + else: + LOGGER.error(f"Trying to get {url} but got unexpected {str(response)}") + # instead of raising for status, mark failed responses as maxtime + return timedelta.max() diff --git a/envs/monkey_zoo/blackbox/test_blackbox.py b/envs/monkey_zoo/blackbox/test_blackbox.py index d2e98f2de..03e4d03c5 100644 --- a/envs/monkey_zoo/blackbox/test_blackbox.py +++ b/envs/monkey_zoo/blackbox/test_blackbox.py @@ -13,7 +13,6 @@ from envs.monkey_zoo.blackbox.tests.basic_test import BasicTest from envs.monkey_zoo.blackbox.log_handlers.test_logs_handler import TestLogsHandler DEFAULT_TIMEOUT_SECONDS = 5*60 -PERFORMANCE_TIMEOUT_SECONDS = 10*60 MACHINE_BOOTUP_WAIT_SECONDS = 30 GCP_TEST_MACHINE_LIST = ['sshkeys-11', 'sshkeys-12', 'elastic-4', 'elastic-5', 'hadoop-2', 'hadoop-3', 'mssql-16', 'mimikatz-14', 'mimikatz-15', 'struts2-23', 'struts2-24', 'tunneling-9', 'tunneling-10', @@ -70,7 +69,7 @@ class TestMonkeyBlackbox(object): log_handler=log_handler).run() @staticmethod - def run_performance_test(island_client, conf_filename, test_name, timeout_in_seconds=DEFAULT_TIMEOUT_SECONDS): + def run_performance_test(island_client, conf_filename, test_name, timeout_in_seconds): config_parser = IslandConfigParser(conf_filename) log_handler = TestLogsHandler(test_name, island_client, TestMonkeyBlackbox.get_log_dir_path()) BasicTest( @@ -141,4 +140,4 @@ class TestMonkeyBlackbox(object): island_client, "PERFORMANCE.conf", "test_report_performance", - timeout_in_seconds=PERFORMANCE_TIMEOUT_SECONDS) + timeout_in_seconds=10*60)