Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3749,8 +3749,11 @@ def containerapp_up_logic(cmd, resource_group_name, name, managed_env, image, en
containerapp_def = None
try:
containerapp_def = ContainerAppClient.show(cmd=cmd, resource_group_name=resource_group_name, name=name)
except:
pass
except Exception as e: # pylint: disable=broad-except
# Only fall through to create when the app does not exist. Any other failure (throttling,
# transient or network errors) must not be treated as "missing", because the create PUT
# would replace the existing app's configuration (identity, ingress, secrets, ...).
handle_non_resource_not_found_exception(e)

if containerapp_def:
return update_containerapp_logic(cmd=cmd, name=name, resource_group_name=resource_group_name, image=image, replace_env_vars=env_vars, ingress=ingress, target_port=target_port,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import json
import unittest
from unittest import mock

from azure.cli.core.azclierror import CLIInternalError, HTTPError

from azure.cli.command_modules.containerapp.custom import containerapp_up_logic

CUSTOM = "azure.cli.command_modules.containerapp.custom"


def _http_error(status_code, code, message):
response = mock.MagicMock()
response.status_code = status_code
body = json.dumps({"error": {"code": code, "message": message}})
return HTTPError("Error({})".format(body), response)


class ContainerappUpLogicTest(unittest.TestCase):
"""`az containerapp up` must only create the app when the GET says it does not exist."""

def _run(self, show_side_effect):
with mock.patch(CUSTOM + ".ContainerAppClient.show", side_effect=show_side_effect), \
mock.patch(CUSTOM + ".create_containerapp", return_value="created") as create, \
mock.patch(CUSTOM + ".update_containerapp_logic", return_value="updated") as update:
result = containerapp_up_logic(
cmd=mock.MagicMock(), resource_group_name="rg", name="app", managed_env="env", image="img",
env_vars=None, ingress=None, target_port=None, registry_server=None, registry_user=None,
workload_profile_name=None, registry_pass=None)
return result, create, update

def test_existing_app_is_updated(self):
result, create, update = self._run([{"name": "app", "properties": {}}])
self.assertEqual(result, "updated")
create.assert_not_called()
update.assert_called_once()

def test_missing_app_is_created(self):
not_found = _http_error(404, "ResourceNotFound",
"The Resource 'Microsoft.App/containerApps/app' under resource group 'rg' was not found.")
result, create, update = self._run(not_found)
self.assertEqual(result, "created")
create.assert_called_once()
update.assert_not_called()

def test_failed_get_does_not_recreate_existing_app(self):
throttled = _http_error(429, "TooManyRequests", "Rate limit exceeded.")
with self.assertRaises(CLIInternalError):
self._run(throttled)

def test_transport_error_does_not_recreate_existing_app(self):
import requests
with self.assertRaises(requests.exceptions.ConnectionError):
self._run(requests.exceptions.ConnectionError("Connection reset by peer"))


if __name__ == '__main__':
unittest.main()