From 736e779f4cb91ab6c08164e39c33a3280faec67e Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Wed, 10 Aug 2022 17:32:39 +0200 Subject: [PATCH 1/3] BB: Gather enabled tests and select GCP machines needed for the specific tests --- envs/monkey_zoo/blackbox/conftest.py | 31 +++++++++++++++++++++-- envs/monkey_zoo/blackbox/test_blackbox.py | 16 +++--------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/envs/monkey_zoo/blackbox/conftest.py b/envs/monkey_zoo/blackbox/conftest.py index d844bc403..09dce3c27 100644 --- a/envs/monkey_zoo/blackbox/conftest.py +++ b/envs/monkey_zoo/blackbox/conftest.py @@ -1,5 +1,10 @@ import pytest +from envs.monkey_zoo.blackbox.gcp_test_machine_list import ( + GCP_SINGLE_TEST_LIST, + GCP_TEST_MACHINE_LIST, +) + def pytest_addoption(parser): parser.addoption( @@ -33,8 +38,30 @@ def no_gcp(request): @pytest.fixture(scope="session") -def machines_to_start(request): - return request.config.getoption("-k") +def list_machines(request): + enabled_tests = [test.name for test in request.node.items] + + if len(enabled_tests) == len(GCP_SINGLE_TEST_LIST.keys()): + return GCP_TEST_MACHINE_LIST + + try: + list_machines_to_start = [GCP_SINGLE_TEST_LIST[test] for test in enabled_tests] + + if len(list_machines_to_start) == 1: + return list_machines_to_start[0] + + single_machine_list = {} + + for machine_dict in list_machines_to_start: + for zone, machines in machine_dict.items(): + for machine in machines: + if machine not in single_machine_list[zone]: + single_machine_list[zone].append(machine) + + except KeyError: + return GCP_TEST_MACHINE_LIST + + return single_machine_list def pytest_runtest_setup(item): diff --git a/envs/monkey_zoo/blackbox/test_blackbox.py b/envs/monkey_zoo/blackbox/test_blackbox.py index 4731a70ae..52e9e10dd 100644 --- a/envs/monkey_zoo/blackbox/test_blackbox.py +++ b/envs/monkey_zoo/blackbox/test_blackbox.py @@ -6,10 +6,6 @@ import pytest from envs.monkey_zoo.blackbox.analyzers.communication_analyzer import CommunicationAnalyzer from envs.monkey_zoo.blackbox.analyzers.zerologon_analyzer import ZerologonAnalyzer -from envs.monkey_zoo.blackbox.gcp_test_machine_list import ( - GCP_SINGLE_TEST_LIST, - GCP_TEST_MACHINE_LIST, -) from envs.monkey_zoo.blackbox.island_client.monkey_island_client import MonkeyIslandClient from envs.monkey_zoo.blackbox.island_client.test_configuration_parser import get_target_ips from envs.monkey_zoo.blackbox.log_handlers.test_logs_handler import TestLogsHandler @@ -38,16 +34,10 @@ LOGGER = logging.getLogger(__name__) @pytest.fixture(autouse=True, scope="session") -def GCPHandler(request, no_gcp, machines_to_start): +def GCPHandler(request, no_gcp, list_machines): if not no_gcp: - list_machines = GCP_TEST_MACHINE_LIST - if machines_to_start: - try: - list_machines = GCP_SINGLE_TEST_LIST[machines_to_start] - except KeyError as err: - LOGGER.warning( - f"Partial or wrong test name provided. " f"Starting all GCP machines:{err}" - ) + LOGGER.info(f"MACHINES TO START: {list_machines}") + try: initialize_gcp_client() start_machines(list_machines) From ea81226c2adf792885dc39d73ef4f7bf8a82468c Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 14:18:23 -0400 Subject: [PATCH 2/3] BB: Simplify list_machines() --- envs/monkey_zoo/blackbox/conftest.py | 36 +++++++++------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/envs/monkey_zoo/blackbox/conftest.py b/envs/monkey_zoo/blackbox/conftest.py index 09dce3c27..ec8852abd 100644 --- a/envs/monkey_zoo/blackbox/conftest.py +++ b/envs/monkey_zoo/blackbox/conftest.py @@ -1,9 +1,8 @@ +from typing import Collection, Dict, Mapping, Set + import pytest -from envs.monkey_zoo.blackbox.gcp_test_machine_list import ( - GCP_SINGLE_TEST_LIST, - GCP_TEST_MACHINE_LIST, -) +from envs.monkey_zoo.blackbox.gcp_test_machine_list import GCP_SINGLE_TEST_LIST def pytest_addoption(parser): @@ -38,30 +37,17 @@ def no_gcp(request): @pytest.fixture(scope="session") -def list_machines(request): - enabled_tests = [test.name for test in request.node.items] +def list_machines(request: pytest.FixtureRequest) -> Mapping[str, Collection[str]]: + machines_to_start: Dict[str, Set[str]] = {} - if len(enabled_tests) == len(GCP_SINGLE_TEST_LIST.keys()): - return GCP_TEST_MACHINE_LIST + enabled_tests = (test.name for test in request.node.items) + machines_for_enabled_tests = (GCP_SINGLE_TEST_LIST[test] for test in enabled_tests) - try: - list_machines_to_start = [GCP_SINGLE_TEST_LIST[test] for test in enabled_tests] + for machine_dict in machines_for_enabled_tests: + for zone, machines in machine_dict.items(): + machines_to_start.setdefault(zone, set()).update(machines) - if len(list_machines_to_start) == 1: - return list_machines_to_start[0] - - single_machine_list = {} - - for machine_dict in list_machines_to_start: - for zone, machines in machine_dict.items(): - for machine in machines: - if machine not in single_machine_list[zone]: - single_machine_list[zone].append(machine) - - except KeyError: - return GCP_TEST_MACHINE_LIST - - return single_machine_list + return machines_to_start def pytest_runtest_setup(item): From ae82578ae0805ab0691534cdbbde1087bdaa03df Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 10 Aug 2022 14:23:16 -0400 Subject: [PATCH 3/3] BB: Rename list_machines -> gcp_machines_to_start --- envs/monkey_zoo/blackbox/conftest.py | 2 +- envs/monkey_zoo/blackbox/test_blackbox.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/envs/monkey_zoo/blackbox/conftest.py b/envs/monkey_zoo/blackbox/conftest.py index ec8852abd..3553f9edc 100644 --- a/envs/monkey_zoo/blackbox/conftest.py +++ b/envs/monkey_zoo/blackbox/conftest.py @@ -37,7 +37,7 @@ def no_gcp(request): @pytest.fixture(scope="session") -def list_machines(request: pytest.FixtureRequest) -> Mapping[str, Collection[str]]: +def gcp_machines_to_start(request: pytest.FixtureRequest) -> Mapping[str, Collection[str]]: machines_to_start: Dict[str, Set[str]] = {} enabled_tests = (test.name for test in request.node.items) diff --git a/envs/monkey_zoo/blackbox/test_blackbox.py b/envs/monkey_zoo/blackbox/test_blackbox.py index 52e9e10dd..16ee4c0be 100644 --- a/envs/monkey_zoo/blackbox/test_blackbox.py +++ b/envs/monkey_zoo/blackbox/test_blackbox.py @@ -34,20 +34,20 @@ LOGGER = logging.getLogger(__name__) @pytest.fixture(autouse=True, scope="session") -def GCPHandler(request, no_gcp, list_machines): +def GCPHandler(request, no_gcp, gcp_machines_to_start): if not no_gcp: - LOGGER.info(f"MACHINES TO START: {list_machines}") + LOGGER.info(f"MACHINES TO START: {gcp_machines_to_start}") try: initialize_gcp_client() - start_machines(list_machines) + start_machines(gcp_machines_to_start) except Exception as e: LOGGER.error("GCP Handler failed to initialize: %s." % e) pytest.exit("Encountered an error while starting GCP machines. Stopping the tests.") wait_machine_bootup() def fin(): - stop_machines(list_machines) + stop_machines(gcp_machines_to_start) request.addfinalizer(fin)