Skip to content

Commit

Permalink
URL encode test result files (#4574)
Browse files Browse the repository at this point in the history
Signed-off-by: Simeon Widdis <[email protected]>
Signed-off-by: Simeon Widdis <[email protected]>
  • Loading branch information
Swiddis authored Apr 1, 2024
1 parent 6a09aeb commit befb59f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
16 changes: 10 additions & 6 deletions src/test_workflow/test_recorder/test_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import logging
import os
import shutil
import urllib.parse
from typing import Any

import yaml
Expand Down Expand Up @@ -49,9 +50,9 @@ def _create_base_folder_structure(self, component_name: str, component_test_conf
return os.path.realpath(dest_directory)

def _generate_std_files(self, stdout: str, stderr: str, output_path: str) -> None:
with open(os.path.join(output_path, "stdout.txt"), "w", encoding='utf-8') as stdout_file:
with open(os.path.join(output_path, "stdout.txt"), "w", encoding="utf-8") as stdout_file:
stdout_file.write(stdout)
with open(os.path.join(output_path, "stderr.txt"), "w", encoding='utf-8') as stderr_file:
with open(os.path.join(output_path, "stderr.txt"), "w", encoding="utf-8") as stderr_file:
stderr_file.write(stderr)

def _generate_yml(self, test_result_data: TestResultData, output_path: str) -> str:
Expand All @@ -66,14 +67,17 @@ def _generate_yml(self, test_result_data: TestResultData, output_path: str) -> s
"component_name": test_result_data.component_name,
"test_config": test_result_data.component_test_config,
"test_result": "PASS" if (test_result_data.exit_code == 0) else "FAIL",
"test_result_files": test_result_file
"test_result_files": test_result_file,
}
with open(os.path.join(output_path, "%s.yml" % test_result_data.component_name), "w", encoding='utf-8') as file:
with open(os.path.join(output_path, "%s.yml" % test_result_data.component_name), "w", encoding="utf-8") as file:
yaml.dump(outcome, file)
return os.path.realpath("%s.yml" % test_result_data.component_name)

def _update_absolute_file_paths(self, files: list, base_path: str, relative_path: str) -> list:
return [os.path.join(base_path, relative_path, file) for file in files]
if base_path.startswith("https://"):
return [f"{base_path}/{relative_path}/{urllib.parse.quote_plus(file)}" for file in files]
else:
return [os.path.join(base_path, relative_path, file) for file in files]

# get a list of files within directory with relative paths.
def _get_list_files(self, dir: str) -> list:
Expand Down Expand Up @@ -133,7 +137,7 @@ def save_test_result_data(self, test_result_data: TestResultData) -> None:


class TestResultsLogs(LogRecorder):
__test__ = False # type:ignore
__test__ = False # type:ignore
parent_class: TestRecorder

def __init__(self, parent_class: TestRecorder) -> None:
Expand Down
24 changes: 22 additions & 2 deletions tests/tests_test_workflow/test_recorder/test_test_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,28 @@ def test_update_absolute_file_paths(self, mock_local_cluster_logs: Mock, mock_re
"working-directory",
"https://ci.opensearch.org/ci/dbc/integ-test/"
)
file_path = test_recorder._update_absolute_file_paths(["file1", "file2"], "working-directory", "sub-directory")
self.assertEqual(file_path, [os.path.join("working-directory", "sub-directory", "file1"), os.path.join("working-directory", "sub-directory", "file2")])
file_path = test_recorder._update_absolute_file_paths(
["file1", "file2 with spaces"],
"working-directory",
"sub-directory"
)
self.assertEqual(file_path, [
os.path.join("working-directory", "sub-directory", "file1"),
os.path.join("working-directory", "sub-directory", "file2 with spaces")
])

@patch("test_workflow.test_recorder.test_recorder.TestResultsLogs")
@patch("test_workflow.test_recorder.test_recorder.RemoteClusterLogs")
@patch("test_workflow.test_recorder.test_recorder.LocalClusterLogs")
def test_update_absolute_file_paths_escaping(self, mock_local_cluster_logs: Mock, mock_remote_cluster_logs: Mock, mock_test_results_logs: Mock, *mock: Any) -> None:
test_recorder = TestRecorder(
"1234",
"integ-test",
"working-directory",
"https://ci.opensearch.org/ci/dbc/integ-test/"
)
file_path = test_recorder._update_absolute_file_paths(["A long test case name"], "https://working-directory", "sub-directory")
self.assertEqual(file_path, ["https://working-directory/sub-directory/A+long+test+case+name"])

@patch("os.walk")
@patch("test_workflow.test_recorder.test_recorder.TestResultsLogs")
Expand Down

0 comments on commit befb59f

Please sign in to comment.