From ca41b1bd73b84c3bb8767500cac4974b1f0cf997 Mon Sep 17 00:00:00 2001 From: Zelin Hao Date: Fri, 14 Feb 2025 16:01:42 -0800 Subject: [PATCH] Add local cluster logs recording to smoke tests framework (#5321) Signed-off-by: Zelin Hao --- .../smoke_test_cluster_opensearch.py | 14 +++++++++++ .../test_smoke_test_cluster_opensearch.py | 25 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/test_workflow/smoke_test/smoke_test_cluster_opensearch.py b/src/test_workflow/smoke_test/smoke_test_cluster_opensearch.py index ef31dd9ceb..c7112e6b82 100644 --- a/src/test_workflow/smoke_test/smoke_test_cluster_opensearch.py +++ b/src/test_workflow/smoke_test/smoke_test_cluster_opensearch.py @@ -97,6 +97,20 @@ def __check_cluster_ready__(self) -> bool: logging.info(f"Cluster check fails: {e}") return False + def __save_local_cluster_logs__(self) -> None: + dest_directory = os.path.join(self.test_recorder.location, "local-cluster-logs") + os.makedirs(dest_directory, exist_ok=True) + logging.info( + f"Recording local cluster logs for at {dest_directory}" + ) + + self.test_recorder._generate_std_files( + self.process_handler.stdout_data, + self.process_handler.stderr_data, + dest_directory, + ) + def __uninstall__(self) -> None: self.process_handler.terminate() + self.__save_local_cluster_logs__() logging.info("Cluster is terminated.") diff --git a/tests/tests_test_workflow/test_smoke_workflow/smoke_test/test_smoke_test_cluster_opensearch.py b/tests/tests_test_workflow/test_smoke_workflow/smoke_test/test_smoke_test_cluster_opensearch.py index baed5fc0ab..62a69ee75f 100644 --- a/tests/tests_test_workflow/test_smoke_workflow/smoke_test/test_smoke_test_cluster_opensearch.py +++ b/tests/tests_test_workflow/test_smoke_workflow/smoke_test/test_smoke_test_cluster_opensearch.py @@ -181,3 +181,28 @@ def test_uninstall(self, mock_process: Mock, mock_test_manifest: Mock, # Verify termination mock_process_handler.terminate.assert_called_once() + + @patch("test_workflow.smoke_test.smoke_test_cluster_opensearch.Distributions.get_distribution") + @patch("test_workflow.smoke_test.smoke_test_cluster_opensearch.BundleManifest.from_urlpath") + @patch("test_workflow.smoke_test.smoke_test_cluster_opensearch.BuildManifest.from_urlpath") + @patch("test_workflow.smoke_test.smoke_test_cluster_opensearch.TestManifest.from_path") + @patch("test_workflow.smoke_test.smoke_test_cluster_opensearch.Process") + def test_save_local_cluster_logs(self, mock_process: Mock, mock_test_manifest: Mock, + mock_build_manifest: Mock, mock_bundle_manifest: Mock, + mock_distribution: Mock) -> None: + args = MagicMock() + test_recorder = MagicMock() + mock_process_handler = mock_process.return_value + + # Simulate process handler outputs + mock_process_handler.stdout_data = "mock_stdout_data" + mock_process_handler.stderr_data = "mock_stderr_data" + + cluster = SmokeTestClusterOpenSearch(args, "/mock/work_dir", test_recorder) + cluster.__save_local_cluster_logs__() + + test_recorder._generate_std_files.assert_called_once_with( + "mock_stdout_data", + "mock_stderr_data", + os.path.join(test_recorder.location, "local-cluster-logs") + )