diff --git a/simulation-system/libs/csle-common/tests/test_container_controller.py b/simulation-system/libs/csle-common/tests/test_container_controller.py index ea21b9e39..84e0453ab 100644 --- a/simulation-system/libs/csle-common/tests/test_container_controller.py +++ b/simulation-system/libs/csle-common/tests/test_container_controller.py @@ -772,11 +772,9 @@ def test_start_docker_stats_thread(self, mocker: pytest_mock.MockFixture, physical_server_ip='123.456.78.99', logger=logging.Logger("test")) - def test_stop_docker_stats_thread(self, mocker: pytest_mock.MockFixture, - example_emulation_execution: EmulationExecution, - true_stop_fixture, false_running_stats_manager, - true_running_stats_manager, - false_stop_fixture, stop_fixture): + def test_stop_docker_stats_thread( + self, mocker: pytest_mock.MockFixture, example_emulation_execution: EmulationExecution, true_stop_fixture, + false_running_stats_manager, true_running_stats_manager, false_stop_fixture, stop_fixture) -> None: """ Testing the stop_docker_stats_thread @@ -814,9 +812,9 @@ def test_stop_docker_stats_thread(self, mocker: pytest_mock.MockFixture, logging.getLogger("test")) assert stopper is None - def test_get_docker_stats_manager_status(self, mocker: pytest_mock.MockFixture, host_ip, - example_docker_stats_manager_config: DockerStatsManagerConfig, - dsm_status, stub): + def test_get_docker_stats_manager_status( + self, mocker: pytest_mock.MockFixture, host_ip, + example_docker_stats_manager_config: DockerStatsManagerConfig, dsm_status, stub) -> None: """ Testing the get_docker_stats_manager_status method in the ContainerController @@ -837,8 +835,8 @@ def test_get_docker_stats_manager_status(self, mocker: pytest_mock.MockFixture, assert dsm_dto.emulations == ["JDoeEmulation"] assert dsm_dto.emulation_executions == [10] - def test_get_docker_stats_manager_status_by_ip_and_port(self, mocker: pytest_mock.MockFixture, - host_ip, dsm_status, stub): + def test_get_docker_stats_manager_status_by_ip_and_port( + self, mocker: pytest_mock.MockFixture, host_ip, dsm_status, stub) -> None: """ Testing the get_docker_stats_manager_status_by_ip_and_port method in the ContainerController @@ -891,8 +889,8 @@ def test_create_network(self, mocker: pytest_mock.MockFixture, client_1, ipam_po mocker.patch('docker.from_env', side_effect=client_1) mocker.patch('docker.types.IPAMPool', side_effect=ipam_pool) mocker.patch('docker.types.IPAMConfig', side_effect=ipam_config) - creator = ContainerController.create_network(name="JDoeCreator", - subnetmask="null", logger=logging.Logger("test")) + creator = ContainerController.create_network(name="JDoeCreator", subnetmask="null", + logger=logging.Logger("test")) assert creator is None def test_remove_network(self, mocker: pytest_mock.MockFixture, client_1) -> None: @@ -950,7 +948,7 @@ def test_rm_network(self, mocker: pytest_mock.MockFixture, client_1) -> None: remover = ContainerController.rm_network("JDoe", logging.getLogger()) assert remover is False - def test_run_command(self, mocker: pytest_mock.MockFixture, client_1, client_2): + def test_run_command(self, mocker: pytest_mock.MockFixture, client_1, client_2) -> None: """ Testing the run_command method in the ContainerController @@ -964,8 +962,9 @@ def test_run_command(self, mocker: pytest_mock.MockFixture, client_1, client_2): runner = ContainerController.run_command(cmd=constants.MANAGEMENT.LIST_STOPPED) assert runner is None - def test_get_docker_stats_managers_ips(self, mocker: pytest_mock.MockFixture, socket_fix, - example_emulation_env_config: EmulationEnvConfig): + def test_get_docker_stats_managers_ips( + self, mocker: pytest_mock.MockFixture, socket_fix, example_emulation_env_config: EmulationEnvConfig) \ + -> None: """ Testing the get_docker_stats_managers_ips @@ -978,7 +977,7 @@ def test_get_docker_stats_managers_ips(self, mocker: pytest_mock.MockFixture, so ip = ContainerController.get_docker_stats_managers_ips(example_emulation_env_config) assert ip == ["123.456.78.99"] - def test_get_docker_stats_managers_ports(self, example_emulation_env_config: EmulationEnvConfig): + def test_get_docker_stats_managers_ports(self, example_emulation_env_config: EmulationEnvConfig) -> None: """ Testing the get_docker_stats_managers_ports in the ContainerController @@ -989,10 +988,9 @@ def test_get_docker_stats_managers_ports(self, example_emulation_env_config: Emu test_list = ContainerController.get_docker_stats_managers_ports(example_emulation_env_config) assert test_list[0] == 50046 - def test_get_docker_stats_managers_info(self, mocker: pytest_mock.MockFixture, socket_fix, - example_emulation_env_config: EmulationEnvConfig, - host_ip, dsm_status, stub, - example_docker_stats_managers_info: DockerStatsManagersInfo): + def test_get_docker_stats_managers_info( + self, mocker: pytest_mock.MockFixture, socket_fix, example_emulation_env_config: EmulationEnvConfig, + host_ip, dsm_status, stub, example_docker_stats_managers_info: DockerStatsManagersInfo) -> None: """ Testing the get_docker_stats_managers_info diff --git a/simulation-system/libs/csle-common/tests/test_elk_controller.py b/simulation-system/libs/csle-common/tests/test_elk_controller.py index 13627db80..331a2751b 100644 --- a/simulation-system/libs/csle-common/tests/test_elk_controller.py +++ b/simulation-system/libs/csle-common/tests/test_elk_controller.py @@ -16,9 +16,7 @@ class TestElkControllerSuite: @patch("csle_common.util.emulation_util.EmulationUtil.connect_admin") @patch("csle_common.util.emulation_util.EmulationUtil.execute_ssh_cmd") - def test_start_stop_elk_manager( - self, mock_execute_ssh_cmd, mock_connect_admin - ) -> None: + def test_start_stop_elk_manager(self, mock_execute_ssh_cmd, mock_connect_admin) -> None: """ Test case for starting or stopping the ELK manager @@ -35,23 +33,16 @@ def test_start_stop_elk_manager( emulation_env_config.elk_config.get_connection.return_value = MagicMock() logger = MagicMock(spec=logging.Logger) ELKController.start_elk_manager(emulation_env_config, logger) - mock_connect_admin.assert_called_once_with( - emulation_env_config=emulation_env_config, - ip="172.17.0.1", - create_producer=False, - ) + mock_connect_admin.assert_called_once_with(emulation_env_config=emulation_env_config, ip="172.17.0.1", + create_producer=False) mock_execute_ssh_cmd.assert_called() ELKController.stop_elk_manager(emulation_env_config, logger) assert 2 == mock_connect_admin.call_count assert 4 == mock_execute_ssh_cmd.call_count @patch("csle_common.controllers.elk_controller.ELKController.start_elk_manager") - @patch( - "csle_common.controllers.elk_controller.ELKController.get_elk_status_by_port_and_ip" - ) - def test_get_elk_status( - self, mock_get_elk_status_by_port_and_ip, mock_start_elk_manager - ) -> None: + @patch("csle_common.controllers.elk_controller.ELKController.get_elk_status_by_port_and_ip") + def test_get_elk_status(self, mock_get_elk_status_by_port_and_ip, mock_start_elk_manager) -> None: """ Test case for querying the ELKManager about the status of the ELK stack @@ -59,9 +50,7 @@ def test_get_elk_status( :param mock_start_elk_manager: mocked start_elk_manager method """ mock_start_elk_manager.return_value = None - expected_elk_dto = MagicMock( - spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO - ) + expected_elk_dto = MagicMock(spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO) mock_get_elk_status_by_port_and_ip.return_value = expected_elk_dto emulation_env_config = MagicMock(spec=EmulationEnvConfig) elk_config = MagicMock() @@ -70,20 +59,15 @@ def test_get_elk_status( emulation_env_config.elk_config.elk_manager_port = 5601 logger = MagicMock(spec=logging.Logger) result = ELKController.get_elk_status(emulation_env_config, logger) - mock_start_elk_manager.assert_called_once_with( - emulation_env_config=emulation_env_config, logger=logger - ) - mock_get_elk_status_by_port_and_ip.assert_called_once_with( - ip="172.17.0.1", port=5601 - ) + mock_start_elk_manager.assert_called_once_with(emulation_env_config=emulation_env_config, logger=logger) + mock_get_elk_status_by_port_and_ip.assert_called_once_with(ip="172.17.0.1", port=5601) assert result == expected_elk_dto @patch("csle_collector.elk_manager.query_elk_manager.get_elk_status") @patch("csle_collector.elk_manager.elk_manager_pb2_grpc.ElkManagerStub") @patch("grpc.insecure_channel") - def test_get_elk_status_by_port_and_ip( - self, mock_insecure_channel, mock_ElkManagerStub, mock_get_elk_status - ) -> None: + def test_get_elk_status_by_port_and_ip(self, mock_insecure_channel, mock_ElkManagerStub, mock_get_elk_status) \ + -> None: """ Test case for querying the ElkManager about the status of the ELK stack @@ -95,16 +79,12 @@ def test_get_elk_status_by_port_and_ip( mock_insecure_channel.return_value.__enter__.return_value = mock_channel mock_stub = MagicMock() mock_ElkManagerStub.return_value = mock_stub - expected_elk_dto = MagicMock( - spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO - ) + expected_elk_dto = MagicMock(spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO) mock_get_elk_status.return_value = expected_elk_dto ip = "172.17.0.1" port = 5601 result = ELKController.get_elk_status_by_port_and_ip(ip, port) - mock_insecure_channel.assert_called_once_with( - f"{ip}:{port}", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + mock_insecure_channel.assert_called_once_with(f"{ip}:{port}", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_ElkManagerStub.assert_called_once_with(mock_channel) mock_get_elk_status.assert_called_once_with(mock_stub) assert result == expected_elk_dto @@ -113,13 +93,8 @@ def test_get_elk_status_by_port_and_ip( @patch("csle_collector.elk_manager.elk_manager_pb2_grpc.ElkManagerStub") @patch("grpc.insecure_channel") @patch("csle_common.controllers.elk_controller.ELKController.start_elk_manager") - def test_stop_elk_stack( - self, - mock_start_elk_manager, - mock_insecure_channel, - mock_ElkManagerStub, - mock_stop_elk, - ) -> None: + def test_stop_elk_stack(self, mock_start_elk_manager, mock_insecure_channel, mock_ElkManagerStub, mock_stop_elk) \ + -> None: """ Test case for requesting the ELKManager to stop the ELK server @@ -133,9 +108,7 @@ def test_stop_elk_stack( mock_insecure_channel.return_value.__enter__.return_value = mock_channel mock_stub = MagicMock() mock_ElkManagerStub.return_value = mock_stub - expected_elk_dto = MagicMock( - spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO - ) + expected_elk_dto = MagicMock(spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO) mock_stop_elk.return_value = expected_elk_dto emulation_env_config = MagicMock(spec=EmulationEnvConfig) elk_config = MagicMock() @@ -144,9 +117,7 @@ def test_stop_elk_stack( emulation_env_config.elk_config.elk_manager_port = 5601 logger = MagicMock(spec=logging.Logger) result = ELKController.stop_elk_stack(emulation_env_config, logger) - mock_insecure_channel.assert_called_once_with( - "172.17.0.1:5601", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + mock_insecure_channel.assert_called_once_with("172.17.0.1:5601", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_ElkManagerStub.assert_called_once_with(mock_channel) mock_stop_elk.asert_called_once_with(mock_stub) assert result == expected_elk_dto @@ -155,13 +126,8 @@ def test_stop_elk_stack( @patch("csle_collector.elk_manager.elk_manager_pb2_grpc.ElkManagerStub") @patch("grpc.insecure_channel") @patch("csle_common.controllers.elk_controller.ELKController.start_elk_manager") - def test_start_elk_stack( - self, - mock_start_elk_manager, - mock_insecure_channel, - mock_ElkManagerStub, - mock_start_elk, - ) -> None: + def test_start_elk_stack(self, mock_start_elk_manager, mock_insecure_channel, mock_ElkManagerStub, + mock_start_elk) -> None: """ Test case for requesting the ELKManager to start the ELK server @@ -175,9 +141,7 @@ def test_start_elk_stack( mock_insecure_channel.return_value.__enter__.return_value = mock_channel mock_stub = MagicMock() mock_ElkManagerStub.return_value = mock_stub - expected_elk_dto = MagicMock( - spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO - ) + expected_elk_dto = MagicMock(spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO) mock_start_elk.return_value = expected_elk_dto emulation_env_config = MagicMock(spec=EmulationEnvConfig) elk_config = MagicMock() @@ -187,12 +151,8 @@ def test_start_elk_stack( emulation_env_config.elk_config.elk_manager_port = 5601 logger = MagicMock(spec=logging.Logger) physical_server_ip = "192.168.0.1" - result = ELKController.start_elk_stack( - emulation_env_config, physical_server_ip, logger - ) - mock_insecure_channel.assert_called_once_with( - "172.17.0.1:5601", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + result = ELKController.start_elk_stack(emulation_env_config, physical_server_ip, logger) + mock_insecure_channel.assert_called_once_with("172.17.0.1:5601", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_ElkManagerStub.assert_called_once_with(mock_channel) mock_start_elk.asert_called_once_with(mock_stub) assert result == expected_elk_dto @@ -201,13 +161,8 @@ def test_start_elk_stack( @patch("csle_collector.elk_manager.elk_manager_pb2_grpc.ElkManagerStub") @patch("grpc.insecure_channel") @patch("csle_common.controllers.elk_controller.ELKController.start_elk_manager") - def test_start_elastic( - self, - mock_start_elk_manager, - mock_insecure_channel, - mock_ElkManagerStub, - mock_start_elastic, - ) -> None: + def test_start_elastic(self, mock_start_elk_manager, mock_insecure_channel, mock_ElkManagerStub, + mock_start_elastic) -> None: """ Test case for requesting the ELKManager to start elasticsearch @@ -221,9 +176,7 @@ def test_start_elastic( mock_insecure_channel.return_value.__enter__.return_value = mock_channel mock_stub = MagicMock() mock_ElkManagerStub.return_value = mock_stub - expected_elk_dto = MagicMock( - spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO - ) + expected_elk_dto = MagicMock(spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO) mock_start_elastic.return_value = expected_elk_dto emulation_env_config = MagicMock(spec=EmulationEnvConfig) elk_config = MagicMock() @@ -232,9 +185,7 @@ def test_start_elastic( emulation_env_config.elk_config.elk_manager_port = 5601 logger = MagicMock(spec=logging.Logger) result = ELKController.start_elastic(emulation_env_config, logger) - mock_insecure_channel.assert_called_once_with( - "172.17.0.1:5601", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + mock_insecure_channel.assert_called_once_with("172.17.0.1:5601", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_ElkManagerStub.assert_called_once_with(mock_channel) mock_start_elastic.asert_called_once_with(mock_stub) assert result == expected_elk_dto @@ -243,13 +194,8 @@ def test_start_elastic( @patch("csle_collector.elk_manager.elk_manager_pb2_grpc.ElkManagerStub") @patch("grpc.insecure_channel") @patch("csle_common.controllers.elk_controller.ELKController.start_elk_manager") - def test_start_kibana( - self, - mock_start_elk_manager, - mock_insecure_channel, - mock_ElkManagerStub, - mock_start_kibana, - ) -> None: + def test_start_kibana(self, mock_start_elk_manager, mock_insecure_channel, mock_ElkManagerStub, + mock_start_kibana) -> None: """ Test case for requesting the ELKManager to start kibana @@ -263,9 +209,7 @@ def test_start_kibana( mock_insecure_channel.return_value.__enter__.return_value = mock_channel mock_stub = MagicMock() mock_ElkManagerStub.return_value = mock_stub - expected_elk_dto = MagicMock( - spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO - ) + expected_elk_dto = MagicMock(spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO) mock_start_kibana.return_value = expected_elk_dto emulation_env_config = MagicMock(spec=EmulationEnvConfig) elk_config = MagicMock() @@ -274,9 +218,7 @@ def test_start_kibana( emulation_env_config.elk_config.elk_manager_port = 5601 logger = MagicMock(spec=logging.Logger) result = ELKController.start_kibana(emulation_env_config, logger) - mock_insecure_channel.assert_called_once_with( - "172.17.0.1:5601", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + mock_insecure_channel.assert_called_once_with("172.17.0.1:5601", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_ElkManagerStub.assert_called_once_with(mock_channel) mock_start_kibana.asert_called_once_with(mock_stub) assert result == expected_elk_dto @@ -285,13 +227,8 @@ def test_start_kibana( @patch("csle_collector.elk_manager.elk_manager_pb2_grpc.ElkManagerStub") @patch("grpc.insecure_channel") @patch("csle_common.controllers.elk_controller.ELKController.start_elk_manager") - def test_start_logstash( - self, - mock_start_elk_manager, - mock_insecure_channel, - mock_ElkManagerStub, - mock_start_logstash, - ) -> None: + def test_start_logstash(self, mock_start_elk_manager, mock_insecure_channel, mock_ElkManagerStub, + mock_start_logstash) -> None: """ Test case for requesting the ELKManager to start logstash @@ -305,9 +242,7 @@ def test_start_logstash( mock_insecure_channel.return_value.__enter__.return_value = mock_channel mock_stub = MagicMock() mock_ElkManagerStub.return_value = mock_stub - expected_elk_dto = MagicMock( - spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO - ) + expected_elk_dto = MagicMock(spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO) mock_start_logstash.return_value = expected_elk_dto emulation_env_config = MagicMock(spec=EmulationEnvConfig) elk_config = MagicMock() @@ -316,9 +251,7 @@ def test_start_logstash( emulation_env_config.elk_config.elk_manager_port = 5601 logger = MagicMock(spec=logging.Logger) result = ELKController.start_logstash(emulation_env_config, logger) - mock_insecure_channel.assert_called_once_with( - "172.17.0.1:5601", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + mock_insecure_channel.assert_called_once_with("172.17.0.1:5601", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_ElkManagerStub.assert_called_once_with(mock_channel) mock_start_logstash.asert_called_once_with(mock_stub) assert result == expected_elk_dto @@ -327,13 +260,8 @@ def test_start_logstash( @patch("csle_collector.elk_manager.elk_manager_pb2_grpc.ElkManagerStub") @patch("grpc.insecure_channel") @patch("csle_common.controllers.elk_controller.ELKController.start_elk_manager") - def test_stop_elastic( - self, - mock_start_elk_manager, - mock_insecure_channel, - mock_ElkManagerStub, - mock_stop_elastic, - ) -> None: + def test_stop_elastic(self, mock_start_elk_manager, mock_insecure_channel, mock_ElkManagerStub, + mock_stop_elastic) -> None: """ Test case for requesting the ELKManager to stop elasticsearch @@ -347,9 +275,7 @@ def test_stop_elastic( mock_insecure_channel.return_value.__enter__.return_value = mock_channel mock_stub = MagicMock() mock_ElkManagerStub.return_value = mock_stub - expected_elk_dto = MagicMock( - spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO - ) + expected_elk_dto = MagicMock(spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO) mock_stop_elastic.return_value = expected_elk_dto emulation_env_config = MagicMock(spec=EmulationEnvConfig) elk_config = MagicMock() @@ -358,9 +284,7 @@ def test_stop_elastic( emulation_env_config.elk_config.elk_manager_port = 5601 logger = MagicMock(spec=logging.Logger) result = ELKController.stop_elastic(emulation_env_config, logger) - mock_insecure_channel.assert_called_once_with( - "172.17.0.1:5601", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + mock_insecure_channel.assert_called_once_with("172.17.0.1:5601", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_ElkManagerStub.assert_called_once_with(mock_channel) mock_stop_elastic.asert_called_once_with(mock_stub) assert result == expected_elk_dto @@ -369,13 +293,8 @@ def test_stop_elastic( @patch("csle_collector.elk_manager.elk_manager_pb2_grpc.ElkManagerStub") @patch("grpc.insecure_channel") @patch("csle_common.controllers.elk_controller.ELKController.start_elk_manager") - def test_stop_kibana( - self, - mock_start_elk_manager, - mock_insecure_channel, - mock_ElkManagerStub, - mock_stop_kibana, - ) -> None: + def test_stop_kibana(self, mock_start_elk_manager, mock_insecure_channel, mock_ElkManagerStub, + mock_stop_kibana) -> None: """ Test case for requesting the ELKManager to stop kibana @@ -389,9 +308,7 @@ def test_stop_kibana( mock_insecure_channel.return_value.__enter__.return_value = mock_channel mock_stub = MagicMock() mock_ElkManagerStub.return_value = mock_stub - expected_elk_dto = MagicMock( - spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO - ) + expected_elk_dto = MagicMock(spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO) mock_stop_kibana.return_value = expected_elk_dto emulation_env_config = MagicMock(spec=EmulationEnvConfig) elk_config = MagicMock() @@ -400,9 +317,7 @@ def test_stop_kibana( emulation_env_config.elk_config.elk_manager_port = 5601 logger = MagicMock(spec=logging.Logger) result = ELKController.stop_kibana(emulation_env_config, logger) - mock_insecure_channel.assert_called_once_with( - "172.17.0.1:5601", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + mock_insecure_channel.assert_called_once_with("172.17.0.1:5601", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_ElkManagerStub.assert_called_once_with(mock_channel) mock_stop_kibana.asert_called_once_with(mock_stub) assert result == expected_elk_dto @@ -411,13 +326,8 @@ def test_stop_kibana( @patch("csle_collector.elk_manager.elk_manager_pb2_grpc.ElkManagerStub") @patch("grpc.insecure_channel") @patch("csle_common.controllers.elk_controller.ELKController.start_elk_manager") - def test_stop_logstash( - self, - mock_start_elk_manager, - mock_insecure_channel, - mock_ElkManagerStub, - mock_stop_logstash, - ) -> None: + def test_stop_logstash(self, mock_start_elk_manager, mock_insecure_channel, mock_ElkManagerStub, + mock_stop_logstash) -> None: """ Test case for requesting the ELKManager to stop logstash @@ -431,9 +341,7 @@ def test_stop_logstash( mock_insecure_channel.return_value.__enter__.return_value = mock_channel mock_stub = MagicMock() mock_ElkManagerStub.return_value = mock_stub - expected_elk_dto = MagicMock( - spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO - ) + expected_elk_dto = MagicMock(spec=csle_collector.elk_manager.elk_manager_pb2.ElkDTO) mock_stop_logstash.return_value = expected_elk_dto emulation_env_config = MagicMock(spec=EmulationEnvConfig) elk_config = MagicMock() @@ -442,9 +350,7 @@ def test_stop_logstash( emulation_env_config.elk_config.elk_manager_port = 5601 logger = MagicMock(spec=logging.Logger) result = ELKController.stop_logstash(emulation_env_config, logger) - mock_insecure_channel.assert_called_once_with( - "172.17.0.1:5601", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + mock_insecure_channel.assert_called_once_with("172.17.0.1:5601", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_ElkManagerStub.assert_called_once_with(mock_channel) mock_stop_logstash.asert_called_once_with(mock_stub) assert result == expected_elk_dto @@ -472,20 +378,11 @@ def test_get_elk_managers_ports(self) -> None: assert result == [5601] @patch("csle_common.controllers.elk_controller.ELKController.get_elk_managers_ips") - @patch( - "csle_common.controllers.elk_controller.ELKController.get_elk_managers_ports" - ) - @patch( - "csle_common.controllers.elk_controller.ELKController.get_elk_status_by_port_and_ip" - ) + @patch("csle_common.controllers.elk_controller.ELKController.get_elk_managers_ports") + @patch("csle_common.controllers.elk_controller.ELKController.get_elk_status_by_port_and_ip") @patch("csle_collector.elk_manager.elk_manager_util.ElkManagerUtil.elk_dto_empty") - def test_get_elk_managers_infos( - self, - mock_elk_dto_empty, - mock_get_elk_status_by_port_and_ip, - mock_get_elk_managers_ports, - mock_get_elk_managers_ips, - ) -> None: + def test_get_elk_managers_infos(self, mock_elk_dto_empty, mock_get_elk_status_by_port_and_ip, + mock_get_elk_managers_ports, mock_get_elk_managers_ips) -> None: """ Test case for extracting the infomation of the ELK managers for a given emulation @@ -509,15 +406,9 @@ def test_get_elk_managers_infos( logger = MagicMock(spec=logging.Logger) active_ips = ["172.17.0.1"] physical_host_ip = "192.168.0.1" - result = ELKController.get_elk_managers_info( - emulation_env_config, active_ips, logger, physical_host_ip - ) - mock_get_elk_managers_ips.assert_called_once_with( - emulation_env_config=emulation_env_config - ) - mock_get_elk_managers_ports.assert_called_once_with( - emulation_env_config=emulation_env_config - ) + result = ELKController.get_elk_managers_info(emulation_env_config, active_ips, logger, physical_host_ip) + mock_get_elk_managers_ips.assert_called_once_with(emulation_env_config=emulation_env_config) + mock_get_elk_managers_ports.assert_called_once_with(emulation_env_config=emulation_env_config) assert result.ips == ["172.17.0.1"] assert result.ports == [5601] assert result.execution_id == 1 diff --git a/simulation-system/libs/csle-common/tests/test_flags_controller.py b/simulation-system/libs/csle-common/tests/test_flags_controller.py index 8cd214c12..089bf5fbc 100644 --- a/simulation-system/libs/csle-common/tests/test_flags_controller.py +++ b/simulation-system/libs/csle-common/tests/test_flags_controller.py @@ -14,9 +14,7 @@ class TestFlagsControllerSuite: @patch("csle_common.util.emulation_util.EmulationUtil.connect_admin") @patch("csle_common.util.emulation_util.EmulationUtil.execute_ssh_cmd") @patch("csle_common.util.emulation_util.EmulationUtil.disconnect_admin") - def test_create_flags( - self, mock_disconnect_admin, mock_execute_ssh_cmd, mock_connect_admin - ) -> None: + def test_create_flags(self, mock_disconnect_admin, mock_execute_ssh_cmd, mock_connect_admin) -> None: """ Test case for creating flags using the FlagsController @@ -41,10 +39,6 @@ def test_create_flags( logger = MagicMock(spec=logging.Logger) physical_server_ip = "192.168.0.1" FlagsController.create_flags(emulation_env_config, physical_server_ip, logger) - mock_connect_admin.assert_called_once_with( - emulation_env_config=emulation_env_config, ip="172.17.0.1" - ) - mock_disconnect_admin.assert_called_once_with( - emulation_env_config=emulation_env_config - ) + mock_connect_admin.assert_called_once_with(emulation_env_config=emulation_env_config, ip="172.17.0.1") + mock_disconnect_admin.assert_called_once_with(emulation_env_config=emulation_env_config) mock_execute_ssh_cmd.assert_called() diff --git a/simulation-system/libs/csle-common/tests/test_host_controller.py b/simulation-system/libs/csle-common/tests/test_host_controller.py index da5efc07e..b89a37a86 100644 --- a/simulation-system/libs/csle-common/tests/test_host_controller.py +++ b/simulation-system/libs/csle-common/tests/test_host_controller.py @@ -41,15 +41,10 @@ def emulation_env_config_setup(self) -> None: kafka_config = MagicMock() emulation_env_config.kafka_config = kafka_config emulation_env_config.kafka_config.container = kafka_container - emulation_env_config.kafka_config.container.get_ips.return_value = [ - "192.168.1.2" - ] + emulation_env_config.kafka_config.container.get_ips.return_value = ["192.168.1.2"] emulation_env_config.kafka_config.kafka_port = 9092 emulation_env_config.kafka_config.time_step_len_seconds = 10 - emulation_env_config.kafka_config.topics = [ - MagicMock(name="topic1"), - MagicMock(name="topic2"), - ] + emulation_env_config.kafka_config.topics = [MagicMock(name="topic1"), MagicMock(name="topic2")] # setup ELK configuration elk_container = MagicMock() elk_container.physical_host_ip = "192.168.1.10" @@ -81,9 +76,7 @@ def emulation_env_config_setup(self) -> None: node_beats_config.log_files_paths = ["/var/log/syslog"] node_beats_config.filebeat_modules = ["system"] node_beats_config.kafka_input = True - emulation_env_config.beats_config.get_node_beats_config_by_ips.return_value = ( - node_beats_config - ) + emulation_env_config.beats_config.get_node_beats_config_by_ips.return_value = (node_beats_config) emulation_env_config.execution_id = "execution_id" emulation_env_config.name = "emulation_name" self.emulation_env_config = emulation_env_config @@ -95,7 +88,6 @@ def test_start_host_managers(self, mock_start_host_manager) -> None: Test start_host_managers :param mock_start_host_manager: mock start_host_manager - :return: None """ HostController.start_host_managers(self.emulation_env_config, self.logger) @@ -105,16 +97,13 @@ def test_start_host_managers(self, mock_start_host_manager) -> None: @patch("csle_common.util.emulation_util.EmulationUtil.connect_admin") @patch("csle_common.util.emulation_util.EmulationUtil.execute_ssh_cmd") @patch("time.sleep", return_value=None) - def test_start_host_manager( - self, mock_sleep, mock_execute_ssh_cmd, mock_connect_admin - ) -> None: + def test_start_host_manager(self, mock_sleep, mock_execute_ssh_cmd, mock_connect_admin) -> None: """ Test start_host_manager :param mock_sleep: mock sleep :param mock_execute_ssh_cmd: mock execute_ssh_cmd :param mock_connect_admin: mock connect_admin - :return: None """ mock_connect_admin.return_value = None @@ -124,19 +113,13 @@ def test_start_host_manager( host_manager_config = MagicMock() emulation_env_config.host_manager_config = host_manager_config emulation_env_config.host_manager_config.host_manager_port = 8080 - emulation_env_config.host_manager_config.host_manager_log_dir = ( - "/var/log/host_manager" - ) - emulation_env_config.host_manager_config.host_manager_log_file = ( - "host_manager.log" - ) + emulation_env_config.host_manager_config.host_manager_log_dir = ("/var/log/host_manager") + emulation_env_config.host_manager_config.host_manager_log_file = ("host_manager.log") emulation_env_config.host_manager_config.host_manager_max_workers = 4 logger = MagicMock(spec=logging.Logger) ip = "172.17.0.1" HostController.start_host_manager(emulation_env_config, ip, logger) - mock_connect_admin.assert_called_once_with( - emulation_env_config=emulation_env_config, ip=ip - ) + mock_connect_admin.assert_called_once_with(emulation_env_config=emulation_env_config, ip=ip) mock_sleep.assert_called_once_with(5) @patch("csle_common.controllers.host_controller.HostController.stop_host_manager") @@ -145,7 +128,6 @@ def test_stop_host_managers(self, mock_stop_host_manager) -> None: Test stop_host_managers :param mock_stop_host_manager: mock stop_host_manager - :return: None """ physical_host_ip = "192.168.1.10" @@ -160,16 +142,13 @@ def test_stop_host_managers(self, mock_stop_host_manager) -> None: @patch("csle_common.controllers.host_controller.EmulationUtil.connect_admin") @patch("csle_common.controllers.host_controller.Logger.__call__") @patch("csle_common.controllers.host_controller.EmulationUtil.execute_ssh_cmd") - def test_stop_host_manager( - self, mock_execute_ssh_cmd, mock_logger, mock_connect_admin - ) -> None: + def test_stop_host_manager(self, mock_execute_ssh_cmd, mock_logger, mock_connect_admin) -> None: """ Test stop_host_manager method :param mock_execute_ssh_cmd: mock execute_ssh_cmd :param mock_logger: mock logger :param mock_connect_admin: mock connect_admin - :return: None """ mock_logger_instance = MagicMock() @@ -177,51 +156,30 @@ def test_stop_host_manager( mock_get_logger = mock_logger_instance.get_logger() mock_execute_ssh_cmd.return_value = ("output", "error", 0) ip = "172.17.0.2" - HostController.stop_host_manager( - emulation_env_config=self.emulation_env_config, ip=ip - ) - mock_connect_admin.assert_called_once_with( - emulation_env_config=self.emulation_env_config, ip=ip - ) - mock_get_logger.info.assert_called_once_with( - f"Stopping host manager on node {ip}" - ) - expected_cmd = ( - constants.COMMANDS.SUDO - + constants.COMMANDS.SPACE_DELIM - + constants.COMMANDS.PKILL - + constants.COMMANDS.SPACE_DELIM - + constants.TRAFFIC_COMMANDS.HOST_MANAGER_FILE_NAME - ) - mock_execute_ssh_cmd.assert_called_once_with( - cmd=expected_cmd, conn=self.emulation_env_config.get_connection(ip) - ) - - @patch( - "csle_common.controllers.host_controller.HostController.start_host_monitor_thread" - ) + HostController.stop_host_manager(emulation_env_config=self.emulation_env_config, ip=ip) + mock_connect_admin.assert_called_once_with(emulation_env_config=self.emulation_env_config, ip=ip) + mock_get_logger.info.assert_called_once_with(f"Stopping host manager on node {ip}") + expected_cmd = (constants.COMMANDS.SUDO + constants.COMMANDS.SPACE_DELIM + constants.COMMANDS.PKILL + + constants.COMMANDS.SPACE_DELIM + constants.TRAFFIC_COMMANDS.HOST_MANAGER_FILE_NAME) + mock_execute_ssh_cmd.assert_called_once_with(cmd=expected_cmd, + conn=self.emulation_env_config.get_connection(ip)) + + @patch("csle_common.controllers.host_controller.HostController.start_host_monitor_thread") def test_start_host_monitor_threads(self, mock_start_host_monitor_thread) -> None: """ Test start_host_monitor_threads method :param mock_start_host_monitor_threads: mock start_host_monitor_threads - :return: None """ physical_server_ip = "192.168.1.10" HostController.start_host_monitor_threads( - emulation_env_config=self.emulation_env_config, - physical_server_ip=physical_server_ip, - logger=self.logger, - ) + emulation_env_config=self.emulation_env_config, physical_server_ip=physical_server_ip, logger=self.logger) assert mock_start_host_monitor_thread.call_count == 3 mock_start_host_monitor_thread.reset_mock() physical_server_ip = "192.168.1.11" - HostController.start_host_monitor_threads( - emulation_env_config=self.emulation_env_config, - physical_server_ip=physical_server_ip, - logger=self.logger, - ) + HostController.start_host_monitor_threads(emulation_env_config=self.emulation_env_config, + physical_server_ip=physical_server_ip, logger=self.logger) assert mock_start_host_monitor_thread.call_count == 2 @patch("csle_common.controllers.host_controller.HostController.start_filebeat") @@ -230,7 +188,6 @@ def test_start_filebeats(self, mock_start_filebeat) -> None: Test start_filebeats method :param mock_start_filebeat: mock start_filebeat - :return: None """ physical_server_ip = "192.168.1.10" @@ -248,7 +205,6 @@ def test_start_packetbeats(self, mock_start_packetbeat) -> None: Test start_packetbeats method :param mock_start_packetbeat: mock start_packetbeat - :return: None """ physical_server_ip = "192.168.1.10" @@ -266,7 +222,6 @@ def test_start_metricbeats(self, mock_start_metricbeat) -> None: Test start_metricbeats method :param mock_start_metricbeat: mock start_metricbeat - :return: None """ physical_server_ip = "192.168.1.10" @@ -284,7 +239,6 @@ def test_start_heartbeats(self, mock_start_heartbeat) -> None: Test start_heartbeats method :param mock_start_heartbeat: mock start_heartbeat - :return: None """ physical_server_ip = "192.168.1.10" @@ -302,7 +256,6 @@ def test_stop_filebeats(self, mock_stop_filebeat) -> None: Test stop_filebeats method :param mock_stop_filebeat: mock stop_filebeat - :return: None """ physical_server_ip = "192.168.1.10" @@ -319,7 +272,6 @@ def test_stop_packetbeats(self, mock_stop_packetbeat) -> None: Test stop_packetbeats method :param mock_stop_packetbeat: mock stop_packetbeat - :return: None """ physical_server_ip = "192.168.1.10" @@ -336,7 +288,6 @@ def test_stop_metricbeats(self, mock_stop_metricbeat) -> None: Test stop_metricbeats method :param mock_stop_metricbeat: mock stop_metricbeat - :return: None """ physical_server_ip = "192.168.1.10" @@ -353,7 +304,6 @@ def test_stop_heartbeats(self, mock_stop_heartbeat) -> None: Test stop_heartbeats method :param mock_stop_heartbeat: mock stop_heartbeat - :return: None """ physical_server_ip = "192.168.1.10" @@ -370,7 +320,6 @@ def test_config_filebeats(self, mock_config_filebeat) -> None: Test config_filebeats method :param mock_config_filebeat: mock config_filebeat - :return: None """ physical_server_ip = "192.168.1.10" @@ -386,7 +335,6 @@ def test_config_packetbeats(self, mock_config_packetbeat) -> None: """ Test config_packetbeats method :param mock_config_packetbeat: mock config_packetbeat - :return: None """ physical_server_ip = "192.168.1.10" @@ -401,8 +349,8 @@ def test_config_packetbeats(self, mock_config_packetbeat) -> None: def test_config_metricbeats(self, mock_config_metricbeat) -> None: """ Test config_metricbeats method - :param mock_config_metricbeat: mock config_metricbeat + :param mock_config_metricbeat: mock config_metricbeat :return: None """ physical_server_ip = "192.168.1.10" @@ -417,8 +365,8 @@ def test_config_metricbeats(self, mock_config_metricbeat) -> None: def test_config_heartbeats(self, mock_config_heartbeat) -> None: """ Test config_heartbeats method - :param mock_config_heartbeat: mock config_heartbeat + :param mock_config_heartbeat: mock config_heartbeat :return: None """ physical_server_ip = "192.168.1.10" @@ -430,24 +378,13 @@ def test_config_heartbeats(self, mock_config_heartbeat) -> None: assert mock_config_heartbeat.call_count == 3 @patch("csle_common.controllers.host_controller.HostController.start_host_manager") - @patch( - "csle_common.controllers.host_controller.HostController.get_host_monitor_thread_status_by_port_and_ip" - ) + @patch("csle_common.controllers.host_controller.HostController.get_host_monitor_thread_status_by_port_and_ip") @patch("csle_common.controllers.host_controller.grpc.insecure_channel") - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.start_host_monitor" - ) - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub" - ) + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.start_host_monitor") + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub") def test_start_host_monitor_thread( - self, - mock_HostManagerStub, - mock_start_host_monitor, - mock_insecure_channel, - mock_get_host_monitor_thread_status, - mock_start_host_manager, - ) -> None: + self, mock_HostManagerStub, mock_start_host_monitor, mock_insecure_channel, + mock_get_host_monitor_thread_status, mock_start_host_manager) -> None: """ Test start_host_monitor_thread method @@ -469,19 +406,13 @@ def test_start_host_monitor_thread( mock_stub = MagicMock() mock_HostManagerStub.return_value = mock_stub ip = "172.17.0.2" - HostController.start_host_monitor_thread( - emulation_env_config=self.emulation_env_config, ip=ip, logger=self.logger - ) - mock_start_host_manager.assert_called_once_with( - emulation_env_config=self.emulation_env_config, ip=ip, logger=self.logger - ) + HostController.start_host_monitor_thread(emulation_env_config=self.emulation_env_config, ip=ip, + logger=self.logger) + mock_start_host_manager.assert_called_once_with(emulation_env_config=self.emulation_env_config, ip=ip, + logger=self.logger) mock_get_host_monitor_thread_status.assert_called_once_with(ip=ip, port=12345) - self.logger.info.assert_called_once_with( - f"Host monitor thread is not running on {ip}, starting it." - ) - mock_insecure_channel.assert_called_once_with( - f"{ip}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + self.logger.info.assert_called_once_with(f"Host monitor thread is not running on {ip}, starting it.") + mock_insecure_channel.assert_called_once_with(f"{ip}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_HostManagerStub.assert_called_once_with(mock_channel) mock_start_host_monitor.assert_called_once_with( stub=mock_stub, @@ -491,24 +422,13 @@ def test_start_host_monitor_thread( ) @patch("csle_common.controllers.host_controller.HostController.start_host_manager") - @patch( - "csle_common.controllers.host_controller.HostController.get_host_monitor_thread_status_by_port_and_ip" - ) + @patch("csle_common.controllers.host_controller.HostController.get_host_monitor_thread_status_by_port_and_ip") @patch("csle_common.controllers.host_controller.grpc.insecure_channel") - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.start_filebeat" - ) - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub" - ) + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.start_filebeat") + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub") def test_start_filebeat( - self, - mock_HostManagerStub, - mock_start_filebeat, - mock_insecure_channel, - mock_get_host_monitor_thread_status, - mock_start_host_manager, - ) -> None: + self, mock_HostManagerStub, mock_start_filebeat, mock_insecure_channel, mock_get_host_monitor_thread_status, + mock_start_host_manager) -> None: """ Test start_filebeat method @@ -523,8 +443,7 @@ def test_start_filebeat( beats_config = MagicMock() self.emulation_env_config.beats_config = beats_config self.emulation_env_config.beats_config.get_node_beats_config_by_ips.return_value = MagicMock( - start_filebeat_automatically=True - ) + start_filebeat_automatically=True) host_monitor_dto = MagicMock() host_monitor_dto.filebeat_running = False mock_get_host_monitor_thread_status.return_value = host_monitor_dto @@ -533,51 +452,26 @@ def test_start_filebeat( mock_stub = MagicMock() mock_HostManagerStub.return_value = mock_stub ips = ["172.17.0.2", "192.168.1.2"] - HostController.start_filebeat( - emulation_env_config=self.emulation_env_config, - ips=ips, - logger=self.logger, - initial_start=True, - ) - mock_start_host_manager.assert_called_once_with( - emulation_env_config=self.emulation_env_config, - ip=ips[0], - logger=self.logger, - ) - self.emulation_env_config.beats_config.get_node_beats_config_by_ips.assert_called_once_with( - ips=ips - ) - mock_get_host_monitor_thread_status.assert_called_once_with( - ip=ips[0], port=12345 - ) - self.logger.info.assert_called_once_with( - f"Filebeat is not running on {ips[0]}, starting it." - ) - mock_insecure_channel.assert_called_once_with( - f"{ips[0]}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + HostController.start_filebeat(emulation_env_config=self.emulation_env_config, ips=ips, logger=self.logger, + initial_start=True) + mock_start_host_manager.assert_called_once_with(emulation_env_config=self.emulation_env_config, ip=ips[0], + logger=self.logger) + self.emulation_env_config.beats_config.get_node_beats_config_by_ips.assert_called_once_with(ips=ips) + mock_get_host_monitor_thread_status.assert_called_once_with(ip=ips[0], port=12345) + self.logger.info.assert_called_once_with(f"Filebeat is not running on {ips[0]}, starting it.") + mock_insecure_channel.assert_called_once_with(f"{ips[0]}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_HostManagerStub.assert_called_once_with(mock_channel) mock_start_filebeat.assert_called_once_with(stub=mock_stub) @patch("csle_common.controllers.host_controller.HostController.start_host_manager") - @patch( - "csle_common.controllers.host_controller.HostController.get_host_monitor_thread_status_by_port_and_ip" - ) + @patch("csle_common.controllers.host_controller.HostController.get_host_monitor_thread_status_by_port_and_ip") @patch("csle_common.controllers.host_controller.grpc.insecure_channel") - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.start_packetbeat" - ) - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub" - ) + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.start_packetbeat") + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub") def test_start_packetbeat( - self, - mock_HostManagerStub, - mock_start_packetbeat, - mock_insecure_channel, - mock_get_host_monitor_thread_status, - mock_start_host_manager, - ) -> None: + self, mock_HostManagerStub, mock_start_packetbeat, mock_insecure_channel, + mock_get_host_monitor_thread_status, + mock_start_host_manager) -> None: """ Test start_packetbeat method @@ -592,8 +486,7 @@ def test_start_packetbeat( beats_config = MagicMock() self.emulation_env_config.beats_config = beats_config self.emulation_env_config.beats_config.get_node_beats_config_by_ips.return_value = MagicMock( - start_packetbeat_automatically=True - ) + start_packetbeat_automatically=True) host_monitor_dto = MagicMock() host_monitor_dto.packetbeat_running = False mock_get_host_monitor_thread_status.return_value = host_monitor_dto @@ -602,51 +495,26 @@ def test_start_packetbeat( mock_stub = MagicMock() mock_HostManagerStub.return_value = mock_stub ips = ["172.17.0.2", "192.168.1.2"] - HostController.start_packetbeat( - emulation_env_config=self.emulation_env_config, - ips=ips, - logger=self.logger, - initial_start=True, - ) - mock_start_host_manager.assert_called_once_with( - emulation_env_config=self.emulation_env_config, - ip=ips[0], - logger=self.logger, - ) - self.emulation_env_config.beats_config.get_node_beats_config_by_ips.assert_called_once_with( - ips=ips - ) - mock_get_host_monitor_thread_status.assert_called_once_with( - ip=ips[0], port=12345 - ) - self.logger.info.assert_called_once_with( - f"Packetbeat is not running on {ips[0]}, starting it." - ) - mock_insecure_channel.assert_called_once_with( - f"{ips[0]}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + HostController.start_packetbeat(emulation_env_config=self.emulation_env_config, ips=ips, logger=self.logger, + initial_start=True) + mock_start_host_manager.assert_called_once_with(emulation_env_config=self.emulation_env_config, ip=ips[0], + logger=self.logger) + self.emulation_env_config.beats_config.get_node_beats_config_by_ips.assert_called_once_with(ips=ips) + mock_get_host_monitor_thread_status.assert_called_once_with(ip=ips[0], port=12345) + self.logger.info.assert_called_once_with(f"Packetbeat is not running on {ips[0]}, starting it.") + mock_insecure_channel.assert_called_once_with(f"{ips[0]}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_HostManagerStub.assert_called_once_with(mock_channel) mock_start_packetbeat.assert_called_once_with(stub=mock_stub) @patch("csle_common.controllers.host_controller.HostController.start_host_manager") - @patch( - "csle_common.controllers.host_controller.HostController.get_host_monitor_thread_status_by_port_and_ip" - ) + @patch("csle_common.controllers.host_controller.HostController.get_host_monitor_thread_status_by_port_and_ip") @patch("csle_common.controllers.host_controller.grpc.insecure_channel") - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.start_metricbeat" - ) - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub" - ) + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.start_metricbeat") + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub") def test_start_metricbeat( - self, - mock_HostManagerStub, - mock_start_metricbeat, - mock_insecure_channel, - mock_get_host_monitor_thread_status, - mock_start_host_manager, - ) -> None: + self, mock_HostManagerStub, mock_start_metricbeat, mock_insecure_channel, + mock_get_host_monitor_thread_status, + mock_start_host_manager) -> None: """ Test start_metricbeat method @@ -655,7 +523,6 @@ def test_start_metricbeat( :param mock_insecure_channel: mock insecure_channel :param mock_get_host_monitor_thread_status: mock get_host_monitor_thread_status :param mock_start_host_manager: mock start_host_manager - :return: None """ beats_config = MagicMock() @@ -682,40 +549,22 @@ def test_start_metricbeat( ip=ips[0], logger=self.logger, ) - self.emulation_env_config.beats_config.get_node_beats_config_by_ips.assert_called_once_with( - ips=ips - ) - mock_get_host_monitor_thread_status.assert_called_once_with( - ip=ips[0], port=12345 - ) - self.logger.info.assert_called_once_with( - f"Metricbeat is not running on {ips[0]}, starting it." - ) - mock_insecure_channel.assert_called_once_with( - f"{ips[0]}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + self.emulation_env_config.beats_config.get_node_beats_config_by_ips.assert_called_once_with(ips=ips) + mock_get_host_monitor_thread_status.assert_called_once_with(ip=ips[0], port=12345) + self.logger.info.assert_called_once_with(f"Metricbeat is not running on {ips[0]}, starting it.") + mock_insecure_channel.assert_called_once_with(f"{ips[0]}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_HostManagerStub.assert_called_once_with(mock_channel) mock_start_metricbeat.assert_called_once_with(stub=mock_stub) @patch("csle_common.controllers.host_controller.HostController.start_host_manager") - @patch( - "csle_common.controllers.host_controller.HostController.get_host_monitor_thread_status_by_port_and_ip" - ) + @patch("csle_common.controllers.host_controller.HostController.get_host_monitor_thread_status_by_port_and_ip") @patch("csle_common.controllers.host_controller.grpc.insecure_channel") - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.start_heartbeat" - ) - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub" - ) + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.start_heartbeat") + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub") def test_start_heartbeat( - self, - mock_HostManagerStub, - mock_start_heartbeat, - mock_insecure_channel, - mock_get_host_monitor_thread_status, - mock_start_host_manager, - ) -> None: + self, mock_HostManagerStub, mock_start_heartbeat, mock_insecure_channel, + mock_get_host_monitor_thread_status, + mock_start_host_manager) -> None: """ Test start_heartbeat method @@ -724,14 +573,12 @@ def test_start_heartbeat( :param mock_insecure_channel: mock insecure_channel :param mock_get_host_monitor_thread_status: mock get_host_monitor_thread_status :param mock_start_host_manager: mock start_host_manager - :return: None """ beats_config = MagicMock() self.emulation_env_config.beats_config = beats_config self.emulation_env_config.beats_config.get_node_beats_config_by_ips.return_value = MagicMock( - start_heartbeat_automatically=True - ) + start_heartbeat_automatically=True) host_monitor_dto = MagicMock() host_monitor_dto.heartbeat_running = False mock_get_host_monitor_thread_status.return_value = host_monitor_dto @@ -741,28 +588,13 @@ def test_start_heartbeat( mock_HostManagerStub.return_value = mock_stub ips = ["172.17.0.2", "192.168.1.2"] HostController.start_heartbeat( - emulation_env_config=self.emulation_env_config, - ips=ips, - logger=self.logger, - initial_start=True, - ) + emulation_env_config=self.emulation_env_config, ips=ips, logger=self.logger, initial_start=True) mock_start_host_manager.assert_called_once_with( - emulation_env_config=self.emulation_env_config, - ip=ips[0], - logger=self.logger, - ) - self.emulation_env_config.beats_config.get_node_beats_config_by_ips.assert_called_once_with( - ips=ips - ) - mock_get_host_monitor_thread_status.assert_called_once_with( - ip=ips[0], port=12345 - ) - self.logger.info.assert_called_once_with( - f"Heartbeat is not running on {ips[0]}, starting it." - ) - mock_insecure_channel.assert_called_once_with( - f"{ips[0]}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + emulation_env_config=self.emulation_env_config, ip=ips[0], logger=self.logger) + self.emulation_env_config.beats_config.get_node_beats_config_by_ips.assert_called_once_with(ips=ips) + mock_get_host_monitor_thread_status.assert_called_once_with(ip=ips[0], port=12345) + self.logger.info.assert_called_once_with(f"Heartbeat is not running on {ips[0]}, starting it.") + mock_insecure_channel.assert_called_once_with(f"{ips[0]}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_HostManagerStub.assert_called_once_with(mock_channel) mock_start_heartbeat.assert_called_once_with(stub=mock_stub) @@ -772,34 +604,22 @@ def test_start_sparks(self, mock_start_spark) -> None: Test start_sparks method :param mock_start_spark: mock start_spark - :return: None """ constants.CONTAINER_IMAGES.SPARK_IMAGES = ["spark-container-1"] container1 = self.emulation_env_config.containers_config.containers[0] physical_server_ip = "192.168.1.10" HostController.start_sparks( - emulation_env_config=self.emulation_env_config, - physical_server_ip=physical_server_ip, - logger=self.logger, - ) - self.logger.info.assert_called_once_with( - f"Starting Spark on IP: {container1.docker_gw_bridge_ip}" - ) + emulation_env_config=self.emulation_env_config, physical_server_ip=physical_server_ip, logger=self.logger) + self.logger.info.assert_called_once_with(f"Starting Spark on IP: {container1.docker_gw_bridge_ip}") mock_start_spark.assert_called_once_with( - emulation_env_config=self.emulation_env_config, - ips=[container1.docker_gw_bridge_ip], - logger=self.logger, - ) + emulation_env_config=self.emulation_env_config, ips=[container1.docker_gw_bridge_ip], logger=self.logger) # Reset mocks to clear previous call history self.logger.reset_mock() mock_start_spark.reset_mock() physical_server_ip = "192.168.1.11" HostController.start_sparks( - emulation_env_config=self.emulation_env_config, - physical_server_ip=physical_server_ip, - logger=self.logger, - ) + emulation_env_config=self.emulation_env_config, physical_server_ip=physical_server_ip, logger=self.logger) self.logger.info.assert_not_called() mock_start_spark.assert_not_called() @@ -809,52 +629,32 @@ def test_stop_sparks(self, mock_stop_spark) -> None: Test stop_sparks method :param mock_stop_spark: mock stop_sparks - :return: None """ constants.CONTAINER_IMAGES.SPARK_IMAGES = ["spark-container-1"] container1 = self.emulation_env_config.containers_config.containers[0] physical_server_ip = "192.168.1.10" - HostController.stop_sparks( - emulation_env_config=self.emulation_env_config, - physical_server_ip=physical_server_ip, - logger=self.logger, - ) - self.logger.info.assert_called_once_with( - f"Stopping Spark on IP: {container1.docker_gw_bridge_ip}" - ) + HostController.stop_sparks(emulation_env_config=self.emulation_env_config, + physical_server_ip=physical_server_ip, + logger=self.logger) + self.logger.info.assert_called_once_with(f"Stopping Spark on IP: {container1.docker_gw_bridge_ip}") mock_stop_spark.assert_called_once_with( - emulation_env_config=self.emulation_env_config, - ips=[container1.docker_gw_bridge_ip], - logger=self.logger, - ) + emulation_env_config=self.emulation_env_config, ips=[container1.docker_gw_bridge_ip], logger=self.logger) # Reset mocks to clear previous call history self.logger.reset_mock() mock_stop_spark.reset_mock() physical_server_ip = "192.168.1.11" HostController.stop_sparks( - emulation_env_config=self.emulation_env_config, - physical_server_ip=physical_server_ip, - logger=self.logger, - ) + emulation_env_config=self.emulation_env_config, physical_server_ip=physical_server_ip, logger=self.logger) self.logger.info.assert_not_called() mock_stop_spark.assert_not_called() @patch("csle_common.controllers.host_controller.HostController.start_host_manager") @patch("csle_common.controllers.host_controller.grpc.insecure_channel") - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.start_spark" - ) - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub" - ) - def test_start_spark( - self, - mock_HostManagerStub, - mock_start_spark, - mock_insecure_channel, - mock_start_host_manager, - ) -> None: + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.start_spark") + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub") + def test_start_spark(self, mock_HostManagerStub, mock_start_spark, mock_insecure_channel, mock_start_host_manager) \ + -> None: """ Test start_spark method @@ -862,7 +662,6 @@ def test_start_spark( :param mock_start_spark: mock start_spark :param mock_insecure_channel: mock insecure_channel :param mock_start_host_manager: mock start_host_manager - :return: None """ mock_channel = MagicMock() @@ -870,35 +669,19 @@ def test_start_spark( mock_stub = MagicMock() mock_HostManagerStub.return_value = mock_stub ips = ["172.17.0.2", "192.168.1.2"] - HostController.start_spark( - emulation_env_config=self.emulation_env_config, ips=ips, logger=self.logger - ) + HostController.start_spark(emulation_env_config=self.emulation_env_config, ips=ips, logger=self.logger) mock_start_host_manager.assert_called_once_with( - emulation_env_config=self.emulation_env_config, - ip=ips[0], - logger=self.logger, - ) - mock_insecure_channel.assert_called_once_with( - f"{ips[0]}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + emulation_env_config=self.emulation_env_config, ip=ips[0], logger=self.logger) + mock_insecure_channel.assert_called_once_with(f"{ips[0]}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_HostManagerStub.assert_called_once_with(mock_channel) mock_start_spark.assert_called_once_with(stub=mock_stub) @patch("csle_common.controllers.host_controller.HostController.start_host_manager") @patch("csle_common.controllers.host_controller.grpc.insecure_channel") - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.stop_spark" - ) - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub" - ) - def test_stop_spark( - self, - mock_HostManagerStub, - mock_stop_spark, - mock_insecure_channel, - mock_start_host_manager, - ) -> None: + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.stop_spark") + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub") + def test_stop_spark(self, mock_HostManagerStub, mock_stop_spark, mock_insecure_channel, mock_start_host_manager) \ + -> None: """ Test stop_spark method @@ -906,7 +689,6 @@ def test_stop_spark( :param mock_stop_spark: mock stop_spark :param mock_insecure_channel: mock insecure_channel :param mock_start_host_manager: mock start_host_manager - :return: None """ mock_channel = MagicMock() @@ -914,35 +696,19 @@ def test_stop_spark( mock_stub = MagicMock() mock_HostManagerStub.return_value = mock_stub ips = ["172.17.0.2", "192.168.1.2"] - HostController.stop_spark( - emulation_env_config=self.emulation_env_config, ips=ips, logger=self.logger - ) + HostController.stop_spark(emulation_env_config=self.emulation_env_config, ips=ips, logger=self.logger) mock_start_host_manager.assert_called_once_with( - emulation_env_config=self.emulation_env_config, - ip=ips[0], - logger=self.logger, - ) - mock_insecure_channel.assert_called_once_with( - f"{ips[0]}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + emulation_env_config=self.emulation_env_config, ip=ips[0], logger=self.logger) + mock_insecure_channel.assert_called_once_with(f"{ips[0]}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_HostManagerStub.assert_called_once_with(mock_channel) mock_stop_spark.assert_called_once_with(stub=mock_stub) @patch("csle_common.controllers.host_controller.HostController.start_host_manager") @patch("csle_common.controllers.host_controller.grpc.insecure_channel") - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.config_filebeat" - ) - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub" - ) + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.config_filebeat") + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub") def test_config_filebeat( - self, - mock_HostManagerStub, - mock_config_filebeat, - mock_insecure_channel, - mock_start_host_manager, - ) -> None: + self, mock_HostManagerStub, mock_config_filebeat, mock_insecure_channel, mock_start_host_manager) -> None: """ Test config_filebeat method @@ -950,7 +716,6 @@ def test_config_filebeat( :param mock_config_filebeat: mock stop_config_filebeat :param mock_insecure_channel: mock insecure_channel :param mock_start_host_manager: mock start_host_manager - :return: None """ # Mock the gRPC channel and stub @@ -960,43 +725,26 @@ def test_config_filebeat( mock_HostManagerStub.return_value = mock_stub # Call the function container = self.emulation_env_config.containers_config.containers[0] - HostController.config_filebeat( - self.emulation_env_config, container, self.logger - ) + HostController.config_filebeat(self.emulation_env_config, container, self.logger) mock_start_host_manager.assert_called_once_with( - emulation_env_config=self.emulation_env_config, - ip=container.docker_gw_bridge_ip, - logger=self.logger, - ) + emulation_env_config=self.emulation_env_config, ip=container.docker_gw_bridge_ip, logger=self.logger) self.emulation_env_config.beats_config.get_node_beats_config_by_ips.assert_called_once_with( - ips=container.get_ips() - ) + ips=container.get_ips()) self.logger.info.assert_called_once_with( f"Configuring filebeat on {container.docker_gw_bridge_ip}, " f"{container.get_full_name()}, ips: {container.get_ips()}" ) mock_insecure_channel.assert_called_once_with( - f"{container.docker_gw_bridge_ip}:12345", - options=constants.GRPC_SERVERS.GRPC_OPTIONS, - ) + f"{container.docker_gw_bridge_ip}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_HostManagerStub.assert_called_once_with(mock_channel) mock_config_filebeat.assert_called() @patch("csle_common.controllers.host_controller.HostController.start_host_manager") @patch("csle_common.controllers.host_controller.grpc.insecure_channel") - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.config_packetbeat" - ) - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub" - ) + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.config_packetbeat") + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub") def test_config_packetbeat( - self, - mock_HostManagerStub, - mock_config_packetbeat, - mock_insecure_channel, - mock_start_host_manager, - ) -> None: + self, mock_HostManagerStub, mock_config_packetbeat, mock_insecure_channel, mock_start_host_manager) -> None: """ Test config_packetbeat method @@ -1004,7 +752,6 @@ def test_config_packetbeat( :param mock_config_packetbeat: mock stop_config_packetbeat :param mock_insecure_channel: mock insecure_channel :param mock_start_host_manager: mock start_host_manager - :return: None """ # Mock the gRPC channel and stub @@ -1035,19 +782,10 @@ def test_config_packetbeat( @patch("csle_common.controllers.host_controller.HostController.start_host_manager") @patch("csle_common.controllers.host_controller.grpc.insecure_channel") - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.config_metricbeat" - ) - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub" - ) + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.config_metricbeat") + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub") def test_config_metricbeat( - self, - mock_HostManagerStub, - mock_config_metricbeat, - mock_insecure_channel, - mock_start_host_manager, - ) -> None: + self, mock_HostManagerStub, mock_config_metricbeat, mock_insecure_channel, mock_start_host_manager) -> None: """ Test config_metricbeat method @@ -1055,7 +793,6 @@ def test_config_metricbeat( :param mock_config_metricbeat: mock stop_config_metricbeat :param mock_insecure_channel: mock insecure_channel :param mock_start_host_manager: mock start_host_manager - :return: None """ # Mock the gRPC channel and stub @@ -1065,9 +802,7 @@ def test_config_metricbeat( mock_HostManagerStub.return_value = mock_stub # Call the function container = self.emulation_env_config.containers_config.containers[0] - HostController.config_metricbeat( - self.emulation_env_config, container, self.logger - ) + HostController.config_metricbeat(self.emulation_env_config, container, self.logger) mock_start_host_manager.assert_called_once_with( emulation_env_config=self.emulation_env_config, ip=container.docker_gw_bridge_ip, @@ -1081,27 +816,16 @@ def test_config_metricbeat( f"{container.get_full_name()}, ips: {container.get_ips()}" ) mock_insecure_channel.assert_called_once_with( - f"{container.docker_gw_bridge_ip}:12345", - options=constants.GRPC_SERVERS.GRPC_OPTIONS, - ) + f"{container.docker_gw_bridge_ip}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_HostManagerStub.assert_called_once_with(mock_channel) mock_config_metricbeat.assert_called() @patch("csle_common.controllers.host_controller.HostController.start_host_manager") @patch("csle_common.controllers.host_controller.grpc.insecure_channel") - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.config_heartbeat" - ) - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub" - ) + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.config_heartbeat") + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub") def test_config_heartbeat( - self, - mock_HostManagerStub, - mock_config_heartbeat, - mock_insecure_channel, - mock_start_host_manager, - ) -> None: + self, mock_HostManagerStub, mock_config_heartbeat, mock_insecure_channel, mock_start_host_manager) -> None: """ Test config_heartbeat method @@ -1109,7 +833,6 @@ def test_config_heartbeat( :param mock_config_heartbeat: mock stop_config_heartbeat :param mock_insecure_channel: mock insecure_channel :param mock_start_host_manager: mock start_host_manager - :return: None """ # Mock the gRPC channel and stub @@ -1119,37 +842,29 @@ def test_config_heartbeat( mock_HostManagerStub.return_value = mock_stub # Call the function container = self.emulation_env_config.containers_config.containers[0] - HostController.config_heartbeat( - self.emulation_env_config, container, self.logger - ) + HostController.config_heartbeat(self.emulation_env_config, container, self.logger) mock_start_host_manager.assert_called_once_with( emulation_env_config=self.emulation_env_config, ip=container.docker_gw_bridge_ip, logger=self.logger, ) self.emulation_env_config.beats_config.get_node_beats_config_by_ips.assert_called_once_with( - ips=container.get_ips() - ) + ips=container.get_ips()) self.logger.info.assert_called_once_with( f"Configuring heartbeat on {container.docker_gw_bridge_ip}, " f"{container.get_full_name()}, ips: {container.get_ips()}" ) mock_insecure_channel.assert_called_once_with( - f"{container.docker_gw_bridge_ip}:12345", - options=constants.GRPC_SERVERS.GRPC_OPTIONS, - ) + f"{container.docker_gw_bridge_ip}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_HostManagerStub.assert_called_once_with(mock_channel) mock_config_heartbeat.assert_called() - @patch( - "csle_common.controllers.host_controller.HostController.stop_host_monitor_thread" - ) + @patch("csle_common.controllers.host_controller.HostController.stop_host_monitor_thread") def test_stop_host_monitor_threads(self, mock_stop_host_monitor_thread) -> None: """ Test stop_host_monitor_threads method :param mock_stop_host_monitor_thread: mock stop_host_monitor_thread - :return: None """ physical_host_ip = "192.168.1.10" @@ -1162,19 +877,11 @@ def test_stop_host_monitor_threads(self, mock_stop_host_monitor_thread) -> None: @patch("csle_common.controllers.host_controller.HostController.start_host_manager") @patch("csle_common.controllers.host_controller.grpc.insecure_channel") - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.stop_host_monitor" - ) - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub" - ) + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.stop_host_monitor") + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub") def test_stop_host_monitor_thread( - self, - mock_HostManagerStub, - mock_stop_host_monitor_thread, - mock_insecure_channel, - mock_start_host_manager, - ) -> None: + self, mock_HostManagerStub, mock_stop_host_monitor_thread, mock_insecure_channel, mock_start_host_manager) \ + -> None: """ Test stop_host_monitor_thread method @@ -1182,7 +889,6 @@ def test_stop_host_monitor_thread( :param mock_stop_host_monitor_thread: mock stop_host_monitor_thread :param mock_insecure_channel: mock insecure_channel :param mock_start_host_manager: mock start_host_manager - :return: None """ mock_channel = MagicMock() @@ -1190,33 +896,20 @@ def test_stop_host_monitor_thread( mock_stub = MagicMock() mock_HostManagerStub.return_value = mock_stub ip = "172.17.0.2" - HostController.stop_host_monitor_thread( - emulation_env_config=self.emulation_env_config, ip=ip, logger=self.logger - ) - mock_start_host_manager.assert_called_once_with( - emulation_env_config=self.emulation_env_config, ip=ip, logger=self.logger - ) - mock_insecure_channel.assert_called_once_with( - f"{ip}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + HostController.stop_host_monitor_thread(emulation_env_config=self.emulation_env_config, ip=ip, + logger=self.logger) + mock_start_host_manager.assert_called_once_with(emulation_env_config=self.emulation_env_config, ip=ip, + logger=self.logger) + mock_insecure_channel.assert_called_once_with(f"{ip}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_HostManagerStub.assert_called_once_with(mock_channel) mock_stop_host_monitor_thread.assert_called_once_with(stub=mock_stub) @patch("csle_common.controllers.host_controller.HostController.start_host_manager") @patch("csle_common.controllers.host_controller.grpc.insecure_channel") - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.stop_filebeat" - ) - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub" - ) + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.stop_filebeat") + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub") def test_stop_filebeat( - self, - mock_HostManagerStub, - mock_stop_filebeat, - mock_insecure_channel, - mock_start_host_manager, - ) -> None: + self, mock_HostManagerStub, mock_stop_filebeat, mock_insecure_channel, mock_start_host_manager) -> None: """ Test stop_filebeat method @@ -1224,7 +917,6 @@ def test_stop_filebeat( :param mock_stop_filebeat: mock stop_filebeat :param mock_insecure_channel: mock insecure_channel :param mock_start_host_manager: mock start_host_manager - :return: None """ mock_channel = MagicMock() @@ -1232,33 +924,19 @@ def test_stop_filebeat( mock_stub = MagicMock() mock_HostManagerStub.return_value = mock_stub ip = "172.17.0.2" - HostController.stop_filebeat( - emulation_env_config=self.emulation_env_config, ip=ip, logger=self.logger - ) - mock_start_host_manager.assert_called_once_with( - emulation_env_config=self.emulation_env_config, ip=ip, logger=self.logger - ) - mock_insecure_channel.assert_called_once_with( - f"{ip}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + HostController.stop_filebeat(emulation_env_config=self.emulation_env_config, ip=ip, logger=self.logger) + mock_start_host_manager.assert_called_once_with(emulation_env_config=self.emulation_env_config, ip=ip, + logger=self.logger) + mock_insecure_channel.assert_called_once_with(f"{ip}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_HostManagerStub.assert_called_once_with(mock_channel) mock_stop_filebeat.assert_called_once_with(stub=mock_stub) @patch("csle_common.controllers.host_controller.HostController.start_host_manager") @patch("csle_common.controllers.host_controller.grpc.insecure_channel") - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.stop_packetbeat" - ) - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub" - ) + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.stop_packetbeat") + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub") def test_stop_packetbeat( - self, - mock_HostManagerStub, - mock_stop_packetbeat, - mock_insecure_channel, - mock_start_host_manager, - ) -> None: + self, mock_HostManagerStub, mock_stop_packetbeat, mock_insecure_channel, mock_start_host_manager) -> None: """ Test stop_packetbeat method @@ -1266,7 +944,6 @@ def test_stop_packetbeat( :param mock_stop_packetbeat: mock stop_packetbeat :param mock_insecure_channel: mock insecure_channel :param mock_start_host_manager: mock start_host_manager - :return: None """ mock_channel = MagicMock() @@ -1274,33 +951,19 @@ def test_stop_packetbeat( mock_stub = MagicMock() mock_HostManagerStub.return_value = mock_stub ip = "172.17.0.2" - HostController.stop_packetbeat( - emulation_env_config=self.emulation_env_config, ip=ip, logger=self.logger - ) - mock_start_host_manager.assert_called_once_with( - emulation_env_config=self.emulation_env_config, ip=ip, logger=self.logger - ) - mock_insecure_channel.assert_called_once_with( - f"{ip}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + HostController.stop_packetbeat(emulation_env_config=self.emulation_env_config, ip=ip, logger=self.logger) + mock_start_host_manager.assert_called_once_with(emulation_env_config=self.emulation_env_config, ip=ip, + logger=self.logger) + mock_insecure_channel.assert_called_once_with(f"{ip}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_HostManagerStub.assert_called_once_with(mock_channel) mock_stop_packetbeat.assert_called_once_with(stub=mock_stub) @patch("csle_common.controllers.host_controller.HostController.start_host_manager") @patch("csle_common.controllers.host_controller.grpc.insecure_channel") - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.stop_metricbeat" - ) - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub" - ) + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.stop_metricbeat") + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub") def test_stop_metricbeat( - self, - mock_HostManagerStub, - mock_stop_metricbeat, - mock_insecure_channel, - mock_start_host_manager, - ) -> None: + self, mock_HostManagerStub, mock_stop_metricbeat, mock_insecure_channel, mock_start_host_manager) -> None: """ Test stop_metricbeat method @@ -1308,7 +971,6 @@ def test_stop_metricbeat( :param mock_stop_metricbeat: mock stop_metricbeat :param mock_insecure_channel: mock insecure_channel :param mock_start_host_manager: mock start_host_manager - :return: None """ mock_channel = MagicMock() @@ -1316,33 +978,19 @@ def test_stop_metricbeat( mock_stub = MagicMock() mock_HostManagerStub.return_value = mock_stub ip = "172.17.0.2" - HostController.stop_metricbeat( - emulation_env_config=self.emulation_env_config, ip=ip, logger=self.logger - ) - mock_start_host_manager.assert_called_once_with( - emulation_env_config=self.emulation_env_config, ip=ip, logger=self.logger - ) - mock_insecure_channel.assert_called_once_with( - f"{ip}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + HostController.stop_metricbeat(emulation_env_config=self.emulation_env_config, ip=ip, logger=self.logger) + mock_start_host_manager.assert_called_once_with(emulation_env_config=self.emulation_env_config, ip=ip, + logger=self.logger) + mock_insecure_channel.assert_called_once_with(f"{ip}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_HostManagerStub.assert_called_once_with(mock_channel) mock_stop_metricbeat.assert_called_once_with(stub=mock_stub) @patch("csle_common.controllers.host_controller.HostController.start_host_manager") @patch("csle_common.controllers.host_controller.grpc.insecure_channel") - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.stop_heartbeat" - ) - @patch( - "csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub" - ) + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.query_host_manager.stop_heartbeat") + @patch("csle_common.controllers.host_controller.csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub") def test_stop_heartbeat( - self, - mock_HostManagerStub, - mock_stop_heartbeat, - mock_insecure_channel, - mock_start_host_manager, - ) -> None: + self, mock_HostManagerStub, mock_stop_heartbeat, mock_insecure_channel, mock_start_host_manager) -> None: """ Test stop_heartbeat method @@ -1350,7 +998,6 @@ def test_stop_heartbeat( :param mock_stop_heartbeat: mock stop_heartbeat :param mock_insecure_channel: mock insecure_channel :param mock_start_host_manager: mock start_host_manager - :return: None """ mock_channel = MagicMock() @@ -1358,44 +1005,30 @@ def test_stop_heartbeat( mock_stub = MagicMock() mock_HostManagerStub.return_value = mock_stub ip = "172.17.0.2" - HostController.stop_heartbeat( - emulation_env_config=self.emulation_env_config, ip=ip, logger=self.logger - ) - mock_start_host_manager.assert_called_once_with( - emulation_env_config=self.emulation_env_config, ip=ip, logger=self.logger - ) - mock_insecure_channel.assert_called_once_with( - f"{ip}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + HostController.stop_heartbeat(emulation_env_config=self.emulation_env_config, ip=ip, logger=self.logger) + mock_start_host_manager.assert_called_once_with(emulation_env_config=self.emulation_env_config, ip=ip, + logger=self.logger) + mock_insecure_channel.assert_called_once_with(f"{ip}:12345", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_HostManagerStub.assert_called_once_with(mock_channel) mock_stop_heartbeat.assert_called_once_with(stub=mock_stub) @patch("csle_common.controllers.host_controller.HostController.start_host_managers") - @patch( - "csle_common.controllers.host_controller.HostController.get_host_monitor_thread_status_by_port_and_ip" - ) - def test_get_host_monitor_threads_statuses( - self, mock_get_status, mock_start_host_managers - ) -> None: + @patch("csle_common.controllers.host_controller.HostController.get_host_monitor_thread_status_by_port_and_ip") + def test_get_host_monitor_threads_statuses(self, mock_get_status, mock_start_host_managers) -> None: """ Test get_host_monitor_threads_statuses method :param mock_get_status: mock get_status method :param mock_start_host_managers: mock start_host_managers - :return: None """ status_mock = MagicMock() mock_get_status.return_value = status_mock physical_server_ip = "192.168.1.10" result = HostController.get_host_monitor_threads_statuses( - emulation_env_config=self.emulation_env_config, - physical_server_ip=physical_server_ip, - logger=self.logger, - ) - mock_start_host_managers.assert_called_once_with( - emulation_env_config=self.emulation_env_config, logger=self.logger - ) + emulation_env_config=self.emulation_env_config, physical_server_ip=physical_server_ip, logger=self.logger) + mock_start_host_managers.assert_called_once_with(emulation_env_config=self.emulation_env_config, + logger=self.logger) assert mock_get_status.call_count == 3 expected_result = [ (status_mock, "172.17.0.1"), @@ -1407,16 +1040,14 @@ def test_get_host_monitor_threads_statuses( @patch("csle_collector.host_manager.query_host_manager.get_host_status") @patch("csle_collector.host_manager.host_manager_pb2_grpc.HostManagerStub") @patch("grpc.insecure_channel") - def test_get_host_monitor_thread_status_by_port_and_ip( - self, mock_insecure_channel, mock_HostManagerStub, mock_get_host_status - ) -> None: + def test_get_host_monitor_thread_status_by_port_and_ip(self, mock_insecure_channel, mock_HostManagerStub, + mock_get_host_status) -> None: """ Test get_host_monitor_thread_status_by_port_and_ip method :param mock_insecure_channel: mock insecure_channel :param mock_HostManagerStub: mock HostManagerStub :param mock_get_host_status: mock get_host_status - :return: None """ # Create a mock status to return @@ -1429,9 +1060,7 @@ def test_get_host_monitor_thread_status_by_port_and_ip( ip = "172.17.0.2" port = 12345 result = HostController.get_host_monitor_thread_status_by_port_and_ip(ip, port) - mock_insecure_channel.assert_called_once_with( - f"{ip}:{port}", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + mock_insecure_channel.assert_called_once_with(f"{ip}:{port}", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_HostManagerStub.assert_called_once_with(mock_channel) mock_get_host_status.assert_called_once_with(stub=mock_stub) assert result == status_mock @@ -1470,7 +1099,6 @@ def test_get_host_managers_info(self) -> None: """ active_ips = ["172.17.0.2", "172.17.0.3"] physical_host_ip = "192.168.1.1" - result = HostController.get_host_managers_info( - self.emulation_env_config, active_ips, self.logger, physical_host_ip - ) + result = HostController.get_host_managers_info(self.emulation_env_config, active_ips, self.logger, + physical_host_ip) assert result diff --git a/simulation-system/libs/csle-common/tests/test_installation_controller.py b/simulation-system/libs/csle-common/tests/test_installation_controller.py index 7e35574f8..b3452460e 100644 --- a/simulation-system/libs/csle-common/tests/test_installation_controller.py +++ b/simulation-system/libs/csle-common/tests/test_installation_controller.py @@ -14,25 +14,16 @@ def test_install_all_emulations(self, mock_popen) -> None: Test the method that installs all emulations in the metastore :param mock_popen: mock Popen method - :return: None """ # mock the process returned by 'subprocess.Popen', which includes 'stdout' and 'poll' mock_process = MagicMock() mock_process.stdout.read.side_effect = [b"c", b"d", b"", b"$"] - mock_process.poll.side_effect = [ - None, - None, - None, - 0, - ] # return 0, means stop running + mock_process.poll.side_effect = [None, None, None, 0] # return 0, means stop running mock_popen.return_value = mock_process InstallationController.install_all_emulations() mock_popen.assert_called_once_with( - "cd $CSLE_HOME/emulation-system/envs/ && make install", - stdout=subprocess.PIPE, - shell=True, - ) + "cd $CSLE_HOME/emulation-system/envs/ && make install", stdout=subprocess.PIPE, shell=True) assert mock_process.stdout.read.call_count == 4 assert mock_process.poll.call_count == 4 @@ -42,25 +33,16 @@ def test_install_emulation(self, mock_popen) -> None: Test the method that installs a given emulation in the metastore :param mock_popen: mock Popen method - :return: None """ # mock the process returned by 'subprocess.Popen', which includes 'stdout' and 'poll' mock_process = MagicMock() mock_process.stdout.read.side_effect = [b"c", b"d", b"", b"$"] - mock_process.poll.side_effect = [ - None, - None, - None, - 0, - ] # return 0, means stop running + mock_process.poll.side_effect = [None, None, None, 0] # return 0, means stop running mock_popen.return_value = mock_process InstallationController.install_emulation(emulation_name="name") mock_popen.assert_called_once_with( - "cd $CSLE_HOME/emulation-system/envs/ && make install_name", - stdout=subprocess.PIPE, - shell=True, - ) + "cd $CSLE_HOME/emulation-system/envs/ && make install_name", stdout=subprocess.PIPE, shell=True) assert mock_process.stdout.read.call_count == 4 assert mock_process.poll.call_count == 4 @@ -70,25 +52,16 @@ def test_uninstall_emulation(self, mock_popen) -> None: Test the method that uninstalls a given emulation in the metastore :param mock_popen: mock Popen method - :return: None """ # mock the process returned by 'subprocess.Popen', which includes 'stdout' and 'poll' mock_process = MagicMock() mock_process.stdout.read.side_effect = [b"c", b"d", b"", b"$"] - mock_process.poll.side_effect = [ - None, - None, - None, - 0, - ] # return 0, means stop running + mock_process.poll.side_effect = [None, None, None, 0] # return 0, means stop running mock_popen.return_value = mock_process InstallationController.uninstall_emulation(emulation_name="name") mock_popen.assert_called_once_with( - "cd $CSLE_HOME/emulation-system/envs/ && make uninstall_name", - stdout=subprocess.PIPE, - shell=True, - ) + "cd $CSLE_HOME/emulation-system/envs/ && make uninstall_name", stdout=subprocess.PIPE, shell=True) assert mock_process.stdout.read.call_count == 4 assert mock_process.poll.call_count == 4 @@ -98,25 +71,16 @@ def test_uninstall_emulations(self, mock_popen) -> None: Test the method that uninstalls all emulations in the metastore :param mock_popen: mock Popen method - :return: None """ # mock the process returned by 'subprocess.Popen', which includes 'stdout' and 'poll' mock_process = MagicMock() mock_process.stdout.read.side_effect = [b"c", b"d", b"", b"$"] - mock_process.poll.side_effect = [ - None, - None, - None, - 0, - ] # return 0, means stop running + mock_process.poll.side_effect = [None, None, None, 0] # return 0, means stop running mock_popen.return_value = mock_process InstallationController.uninstall_all_emulations() - mock_popen.assert_called_once_with( - "cd $CSLE_HOME/emulation-system/envs/ && make uninstall", - stdout=subprocess.PIPE, - shell=True, - ) + mock_popen.assert_called_once_with("cd $CSLE_HOME/emulation-system/envs/ && make uninstall", + stdout=subprocess.PIPE, shell=True) assert mock_process.stdout.read.call_count == 4 assert mock_process.poll.call_count == 4 @@ -126,25 +90,16 @@ def test_install_all_simulations(self, mock_popen) -> None: Test the method that installs all simulations in the metastore :param mock_popen: mock Popen method - :return: None """ # mock the process returned by 'subprocess.Popen', which includes 'stdout' and 'poll' mock_process = MagicMock() mock_process.stdout.read.side_effect = [b"c", b"d", b"", b"$"] - mock_process.poll.side_effect = [ - None, - None, - None, - 0, - ] # return 0, means stop running + mock_process.poll.side_effect = [None, None, None, 0] # return 0, means stop running mock_popen.return_value = mock_process InstallationController.install_all_simulations() mock_popen.assert_called_once_with( - "cd $CSLE_HOME/simulation-system/envs/ && make install", - stdout=subprocess.PIPE, - shell=True, - ) + "cd $CSLE_HOME/simulation-system/envs/ && make install", stdout=subprocess.PIPE, shell=True) assert mock_process.stdout.read.call_count == 4 assert mock_process.poll.call_count == 4 @@ -154,25 +109,17 @@ def test_install_simulation(self, mock_popen) -> None: Test the method that installs a given simulation in the metastore :param mock_popen: mock Popen method - :return: None """ # mock the process returned by 'subprocess.Popen', which includes 'stdout' and 'poll' mock_process = MagicMock() mock_process.stdout.read.side_effect = [b"c", b"d", b"", b"$"] - mock_process.poll.side_effect = [ - None, - None, - None, - 0, - ] # return 0, means stop running + mock_process.poll.side_effect = [None, None, None, 0] # return 0, means stop running mock_popen.return_value = mock_process InstallationController.install_simulation(simulation_name="name") mock_popen.assert_called_once_with( "cd $CSLE_HOME/simulation-system/envs/ && make install_name", - stdout=subprocess.PIPE, - shell=True, - ) + stdout=subprocess.PIPE, shell=True) assert mock_process.stdout.read.call_count == 4 assert mock_process.poll.call_count == 4 @@ -182,25 +129,16 @@ def test_uninstall_simulation(self, mock_popen) -> None: Test the method that uninstalls a given simulation in the metastore :param mock_popen: mock Popen method - :return: None """ # mock the process returned by 'subprocess.Popen', which includes 'stdout' and 'poll' mock_process = MagicMock() mock_process.stdout.read.side_effect = [b"c", b"d", b"", b"$"] - mock_process.poll.side_effect = [ - None, - None, - None, - 0, - ] # return 0, means stop running + mock_process.poll.side_effect = [None, None, None, 0] # return 0, means stop running mock_popen.return_value = mock_process InstallationController.uninstall_simulation(simulation_name="name") mock_popen.assert_called_once_with( - "cd $CSLE_HOME/simulation-system/envs/ && make uninstall_name", - stdout=subprocess.PIPE, - shell=True, - ) + "cd $CSLE_HOME/simulation-system/envs/ && make uninstall_name", stdout=subprocess.PIPE, shell=True) assert mock_process.stdout.read.call_count == 4 assert mock_process.poll.call_count == 4 @@ -210,25 +148,16 @@ def test_uninstall_all_simulations(self, mock_popen) -> None: Test the method that uninstalls all simulations in the metastore :param mock_popen: mock Popen method - :return: None """ # mock the process returned by 'subprocess.Popen', which includes 'stdout' and 'poll' mock_process = MagicMock() mock_process.stdout.read.side_effect = [b"c", b"d", b"", b"$"] - mock_process.poll.side_effect = [ - None, - None, - None, - 0, - ] # return 0, means stop running + mock_process.poll.side_effect = [None, None, None, 0] # return 0, means stop running mock_popen.return_value = mock_process InstallationController.uninstall_all_simulations() mock_popen.assert_called_once_with( - "cd $CSLE_HOME/simulation-system/envs/ && make uninstall", - stdout=subprocess.PIPE, - shell=True, - ) + "cd $CSLE_HOME/simulation-system/envs/ && make uninstall", stdout=subprocess.PIPE, shell=True) assert mock_process.stdout.read.call_count == 4 assert mock_process.poll.call_count == 4 @@ -238,25 +167,16 @@ def test_install_derived_images(self, mock_popen) -> None: Test the method that installs all derived Docker images :param mock_popen: mock Popen method - :return: None """ # mock the process returned by 'subprocess.Popen', which includes 'stdout' and 'poll' mock_process = MagicMock() mock_process.stdout.read.side_effect = [b"c", b"d", b"", b"$"] - mock_process.poll.side_effect = [ - None, - None, - None, - 0, - ] # return 0, means stop running + mock_process.poll.side_effect = [None, None, None, 0] # return 0, means stop running mock_popen.return_value = mock_process InstallationController.install_derived_images() mock_popen.assert_called_once_with( - "cd $CSLE_HOME/emulation-system/derived_images/ && make build", - stdout=subprocess.PIPE, - shell=True, - ) + "cd $CSLE_HOME/emulation-system/derived_images/ && make build", stdout=subprocess.PIPE, shell=True) assert mock_process.stdout.read.call_count == 4 assert mock_process.poll.call_count == 4 @@ -272,19 +192,11 @@ def test_uninstall_derived_images(self, mock_popen) -> None: # mock the process returned by 'subprocess.Popen', which includes 'stdout' and 'poll' mock_process = MagicMock() mock_process.stdout.read.side_effect = [b"c", b"d", b"", b"$"] - mock_process.poll.side_effect = [ - None, - None, - None, - 0, - ] # return 0, means stop running + mock_process.poll.side_effect = [None, None, None, 0] # return 0, means stop running mock_popen.return_value = mock_process InstallationController.uninstall_derived_images() mock_popen.assert_called_once_with( - "cd $CSLE_HOME/emulation-system/derived_images/ && make rm_image", - stdout=subprocess.PIPE, - shell=True, - ) + "cd $CSLE_HOME/emulation-system/derived_images/ && make rm_image", stdout=subprocess.PIPE, shell=True) assert mock_process.stdout.read.call_count == 4 assert mock_process.poll.call_count == 4 @@ -294,25 +206,16 @@ def test_uninstall_derived_image(self, mock_popen) -> None: Test the method that uninstalls a given derived Docker images :param mock_popen: mock Popen method - :return: None """ # mock the process returned by 'subprocess.Popen', which includes 'stdout' and 'poll' mock_process = MagicMock() mock_process.stdout.read.side_effect = [b"c", b"d", b"", b"$"] - mock_process.poll.side_effect = [ - None, - None, - None, - 0, - ] # return 0, means stop running + mock_process.poll.side_effect = [None, None, None, 0] # return 0, means stop running mock_popen.return_value = mock_process InstallationController.uninstall_derived_image(image_name="image") mock_popen.assert_called_once_with( - "cd $CSLE_HOME/emulation-system/derived_images/ && make rm_image", - stdout=subprocess.PIPE, - shell=True, - ) + "cd $CSLE_HOME/emulation-system/derived_images/ && make rm_image", stdout=subprocess.PIPE, shell=True) assert mock_process.stdout.read.call_count == 4 assert mock_process.poll.call_count == 4 @@ -322,25 +225,16 @@ def test_install_base_images(self, mock_popen) -> None: Test the method that installs all base Docker images :param mock_popen: mock Popen method - :return: None """ # mock the process returned by 'subprocess.Popen', which includes 'stdout' and 'poll' mock_process = MagicMock() mock_process.stdout.read.side_effect = [b"c", b"d", b"", b"$"] - mock_process.poll.side_effect = [ - None, - None, - None, - 0, - ] # return 0, means stop running + mock_process.poll.side_effect = [None, None, None, 0] # return 0, means stop running mock_popen.return_value = mock_process InstallationController.install_base_images() mock_popen.assert_called_once_with( - "cd $CSLE_HOME/emulation-system/base_images/ && make build", - stdout=subprocess.PIPE, - shell=True, - ) + "cd $CSLE_HOME/emulation-system/base_images/ && make build", stdout=subprocess.PIPE, shell=True) assert mock_process.stdout.read.call_count == 4 assert mock_process.poll.call_count == 4 @@ -350,25 +244,16 @@ def test_install_base_image(self, mock_popen) -> None: Test the method that installs a given base Docker images :param mock_popen: mock Popen method - :return: None """ # mock the process returned by 'subprocess.Popen', which includes 'stdout' and 'poll' mock_process = MagicMock() mock_process.stdout.read.side_effect = [b"c", b"d", b"", b"$"] - mock_process.poll.side_effect = [ - None, - None, - None, - 0, - ] # return 0, means stop running + mock_process.poll.side_effect = [None, None, None, 0] # return 0, means stop running mock_popen.return_value = mock_process InstallationController.install_base_image(image_name="name") mock_popen.assert_called_once_with( - "cd $CSLE_HOME/emulation-system/base_images/ && make name", - stdout=subprocess.PIPE, - shell=True, - ) + "cd $CSLE_HOME/emulation-system/base_images/ && make name", stdout=subprocess.PIPE, shell=True) assert mock_process.stdout.read.call_count == 4 assert mock_process.poll.call_count == 4 @@ -384,19 +269,11 @@ def test_uninstall_base_images(self, mock_popen) -> None: # mock the process returned by 'subprocess.Popen', which includes 'stdout' and 'poll' mock_process = MagicMock() mock_process.stdout.read.side_effect = [b"c", b"d", b"", b"$"] - mock_process.poll.side_effect = [ - None, - None, - None, - 0, - ] # return 0, means stop running + mock_process.poll.side_effect = [None, None, None, 0] # return 0, means stop running mock_popen.return_value = mock_process InstallationController.uninstall_base_images() mock_popen.assert_called_once_with( - "cd $CSLE_HOME/emulation-system/base_images/ && make rm_image", - stdout=subprocess.PIPE, - shell=True, - ) + "cd $CSLE_HOME/emulation-system/base_images/ && make rm_image", stdout=subprocess.PIPE, shell=True) assert mock_process.stdout.read.call_count == 4 assert mock_process.poll.call_count == 4 @@ -406,25 +283,16 @@ def test_uninstall_base_image(self, mock_popen) -> None: Test the method that uninstalls a given base Docker images :param mock_popen: mock Popen method - :return: None """ # mock the process returned by 'subprocess.Popen', which includes 'stdout' and 'poll' mock_process = MagicMock() mock_process.stdout.read.side_effect = [b"c", b"d", b"", b"$"] - mock_process.poll.side_effect = [ - None, - None, - None, - 0, - ] # return 0, means stop running + mock_process.poll.side_effect = [None, None, None, 0] # return 0, means stop running mock_popen.return_value = mock_process InstallationController.uninstall_base_image(image_name="name") mock_popen.assert_called_once_with( - "cd $CSLE_HOME/emulation-system/base_images/ && make rm_name", - stdout=subprocess.PIPE, - shell=True, - ) + "cd $CSLE_HOME/emulation-system/base_images/ && make rm_name", stdout=subprocess.PIPE, shell=True) assert mock_process.stdout.read.call_count == 4 assert mock_process.poll.call_count == 4 @@ -434,23 +302,15 @@ def test_uninstall_metastore(self, mock_popen) -> None: Test the method that uninstalls the metastore :param mock_popen: mock Popen method - :return: None """ # mock the process returned by 'subprocess.Popen', which includes 'stdout' and 'poll' mock_process = MagicMock() mock_process.stdout.read.side_effect = [b"c", b"d", b"", b"$"] - mock_process.poll.side_effect = [ - None, - None, - None, - 0, - ] # return 0, means stop running + mock_process.poll.side_effect = [None, None, None, 0] # return 0, means stop running mock_popen.return_value = mock_process InstallationController.uninstall_metastore() - mock_popen.assert_called_once_with( - "cd $CSLE_HOME/metastore/ && make clean", stdout=subprocess.PIPE, shell=True - ) + mock_popen.assert_called_once_with("cd $CSLE_HOME/metastore/ && make clean", stdout=subprocess.PIPE, shell=True) assert mock_process.stdout.read.call_count == 4 assert mock_process.poll.call_count == 4 @@ -460,22 +320,14 @@ def test_install_metastore(self, mock_popen) -> None: Test the method that installs the metastore :param mock_popen: mock Popen method - :return: None """ # mock the process returned by 'subprocess.Popen', which includes 'stdout' and 'poll' mock_process = MagicMock() mock_process.stdout.read.side_effect = [b"c", b"d", b"", b"$"] - mock_process.poll.side_effect = [ - None, - None, - None, - 0, - ] # return 0, means stop running + mock_process.poll.side_effect = [None, None, None, 0] # return 0, means stop running mock_popen.return_value = mock_process InstallationController.install_metastore() - mock_popen.assert_called_once_with( - "cd $CSLE_HOME/metastore/ && make build", stdout=subprocess.PIPE, shell=True - ) + mock_popen.assert_called_once_with("cd $CSLE_HOME/metastore/ && make build", stdout=subprocess.PIPE, shell=True) assert mock_process.stdout.read.call_count == 4 assert mock_process.poll.call_count == 4 diff --git a/simulation-system/libs/csle-common/tests/test_kafka_controller.py b/simulation-system/libs/csle-common/tests/test_kafka_controller.py index de4b5cedf..b16fe12f3 100644 --- a/simulation-system/libs/csle-common/tests/test_kafka_controller.py +++ b/simulation-system/libs/csle-common/tests/test_kafka_controller.py @@ -16,9 +16,7 @@ class TestKafkaControllerSuite: @patch("csle_common.util.emulation_util.EmulationUtil.connect_admin") @patch("csle_common.util.emulation_util.EmulationUtil.execute_ssh_cmd") - def test_start_stop_kafka_manager( - self, mock_execute_ssh_cmd, mock_connect_admin - ) -> None: + def test_start_stop_kafka_manager(self, mock_execute_ssh_cmd, mock_connect_admin) -> None: """ Test case for starting or stopping the kafka manager @@ -36,11 +34,8 @@ def test_start_stop_kafka_manager( emulation_env_config.kafka_config.container.docker_gw_bridge_ip = "172.17.0.1" emulation_env_config.kafka_config.get_connection.return_value = MagicMock() KafkaController.start_kafka_manager(emulation_env_config) - mock_connect_admin.assert_called_once_with( - emulation_env_config=emulation_env_config, - ip="172.17.0.1", - create_producer=False, - ) + mock_connect_admin.assert_called_once_with(emulation_env_config=emulation_env_config, ip="172.17.0.1", + create_producer=False) mock_execute_ssh_cmd.assert_called() KafkaController.stop_kafka_manager(emulation_env_config) assert 2 == mock_connect_admin.call_count @@ -49,22 +44,13 @@ def test_start_stop_kafka_manager( @patch("csle_collector.kafka_manager.query_kafka_server.create_topic") @patch("csle_collector.kafka_manager.query_kafka_server.start_kafka") @patch("csle_collector.kafka_manager.kafka_manager_pb2_grpc.KafkaManagerStub") - @patch( - "csle_common.controllers.kafka_controller.KafkaController.start_kafka_manager" - ) - @patch( - "csle_common.controllers.kafka_controller.KafkaController.get_kafka_status_by_port_and_ip" - ) + @patch("csle_common.controllers.kafka_controller.KafkaController.start_kafka_manager") + @patch("csle_common.controllers.kafka_controller.KafkaController.get_kafka_status_by_port_and_ip") @patch("time.sleep", return_value=None) def test_create_topics( - self, - mock_time_sleep, - mock_get_kafka_status_by_port_and_ip, - mock_start_kafka_manager, - mock_KafkaManagerStub, - mock_start_kafka, - mock_create_topic, - ) -> None: + self, mock_time_sleep, mock_get_kafka_status_by_port_and_ip, mock_start_kafka_manager, + mock_KafkaManagerStub, + mock_start_kafka, mock_create_topic) -> None: """ Tests the method that sends a request to the KafkaManager to create topics according to the given configuration @@ -74,7 +60,6 @@ def test_create_topics( :param mock_KafkaManagerStub: mock KafkaManagerStub :param mock_start_kafka: mock start_kafka :param mock_create_topic: mock create_topic - :return: None """ mock_stub = MagicMock() @@ -97,39 +82,21 @@ def test_create_topics( topic2.retention_time_hours = 48 emulation_env_config.kafka_config.topics = [topic1, topic2] mock_get_kafka_status_by_port_and_ip.return_value.running = False - KafkaController.create_topics( - emulation_env_config=emulation_env_config, logger=mock_logger - ) - mock_logger.info.assert_any_call( - "creating kafka topics on container: 127.0.0.1" - ) + KafkaController.create_topics(emulation_env_config=emulation_env_config, logger=mock_logger) + mock_logger.info.assert_any_call("creating kafka topics on container: 127.0.0.1") mock_logger.info.assert_any_call("Kafka server is not running, starting it.") mock_logger.info.assert_any_call("Creating topic: topic1") mock_logger.info.assert_any_call("Creating topic: topic2") - mock_start_kafka_manager.assert_called_once_with( - emulation_env_config=emulation_env_config - ) - mock_get_kafka_status_by_port_and_ip.assert_called_once_with( - ip="127.0.0.1", port=9092 - ) + mock_start_kafka_manager.assert_called_once_with(emulation_env_config=emulation_env_config) + mock_get_kafka_status_by_port_and_ip.assert_called_once_with(ip="127.0.0.1", port=9092) mock_start_kafka.assert_called_once_with(mock_stub) - mock_create_topic.assert_any_call( - mock_stub, name="topic1", partitions=1, replicas=1, retention_time_hours=24 - ) - mock_create_topic.assert_any_call( - mock_stub, name="topic2", partitions=2, replicas=2, retention_time_hours=48 - ) + mock_create_topic.assert_any_call(mock_stub, name="topic1", partitions=1, replicas=1, retention_time_hours=24) + mock_create_topic.assert_any_call(mock_stub, name="topic2", partitions=2, replicas=2, retention_time_hours=48) mock_time_sleep.assert_called_once_with(20) - @patch( - "csle_common.controllers.kafka_controller.KafkaController.start_kafka_manager" - ) - @patch( - "csle_common.controllers.kafka_controller.KafkaController.get_kafka_status_by_port_and_ip" - ) - def test_get_kafka_status( - self, mock_get_kafka_status_by_port_and_ip, mock_start_kafka_manager - ) -> None: + @patch("csle_common.controllers.kafka_controller.KafkaController.start_kafka_manager") + @patch("csle_common.controllers.kafka_controller.KafkaController.get_kafka_status_by_port_and_ip") + def test_get_kafka_status(self, mock_get_kafka_status_by_port_and_ip, mock_start_kafka_manager) -> None: """ Test case for querying the KafkaManager about the status of the Kafka server @@ -137,9 +104,7 @@ def test_get_kafka_status( :param mock_start_kafka_manager: mocked start_kafka_manager method """ mock_start_kafka_manager.return_value = None - expected_kafka_dto = MagicMock( - spec=csle_collector.kafka_manager.kafka_manager_pb2.KafkaDTO - ) + expected_kafka_dto = MagicMock(spec=csle_collector.kafka_manager.kafka_manager_pb2.KafkaDTO) mock_get_kafka_status_by_port_and_ip.return_value = expected_kafka_dto emulation_env_config = MagicMock(spec=EmulationEnvConfig) kafka_config = MagicMock() @@ -147,60 +112,43 @@ def test_get_kafka_status( emulation_env_config.kafka_config.container.docker_gw_bridge_ip = "172.17.0.1" emulation_env_config.kafka_config.kafka_manager_port = 5601 result = KafkaController.get_kafka_status(emulation_env_config) - mock_start_kafka_manager.assert_called_once_with( - emulation_env_config=emulation_env_config - ) - mock_get_kafka_status_by_port_and_ip.assert_called_once_with( - ip="172.17.0.1", port=5601 - ) + mock_start_kafka_manager.assert_called_once_with(emulation_env_config=emulation_env_config) + mock_get_kafka_status_by_port_and_ip.assert_called_once_with(ip="172.17.0.1", port=5601) assert result == expected_kafka_dto @patch("csle_collector.kafka_manager.query_kafka_server.get_kafka_status") @patch("csle_collector.kafka_manager.kafka_manager_pb2_grpc.KafkaManagerStub") @patch("grpc.insecure_channel") - def test_get_kafka_status_by_port_and_ip( - self, mock_insecure_channel, mock_KafkaManagerStub, mock_get_kafka_status - ) -> None: + def test_get_kafka_status_by_port_and_ip(self, mock_insecure_channel, mock_KafkaManagerStub, + mock_get_kafka_status) -> None: """ Test case for querying the KafkaManager about the status of the kafka stack :param mock_insecure_channel: mocked insecure_channel method :param mock_KafkaManagerStub: mocked KafkaManagerStub method :param mock_get_kafka_status: mocked get_kafka_status method - :return: None """ mock_channel = MagicMock() mock_insecure_channel.return_value.__enter__.return_value = mock_channel mock_stub = MagicMock() mock_KafkaManagerStub.return_value = mock_stub - expected_kafka_dto = MagicMock( - spec=csle_collector.kafka_manager.kafka_manager_pb2.KafkaDTO - ) + expected_kafka_dto = MagicMock(spec=csle_collector.kafka_manager.kafka_manager_pb2.KafkaDTO) mock_get_kafka_status.return_value = expected_kafka_dto ip = "172.17.0.1" port = 5601 result = KafkaController.get_kafka_status_by_port_and_ip(ip, port) - mock_insecure_channel.assert_called_once_with( - f"{ip}:{port}", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + mock_insecure_channel.assert_called_once_with(f"{ip}:{port}", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_KafkaManagerStub.assert_called_once_with(mock_channel) mock_get_kafka_status.assert_called_once_with(mock_stub) assert result == expected_kafka_dto - @patch( - "csle_common.controllers.kafka_controller.KafkaController.start_kafka_manager" - ) + @patch("csle_common.controllers.kafka_controller.KafkaController.start_kafka_manager") @patch("csle_collector.kafka_manager.query_kafka_server.stop_kafka") @patch("csle_collector.kafka_manager.kafka_manager_pb2_grpc.KafkaManagerStub") @patch("grpc.insecure_channel") - def test_stop_kafka_server( - self, - mock_insecure_channel, - mock_KafkaManagerStub, - mock_stop_kafka, - mock_start_kafka_manager, - ) -> None: + def test_stop_kafka_server(self, mock_insecure_channel, mock_KafkaManagerStub, mock_stop_kafka, + mock_start_kafka_manager) -> None: """ Test the method for requesting the KafkaManager to stop the Kafka server @@ -208,16 +156,13 @@ def test_stop_kafka_server( :param mock_KafkaManagerStub: mock KafkaManagerStub :param mock_stop_kafka: mock stop_kafka :param mock_start_kafka_manager: mock start_kafka_manager - :return: None """ mock_channel = MagicMock() mock_insecure_channel.return_value.__enter__.return_value = mock_channel mock_stub = MagicMock() mock_KafkaManagerStub.return_value = mock_stub - expected_kafka_dto = MagicMock( - spec=csle_collector.kafka_manager.kafka_manager_pb2.KafkaDTO - ) + expected_kafka_dto = MagicMock(spec=csle_collector.kafka_manager.kafka_manager_pb2.KafkaDTO) mock_stop_kafka.return_value = expected_kafka_dto mock_logger = MagicMock(spec=logging.Logger) emulation_env_config = MagicMock(spec=EmulationEnvConfig) @@ -225,37 +170,22 @@ def test_stop_kafka_server( emulation_env_config.kafka_config = kafka_config emulation_env_config.kafka_config.container.docker_gw_bridge_ip = "127.0.0.1" emulation_env_config.kafka_config.kafka_manager_port = 9092 - result = KafkaController.stop_kafka_server( - emulation_env_config=emulation_env_config, logger=mock_logger - ) - mock_logger.info.assert_any_call( - "Stopping kafka server on container: 127.0.0.1" - ) - mock_start_kafka_manager.assert_called_once_with( - emulation_env_config=emulation_env_config - ) + result = KafkaController.stop_kafka_server(emulation_env_config=emulation_env_config, logger=mock_logger) + mock_logger.info.assert_any_call("Stopping kafka server on container: 127.0.0.1") + mock_start_kafka_manager.assert_called_once_with(emulation_env_config=emulation_env_config) mock_insecure_channel.assert_called_once_with( f"{emulation_env_config.kafka_config.container.docker_gw_bridge_ip}:" - f"{emulation_env_config.kafka_config.kafka_manager_port}", - options=constants.GRPC_SERVERS.GRPC_OPTIONS, - ) + f"{emulation_env_config.kafka_config.kafka_manager_port}", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_KafkaManagerStub.assert_called_once_with(mock_channel) mock_stop_kafka.assert_called_once_with(mock_stub) assert result == expected_kafka_dto - @patch( - "csle_common.controllers.kafka_controller.KafkaController.start_kafka_manager" - ) + @patch("csle_common.controllers.kafka_controller.KafkaController.start_kafka_manager") @patch("csle_collector.kafka_manager.query_kafka_server.start_kafka") @patch("csle_collector.kafka_manager.kafka_manager_pb2_grpc.KafkaManagerStub") @patch("grpc.insecure_channel") - def test_start_kafka_server( - self, - mock_insecure_channel, - mock_KafkaManagerStub, - mock_start_kafka, - mock_start_kafka_manager, - ) -> None: + def test_start_kafka_server(self, mock_insecure_channel, mock_KafkaManagerStub, mock_start_kafka, + mock_start_kafka_manager) -> None: """ Test the method for requesting the KafkaManager to start the Kafka server @@ -263,16 +193,13 @@ def test_start_kafka_server( :param mock_KafkaManagerStub: mock KafkaManagerStub :param mock_start_kafka: mock start_kafka :param mock_start_kafka_manager: mock start_kafka_manager - :return: None """ mock_channel = MagicMock() mock_insecure_channel.return_value.__enter__.return_value = mock_channel mock_stub = MagicMock() mock_KafkaManagerStub.return_value = mock_stub - expected_kafka_dto = MagicMock( - spec=csle_collector.kafka_manager.kafka_manager_pb2.KafkaDTO - ) + expected_kafka_dto = MagicMock(spec=csle_collector.kafka_manager.kafka_manager_pb2.KafkaDTO) mock_start_kafka.return_value = expected_kafka_dto mock_logger = MagicMock(spec=logging.Logger) emulation_env_config = MagicMock(spec=EmulationEnvConfig) @@ -280,36 +207,24 @@ def test_start_kafka_server( emulation_env_config.kafka_config = kafka_config emulation_env_config.kafka_config.container.docker_gw_bridge_ip = "127.0.0.1" emulation_env_config.kafka_config.kafka_manager_port = 9092 - result = KafkaController.start_kafka_server( - emulation_env_config=emulation_env_config, logger=mock_logger - ) - mock_logger.info.assert_any_call( - "Starting kafka server on container: 127.0.0.1" - ) - mock_start_kafka_manager.assert_called_once_with( - emulation_env_config=emulation_env_config - ) + result = KafkaController.start_kafka_server(emulation_env_config=emulation_env_config, logger=mock_logger) + mock_logger.info.assert_any_call("Starting kafka server on container: 127.0.0.1") + mock_start_kafka_manager.assert_called_once_with(emulation_env_config=emulation_env_config) mock_insecure_channel.assert_called_once_with( f"{emulation_env_config.kafka_config.container.docker_gw_bridge_ip}:" - f"{emulation_env_config.kafka_config.kafka_manager_port}", - options=constants.GRPC_SERVERS.GRPC_OPTIONS, - ) + f"{emulation_env_config.kafka_config.kafka_manager_port}", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_KafkaManagerStub.assert_called_once_with(mock_channel) mock_start_kafka.assert_called_once_with(mock_stub) assert result == expected_kafka_dto @patch("csle_common.util.emulation_util.EmulationUtil.connect_admin") @patch("csle_collector.constants.constants.KAFKA.KAFKA_CONFIG_FILE") - def test_configure_broker_ips( - self, mock_kafka_config_file, mock_connect_admin - ) -> None: + def test_configure_broker_ips(self, mock_kafka_config_file, mock_connect_admin) -> None: """ Test the method for configuring the broker IPs on the Kafka container - :param mock_kafka_config_file: mock kafka_config_file :param mock_connect_admin: mock_connect_admin - :return: None """ mock_logger = MagicMock(spec=logging.Logger) @@ -317,30 +232,18 @@ def test_configure_broker_ips( kafka_config = MagicMock() emulation_env_config.kafka_config = kafka_config emulation_env_config.kafka_config.container.docker_gw_bridge_ip = "127.0.0.1" - emulation_env_config.kafka_config.container.get_ips.return_value = [ - "192.168.1.100" - ] + emulation_env_config.kafka_config.container.get_ips.return_value = ["192.168.1.100"] mock_sftp_client = MagicMock() mock_file = MagicMock() - emulation_env_config.get_connection.return_value.open_sftp.return_value = ( - mock_sftp_client - ) + emulation_env_config.get_connection.return_value.open_sftp.return_value = (mock_sftp_client) mock_file.read.return_value = b"internal_ip_placeholder external_ip_placeholder" mock_sftp_client.open.side_effect = [mock_file, mock_file] # for read and write - KafkaController.configure_broker_ips( - emulation_env_config=emulation_env_config, logger=mock_logger - ) - mock_logger.info.assert_any_call( - "Configuring broker IPs on container: 127.0.0.1" - ) - mock_connect_admin.assert_called_once_with( - emulation_env_config=emulation_env_config, ip="127.0.0.1" - ) + KafkaController.configure_broker_ips(emulation_env_config=emulation_env_config, logger=mock_logger) + mock_logger.info.assert_any_call("Configuring broker IPs on container: 127.0.0.1") + mock_connect_admin.assert_called_once_with(emulation_env_config=emulation_env_config, ip="127.0.0.1") emulation_env_config.get_connection.assert_called_once_with(ip="127.0.0.1") mock_sftp_client.open.assert_any_call(mock_kafka_config_file, mode="r") mock_sftp_client.open.assert_any_call(mock_kafka_config_file, mode="w") - # expected_file_contents = "192.168.1.100 127.0.0.1" - # mock_file.write.assert_called_once_with(data=expected_file_contents) mock_file.flush.assert_called_once() mock_file.close.assert_called() @@ -370,25 +273,13 @@ def test_get_kafka_managers_ports(self) -> None: result = KafkaController.get_kafka_managers_ports(emulation_env_config) assert result == [5601] - @patch( - "csle_common.controllers.kafka_controller.KafkaController.get_kafka_managers_ips" - ) - @patch( - "csle_common.controllers.kafka_controller.KafkaController.get_kafka_managers_ports" - ) - @patch( - "csle_common.controllers.kafka_controller.KafkaController.get_kafka_status_by_port_and_ip" - ) - @patch( - "csle_collector.kafka_manager.kafka_manager_util.KafkaManagerUtil.kafka_dto_empty" - ) + @patch("csle_common.controllers.kafka_controller.KafkaController.get_kafka_managers_ips") + @patch("csle_common.controllers.kafka_controller.KafkaController.get_kafka_managers_ports") + @patch("csle_common.controllers.kafka_controller.KafkaController.get_kafka_status_by_port_and_ip") + @patch("csle_collector.kafka_manager.kafka_manager_util.KafkaManagerUtil.kafka_dto_empty") def test_get_kafka_managers_infos( - self, - mock_kafka_dto_empty, - mock_get_kafka_status_by_port_and_ip, - mock_get_kafka_managers_ports, - mock_get_kafka_managers_ips, - ) -> None: + self, mock_kafka_dto_empty, mock_get_kafka_status_by_port_and_ip, mock_get_kafka_managers_ports, + mock_get_kafka_managers_ips) -> None: """ Test case for extracting the infomation of the kafka managers for a given emulation @@ -396,7 +287,6 @@ def test_get_kafka_managers_infos( :param mock_get_kafka_status_by_port_and_ip: mocked get_kafka_status_by_port_and_ip method :param mock_get_kafka_managers_ports: mocked get_kafka_managers_ports :param mock_get_kafka_managers_ips: mocked get_kafka_managers_ips - :return: None """ mock_get_kafka_managers_ips.return_value = ["172.17.0.1"] @@ -414,15 +304,9 @@ def test_get_kafka_managers_infos( logger = MagicMock(spec=logging.Logger) active_ips = ["172.17.0.1"] physical_host_ip = "192.168.0.1" - result = KafkaController.get_kafka_managers_info( - emulation_env_config, active_ips, logger, physical_host_ip - ) - mock_get_kafka_managers_ips.assert_called_once_with( - emulation_env_config=emulation_env_config - ) - mock_get_kafka_managers_ports.assert_called_once_with( - emulation_env_config=emulation_env_config - ) + result = KafkaController.get_kafka_managers_info(emulation_env_config, active_ips, logger, physical_host_ip) + mock_get_kafka_managers_ips.assert_called_once_with(emulation_env_config=emulation_env_config) + mock_get_kafka_managers_ports.assert_called_once_with(emulation_env_config=emulation_env_config) assert result.ips == ["172.17.0.1"] assert result.ports == [5601] assert result.execution_id == 10 diff --git a/simulation-system/libs/csle-common/tests/test_management_controller.py b/simulation-system/libs/csle-common/tests/test_management_controller.py index cd9f6f4c2..6094b8bab 100644 --- a/simulation-system/libs/csle-common/tests/test_management_controller.py +++ b/simulation-system/libs/csle-common/tests/test_management_controller.py @@ -21,7 +21,6 @@ def test_read_pid_file(self, mock_exists, mock_open) -> None: :param mock_exists: mock exists :param mock_open: mock open - :return: None """ path = "/path/to/pidfile" @@ -32,29 +31,23 @@ def test_read_pid_file(self, mock_exists, mock_open) -> None: mock_exists.assert_called_once_with(path) mock_open.assert_called_once_with(path, "r") - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.read_pid_file" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.read_pid_file") def test_is_prometheus_running(self, mock_read_pid_file) -> None: """ Tests the method that checks if prometheus is running on the host :param mock_read_pid_file: mock read_pid_file - :return: None """ mock_read_pid_file.return_value = -1 assert not ManagementSystemController.is_prometheus_running() - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.read_pid_file" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.read_pid_file") def test_is_node_exporter_running(self, mock_read_pid_file) -> None: """ Tests the method that checks if node_exporter is running on the host :param mock_read_pid_file: mock read_pid_file - :return: None """ mock_read_pid_file.return_value = -1 @@ -66,16 +59,12 @@ def test_is_nginx_running(self, mock_run) -> None: Tests the method that checks if Nginx is running on the host :param mock_run: mock run - :return: None """ - constants.COMMANDS = MagicMock() # type: ignore constants.COMMANDS.NGINX_STATUS = "nginx status" mock_run.return_value = MagicMock(stdout="Active: active (running)") assert ManagementSystemController.is_nginx_running() - mock_run.assert_called_once_with( - ["nginx", "status"], capture_output=True, text=True - ) + mock_run.assert_called_once_with(["nginx", "status"], capture_output=True, text=True) @patch("subprocess.run") def test_is_postgresql_running(self, mock_run) -> None: @@ -83,16 +72,12 @@ def test_is_postgresql_running(self, mock_run) -> None: Tests the method that checks if postgresql is running on the host :param mock_run: mock run - :return: None """ - constants.COMMANDS = MagicMock() # type: ignore constants.COMMANDS.POSTGRESQL_STATUS = "postgresql status" mock_run.return_value = MagicMock(stdout="Active: active (running)") assert ManagementSystemController.is_postgresql_running() - mock_run.assert_called_once_with( - ["postgresql", "status"], capture_output=True, text=True - ) + mock_run.assert_called_once_with(["postgresql", "status"], capture_output=True, text=True) @patch("subprocess.run") def test_is_docker_engine_running(self, mock_run) -> None: @@ -100,34 +85,25 @@ def test_is_docker_engine_running(self, mock_run) -> None: Tests the method that checks if Docker engine is running on the host :param mock_run: mock run - :return: None """ - constants.COMMANDS = MagicMock() # type: ignore constants.COMMANDS.DOCKER_ENGINE_STATUS = "docker engine status" mock_run.return_value = MagicMock(stdout="Active: active (running)") assert ManagementSystemController.is_docker_engine_running() - mock_run.assert_called_once_with( - ["docker", "engine", "status"], capture_output=True, text=True - ) + mock_run.assert_called_once_with(["docker", "engine", "status"], capture_output=True, text=True) - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.read_pid_file" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.read_pid_file") def test_is_flask_running(self, mock_read_pid_file) -> None: """ Tests the method that checks if flask web server is running on the host :param mock_run: mock run - :return: None """ mock_read_pid_file.return_value = -1 assert not ManagementSystemController.is_flask_running() - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.is_node_exporter_running" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.is_node_exporter_running") @patch("subprocess.Popen") def test_start_node_exporter(self, mock_popen, mock_is_running) -> None: """ @@ -135,7 +111,6 @@ def test_start_node_exporter(self, mock_popen, mock_is_running) -> None: :param mock_popen: mock popen :param mock_is_running: mock is_node_exporter_running - :return: None """ logger = MagicMock(spec=logging.Logger) @@ -146,17 +121,14 @@ def test_start_node_exporter(self, mock_popen, mock_is_running) -> None: logger.info.assert_called_with("Node exporter is already running") mock_popen.assert_not_called() - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.is_flask_running" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.is_flask_running") @patch("subprocess.Popen") def test_start_flask(self, mock_popen, mock_is_running) -> None: """ - Tests the method thats starts Flask REST API Server + Tests the method that starts Flask REST API Server :param mock_popen: mock popen :param mock_is_running: mock is_node_exporter_running - :return: None """ logger = MagicMock(spec=logging.Logger) @@ -167,17 +139,14 @@ def test_start_flask(self, mock_popen, mock_is_running) -> None: logger.info.assert_called_with("Flask is already running") mock_popen.assert_not_called() - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.is_node_exporter_running" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.is_node_exporter_running") @patch("subprocess.Popen") def test_stop_node_exporter(self, mock_popen, mock_is_running) -> None: """ - Tests the method thats stops the node exporter + Tests the method that stops the node exporter :param mock_popen: mock popen :param mock_is_running: mock is_node_exporter_running - :return: None """ logger = MagicMock(spec=logging.Logger) @@ -188,17 +157,14 @@ def test_stop_node_exporter(self, mock_popen, mock_is_running) -> None: logger.info.assert_called_with("Node exporter is not running") mock_popen.assert_not_called() - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.is_flask_running" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.is_flask_running") @patch("subprocess.Popen") def test_stop_flask(self, mock_popen, mock_is_running) -> None: """ - Tests the method thats stops the flask REST API + Tests the method that stops the flask REST API :param mock_popen: mock popen :param mock_is_running: mock is_flask_running - :return: None """ logger = MagicMock(spec=logging.Logger) @@ -209,17 +175,14 @@ def test_stop_flask(self, mock_popen, mock_is_running) -> None: logger.info.assert_called_with("Flask is not running") mock_popen.assert_not_called() - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.is_prometheus_running" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.is_prometheus_running") @patch("subprocess.Popen") def test_start_prometheus(self, mock_popen, mock_is_running) -> None: """ - Tests the method thats starts Prometheus + Tests the method that starts Prometheus :param mock_popen: mock popen :param mock_is_running: mock is_prometheus_running - :return: None """ logger = MagicMock(spec=logging.Logger) @@ -230,17 +193,14 @@ def test_start_prometheus(self, mock_popen, mock_is_running) -> None: logger.info.assert_called_with("Prometheus is already running") mock_popen.assert_not_called() - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.is_prometheus_running" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.is_prometheus_running") @patch("subprocess.Popen") def test_stop_prometheus(self, mock_popen, mock_is_running) -> None: """ - Tests the method thats stops Prometheus + Tests the method that stops Prometheus :param mock_popen: mock popen :param mock_is_running: mock is_prometheus_running - :return: None """ logger = MagicMock(spec=logging.Logger) @@ -257,10 +217,9 @@ def test_is_cadvisor_running(self, mock_from_env) -> None: Tests if cadvisor is running :param mock_from_env: mock from_env - :return: None """ - constants.CONTAINER_IMAGES = MagicMock() # type: ignore + constants.CONTAINER_IMAGES = MagicMock() # type: ignore constants.CONTAINER_IMAGES.CADVISOR = "name" mock_client = MagicMock() mock_from_env.return_value = mock_client @@ -277,10 +236,9 @@ def test_is_pgadmin_running(self, mock_from_env) -> None: Tests if Prometheus is running :param mock_from_env: mock from_env - :return: None """ - constants.CONTAINER_IMAGES = MagicMock() # type: ignore + constants.CONTAINER_IMAGES = MagicMock() # type: ignore constants.CONTAINER_IMAGES.PGADMIN = "name" mock_client = MagicMock() mock_from_env.return_value = mock_client @@ -297,10 +255,8 @@ def test_stop_cadvisor(self, mock_from_env) -> None: Tests if cadvisor is stopped :param mock_from_env: mock from_env - :return: None """ - constants.CONTAINER_IMAGES = MagicMock() # type: ignore constants.CONTAINER_IMAGES.CADVISOR = "name" mock_client = MagicMock() mock_from_env.return_value = mock_client @@ -318,10 +274,8 @@ def test_stop_pgadmin(self, mock_from_env) -> None: Tests if pgadmin is stopped :param mock_from_env: mock from_env - :return: None """ - constants.CONTAINER_IMAGES = MagicMock() # type: ignore constants.CONTAINER_IMAGES.PGADMIN = "name" mock_client = MagicMock() mock_from_env.return_value = mock_client @@ -333,15 +287,12 @@ def test_stop_pgadmin(self, mock_from_env) -> None: mock_client.containers.list.assert_called_once() mock_container.stop.assert_called_once() - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.is_cadvisor_running" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.is_cadvisor_running") def test_start_cadvisor(self, mock_is_running) -> None: """ Tests if cadvisor starts :param mock_is_running: mock is_cadvisor_running - :return: None """ logger = MagicMock(spec=logging.Logger) @@ -351,15 +302,12 @@ def test_start_cadvisor(self, mock_is_running) -> None: mock_is_running.assert_called_once() logger.info.assert_called_with("cAdvisor is already running") - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.is_postgresql_running" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.is_postgresql_running") def test_start_postgresql(self, mock_is_running) -> None: """ Tests if postgresql starts :param mock_is_running: mock is_postgresql_running - :return: None """ logger = MagicMock(spec=logging.Logger) @@ -369,15 +317,12 @@ def test_start_postgresql(self, mock_is_running) -> None: mock_is_running.assert_called_once() logger.info.assert_called_with("PostgreSQL is already running") - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.is_postgresql_running" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.is_postgresql_running") def test_stop_postgresql(self, mock_is_running) -> None: """ Tests if postgresql stopped :param mock_is_running: mock is_postgresql_running - :return: None """ logger = MagicMock(spec=logging.Logger) @@ -387,15 +332,12 @@ def test_stop_postgresql(self, mock_is_running) -> None: mock_is_running.assert_called_once() logger.info.assert_called_with("PostgreSQL is not running") - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.is_nginx_running" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.is_nginx_running") def test_start_nginx(self, mock_is_running) -> None: """ Tests if Nginx starts :param mock_is_running: mock is_nginx_running - :return: None """ logger = MagicMock(spec=logging.Logger) @@ -405,15 +347,12 @@ def test_start_nginx(self, mock_is_running) -> None: mock_is_running.assert_called_once() logger.info.assert_called_with("Nginx is already running") - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.is_nginx_running" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.is_nginx_running") def test_stop_nginx(self, mock_is_running) -> None: """ Tests if Nginx stops :param mock_is_running: mock is_nginx_running - :return: None """ logger = MagicMock(spec=logging.Logger) @@ -423,15 +362,12 @@ def test_stop_nginx(self, mock_is_running) -> None: mock_is_running.assert_called_once() logger.info.assert_called_with("Nginx is not running") - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.is_docker_engine_running" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.is_docker_engine_running") def test_start_docker_engine(self, mock_is_running) -> None: """ Tests if Docker engine starts :param mock_is_running: mock is_docker_engine_running - :return: None """ logger = MagicMock(spec=logging.Logger) @@ -441,15 +377,12 @@ def test_start_docker_engine(self, mock_is_running) -> None: mock_is_running.assert_called_once() logger.info.assert_called_with("The Docker engine is already running") - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.is_docker_engine_running" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.is_docker_engine_running") def test_stop_docker_engine(self, mock_is_running) -> None: """ Tests if Docker engine stops :param mock_is_running: mock is_docker_engine_running - :return: None """ logger = MagicMock(spec=logging.Logger) @@ -459,15 +392,12 @@ def test_stop_docker_engine(self, mock_is_running) -> None: mock_is_running.assert_called_once() logger.info.assert_called_with("The Docker engine is not running") - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.is_pgadmin_running" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.is_pgadmin_running") def test_start_pgadmin(self, mock_is_running) -> None: """ Tests if pgadmin starts :param mock_is_running: mock is_pgadmin_running - :return: None """ logger = MagicMock(spec=logging.Logger) @@ -483,10 +413,8 @@ def test_stop_grafana(self, mock_from_env) -> None: Tests if grafana stops :param mock_from_env: mock from_env - :return: None """ - constants.CONTAINER_IMAGES = MagicMock() # type: ignore constants.CONTAINER_IMAGES.GRAFANA = "name" mock_client = MagicMock() mock_from_env.return_value = mock_client @@ -498,15 +426,12 @@ def test_stop_grafana(self, mock_from_env) -> None: mock_client.containers.list.assert_called_once() mock_container.stop.assert_called_once() - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.is_grafana_running" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.is_grafana_running") def test_start_grafana(self, mock_is_running) -> None: """ Tests if grafana starts :param mock_is_running: mock is_grafana_running - :return: None """ logger = MagicMock(spec=logging.Logger) @@ -522,10 +447,8 @@ def test_is_grafana_running(self, mock_from_env) -> None: Tests if grafana is running :param mock_from_env: mock from_env - :return: None """ - constants.CONTAINER_IMAGES = MagicMock() # type: ignore constants.CONTAINER_IMAGES.GRAFANA = "name" mock_client = MagicMock() mock_from_env.return_value = mock_client @@ -536,29 +459,23 @@ def test_is_grafana_running(self, mock_from_env) -> None: mock_from_env.assert_called_once() mock_client.containers.list.assert_called_once() - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.read_pid_file" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.read_pid_file") def test_is_statsmanager_running(self, mock_read_pid_file) -> None: """ Tests the method that checks if statsmanager is running on the host :param mock_read_pid_file: mock read_pid_file - :return: None """ mock_read_pid_file.return_value = -1 assert not ManagementSystemController.is_statsmanager_running() - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.is_statsmanager_running" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.is_statsmanager_running") def test_start_docker_statsmanager(self, mock_is_running) -> None: """ Tests the method that checks if docker statsmanager is running on the host :param mock_is_running: mock is_statsmanager_running - :return: None """ logger = MagicMock(spec=logging.Logger) @@ -574,15 +491,12 @@ def test_start_docker_statsmanager(self, mock_is_running) -> None: mock_is_running.assert_called_once() logger.info.assert_called_with("The docker statsmanager is already running") - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.is_statsmanager_running" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.is_statsmanager_running") def test_stop_docker_statsmanager(self, mock_is_running) -> None: """ Tests the method that stops docker statsmanager :param mock_is_running: mock is_statsmanager_running - :return: None """ logger = MagicMock(spec=logging.Logger) @@ -592,30 +506,22 @@ def test_stop_docker_statsmanager(self, mock_is_running) -> None: mock_is_running.assert_called_once() logger.info.assert_called_with("The statsmanager is not running") - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.read_pid_file" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.read_pid_file") @patch("subprocess.Popen") def test_stop_cluster_manager(self, mock_popen, mock_read_pid_file) -> None: """ Tests the method that stops the local cluster manager :param mock_read_pid_file: mock read_pid_file - :return: None """ mock_read_pid_file.return_value = 1234 mock_popen.return_value.communicate.return_value = (None, None) result = ManagementSystemController.stop_cluster_manager() assert result - mock_read_pid_file.assert_called_once_with( - constants.COMMANDS.CLUSTER_MANAGER_PIDFILE - ) - mock_popen.assert_called_once_with( - constants.COMMANDS.KILL_PROCESS.format(1234), - stdout=subprocess.DEVNULL, - shell=True, - ) + mock_read_pid_file.assert_called_once_with(constants.COMMANDS.CLUSTER_MANAGER_PIDFILE) + mock_popen.assert_called_once_with(constants.COMMANDS.KILL_PROCESS.format(1234), stdout=subprocess.DEVNULL, + shell=True) mock_popen.return_value.communicate.assert_called_once() @patch("psutil.pid_exists") @@ -635,15 +541,12 @@ def test_is_pid_running(self, mock_pid_exists) -> None: logger.info.assert_called_once_with(f"Checking if PID: {pid} is running") mock_pid_exists.assert_called_once_with(pid) - @patch( - "csle_common.controllers.management_system_controller.ManagementSystemController.is_pid_running" - ) + @patch("csle_common.controllers.management_system_controller.ManagementSystemController.is_pid_running") def test_stop_pid(self, mock_is_running) -> None: """ Tests the method that stops a process with a given pid :param mock_is_running: mock is_pid_running - :return: None """ logger = MagicMock(spec=logging.Logger) diff --git a/simulation-system/libs/csle-common/tests/test_ossec_ids_controller.py b/simulation-system/libs/csle-common/tests/test_ossec_ids_controller.py index ce1f00d17..ac1e790d3 100644 --- a/simulation-system/libs/csle-common/tests/test_ossec_ids_controller.py +++ b/simulation-system/libs/csle-common/tests/test_ossec_ids_controller.py @@ -49,52 +49,37 @@ def emulation_env_config_setup(self) -> None: self.emulation_env_config = emulation_env_config self.logger = MagicMock(spec=logging.Logger) - @patch( - "csle_common.controllers.ossec_ids_controller.OSSECIDSController.stop_ossec_ids" - ) + @patch("csle_common.controllers.ossec_ids_controller.OSSECIDSController.stop_ossec_ids") def test_stop_ossec_idses(self, mock_stop_ossec_ids) -> None: """ Test the method for stopping the OSSEC IDSes :param mock_stop_ossec_ids: mock stop_ossec_ids - :return: None """ - constants.CONTAINER_IMAGES = MagicMock() # type: ignore constants.CONTAINER_IMAGES.OSSEC_IDS_IMAGES = ["container-1", "container-2"] physical_host_ip = "192.168.1.10" - OSSECIDSController.stop_ossec_idses( - emulation_env_config=self.emulation_env_config, - physical_host_ip=physical_host_ip, - ) + OSSECIDSController.stop_ossec_idses(emulation_env_config=self.emulation_env_config, + physical_host_ip=physical_host_ip) assert mock_stop_ossec_ids.call_count == 2 - @patch( - "csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_ids" - ) + @patch("csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_ids") def test_start_ossec_idses(self, mock_start_ossec_ids) -> None: """ Test the method for starting the OSSEC IDSes :param mock_start_ossec_ids: mock start_ossec_ids - :return: None """ - constants.CONTAINER_IMAGES = MagicMock() # type: ignore constants.CONTAINER_IMAGES.OSSEC_IDS_IMAGES = ["container-1", "container-2"] physical_server_ip = "192.168.1.10" OSSECIDSController.start_ossec_idses( - emulation_env_config=self.emulation_env_config, - physical_server_ip=physical_server_ip, - logger=self.logger, - ) + emulation_env_config=self.emulation_env_config, physical_server_ip=physical_server_ip, logger=self.logger) self.logger.info.assert_any_call("Starting the OSSEC IDS on ip: 172.17.0.1") self.logger.info.assert_any_call("Starting the OSSEC IDS on ip: 172.17.0.2") assert mock_start_ossec_ids.call_count == 2 - @patch( - "csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_ids_manager" - ) + @patch("csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_ids_manager") @patch( "csle_common.controllers.ossec_ids_controller.OSSECIDSController" ".get_ossec_ids_monitor_thread_status_by_ip_and_port" @@ -102,12 +87,7 @@ def test_start_ossec_idses(self, mock_start_ossec_ids) -> None: @patch("grpc.insecure_channel") @patch("csle_common.controllers.ossec_ids_controller.Logger") def test_start_ossec_ids( - self, - mock_logger, - mock_insecure_channel, - mock_get_monitor_status, - mock_start_manager, - ) -> None: + self, mock_logger, mock_insecure_channel, mock_get_monitor_status, mock_start_manager) -> None: """ Test the method for starting a OSSEC IDS with a specific IP @@ -122,31 +102,19 @@ def test_start_ossec_ids( mock_channel = MagicMock() mock_insecure_channel.return_value.__enter__.return_value = mock_channel ip = "192.168.1.10" - OSSECIDSController.start_ossec_ids( - emulation_env_config=self.emulation_env_config, ip=ip - ) - mock_start_manager.assert_called_once_with( - emulation_env_config=self.emulation_env_config, ip=ip - ) + OSSECIDSController.start_ossec_ids(emulation_env_config=self.emulation_env_config, ip=ip) + mock_start_manager.assert_called_once_with(emulation_env_config=self.emulation_env_config, ip=ip) mock_get_monitor_status.assert_called_once_with(port=1515, ip=ip) - mock_insecure_channel.assert_called_once_with( - f"{ip}:1515", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) - mock_logger.__call__().get_logger().info.assert_called_once_with( - f"Starting OSSEC IDS on {ip}." - ) + mock_insecure_channel.assert_called_once_with(f"{ip}:1515", options=constants.GRPC_SERVERS.GRPC_OPTIONS) + mock_logger.__call__().get_logger().info.assert_called_once_with(f"Starting OSSEC IDS on {ip}.") - @patch( - "csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_ids_manager" - ) + @patch("csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_ids_manager") @patch( "csle_common.controllers.ossec_ids_controller.OSSECIDSController" ".get_ossec_ids_monitor_thread_status_by_ip_and_port" ) @patch("grpc.insecure_channel") - def test_stop_ossec_ids( - self, mock_insecure_channel, mock_get_monitor_status, mock_start_manager - ) -> None: + def test_stop_ossec_ids(self, mock_insecure_channel, mock_get_monitor_status, mock_start_manager) -> None: """ Test the method for stopping a OSSEC IDS with a specific IP @@ -160,17 +128,11 @@ def test_stop_ossec_ids( mock_channel = MagicMock() mock_insecure_channel.return_value.__enter__.return_value = mock_channel ip = "192.168.1.10" - OSSECIDSController.start_ossec_ids( - emulation_env_config=self.emulation_env_config, ip=ip - ) - mock_start_manager.assert_called_once_with( - emulation_env_config=self.emulation_env_config, ip=ip - ) + OSSECIDSController.start_ossec_ids(emulation_env_config=self.emulation_env_config, ip=ip) + mock_start_manager.assert_called_once_with(emulation_env_config=self.emulation_env_config, ip=ip) mock_get_monitor_status.assert_called_once_with(port=1515, ip=ip) - @patch( - "csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_ids_manager" - ) + @patch("csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_ids_manager") def test_start_ossec_idses_managers(self, mock_start_manager) -> None: """ Test the method for starting OSSEC IDS managers @@ -184,8 +146,7 @@ def test_start_ossec_idses_managers(self, mock_start_manager) -> None: physical_server_ip = "192.168.1.10" OSSECIDSController.start_ossec_idses_managers( emulation_env_config=self.emulation_env_config, - physical_server_ip=physical_server_ip, - ) + physical_server_ip=physical_server_ip) assert mock_start_manager.call_count == 2 @patch("csle_common.util.emulation_util.EmulationUtil.connect_admin") @@ -196,43 +157,32 @@ def test_start_ossec_ids_manager(self, mock_execute_ssh_cmd, mock_connect_admin) :param mock_execute_ssh_cmd: mock execute_ssh_cmd :param mock_connect_admin: mock connect_admin - :return: None """ ip = "192.168.1.10" mock_execute_ssh_cmd.return_value = ("ossec_ids_manager running", "", 0) constants.COMMANDS.SEARCH_OSSEC_IDS_MANAGER = "ossec_ids_manager running" - OSSECIDSController.start_ossec_ids_manager( - emulation_env_config=self.emulation_env_config, ip=ip - ) + OSSECIDSController.start_ossec_ids_manager(emulation_env_config=self.emulation_env_config, ip=ip) mock_execute_ssh_cmd.assert_called() mock_connect_admin.assert_called() - @patch( - "csle_common.controllers.ossec_ids_controller.OSSECIDSController.stop_ossec_ids_manager" - ) + @patch("csle_common.controllers.ossec_ids_controller.OSSECIDSController.stop_ossec_ids_manager") def test_stop_ossec_idses_managers(self, mock_stop_manager) -> None: """ Test method for stopping ossec ids managers :param mock_stop_manager: mock stop_manager - :return: None """ - constants.CONTAINER_IMAGES = MagicMock() # type: ignore constants.CONTAINER_IMAGES.OSSEC_IDS_IMAGES = ["container-1", "container-2"] physical_server_ip = "192.168.1.10" - OSSECIDSController.stop_ossec_idses_managers( - emulation_env_config=self.emulation_env_config, - physical_server_ip=physical_server_ip, - ) + OSSECIDSController.stop_ossec_idses_managers(emulation_env_config=self.emulation_env_config, + physical_server_ip=physical_server_ip) assert mock_stop_manager.call_count == 2 @patch("csle_common.util.emulation_util.EmulationUtil.connect_admin") @patch("csle_common.util.emulation_util.EmulationUtil.execute_ssh_cmd") - def test_stop_ossec_ids_manager( - self, mock_execute_ssh_cmd, mock_connect_admin - ) -> None: + def test_stop_ossec_ids_manager(self, mock_execute_ssh_cmd, mock_connect_admin) -> None: """ Test method for stopping the OSSEC IDS manager on a specific container @@ -243,9 +193,7 @@ def test_stop_ossec_ids_manager( """ ip = "192.168.1.10" mock_execute_ssh_cmd.return_value = ("ossec_ids_manager running", "", 0) - OSSECIDSController.stop_ossec_ids_manager( - emulation_env_config=self.emulation_env_config, ip=ip - ) + OSSECIDSController.stop_ossec_ids_manager(emulation_env_config=self.emulation_env_config, ip=ip) mock_execute_ssh_cmd.assert_called() mock_connect_admin.assert_called() @@ -268,17 +216,14 @@ def test_start_ossec_idses_monitor_threads(self, mock_start_monitor, mock_start_ physical_server_ip=physical_server_ip) assert mock_start_monitor.call_count == 2 - @patch( - "csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_ids_manager" - ) + @patch("csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_ids_manager") @patch( "csle_common.controllers.ossec_ids_controller.OSSECIDSController" ".get_ossec_ids_monitor_thread_status_by_ip_and_port" ) @patch("grpc.insecure_channel") - def test_start_ossec_ids_monitor_thread( - self, mock_insecure_channel, mock_get_monitor_status, mock_start_manager - ) -> None: + def test_start_ossec_ids_monitor_thread(self, mock_insecure_channel, mock_get_monitor_status, mock_start_manager) \ + -> None: """ A method that sends a request to the OSSECIDSManager on a specific IP to start to start the IDS manager and the monitor thread @@ -286,24 +231,17 @@ def test_start_ossec_ids_monitor_thread( :param mock_insecure_channel: mock insecure_channel :param mock_get_monitor_status: mock get_monitor_status :param mock_start_manager: mock start_manager - :return: None """ mock_get_monitor_status.return_value.ossec_ids_running = False mock_channel = MagicMock() mock_insecure_channel.return_value.__enter__.return_value = mock_channel ip = "192.168.1.10" - OSSECIDSController.start_ossec_ids_monitor_thread( - emulation_env_config=self.emulation_env_config, ip=ip - ) - mock_start_manager.assert_called_once_with( - emulation_env_config=self.emulation_env_config, ip=ip - ) + OSSECIDSController.start_ossec_ids_monitor_thread(emulation_env_config=self.emulation_env_config, ip=ip) + mock_start_manager.assert_called_once_with(emulation_env_config=self.emulation_env_config, ip=ip) mock_get_monitor_status.assert_called_once_with(port=1515, ip=ip) - @patch( - "csle_common.controllers.ossec_ids_controller.OSSECIDSController.stop_ossec_ids_monitor_thread" - ) + @patch("csle_common.controllers.ossec_ids_controller.OSSECIDSController.stop_ossec_ids_monitor_thread") def test_stop_ossec_ids_monitor_threads(self, mock_stop_monitor) -> None: """ Test a method that sends a request to the OSSECIDSManager on every container that runs @@ -316,18 +254,12 @@ def test_stop_ossec_ids_monitor_threads(self, mock_stop_monitor) -> None: constants.CONTAINER_IMAGES.OSSEC_IDS_IMAGES = ["container-1", "container-2"] physical_server_ip = "192.168.1.10" OSSECIDSController.stop_ossec_idses_monitor_threads( - emulation_env_config=self.emulation_env_config, - physical_server_ip=physical_server_ip, - ) + emulation_env_config=self.emulation_env_config, physical_server_ip=physical_server_ip) assert mock_stop_monitor.call_count == 2 - @patch( - "csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_ids_manager" - ) + @patch("csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_ids_manager") @patch("grpc.insecure_channel") - def test_stop_ossec_ids_monitor_thread( - self, mock_insecure_channel, mock_start_manager - ) -> None: + def test_stop_ossec_ids_monitor_thread(self, mock_insecure_channel, mock_start_manager) -> None: """ Test a method that sends a request to the OSSECIDSManager for a specific IP to stop the monitor thread @@ -338,16 +270,10 @@ def test_stop_ossec_ids_monitor_thread( mock_channel = MagicMock() mock_insecure_channel.return_value.__enter__.return_value = mock_channel ip = "192.168.1.10" - OSSECIDSController.stop_ossec_ids_monitor_thread( - emulation_env_config=self.emulation_env_config, ip=ip - ) - mock_start_manager.assert_called_once_with( - emulation_env_config=self.emulation_env_config, ip=ip - ) + OSSECIDSController.stop_ossec_ids_monitor_thread(emulation_env_config=self.emulation_env_config, ip=ip) + mock_start_manager.assert_called_once_with(emulation_env_config=self.emulation_env_config, ip=ip) - @patch( - "csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_idses_managers" - ) + @patch("csle_common.controllers.ossec_ids_controller.OSSECIDSController.start_ossec_idses_managers") @patch("grpc.insecure_channel") def test_get_ossec_idses_monitor_threads_statuses(self, mock_insecure_channel, mock_start_managers) -> None: """ @@ -364,13 +290,9 @@ def test_get_ossec_idses_monitor_threads_statuses(self, mock_insecure_channel, m constants.CONTAINER_IMAGES = MagicMock() # type: ignore constants.CONTAINER_IMAGES.OSSEC_IDS_IMAGES = ["container-1", "container-2"] OSSECIDSController.get_ossec_idses_monitor_threads_statuses( - emulation_env_config=self.emulation_env_config, - physical_server_ip=physical_server_ip, - ) + emulation_env_config=self.emulation_env_config, physical_server_ip=physical_server_ip) mock_start_managers.assert_called_once_with( - emulation_env_config=self.emulation_env_config, - physical_server_ip=physical_server_ip, - ) + emulation_env_config=self.emulation_env_config, physical_server_ip=physical_server_ip) def test_get_ossec_idses_managers_ips(self) -> None: """ @@ -380,9 +302,7 @@ def test_get_ossec_idses_managers_ips(self) -> None: """ constants.CONTAINER_IMAGES = MagicMock() # type: ignore constants.CONTAINER_IMAGES.OSSEC_IDS_IMAGES = ["container-1", "container-2"] - ips = OSSECIDSController.get_ossec_idses_managers_ips( - emulation_env_config=self.emulation_env_config - ) + ips = OSSECIDSController.get_ossec_idses_managers_ips(emulation_env_config=self.emulation_env_config) assert ips == ["172.17.0.1", "172.17.0.2"] def test_get_ossec_idses_managers_ports(self) -> None: @@ -393,56 +313,35 @@ def test_get_ossec_idses_managers_ports(self) -> None: """ constants.CONTAINER_IMAGES = MagicMock() # type: ignore constants.CONTAINER_IMAGES.OSSEC_IDS_IMAGES = ["container-1", "container-2"] - ports = OSSECIDSController.get_ossec_idses_managers_ports( - emulation_env_config=self.emulation_env_config - ) + ports = OSSECIDSController.get_ossec_idses_managers_ports(emulation_env_config=self.emulation_env_config) assert ports == [1515, 1515] @patch("grpc.insecure_channel") - def test_get_ossec_ids_monitor_thread_status_by_ip_and_port( - self, mock_insecure_channel - ) -> None: + def test_get_ossec_ids_monitor_thread_status_by_ip_and_port(self, mock_insecure_channel) -> None: """ Tests a method that sends a request to the OSSECIDSManager with a specific port and ip to get the status of the IDS monitor thread :param mock_insecure_channel: mock insecure_channel - :return: None """ port = 1515 ip = "192.168.1.100" mock_channel = MagicMock() mock_insecure_channel.return_value.__enter__.return_value = mock_channel - OSSECIDSController.get_ossec_ids_monitor_thread_status_by_ip_and_port( - port=port, ip=ip - ) - mock_insecure_channel.assert_called_once_with( - f"{ip}:{port}", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + OSSECIDSController.get_ossec_ids_monitor_thread_status_by_ip_and_port(port=port, ip=ip) + mock_insecure_channel.assert_called_once_with(f"{ip}:{port}", options=constants.GRPC_SERVERS.GRPC_OPTIONS) - @patch( - "csle_common.controllers.ossec_ids_controller.OSSECIDSController.get_ossec_idses_managers_ips" - ) - @patch( - "csle_common.controllers.ossec_ids_controller.OSSECIDSController.get_ossec_idses_managers_ports" - ) + @patch("csle_common.controllers.ossec_ids_controller.OSSECIDSController.get_ossec_idses_managers_ips") + @patch("csle_common.controllers.ossec_ids_controller.OSSECIDSController.get_ossec_idses_managers_ports") @patch( "csle_common.controllers.ossec_ids_controller.OSSECIDSController" ".get_ossec_ids_monitor_thread_status_by_ip_and_port" ) @patch("csle_common.util.emulation_util.EmulationUtil.physical_ip_match") - @patch( - "csle_collector.ossec_ids_manager.ossec_ids_manager_util.OSSecManagerUtil.ossec_ids_monitor_dto_empty" - ) + @patch("csle_collector.ossec_ids_manager.ossec_ids_manager_util.OSSecManagerUtil.ossec_ids_monitor_dto_empty") def test_get_ossec_managers_info( - self, - mock_ossec_dto_empty, - mock_physical_ip_match, - mock_get_status, - mock_get_ports, - mock_get_ips, - ) -> None: + self, mock_ossec_dto_empty, mock_physical_ip_match, mock_get_status, mock_get_ports, mock_get_ips) -> None: """ Tests the method that extracts the information of the OSSEC IDS managers for a given emulation @@ -451,7 +350,6 @@ def test_get_ossec_managers_info( :param mock_get_status: mock get_status :param mock_get_ports: mock get_ports :param mock_get_ips: mock get_ips - :retun: None """ emulation_env_config = self.emulation_env_config @@ -467,11 +365,7 @@ def test_get_ossec_managers_info( mock_get_status.side_effect = [mock_status_1, Exception("Test Exception")] mock_physical_ip_match.side_effect = [True, True] mock_ossec_dto_empty.return_value = MagicMock() - OSSECIDSController.get_ossec_managers_info( - emulation_env_config, active_ips, logger, physical_host_ip - ) + OSSECIDSController.get_ossec_managers_info(emulation_env_config, active_ips, logger, physical_host_ip) mock_get_ips.assert_called_once_with(emulation_env_config=emulation_env_config) - mock_get_ports.assert_called_once_with( - emulation_env_config=emulation_env_config - ) + mock_get_ports.assert_called_once_with(emulation_env_config=emulation_env_config) assert mock_get_status.call_count == 1 diff --git a/simulation-system/libs/csle-common/tests/test_ovs_controller.py b/simulation-system/libs/csle-common/tests/test_ovs_controller.py index 4839b4bb3..68fda4dd5 100644 --- a/simulation-system/libs/csle-common/tests/test_ovs_controller.py +++ b/simulation-system/libs/csle-common/tests/test_ovs_controller.py @@ -22,26 +22,19 @@ def mock_containers_config(self) -> ContainersConfig: container1.physical_host_ip = "192.168.1.1" container1.name = "ovs_container1" container1.get_full_name.return_value = "container1" - container1.ips_and_networks = [ - ("192.168.1.100", MagicMock(interface="eth0", bitmask="255.255.255.0")) - ] - + container1.ips_and_networks = [("192.168.1.100", MagicMock(interface="eth0", bitmask="255.255.255.0"))] container2 = MagicMock() container2.physical_host_ip = "192.168.1.2" container2.name = "ovs_container2" container2.get_full_name.return_value = "container2" - container2.ips_and_networks = [ - ("192.168.1.101", MagicMock(interface="eth0", bitmask="255.255.255.0")) - ] + container2.ips_and_networks = [("192.168.1.101", MagicMock(interface="eth0", bitmask="255.255.255.0"))] containers_config = MagicMock(spec=ContainersConfig) containers_config.containers = [container1, container2] return containers_config @patch("subprocess.Popen") @patch("time.sleep") - def test_create_virtual_switches_on_container( - self, mock_sleep, mock_popen, mock_containers_config - ) -> None: + def test_create_virtual_switches_on_container(self, mock_sleep, mock_popen, mock_containers_config) -> None: """ Test method that creates the OVS switches @@ -52,9 +45,7 @@ def test_create_virtual_switches_on_container( logger = MagicMock() physical_server_ip = "192.168.1.1" constants.CONTAINER_IMAGES.OVS_IMAGES = ["ovs_container1"] - OVSController.create_virtual_switches_on_container( - mock_containers_config, physical_server_ip, logger - ) + OVSController.create_virtual_switches_on_container(mock_containers_config, physical_server_ip, logger) mock_popen.assert_called() assert mock_sleep.call_count == 5 @@ -78,10 +69,7 @@ def test_apply_ovs_config(self, mock_execute_ssh_cmd, mock_connect_admin) -> Non emulation_env_config.connections = {"192.168.1.3": MagicMock()} physical_server_ip = "192.168.1.2" logger = MagicMock(spec=logging.Logger) - OVSController.apply_ovs_config( - emulation_env_config=emulation_env_config, - physical_server_ip=physical_server_ip, - logger=logger, - ) + OVSController.apply_ovs_config(emulation_env_config=emulation_env_config, physical_server_ip=physical_server_ip, + logger=logger) mock_connect_admin.assert_not_called() mock_execute_ssh_cmd.assert_not_called() diff --git a/simulation-system/libs/csle-common/tests/test_resource_constraints_controller.py b/simulation-system/libs/csle-common/tests/test_resource_constraints_controller.py index e4c11e2f7..0368f9252 100644 --- a/simulation-system/libs/csle-common/tests/test_resource_constraints_controller.py +++ b/simulation-system/libs/csle-common/tests/test_resource_constraints_controller.py @@ -1,8 +1,6 @@ import subprocess from unittest.mock import patch, MagicMock -from csle_common.controllers.resource_constraints_controller import ( - ResourceConstraintsController, -) +from csle_common.controllers.resource_constraints_controller import ResourceConstraintsController class TestResourceConstraintsControllerSuite: @@ -15,12 +13,7 @@ class TestResourceConstraintsControllerSuite: @patch("csle_common.util.emulation_util.EmulationUtil.execute_ssh_cmd") @patch("subprocess.Popen") def test_apply_resource_constraints( - self, - mock_popen, - mock_execute_ssh_cmd, - mock_disconnect_admin, - mock_connect_admin, - ) -> None: + self, mock_popen, mock_execute_ssh_cmd, mock_disconnect_admin, mock_connect_admin) -> None: """ Test the method that creates users in an emulation environment according to a specified users-configuration @@ -29,7 +22,6 @@ def test_apply_resource_constraints( :param mock_execute_ssh_cmd: mock_execute_ssh_cmd :param mock_disconnect_admin: mock_disconnect_admin :param mock_connect_admin: mock_connect_admin - :return: None """ emulation_env_config = MagicMock() @@ -59,9 +51,7 @@ def test_apply_resource_constraints( node_resource_config.num_cpus = 1 node_resource_config.ips_and_network_configs = [("192.168.1.2", network_config)] - emulation_env_config.resources_config.node_resources_configurations = [ - node_resource_config - ] + emulation_env_config.resources_config.node_resources_configurations = [node_resource_config] emulation_env_config.kafka_config.resources = node_resource_config emulation_env_config.sdn_controller_config.resources = node_resource_config emulation_env_config.connections = {"192.168.1.2": MagicMock()} @@ -69,19 +59,12 @@ def test_apply_resource_constraints( mock_execute_ssh_cmd.return_value = ("output", "error", 0) logger = MagicMock() ResourceConstraintsController.apply_resource_constraints( - emulation_env_config=emulation_env_config, - physical_server_ip=physical_server_ip, - logger=logger, - ) + emulation_env_config=emulation_env_config, physical_server_ip=physical_server_ip, logger=logger) mock_connect_admin.assert_any_call( emulation_env_config=emulation_env_config, ip="192.168.1.2" ) expected_docker_update_cmd = "docker update --memory=2G --cpus=1 container_1" - mock_popen.assert_any_call( - expected_docker_update_cmd, - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL, - shell=True, - ) + mock_popen.assert_any_call(expected_docker_update_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, + shell=True) mock_execute_ssh_cmd.assert_called() mock_disconnect_admin.assert_any_call(emulation_env_config=emulation_env_config) diff --git a/simulation-system/libs/csle-common/tests/test_sdn_controller.py b/simulation-system/libs/csle-common/tests/test_sdn_controller.py index eed6f5906..166b79961 100644 --- a/simulation-system/libs/csle-common/tests/test_sdn_controller.py +++ b/simulation-system/libs/csle-common/tests/test_sdn_controller.py @@ -23,27 +23,17 @@ def test_start_ryu_manager(self, mock_execute_ssh_cmd, mock_connect_admin) -> No """ logger = MagicMock() sdn_controller_config = MagicMock( - container=MagicMock(docker_gw_bridge_ip="192.168.1.2"), - manager_port=6633, - manager_log_dir="/var/log/ryu", - manager_log_file="ryu.log", - manager_max_workers=4, - ) - emulation_env_config = MagicMock( - sdn_controller_config=sdn_controller_config, - name="Test Emulation", - ) + container=MagicMock(docker_gw_bridge_ip="192.168.1.2"), manager_port=6633, manager_log_dir="/var/log/ryu", + manager_log_file="ryu.log", manager_max_workers=4) + emulation_env_config = MagicMock(sdn_controller_config=sdn_controller_config, name="Test Emulation") mock_execute_ssh_cmd.side_effect = [ (b"", b"", 0), # output of cp /etc/sudoers.bak /etc/sudoers (b"", b"", 0), # output of adding CVE entry to sudoers (b"", b"", 0), # output of chmod 440 /etc/sudoers ] SDNControllerManager.start_ryu_manager(emulation_env_config, logger) - mock_connect_admin.assert_any_call( - emulation_env_config=emulation_env_config, - ip="192.168.1.2", - create_producer=False, - ) + mock_connect_admin.assert_any_call(emulation_env_config=emulation_env_config, ip="192.168.1.2", + create_producer=False) mock_execute_ssh_cmd.assert_called() @patch("csle_common.util.emulation_util.EmulationUtil.connect_admin") @@ -58,33 +48,17 @@ def test_stop_ryu_manager(self, mock_execute_ssh_cmd, mock_connect_admin) -> Non :return: None """ logger = MagicMock() - sdn_controller_config = MagicMock( - container=MagicMock(docker_gw_bridge_ip="192.168.1.2"), - ) - emulation_env_config = MagicMock( - sdn_controller_config=sdn_controller_config, - name="Test Emulation", - ) - mock_execute_ssh_cmd.side_effect = [ - (b"", b"", 0), # output of pkill ryu_manager - ] + sdn_controller_config = MagicMock(container=MagicMock(docker_gw_bridge_ip="192.168.1.2")) + emulation_env_config = MagicMock(sdn_controller_config=sdn_controller_config, name="Test Emulation") + mock_execute_ssh_cmd.side_effect = [(b"", b"", 0)] # output of pkill ryu_manager SDNControllerManager.stop_ryu_manager(emulation_env_config, logger) - mock_connect_admin.assert_any_call( - emulation_env_config=emulation_env_config, - ip="192.168.1.2", - create_producer=False, - ) + mock_connect_admin.assert_any_call(emulation_env_config=emulation_env_config, ip="192.168.1.2", + create_producer=False) mock_execute_ssh_cmd.assert_called() - @patch( - "csle_common.controllers.sdn_controller_manager.SDNControllerManager.start_ryu_manager" - ) - @patch( - "csle_common.controllers.sdn_controller_manager.SDNControllerManager.get_ryu_status_by_port_and_ip" - ) - def test_get_ryu_status( - self, mock_get_ryu_status_by_port_and_ip, mock_get_ryu_manager - ) -> None: + @patch("csle_common.controllers.sdn_controller_manager.SDNControllerManager.start_ryu_manager") + @patch("csle_common.controllers.sdn_controller_manager.SDNControllerManager.get_ryu_status_by_port_and_ip") + def test_get_ryu_status(self, mock_get_ryu_status_by_port_and_ip, mock_get_ryu_manager) -> None: """ Test method for querying the RyuManager about the status of the Ryu SDN controller @@ -96,26 +70,16 @@ def test_get_ryu_status( logger = MagicMock() ryu_dto_mock = MagicMock(spec=ryu_manager_pb2.RyuDTO) mock_get_ryu_status_by_port_and_ip.return_value = ryu_dto_mock - sdn_controller_config = MagicMock( - container=MagicMock(docker_gw_bridge_ip="192.168.1.2"), - manager_port=6633, - ) - emulation_env_config = MagicMock( - sdn_controller_config=sdn_controller_config, - name="Test Emulation", - ) - result = SDNControllerManager.get_ryu_status( - emulation_env_config=emulation_env_config, logger=logger - ) + sdn_controller_config = MagicMock(container=MagicMock(docker_gw_bridge_ip="192.168.1.2"), manager_port=6633) + emulation_env_config = MagicMock(sdn_controller_config=sdn_controller_config, name="Test Emulation") + result = SDNControllerManager.get_ryu_status(emulation_env_config=emulation_env_config, logger=logger) assert result == ryu_dto_mock mock_get_ryu_manager.assert_called() mock_get_ryu_status_by_port_and_ip.assert_called() @patch("grpc.insecure_channel") @patch("csle_collector.ryu_manager.query_ryu_manager.get_ryu_status") - def test_get_ryu_status_by_port_and_ip( - self, mock_get_ryu_status, mock_insecure_channel - ) -> None: + def test_get_ryu_status_by_port_and_ip(self, mock_get_ryu_status, mock_insecure_channel) -> None: """ Test method for querying the RyuManager about the status of the Ryu SDN controller @@ -131,23 +95,15 @@ def test_get_ryu_status_by_port_and_ip( ip = "192.168.1.2" port = 6633 result = SDNControllerManager.get_ryu_status_by_port_and_ip(ip, port) - mock_insecure_channel.assert_called_once_with( - f"{ip}:{port}", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + mock_insecure_channel.assert_called_once_with(f"{ip}:{port}", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_get_ryu_status.assert_called_once() - assert isinstance( - mock_get_ryu_status.call_args[0][0], ryu_manager_pb2_grpc.RyuManagerStub - ) + assert isinstance(mock_get_ryu_status.call_args[0][0], ryu_manager_pb2_grpc.RyuManagerStub) assert result == ryu_dto_mock - @patch( - "csle_common.controllers.sdn_controller_manager.SDNControllerManager.start_ryu_manager" - ) + @patch("csle_common.controllers.sdn_controller_manager.SDNControllerManager.start_ryu_manager") @patch("grpc.insecure_channel") @patch("csle_collector.ryu_manager.query_ryu_manager.stop_ryu") - def test_stop_ryu( - self, mock_stop_ryu, mock_insecure_channel, mock_start_manager - ) -> None: + def test_stop_ryu(self, mock_stop_ryu, mock_insecure_channel, mock_start_manager) -> None: """ Test method for requesting the RyuManager to stop the RYU SDN controller @@ -162,36 +118,20 @@ def test_stop_ryu( mock_stop_ryu.return_value = ryu_dto_mock channel_mock = MagicMock() mock_insecure_channel.return_value.__enter__.return_value = channel_mock - sdn_controller_config = MagicMock( - container=MagicMock(docker_gw_bridge_ip="192.168.1.2"), - manager_port=6633, - ) - emulation_env_config = MagicMock( - sdn_controller_config=sdn_controller_config, - name="Test Emulation", - ) + sdn_controller_config = MagicMock(container=MagicMock(docker_gw_bridge_ip="192.168.1.2"), manager_port=6633) + emulation_env_config = MagicMock(sdn_controller_config=sdn_controller_config, name="Test Emulation") ip = "192.168.1.2" port = 6633 - result = SDNControllerManager.stop_ryu( - emulation_env_config=emulation_env_config, logger=logger - ) - mock_insecure_channel.assert_called_once_with( - f"{ip}:{port}", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + result = SDNControllerManager.stop_ryu(emulation_env_config=emulation_env_config, logger=logger) + mock_insecure_channel.assert_called_once_with(f"{ip}:{port}", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_stop_ryu.assert_called_once() - assert isinstance( - mock_stop_ryu.call_args[0][0], ryu_manager_pb2_grpc.RyuManagerStub - ) + assert isinstance(mock_stop_ryu.call_args[0][0], ryu_manager_pb2_grpc.RyuManagerStub) assert result == ryu_dto_mock - @patch( - "csle_common.controllers.sdn_controller_manager.SDNControllerManager.start_ryu_manager" - ) + @patch("csle_common.controllers.sdn_controller_manager.SDNControllerManager.start_ryu_manager") @patch("grpc.insecure_channel") @patch("csle_collector.ryu_manager.query_ryu_manager.start_ryu") - def test_start_ryu( - self, mock_start_ryu, mock_insecure_channel, mock_start_manager - ) -> None: + def test_start_ryu(self, mock_start_ryu, mock_insecure_channel, mock_start_manager) -> None: """ Test method for requesting the RyuManager to start the RYU SDN controller @@ -207,42 +147,26 @@ def test_start_ryu( channel_mock = MagicMock() mock_insecure_channel.return_value.__enter__.return_value = channel_mock sdn_controller_config = MagicMock( - container=MagicMock( - docker_gw_bridge_ip="192.168.1.2", physical_host_ip="192.168.1.1" - ), + container=MagicMock(docker_gw_bridge_ip="192.168.1.2", physical_host_ip="192.168.1.1"), manager_port=6633, controller_port=6653, controller_web_api_port=8080, controller_module_name="controller_module", ) - emulation_env_config = MagicMock( - sdn_controller_config=sdn_controller_config, - name="Test Emulation", - ) + emulation_env_config = MagicMock(sdn_controller_config=sdn_controller_config, name="Test Emulation") ip = "192.168.1.1" - result = SDNControllerManager.start_ryu( - emulation_env_config=emulation_env_config, - physical_server_ip=ip, - logger=logger, - ) - mock_insecure_channel.assert_called_once_with( - "192.168.1.2:6633", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + result = SDNControllerManager.start_ryu(emulation_env_config=emulation_env_config, physical_server_ip=ip, + logger=logger) + mock_insecure_channel.assert_called_once_with("192.168.1.2:6633", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_start_ryu.assert_called_once() - assert isinstance( - mock_start_ryu.call_args[0][0], ryu_manager_pb2_grpc.RyuManagerStub - ) + assert isinstance(mock_start_ryu.call_args[0][0], ryu_manager_pb2_grpc.RyuManagerStub) assert result == ryu_dto_mock - @patch( - "csle_common.controllers.sdn_controller_manager.SDNControllerManager.start_ryu_manager" - ) + @patch("csle_common.controllers.sdn_controller_manager.SDNControllerManager.start_ryu_manager") @patch("grpc.insecure_channel") @patch("csle_collector.ryu_manager.query_ryu_manager.start_ryu_monitor") - def test_start_ryu_monitor( - self, mock_start_ryu_monitor, mock_insecure_channel, mock_start_manager - ) -> None: + def test_start_ryu_monitor(self, mock_start_ryu_monitor, mock_insecure_channel, mock_start_manager) -> None: """ Test method for requesting the RyuManager to start the Ryu monitor @@ -258,44 +182,23 @@ def test_start_ryu_monitor( channel_mock = MagicMock() mock_insecure_channel.return_value.__enter__.return_value = channel_mock sdn_controller_config = MagicMock( - container=MagicMock( - docker_gw_bridge_ip="192.168.1.2", physical_host_ip="192.168.1.1" - ), - manager_port=6633, - time_step_len_seconds=10, - ) - kafka_config = MagicMock( - container=MagicMock(get_ips=MagicMock(return_value=["192.168.1.3"])), - kafka_port=9092, - ) - emulation_env_config = MagicMock( - sdn_controller_config=sdn_controller_config, - kafka_config=kafka_config, - name="Test Emulation", - ) + container=MagicMock(docker_gw_bridge_ip="192.168.1.2", physical_host_ip="192.168.1.1"), + manager_port=6633, time_step_len_seconds=10) + kafka_config = MagicMock(container=MagicMock(get_ips=MagicMock(return_value=["192.168.1.3"])), kafka_port=9092) + emulation_env_config = MagicMock(sdn_controller_config=sdn_controller_config, kafka_config=kafka_config, + name="Test Emulation") ip = "192.168.1.1" - result = SDNControllerManager.start_ryu_monitor( - emulation_env_config=emulation_env_config, - physical_server_ip=ip, - logger=logger, - ) - mock_insecure_channel.assert_called_once_with( - "192.168.1.2:6633", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + result = SDNControllerManager.start_ryu_monitor(emulation_env_config=emulation_env_config, + physical_server_ip=ip, logger=logger) + mock_insecure_channel.assert_called_once_with("192.168.1.2:6633", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_start_ryu_monitor.assert_called_once() - assert isinstance( - mock_start_ryu_monitor.call_args[0][0], ryu_manager_pb2_grpc.RyuManagerStub - ) + assert isinstance(mock_start_ryu_monitor.call_args[0][0], ryu_manager_pb2_grpc.RyuManagerStub) assert result == ryu_dto_mock - @patch( - "csle_common.controllers.sdn_controller_manager.SDNControllerManager.start_ryu_manager" - ) + @patch("csle_common.controllers.sdn_controller_manager.SDNControllerManager.start_ryu_manager") @patch("grpc.insecure_channel") @patch("csle_collector.ryu_manager.query_ryu_manager.stop_ryu_monitor") - def test_stop_ryu_monitor( - self, mock_stop_ryu_monitor, mock_insecure_channel, mock_start_manager - ) -> None: + def test_stop_ryu_monitor(self, mock_stop_ryu_monitor, mock_insecure_channel, mock_start_manager) -> None: """ Test method for requesting the RyuManager to stop the Ryu monitor @@ -311,25 +214,12 @@ def test_stop_ryu_monitor( channel_mock = MagicMock() mock_insecure_channel.return_value.__enter__.return_value = channel_mock sdn_controller_config = MagicMock( - container=MagicMock( - docker_gw_bridge_ip="192.168.1.2", physical_host_ip="192.168.1.1" - ), - manager_port=6633, - ) - emulation_env_config = MagicMock( - sdn_controller_config=sdn_controller_config, - name="Test Emulation", - ) - result = SDNControllerManager.stop_ryu_monitor( - emulation_env_config=emulation_env_config, logger=logger - ) - mock_insecure_channel.assert_called_once_with( - "192.168.1.2:6633", options=constants.GRPC_SERVERS.GRPC_OPTIONS - ) + container=MagicMock(docker_gw_bridge_ip="192.168.1.2", physical_host_ip="192.168.1.1"), manager_port=6633) + emulation_env_config = MagicMock(sdn_controller_config=sdn_controller_config, name="Test Emulation") + result = SDNControllerManager.stop_ryu_monitor(emulation_env_config=emulation_env_config, logger=logger) + mock_insecure_channel.assert_called_once_with("192.168.1.2:6633", options=constants.GRPC_SERVERS.GRPC_OPTIONS) mock_stop_ryu_monitor.assert_called_once() - assert isinstance( - mock_stop_ryu_monitor.call_args[0][0], ryu_manager_pb2_grpc.RyuManagerStub - ) + assert isinstance(mock_stop_ryu_monitor.call_args[0][0], ryu_manager_pb2_grpc.RyuManagerStub) assert result == ryu_dto_mock def test_get_ryu_managers_ips(self) -> None: @@ -360,28 +250,13 @@ def test_get_ryu_managers_ports(self) -> None: result = SDNControllerManager.get_ryu_managers_ports(emulation_env_config) assert result == [6633] - @patch( - "csle_common.controllers.sdn_controller_manager.SDNControllerManager.get_ryu_managers_ips" - ) - @patch( - "csle_common.controllers.sdn_controller_manager.SDNControllerManager.get_ryu_managers_ports" - ) - @patch( - "csle_common.controllers.sdn_controller_manager.SDNControllerManager.get_ryu_status_by_port_and_ip" - ) + @patch("csle_common.controllers.sdn_controller_manager.SDNControllerManager.get_ryu_managers_ips") + @patch("csle_common.controllers.sdn_controller_manager.SDNControllerManager.get_ryu_managers_ports") + @patch("csle_common.controllers.sdn_controller_manager.SDNControllerManager.get_ryu_status_by_port_and_ip") @patch("csle_common.util.emulation_util.EmulationUtil.physical_ip_match") - @patch( - "csle_collector.ryu_manager.ryu_manager_util.RyuManagerUtil.ryu_dto_empty", - return_value=MagicMock(), - ) - def test_get_ryu_managers_info( - self, - mock_ryu_dto_empty, - mock_physical_ip_match, - mock_get_ryu_status_by_port_and_ip, - mock_get_ryu_managers_ports, - mock_get_ryu_managers_ips, - ) -> None: + @patch("csle_collector.ryu_manager.ryu_manager_util.RyuManagerUtil.ryu_dto_empty", return_value=MagicMock()) + def test_get_ryu_managers_info(self, mock_ryu_dto_empty, mock_physical_ip_match, mock_get_ryu_status_by_port_and_ip, + mock_get_ryu_managers_ports, mock_get_ryu_managers_ips) -> None: """ Test method that extracts the information of the Ryu managers for a given emulation @@ -397,14 +272,9 @@ def test_get_ryu_managers_info( sdn_controller_config = MagicMock( container=MagicMock( docker_gw_bridge_ip="192.168.1.2", physical_host_ip="192.168.1.1" - ), - manager_port=6633, - ) - emulation_env_config = MagicMock( - sdn_controller_config=sdn_controller_config, - execution_id="123", - name="Test Emulation", - ) + ), manager_port=6633) + emulation_env_config = MagicMock(sdn_controller_config=sdn_controller_config, execution_id="123", + name="Test Emulation") mock_get_ryu_managers_ips.return_value = ["192.168.1.2"] mock_get_ryu_managers_ports.return_value = [6633] mock_physical_ip_match.return_value = True @@ -413,21 +283,11 @@ def test_get_ryu_managers_info( active_ips = ["192.168.1.2"] physical_server_ip = "192.168.1.1" - result = SDNControllerManager.get_ryu_managers_info( - emulation_env_config, active_ips, logger, physical_server_ip - ) - mock_get_ryu_managers_ips.assert_called_once_with( - emulation_env_config=emulation_env_config - ) - mock_get_ryu_managers_ports.assert_called_once_with( - emulation_env_config=emulation_env_config - ) - mock_physical_ip_match.assert_called_once_with( - emulation_env_config=emulation_env_config, - ip="192.168.1.2", - physical_host_ip="192.168.1.1", - ) - mock_get_ryu_status_by_port_and_ip.assert_called_once_with( - port=6633, ip="192.168.1.2" - ) + result = SDNControllerManager.get_ryu_managers_info(emulation_env_config, active_ips, logger, + physical_server_ip) + mock_get_ryu_managers_ips.assert_called_once_with(emulation_env_config=emulation_env_config) + mock_get_ryu_managers_ports.assert_called_once_with(emulation_env_config=emulation_env_config) + mock_physical_ip_match.assert_called_once_with(emulation_env_config=emulation_env_config, + ip="192.168.1.2", physical_host_ip="192.168.1.1") + mock_get_ryu_status_by_port_and_ip.assert_called_once_with(port=6633, ip="192.168.1.2") assert result diff --git a/simulation-system/libs/csle-common/tests/test_simulation_env_controller.py b/simulation-system/libs/csle-common/tests/test_simulation_env_controller.py index 0f3e004aa..dd2e73d45 100644 --- a/simulation-system/libs/csle-common/tests/test_simulation_env_controller.py +++ b/simulation-system/libs/csle-common/tests/test_simulation_env_controller.py @@ -1,8 +1,6 @@ from unittest.mock import patch, MagicMock from csle_common.dao.simulation_config.simulation_env_config import SimulationEnvConfig -from csle_common.controllers.simulation_env_controller import ( - SimulationEnvController, -) +from csle_common.controllers.simulation_env_controller import SimulationEnvController class TestSimulationEnvControllerSuite: @@ -16,42 +14,33 @@ def test_install_simulation(self, mock_install_simulation) -> None: Test method that installs the simulation configuration in the metastore :param mock_install_simulation: mock_install_simulation - :return: None """ config = MagicMock(spec=SimulationEnvConfig) SimulationEnvController.install_simulation(config) mock_install_simulation.assert_called_once_with(config=config) - @patch( - "csle_common.metastore.metastore_facade.MetastoreFacade.uninstall_simulation" - ) + @patch("csle_common.metastore.metastore_facade.MetastoreFacade.uninstall_simulation") def test_uninstall_simulation(self, mock_uninstall_simulation) -> None: """ Test method that uninstalls a simulation config from the metastore :param mock_uninstall_simulation: mock_uninstall_simulation - :return: None """ config = MagicMock(spec=SimulationEnvConfig) SimulationEnvController.uninstall_simulation(config) mock_uninstall_simulation.assert_called_once_with(config=config) - @patch( - "csle_common.metastore.metastore_facade.MetastoreFacade.save_simulation_image" - ) + @patch("csle_common.metastore.metastore_facade.MetastoreFacade.save_simulation_image") def test_save_simulation_image(self, mock_save_simulation_image) -> None: """ Test method that saves the simulation image :param mock_save_simulation_image: mock_save_simulation_image - :return: None """ img = b"image" simulation = "test_simulation" SimulationEnvController.save_simulation_image(img, simulation) - mock_save_simulation_image.assert_called_once_with( - img=img, simulation_name=simulation - ) + mock_save_simulation_image.assert_called_once_with(img=img, simulation_name=simulation) diff --git a/simulation-system/libs/csle-common/tests/test_users_controller.py b/simulation-system/libs/csle-common/tests/test_users_controller.py index 828b5139e..f7cb24136 100644 --- a/simulation-system/libs/csle-common/tests/test_users_controller.py +++ b/simulation-system/libs/csle-common/tests/test_users_controller.py @@ -10,42 +10,28 @@ class TestUsersControllerSuite: @patch("csle_common.util.emulation_util.EmulationUtil.connect_admin") @patch("csle_common.util.emulation_util.EmulationUtil.disconnect_admin") @patch("csle_common.util.emulation_util.EmulationUtil.execute_ssh_cmd") - def test_create_users( - self, mock_execute_ssh_cmd, mock_disconnect_admin, mock_connect_admin - ) -> None: + def test_create_users(self, mock_execute_ssh_cmd, mock_disconnect_admin, mock_connect_admin) -> None: """ Test method that creates users in an emulation environment according to a specified users-configuration :param mock_execute_ssh_cmd: mock_execute_ssh_cmd :param mock_disconnect_admin: mock_disconnect_admin :param mock_connect_admin: mock_connect_admin - :return: None """ logger = MagicMock() user1 = MagicMock(username="user1", pw="password1", root=False) user2 = MagicMock(username="user2", pw="password2", root=True) users_conf = MagicMock( - physical_host_ip="192.168.1.1", - docker_gw_bridge_ip="192.168.1.2", - users=[user1, user2], - ) - + physical_host_ip="192.168.1.1", docker_gw_bridge_ip="192.168.1.2", users=[user1, user2]) cred1 = MagicMock(username="cred1", pw="password1", root=False) cred2 = MagicMock(username="cred2", pw="password2", root=True) vuln_conf = MagicMock( - physical_host_ip="192.168.1.1", - docker_gw_bridge_ip="192.168.1.3", - credentials=[cred1, cred2], - ) - + physical_host_ip="192.168.1.1", docker_gw_bridge_ip="192.168.1.3", credentials=[cred1, cred2]) emulation_env_config = MagicMock() emulation_env_config.users_config.users_configs = [users_conf] emulation_env_config.vuln_config.node_vulnerability_configs = [vuln_conf] - emulation_env_config.connections = { - "192.168.1.2": MagicMock(), - "192.168.1.3": MagicMock(), - } + emulation_env_config.connections = {"192.168.1.2": MagicMock(), "192.168.1.3": MagicMock()} physical_server_ip = "192.168.1.1" mock_execute_ssh_cmd.side_effect = [ @@ -61,11 +47,7 @@ def test_create_users( ] UsersController.create_users(emulation_env_config, physical_server_ip, logger) - mock_connect_admin.assert_any_call( - emulation_env_config=emulation_env_config, ip="192.168.1.2" - ) - mock_connect_admin.assert_any_call( - emulation_env_config=emulation_env_config, ip="192.168.1.3" - ) + mock_connect_admin.assert_any_call(emulation_env_config=emulation_env_config, ip="192.168.1.2") + mock_connect_admin.assert_any_call(emulation_env_config=emulation_env_config, ip="192.168.1.3") mock_execute_ssh_cmd.assert_called() mock_disconnect_admin.assert_called() diff --git a/simulation-system/libs/csle-common/tests/test_vulnerabilities_controller.py b/simulation-system/libs/csle-common/tests/test_vulnerabilities_controller.py index 6bb9aaa8a..0c664957b 100644 --- a/simulation-system/libs/csle-common/tests/test_vulnerabilities_controller.py +++ b/simulation-system/libs/csle-common/tests/test_vulnerabilities_controller.py @@ -23,28 +23,17 @@ def test_create_vulns(self, mock_execute_ssh_cmd, mock_connect_admin) -> None: logger = MagicMock() credential = MagicMock(username="testuser") vuln = MagicMock( - physical_host_ip="192.168.1.1", - docker_gw_bridge_ip="192.168.1.2", - vuln_type=VulnType.PRIVILEGE_ESCALATION, - cve=constants.EXPLOIT_VULNERABILITES.CVE_2010_0426, - credentials=[credential], - ) + physical_host_ip="192.168.1.1", docker_gw_bridge_ip="192.168.1.2", vuln_type=VulnType.PRIVILEGE_ESCALATION, + cve=constants.EXPLOIT_VULNERABILITES.CVE_2010_0426, credentials=[credential]) emulation_env_config = MagicMock() emulation_env_config.vuln_config.node_vulnerability_configs = [vuln] emulation_env_config.connections = {"192.168.1.2": MagicMock()} - physical_server_ip = "192.168.1.1" - mock_execute_ssh_cmd.side_effect = [ ("", "", 0), # output of cp /etc/sudoers.bak /etc/sudoers ("", "", 0), # output of adding CVE entry to sudoers ("", "", 0), # output of chmod 440 /etc/sudoers ] - - VulnerabilitiesController.create_vulns( - emulation_env_config, physical_server_ip, logger - ) - mock_connect_admin.assert_any_call( - emulation_env_config=emulation_env_config, ip="192.168.1.2" - ) + VulnerabilitiesController.create_vulns(emulation_env_config, physical_server_ip, logger) + mock_connect_admin.assert_any_call(emulation_env_config=emulation_env_config, ip="192.168.1.2") mock_execute_ssh_cmd.assert_called()