Skip to content

Commit

Permalink
fix: catch not-ok http response codes for sam init (#7678)
Browse files Browse the repository at this point in the history
* fix: catch not-ok http response codes for sam init

* add unit tests

* fix formatting

* fix formatting
  • Loading branch information
mndeveci authored Nov 13, 2024
1 parent da1d87a commit 7b723b5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
23 changes: 14 additions & 9 deletions samcli/commands/init/init_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,21 @@ def _get_manifest(self):
"""
try:
response = requests.get(MANIFEST_URL, timeout=10)
body = response.text
# if the commit is not exist then MANIFEST_URL will be invalid, fall back to use manifest in latest commit
if response.status_code == Status.NOT_FOUND.value:
LOG.warning(
"Request to MANIFEST_URL: %s failed, the commit hash in this url maybe invalid, "
"Using manifest.json in the latest commit instead.",
MANIFEST_URL,
)
if not response.ok:
# if the commit is not exist then MANIFEST_URL will be invalid,
# fall back to use manifest in latest commit
if response.status_code == Status.NOT_FOUND.value:
LOG.warning(
"Request to MANIFEST_URL: %s failed, the commit hash in this url maybe invalid, "
"Using manifest.json in the latest commit instead.",
MANIFEST_URL,
)
else:
LOG.debug(
"Request to MANIFEST_URL: %s failed, with %s status code", MANIFEST_URL, response.status_code
)
raise ManifestNotFoundException()

body = response.text
except (requests.Timeout, requests.ConnectionError, ManifestNotFoundException):
LOG.debug("Request to get Manifest failed, attempting to clone the repository")
self.clone_templates_repo()
Expand Down
20 changes: 19 additions & 1 deletion tests/unit/commands/init/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pathlib import Path
from typing import Dict, Any
from unittest import TestCase
from unittest.mock import patch, ANY
from unittest.mock import patch, ANY, Mock

import botocore.exceptions
import click
Expand Down Expand Up @@ -2204,6 +2204,24 @@ def test_must_return_runtime_from_base_image_name(self):
runtime = get_runtime(IMAGE, base_image)
self.assertEqual(runtime, expected_runtime[index])

@patch("samcli.commands.init.init_templates.requests")
@patch.object(InitTemplates, "__init__", MockInitTemplates.__init__)
def test_must_fallback_for_non_ok_http_response(self, requests_mock):
requests_mock.get.return_value = Mock(ok=False)
requests_mock.Timeout = requests.Timeout
requests_mock.ConnectionError = requests.ConnectionError

template = InitTemplates()
with mock.patch.object(
template, "clone_templates_repo", wraps=template.clone_templates_repo
) as mocked_clone_templates_repo:
with mock.patch.object(
template, "get_manifest_path", wraps=template.get_manifest_path
) as mocked_get_manifest_path:
template.get_preprocessed_manifest()
mocked_clone_templates_repo.assert_called_once()
mocked_get_manifest_path.assert_called_once()

@patch("samcli.commands.init.init_templates.InitTemplates._get_manifest")
@patch.object(InitTemplates, "__init__", MockInitTemplates.__init__)
def test_must_process_manifest(self, _get_manifest_mock):
Expand Down

0 comments on commit 7b723b5

Please sign in to comment.