From 80f4ba4e5172387126d77b53946b0d46e0c20259 Mon Sep 17 00:00:00 2001 From: mabelegba Date: Mon, 17 Aug 2026 15:45:00 -0400 Subject: [PATCH 1/6] [ACR] Warn before overwriting an existing cache rule Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: e82f65c8-d0e4-4013-8ba8-5f3e0e19f031 --- .../azure/cli/command_modules/acr/cache.py | 4 ++++ .../tests/latest/test_acr_commands_mock.py | 21 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/azure-cli/azure/cli/command_modules/acr/cache.py b/src/azure-cli/azure/cli/command_modules/acr/cache.py index a2ab97c6cee..d1e85733b9c 100644 --- a/src/azure-cli/azure/cli/command_modules/acr/cache.py +++ b/src/azure-cli/azure/cli/command_modules/acr/cache.py @@ -8,6 +8,7 @@ from azure.cli.core.azclierror import InvalidArgumentValueError from azure.cli.core.commands.client_factory import get_subscription_id from azure.core.serialization import NULL as AzureCoreNull +from knack.log import get_logger from azure.mgmt.containerregistry.models import ( CacheRule, CacheRuleProperties, @@ -17,6 +18,8 @@ UserIdentityProperties ) +logger = get_logger(__name__) + def acr_cache_show(cmd, client, @@ -66,6 +69,7 @@ def acr_cache_create(cmd, identity=None): rg = get_resource_group_name_by_registry_name(cmd.cli_ctx, registry_name, resource_group_name) + logger.warning("If cache rule '%s' already exists, it will be overwritten.", name) # Handle credential set if cred_set: diff --git a/src/azure-cli/azure/cli/command_modules/acr/tests/latest/test_acr_commands_mock.py b/src/azure-cli/azure/cli/command_modules/acr/tests/latest/test_acr_commands_mock.py index 95c8e3c34db..4c6fa0bfff7 100644 --- a/src/azure-cli/azure/cli/command_modules/acr/tests/latest/test_acr_commands_mock.py +++ b/src/azure-cli/azure/cli/command_modules/acr/tests/latest/test_acr_commands_mock.py @@ -51,6 +51,7 @@ ) from azure.cli.command_modules.acr._docker_utils import ResourceNotFound from azure.cli.command_modules.acr._constants import ACR_AUDIENCE_RESOURCE_NAME +from azure.cli.command_modules.acr.cache import acr_cache_create from azure.cli.core.mock import DummyCli @@ -64,6 +65,26 @@ class AcrMockCommandsTests(unittest.TestCase): + @mock.patch('azure.cli.command_modules.acr.cache.logger.warning') + @mock.patch('azure.cli.command_modules.acr.cache.get_resource_group_name_by_registry_name') + def test_cache_create_warns_that_existing_rule_is_overwritten( + self, mock_get_resource_group, mock_warning): + cmd = self._setup_cmd() + client = mock.MagicMock() + mock_get_resource_group.return_value = 'testresourcegroup' + + acr_cache_create( + cmd=cmd, + client=client, + registry_name='testregistry', + name='testcache', + source_repo='mcr.microsoft.com/mcr/hello-world', + target_repo='hello-world') + + mock_warning.assert_called_once_with( + "If cache rule '%s' already exists, it will be overwritten.", + 'testcache') + @mock.patch('azure.cli.command_modules.acr.repository.get_access_credentials', autospec=True) @mock.patch('requests.request', autospec=True) def test_repository_list(self, mock_requests_get, mock_get_access_credentials): From 17a1a2e787a084be184a80a991fe184dc947f021 Mon Sep 17 00:00:00 2001 From: mabelegba Date: Mon, 17 Aug 2026 16:27:51 -0400 Subject: [PATCH 2/6] add warning only when overwriting a cache rule --- src/azure-cli/azure/cli/command_modules/acr/cache.py | 11 ++++++++++- .../acr/tests/latest/test_acr_commands_mock.py | 7 ++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/acr/cache.py b/src/azure-cli/azure/cli/command_modules/acr/cache.py index d1e85733b9c..b2b284bb2d4 100644 --- a/src/azure-cli/azure/cli/command_modules/acr/cache.py +++ b/src/azure-cli/azure/cli/command_modules/acr/cache.py @@ -7,6 +7,7 @@ from ._utils import get_resource_group_name_by_registry_name from azure.cli.core.azclierror import InvalidArgumentValueError from azure.cli.core.commands.client_factory import get_subscription_id +from azure.core.exceptions import ResourceNotFoundError from azure.core.serialization import NULL as AzureCoreNull from knack.log import get_logger from azure.mgmt.containerregistry.models import ( @@ -69,7 +70,15 @@ def acr_cache_create(cmd, identity=None): rg = get_resource_group_name_by_registry_name(cmd.cli_ctx, registry_name, resource_group_name) - logger.warning("If cache rule '%s' already exists, it will be overwritten.", name) + try: + client.get(resource_group_name=rg, + registry_name=registry_name, + cache_rule_name=name) + logger.warning( + "A cache rule named '%s' already exists. The existing cache rule will be overwritten.", + name) + except ResourceNotFoundError: + pass # Handle credential set if cred_set: diff --git a/src/azure-cli/azure/cli/command_modules/acr/tests/latest/test_acr_commands_mock.py b/src/azure-cli/azure/cli/command_modules/acr/tests/latest/test_acr_commands_mock.py index 4c6fa0bfff7..785d93a73de 100644 --- a/src/azure-cli/azure/cli/command_modules/acr/tests/latest/test_acr_commands_mock.py +++ b/src/azure-cli/azure/cli/command_modules/acr/tests/latest/test_acr_commands_mock.py @@ -53,6 +53,7 @@ from azure.cli.command_modules.acr._constants import ACR_AUDIENCE_RESOURCE_NAME from azure.cli.command_modules.acr.cache import acr_cache_create from azure.cli.core.mock import DummyCli +from azure.core.exceptions import ResourceNotFoundError TEST_TENANT = 'testtenant' @@ -81,8 +82,12 @@ def test_cache_create_warns_that_existing_rule_is_overwritten( source_repo='mcr.microsoft.com/mcr/hello-world', target_repo='hello-world') + client.get.assert_called_once_with( + resource_group_name='testresourcegroup', + registry_name='testregistry', + cache_rule_name='testcache') mock_warning.assert_called_once_with( - "If cache rule '%s' already exists, it will be overwritten.", + "A cache rule named '%s' already exists. The existing cache rule will be overwritten.", 'testcache') @mock.patch('azure.cli.command_modules.acr.repository.get_access_credentials', autospec=True) From 20c97ceabde666c10088babc62f016fa37e88f5a Mon Sep 17 00:00:00 2001 From: mabelegba Date: Mon, 17 Aug 2026 23:38:17 -0400 Subject: [PATCH 3/6] Group ACR cache imports --- src/azure-cli/azure/cli/command_modules/acr/cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/azure-cli/azure/cli/command_modules/acr/cache.py b/src/azure-cli/azure/cli/command_modules/acr/cache.py index b2b284bb2d4..8ff67fc0ea2 100644 --- a/src/azure-cli/azure/cli/command_modules/acr/cache.py +++ b/src/azure-cli/azure/cli/command_modules/acr/cache.py @@ -9,7 +9,6 @@ from azure.cli.core.commands.client_factory import get_subscription_id from azure.core.exceptions import ResourceNotFoundError from azure.core.serialization import NULL as AzureCoreNull -from knack.log import get_logger from azure.mgmt.containerregistry.models import ( CacheRule, CacheRuleProperties, @@ -18,6 +17,7 @@ IdentityProperties, UserIdentityProperties ) +from knack.log import get_logger logger = get_logger(__name__) From 27e4aaa3914cd368bc429b8a94e07e5eb1ca6279 Mon Sep 17 00:00:00 2001 From: mabelegba Date: Tue, 18 Aug 2026 10:45:56 -0400 Subject: [PATCH 4/6] [ACR] Update cache rule test recordings Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: e82f65c8-d0e4-4013-8ba8-5f3e0e19f031 --- .../latest/recordings/test_acr_cache.yaml | 27 +++++++++++++++++++ .../test_acr_cache_managed_identity.yaml | 27 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/azure-cli/azure/cli/command_modules/acr/tests/latest/recordings/test_acr_cache.yaml b/src/azure-cli/azure/cli/command_modules/acr/tests/latest/recordings/test_acr_cache.yaml index d6428f672e6..21fa3eab900 100644 --- a/src/azure-cli/azure/cli/command_modules/acr/tests/latest/recordings/test_acr_cache.yaml +++ b/src/azure-cli/azure/cli/command_modules/acr/tests/latest/recordings/test_acr_cache.yaml @@ -770,6 +770,33 @@ interactions: status: code: 200 message: OK +- request: + body: null + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + CommandName: + - acr cache create + Connection: + - keep-alive + ParameterSetName: + - -n -r -s -t -c + User-Agent: + - AZURECLI/2.85.0 azsdk-python-core/1.39.0 Python/3.13.12 (Windows-11-10.0.26200-SP0) + method: GET + uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.ContainerRegistry/registries/clireg000002/cacheRules/test1?api-version=2026-03-01-preview + response: + body: + string: '{"error":{"code":"ResourceNotFound","message":"The cache rule test1 could + not be found."},"status":"Failed"}' + headers: + content-type: + - application/json; charset=utf-8 + status: + code: 404 + message: Not Found - request: body: '{"properties": {"sourceRepository": "mcr.microsoft.com/mcr/hello-world", "targetRepository": "hello-world", "credentialSetResourceId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.ContainerRegistry/registries/clireg000002/credentialSets/test1"}}' diff --git a/src/azure-cli/azure/cli/command_modules/acr/tests/latest/recordings/test_acr_cache_managed_identity.yaml b/src/azure-cli/azure/cli/command_modules/acr/tests/latest/recordings/test_acr_cache_managed_identity.yaml index 324525fa9aa..5a5597779c4 100644 --- a/src/azure-cli/azure/cli/command_modules/acr/tests/latest/recordings/test_acr_cache_managed_identity.yaml +++ b/src/azure-cli/azure/cli/command_modules/acr/tests/latest/recordings/test_acr_cache_managed_identity.yaml @@ -306,6 +306,33 @@ interactions: status: code: 200 message: OK +- request: + body: null + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + CommandName: + - acr cache create + Connection: + - keep-alive + ParameterSetName: + - -n -r -s -t --identity + User-Agent: + - AZURECLI/2.84.0 azsdk-python-core/1.39.0 Python/3.11.9 (Windows-10-10.0.26200-SP0) + method: GET + uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.ContainerRegistry/registries/clireg000002/cacheRules/test-mi?api-version=2026-03-01-preview + response: + body: + string: '{"error":{"code":"ResourceNotFound","message":"The cache rule test-mi + could not be found."},"status":"Failed"}' + headers: + content-type: + - application/json; charset=utf-8 + status: + code: 404 + message: Not Found - request: body: '{"properties": {"sourceRepository": "upstreamregistry.azurecr.io/hello-world", "targetRepository": "hello-world-mi", "credentialSetResourceId": null}, "identity": From 87ece07fc177829001129895987b0cc56fa27d4c Mon Sep 17 00:00:00 2001 From: mabelegba Date: Tue, 18 Aug 2026 11:29:46 -0400 Subject: [PATCH 5/6] [ACR] Avoid cache rule read preflight Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: e82f65c8-d0e4-4013-8ba8-5f3e0e19f031 --- .../azure/cli/command_modules/acr/cache.py | 11 +------- .../latest/recordings/test_acr_cache.yaml | 27 ------------------- .../test_acr_cache_managed_identity.yaml | 27 ------------------- .../tests/latest/test_acr_commands_mock.py | 6 +---- 4 files changed, 2 insertions(+), 69 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/acr/cache.py b/src/azure-cli/azure/cli/command_modules/acr/cache.py index 8ff67fc0ea2..26aa7897c1b 100644 --- a/src/azure-cli/azure/cli/command_modules/acr/cache.py +++ b/src/azure-cli/azure/cli/command_modules/acr/cache.py @@ -7,7 +7,6 @@ from ._utils import get_resource_group_name_by_registry_name from azure.cli.core.azclierror import InvalidArgumentValueError from azure.cli.core.commands.client_factory import get_subscription_id -from azure.core.exceptions import ResourceNotFoundError from azure.core.serialization import NULL as AzureCoreNull from azure.mgmt.containerregistry.models import ( CacheRule, @@ -70,15 +69,7 @@ def acr_cache_create(cmd, identity=None): rg = get_resource_group_name_by_registry_name(cmd.cli_ctx, registry_name, resource_group_name) - try: - client.get(resource_group_name=rg, - registry_name=registry_name, - cache_rule_name=name) - logger.warning( - "A cache rule named '%s' already exists. The existing cache rule will be overwritten.", - name) - except ResourceNotFoundError: - pass + logger.warning("If cache rule '%s' already exists, it will be overwritten.", name) # Handle credential set if cred_set: diff --git a/src/azure-cli/azure/cli/command_modules/acr/tests/latest/recordings/test_acr_cache.yaml b/src/azure-cli/azure/cli/command_modules/acr/tests/latest/recordings/test_acr_cache.yaml index 21fa3eab900..d6428f672e6 100644 --- a/src/azure-cli/azure/cli/command_modules/acr/tests/latest/recordings/test_acr_cache.yaml +++ b/src/azure-cli/azure/cli/command_modules/acr/tests/latest/recordings/test_acr_cache.yaml @@ -770,33 +770,6 @@ interactions: status: code: 200 message: OK -- request: - body: null - headers: - Accept: - - application/json - Accept-Encoding: - - gzip, deflate - CommandName: - - acr cache create - Connection: - - keep-alive - ParameterSetName: - - -n -r -s -t -c - User-Agent: - - AZURECLI/2.85.0 azsdk-python-core/1.39.0 Python/3.13.12 (Windows-11-10.0.26200-SP0) - method: GET - uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.ContainerRegistry/registries/clireg000002/cacheRules/test1?api-version=2026-03-01-preview - response: - body: - string: '{"error":{"code":"ResourceNotFound","message":"The cache rule test1 could - not be found."},"status":"Failed"}' - headers: - content-type: - - application/json; charset=utf-8 - status: - code: 404 - message: Not Found - request: body: '{"properties": {"sourceRepository": "mcr.microsoft.com/mcr/hello-world", "targetRepository": "hello-world", "credentialSetResourceId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.ContainerRegistry/registries/clireg000002/credentialSets/test1"}}' diff --git a/src/azure-cli/azure/cli/command_modules/acr/tests/latest/recordings/test_acr_cache_managed_identity.yaml b/src/azure-cli/azure/cli/command_modules/acr/tests/latest/recordings/test_acr_cache_managed_identity.yaml index 5a5597779c4..324525fa9aa 100644 --- a/src/azure-cli/azure/cli/command_modules/acr/tests/latest/recordings/test_acr_cache_managed_identity.yaml +++ b/src/azure-cli/azure/cli/command_modules/acr/tests/latest/recordings/test_acr_cache_managed_identity.yaml @@ -306,33 +306,6 @@ interactions: status: code: 200 message: OK -- request: - body: null - headers: - Accept: - - application/json - Accept-Encoding: - - gzip, deflate - CommandName: - - acr cache create - Connection: - - keep-alive - ParameterSetName: - - -n -r -s -t --identity - User-Agent: - - AZURECLI/2.84.0 azsdk-python-core/1.39.0 Python/3.11.9 (Windows-10-10.0.26200-SP0) - method: GET - uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/clitest.rg000001/providers/Microsoft.ContainerRegistry/registries/clireg000002/cacheRules/test-mi?api-version=2026-03-01-preview - response: - body: - string: '{"error":{"code":"ResourceNotFound","message":"The cache rule test-mi - could not be found."},"status":"Failed"}' - headers: - content-type: - - application/json; charset=utf-8 - status: - code: 404 - message: Not Found - request: body: '{"properties": {"sourceRepository": "upstreamregistry.azurecr.io/hello-world", "targetRepository": "hello-world-mi", "credentialSetResourceId": null}, "identity": diff --git a/src/azure-cli/azure/cli/command_modules/acr/tests/latest/test_acr_commands_mock.py b/src/azure-cli/azure/cli/command_modules/acr/tests/latest/test_acr_commands_mock.py index 785d93a73de..77009465a60 100644 --- a/src/azure-cli/azure/cli/command_modules/acr/tests/latest/test_acr_commands_mock.py +++ b/src/azure-cli/azure/cli/command_modules/acr/tests/latest/test_acr_commands_mock.py @@ -82,12 +82,8 @@ def test_cache_create_warns_that_existing_rule_is_overwritten( source_repo='mcr.microsoft.com/mcr/hello-world', target_repo='hello-world') - client.get.assert_called_once_with( - resource_group_name='testresourcegroup', - registry_name='testregistry', - cache_rule_name='testcache') mock_warning.assert_called_once_with( - "A cache rule named '%s' already exists. The existing cache rule will be overwritten.", + "If cache rule '%s' already exists, it will be overwritten.", 'testcache') @mock.patch('azure.cli.command_modules.acr.repository.get_access_credentials', autospec=True) From f8eec710e9a4168ce05418d55e946e4df343f87e Mon Sep 17 00:00:00 2001 From: mabelegba Date: Tue, 18 Aug 2026 11:52:08 -0400 Subject: [PATCH 6/6] remove unused import --- .../command_modules/acr/tests/latest/test_acr_commands_mock.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/azure-cli/azure/cli/command_modules/acr/tests/latest/test_acr_commands_mock.py b/src/azure-cli/azure/cli/command_modules/acr/tests/latest/test_acr_commands_mock.py index 77009465a60..4c6fa0bfff7 100644 --- a/src/azure-cli/azure/cli/command_modules/acr/tests/latest/test_acr_commands_mock.py +++ b/src/azure-cli/azure/cli/command_modules/acr/tests/latest/test_acr_commands_mock.py @@ -53,7 +53,6 @@ from azure.cli.command_modules.acr._constants import ACR_AUDIENCE_RESOURCE_NAME from azure.cli.command_modules.acr.cache import acr_cache_create from azure.cli.core.mock import DummyCli -from azure.core.exceptions import ResourceNotFoundError TEST_TENANT = 'testtenant'