From c0921d03718a79487f78ac1150bbfaa4a3296971 Mon Sep 17 00:00:00 2001 From: Yuhu-kth Date: Tue, 16 Jul 2024 21:03:02 +0200 Subject: [PATCH 1/3] add test_grpc_util --- .../libs/csle-common/tests/test_grpc_util.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 simulation-system/libs/csle-common/tests/test_grpc_util.py diff --git a/simulation-system/libs/csle-common/tests/test_grpc_util.py b/simulation-system/libs/csle-common/tests/test_grpc_util.py new file mode 100644 index 000000000..645f56fb2 --- /dev/null +++ b/simulation-system/libs/csle-common/tests/test_grpc_util.py @@ -0,0 +1,40 @@ +import grpc +from unittest.mock import patch, MagicMock +from csle_common.util.grpc_util import GrpcUtil + + +class TestGrpcUtilSuite: + """ + Test suite for grpc util + """ + + @patch("grpc.channel_ready_future") + def test_grpc_server_on(self, mock_channel_ready_future) -> None: + """ + Test utility function to test if a given gRPC channel is working or not + + :param mock_channel_ready_future: mock_channel_ready_future + + :return: None + """ + mock_future = MagicMock() + mock_channel_ready_future.return_value = mock_future + result = GrpcUtil.grpc_server_on(mock_channel_ready_future) + mock_future.result.assert_called() + assert result + + @patch("grpc.channel_ready_future") + def test_grpc_server_on_timeout(self, mock_channel_ready_future) -> None: + """ + Test utility function to test if a given gRPC channel is working or not + + :param mock_channel_ready_future: mock_channel_ready_future + + :return: None + """ + mock_future = MagicMock() + mock_future.result.side_effect = grpc.FutureTimeoutError() + mock_channel_ready_future.return_value = mock_future + result = GrpcUtil.grpc_server_on(mock_channel_ready_future) + mock_future.result.assert_called() + assert not result From d1190474281e65fe35e1f6bac2d8108976684efb Mon Sep 17 00:00:00 2001 From: Yuhu-kth Date: Tue, 16 Jul 2024 22:41:41 +0200 Subject: [PATCH 2/3] add test_import_util --- .../csle-common/tests/test_import_util.py | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 simulation-system/libs/csle-common/tests/test_import_util.py diff --git a/simulation-system/libs/csle-common/tests/test_import_util.py b/simulation-system/libs/csle-common/tests/test_import_util.py new file mode 100644 index 000000000..2cb3374a3 --- /dev/null +++ b/simulation-system/libs/csle-common/tests/test_import_util.py @@ -0,0 +1,64 @@ +from unittest.mock import patch, MagicMock +from csle_common.util.import_util import ImportUtil + + +class TestImportUtilSuite: + """ + Test suite for import_util + """ + + @patch("os.path.exists") + @patch("csle_common.dao.system_identification.emulation_statistics.EmulationStatistics.from_json_file") + @patch("csle_common.metastore.metastore_facade.MetastoreFacade.save_emulation_statistic") + def test_import_emulation_statistics_from_disk_json( + self, mock_save_emulation_statistic, mock_from_json_file, mock_path_exists + ) -> None: + """ + Test the method that imports emulation statistics from disk to the metastore + + :param mock_save_emulation_statistic: mock_save_emulation_statistic + :param mock_from_json_file: mock_from_json_file + :param mock_path_exists: mock_path_exists + + :return: None + """ + mock_path_exists.return_value = True + mock_statistics = MagicMock() + mock_from_json_file.return_value = mock_statistics + input_file = "file.json" + emulation_name = "test_emulation" + ImportUtil.import_emulation_statistics_from_disk_json(input_file=input_file, emulation_name=emulation_name) + + mock_path_exists.assert_called() + mock_from_json_file.assert_called_once_with(input_file) + assert mock_statistics.emulation_name == emulation_name + mock_save_emulation_statistic.assert_called() + + @patch("os.path.exists") + @patch("csle_common.dao.emulation_config.emulation_trace.EmulationTrace.load_traces_from_disk") + @patch("csle_common.metastore.metastore_facade.MetastoreFacade.save_emulation_trace") + def test_import_emulation_traces_from_disk_json( + self, mock_save_emulation_trace, mock_load_traces_from_disk, mock_path_exists + ) -> None: + """ + Test the method that imports emulation traces from disk to the metastore + + :param mock_save_emulation_trace: mock_save_emulation_trac + :param mock_load_traces_from_disk: mock_load_traces_from_disk + :param mock_path_exists: mock_path_exists + + :return: None + """ + mock_path_exists.return_value = True + mock_trace_1 = MagicMock() + mock_trace_2 = MagicMock() + mock_load_traces_from_disk.return_value = [mock_trace_1, mock_trace_2] + input_file = "file.json" + emulation_name = "test_emulation" + ImportUtil.import_emulation_traces_from_disk_json(input_file=input_file, emulation_name=emulation_name) + + mock_path_exists.assert_called() + mock_load_traces_from_disk.assert_called() + assert mock_trace_1.emulation_name == emulation_name + assert mock_trace_2.emulation_name == emulation_name + assert mock_save_emulation_trace.call_count == 2 From 971d097cbfd301a695ffcb1599f227c2526b899e Mon Sep 17 00:00:00 2001 From: Yuhu-kth Date: Wed, 17 Jul 2024 11:08:12 +0200 Subject: [PATCH 3/3] add test_import_util --- simulation-system/libs/csle-common/tests/test_import_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simulation-system/libs/csle-common/tests/test_import_util.py b/simulation-system/libs/csle-common/tests/test_import_util.py index 2cb3374a3..0cb158cf9 100644 --- a/simulation-system/libs/csle-common/tests/test_import_util.py +++ b/simulation-system/libs/csle-common/tests/test_import_util.py @@ -6,7 +6,7 @@ class TestImportUtilSuite: """ Test suite for import_util """ - + @patch("os.path.exists") @patch("csle_common.dao.system_identification.emulation_statistics.EmulationStatistics.from_json_file") @patch("csle_common.metastore.metastore_facade.MetastoreFacade.save_emulation_statistic")