From 7835bb80b93b90f1cc9128cf0f58cfaf156cf5d6 Mon Sep 17 00:00:00 2001 From: Nabeel Aslam <17185164+nabeleon94@users.noreply.github.com> Date: Fri, 30 Oct 2026 10:12:59 -0400 Subject: [PATCH] [Container app] `az containerapp up`: Fix existing app being re-created when the existence check fails containerapp_up_logic() swallowed every exception from the GET that decides between update and create, so a throttled/transient/network failure made an existing app look missing and up sent a full create PUT, resetting identity, ingress, secrets and scale. Only fall through to create on ResourceNotFound; re-raise anything else. Fixes #34193 --- .../command_modules/containerapp/custom.py | 7 ++- .../latest/test_containerapp_up_logic_unit.py | 63 +++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 src/azure-cli/azure/cli/command_modules/containerapp/tests/latest/test_containerapp_up_logic_unit.py diff --git a/src/azure-cli/azure/cli/command_modules/containerapp/custom.py b/src/azure-cli/azure/cli/command_modules/containerapp/custom.py index a3b97b0e9da..ff116149db6 100644 --- a/src/azure-cli/azure/cli/command_modules/containerapp/custom.py +++ b/src/azure-cli/azure/cli/command_modules/containerapp/custom.py @@ -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, diff --git a/src/azure-cli/azure/cli/command_modules/containerapp/tests/latest/test_containerapp_up_logic_unit.py b/src/azure-cli/azure/cli/command_modules/containerapp/tests/latest/test_containerapp_up_logic_unit.py new file mode 100644 index 00000000000..4ac6bab3739 --- /dev/null +++ b/src/azure-cli/azure/cli/command_modules/containerapp/tests/latest/test_containerapp_up_logic_unit.py @@ -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()