Skip to content

Commit

Permalink
Update the path to avoid overriding
Browse files Browse the repository at this point in the history
Signed-off-by: Zelin Hao <zelinhao@amazon.com>
  • Loading branch information
zelinh committed Feb 5, 2025
1 parent 57d84a4 commit 7f47e1d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
14 changes: 9 additions & 5 deletions src/test_workflow/smoke_test/smoke_test_runner_opensearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,30 @@ def __init__(self, args: TestArgs, test_manifest: TestManifest) -> None:
super().__init__(args, test_manifest)
logging.info("Entering Smoke test for OpenSearch Bundle.")

# Below URL is for the pre-release latest. In the future may consider use formal released spec.
# Below URL is for the pre-release latest. In the future may consider use formal released spec when available.
self.spec_url = "https://github.com/opensearch-project/opensearch-api-specification/releases/download/main-latest/opensearch-openapi.yaml"
self.spec_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "smoke_tests_spec", "opensearch-openapi.yaml")
self.download_spec(self.spec_url, self.spec_path)
self.spec_local_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "smoke_tests_spec", "opensearch-openapi-local.yaml")
self.spec_download_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "smoke_tests_spec", "opensearch-openapi.yaml")
self.spec_path = self.download_spec(self.spec_url, self.spec_local_path, self.spec_download_path)
self.spec_ = Spec.from_file_path(self.spec_path)
self.mimetype = {
"Content-Type": "application/json"
}

def download_spec(self, url: str, local_path: str) -> None:
def download_spec(self, url: str, local_path: str, download_path: str) -> str:
try:
response = requests.get(url, timeout=10)
if response.status_code == 200:
with open(local_path, "wb") as file:
with open(download_path, "wb") as file:
file.write(response.content)
logging.info(f"Downloaded latest spec from {url}")
return download_path
else:
logging.info(f"Failed to fetch remote API spec, using local file: {local_path}")
return local_path
except requests.RequestException:
logging.info(f"Could not reach {url}, using local file: {local_path}")
return local_path

def validate_request_swagger(self, request: Any) -> None:
request = RequestsOpenAPIRequest(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ def test_validate_response_swagger_with_invalid_response(self, mock_get: Mock, m
with self.assertRaises(OpenAPIError):
runner.validate_response_swagger(response)

@patch("test_workflow.smoke_test.smoke_test_runner_opensearch.Spec")
@patch("test_workflow.smoke_test.smoke_test_runner.TestRecorder")
@patch("requests.get")
@patch("builtins.open", new_callable=mock_open)
def test_download_spec_success(self, mock_file: Mock, mock_get: Mock, mock_test_recorder: Mock) -> None:
def test_download_spec_success(self, mock_file: Mock, mock_get: Mock, mock_test_recorder: Mock, mock_spec: Mock) -> None:
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.content = "Mock OpenSearch API Spec Yaml content"
Expand All @@ -111,32 +112,40 @@ def test_download_spec_success(self, mock_file: Mock, mock_get: Mock, mock_test_
timeout=10
)

mock_file.assert_any_call(runner.spec_path, "wb")
mock_file.assert_any_call(runner.spec_download_path, "wb")
mock_file().write.assert_called_once_with("Mock OpenSearch API Spec Yaml content")

self.assertTrue(runner.spec_path.endswith("smoke_tests_spec/opensearch-openapi.yaml"))

@patch("test_workflow.smoke_test.smoke_test_runner_opensearch.Spec")
@patch("test_workflow.smoke_test.smoke_test_runner.TestRecorder")
@patch("requests.get")
@patch("builtins.open", new_callable=mock_open)
def test_download_spec_fail_local(self, mock_file: Mock, mock_get: Mock, mock_test_recorder: Mock) -> None:
def test_download_spec_fail_local(self, mock_file: Mock, mock_get: Mock, mock_test_recorder: Mock, mock_spec: Mock) -> None:
# Mock request failure
mock_get.side_effect = requests.RequestException

SmokeTestRunnerOpenSearch(MagicMock(), MagicMock())
runner = SmokeTestRunnerOpenSearch(MagicMock(), MagicMock())

mock_get.assert_called_once()

mock_file().write.assert_not_called()

self.assertTrue(runner.spec_path.endswith("smoke_tests_spec/opensearch-openapi-local.yaml"))

@patch("test_workflow.smoke_test.smoke_test_runner_opensearch.Spec")
@patch("test_workflow.smoke_test.smoke_test_runner.TestRecorder")
@patch("requests.get")
@patch("builtins.open", new_callable=mock_open)
def test_download_spec_https_fail(self, mock_file: Mock, mock_get: Mock, mock_test_recorder: Mock) -> None:
def test_download_spec_https_fail(self, mock_file: Mock, mock_get: Mock, mock_test_recorder: Mock, mock_spec: Mock) -> None:
mock_response = MagicMock()
mock_response.status_code = 404
mock_get.return_value = mock_response

SmokeTestRunnerOpenSearch(MagicMock(), MagicMock())
runner = SmokeTestRunnerOpenSearch(MagicMock(), MagicMock())

mock_get.assert_called_once()

mock_file().write.assert_not_called()

self.assertTrue(runner.spec_path.endswith("smoke_tests_spec/opensearch-openapi-local.yaml"))

0 comments on commit 7f47e1d

Please sign in to comment.