-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add --image and --entrypoint params to containerapp debug command #9868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
khkh-ms
wants to merge
4
commits into
Azure:main
Choose a base branch
from
khkh-ms:khkh/debug-custom-image
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b314ccd
Add --image and --entrypoint params to containerapp debug command
khkh-ms 38bf5b1
Add unit tests for custom debug image parameters
khkh-ms acf432b
Validate --image/--entrypoint require --command and thread params thr…
khkh-ms b978fd8
Address PR review: move entrypoint validation, bump version, update H…
khkh-ms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
220 changes: 220 additions & 0 deletions
220
src/containerapp/azext_containerapp/tests/latest/test_containerapp_debug_unit.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| # coding=utf-8 | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| import unittest | ||
| from unittest import mock | ||
|
|
||
| from azure.cli.core.azclierror import ValidationError | ||
| from azext_containerapp.containerapp_debug_command_decorator import ContainerAppDebugCommandDecorator | ||
|
|
||
|
|
||
| class TestDebugCommandUrlBuilding(unittest.TestCase): | ||
| """Unit tests for the debug command URL building with custom image parameters.""" | ||
|
|
||
| def _create_decorator_with_params(self, params): | ||
| """Helper to create a decorator instance with mocked params.""" | ||
| with mock.patch.object(ContainerAppDebugCommandDecorator, '__init__', lambda self, *a, **kw: None): | ||
| decorator = ContainerAppDebugCommandDecorator() | ||
| decorator.raw_parameters = params | ||
| # Mock get_param to return from our dict | ||
| decorator.get_param = lambda key: params.get(key) | ||
| return decorator | ||
|
|
||
| def _mock_get_url(self, decorator, cmd_mock, **kwargs): | ||
| """Helper to call _get_url with mocked logstream endpoint and subscription.""" | ||
| base_endpoint = "https://proxy.example.com/subscriptions/test-sub/resourceGroups/test-rg/containerApps/test-app/revisions/test-rev/replicas/test-replica/logstream" | ||
| with mock.patch.object(decorator, '_get_logstream_endpoint', return_value=base_endpoint): | ||
| with mock.patch('azext_containerapp.containerapp_debug_command_decorator.get_subscription_id', return_value='test-sub'): | ||
| return decorator._get_url( | ||
| cmd_mock, | ||
| kwargs.get('resource_group_name', 'test-rg'), | ||
| kwargs.get('container_app_name', 'test-app'), | ||
| kwargs.get('revision_name', 'test-rev'), | ||
| kwargs.get('replica_name', 'test-replica'), | ||
| kwargs.get('container_name', 'test-container'), | ||
| kwargs.get('command', '/bin/bash'), | ||
| kwargs.get('custom_debug_image_name'), | ||
| kwargs.get('custom_debug_image_entrypoint_command'), | ||
| ) | ||
|
|
||
| def test_url_without_custom_image(self): | ||
| """URL should not contain custom image params when not specified.""" | ||
| decorator = self._create_decorator_with_params({}) | ||
| cmd_mock = mock.MagicMock() | ||
| url = self._mock_get_url(decorator, cmd_mock) | ||
|
|
||
| self.assertIn("targetContainer=test-container", url) | ||
| self.assertNotIn("customDebugImageName", url) | ||
| self.assertNotIn("customDebugImageEntrypointCommand", url) | ||
|
|
||
| def test_url_with_custom_image_only(self): | ||
| """URL should contain customDebugImageName when --image is specified.""" | ||
| decorator = self._create_decorator_with_params({}) | ||
| cmd_mock = mock.MagicMock() | ||
| url = self._mock_get_url(decorator, cmd_mock, custom_debug_image_name="ubuntu:22.04") | ||
|
|
||
| self.assertIn("customDebugImageName=ubuntu%3A22.04", url) | ||
| self.assertNotIn("customDebugImageEntrypointCommand", url) | ||
|
|
||
| def test_url_with_custom_image_and_entrypoint(self): | ||
| """URL should contain both params when --image and --entrypoint are specified.""" | ||
| decorator = self._create_decorator_with_params({}) | ||
| cmd_mock = mock.MagicMock() | ||
| url = self._mock_get_url( | ||
| decorator, cmd_mock, | ||
| custom_debug_image_name="mcr.microsoft.com/dotnet/sdk:8.0", | ||
| custom_debug_image_entrypoint_command="/bin/bash", | ||
| ) | ||
|
|
||
| self.assertIn("customDebugImageName=mcr.microsoft.com%2Fdotnet%2Fsdk%3A8.0", url) | ||
| self.assertIn("customDebugImageEntrypointCommand=%2Fbin%2Fbash", url) | ||
|
|
||
| def test_url_encodes_special_characters(self): | ||
| """Custom image params should be URL-encoded.""" | ||
| decorator = self._create_decorator_with_params({}) | ||
| cmd_mock = mock.MagicMock() | ||
| url = self._mock_get_url( | ||
| decorator, cmd_mock, | ||
| custom_debug_image_name="myregistry.azurecr.io/my-image:v1.0", | ||
| custom_debug_image_entrypoint_command="/bin/sh -c 'echo hello'", | ||
| ) | ||
|
|
||
| self.assertIn("customDebugImageName=myregistry.azurecr.io%2Fmy-image%3Av1.0", url) | ||
| self.assertIn("customDebugImageEntrypointCommand=%2Fbin%2Fsh+-c+%27echo+hello%27", url) | ||
|
|
||
|
|
||
| class TestDebugCommandValidation(unittest.TestCase): | ||
| """Unit tests for client-side validation of custom image parameters.""" | ||
|
|
||
| def _create_decorator_with_params(self, params): | ||
| """Helper to create a decorator instance with mocked params.""" | ||
| with mock.patch.object(ContainerAppDebugCommandDecorator, '__init__', lambda self, *a, **kw: None): | ||
| decorator = ContainerAppDebugCommandDecorator() | ||
| decorator.get_param = lambda key: params.get(key) | ||
| return decorator | ||
|
|
||
| def test_image_without_entrypoint_succeeds(self): | ||
| """--image without --entrypoint should not raise.""" | ||
| decorator = self._create_decorator_with_params({ | ||
| 'custom_debug_image_name': 'ubuntu:22.04', | ||
| 'custom_debug_image_entrypoint_command': None, | ||
| 'resource_group_name': 'rg', | ||
| 'container_app_name': 'app', | ||
| 'revision_name': 'rev', | ||
| 'replica_name': 'replica', | ||
| 'container_name': 'container', | ||
| 'command': '/bin/bash', | ||
| }) | ||
|
|
||
| cmd_mock = mock.MagicMock() | ||
| mock_response = mock.MagicMock() | ||
| mock_response.json.return_value = {"status": "ok"} | ||
| with mock.patch.object(decorator, '_get_url', return_value='https://example.com/debug'), \ | ||
| mock.patch.object(decorator, '_get_auth_token', return_value='token'), \ | ||
| mock.patch('azext_containerapp.containerapp_debug_command_decorator.send_raw_request', return_value=mock_response), \ | ||
| mock.patch('azext_containerapp.containerapp_debug_command_decorator.transform_debug_command_output', return_value={"status": "ok"}): | ||
| # Should not raise | ||
| decorator.execute_Command(cmd_mock) | ||
|
|
||
| def test_no_custom_params_succeeds(self): | ||
| """No custom image params should not raise.""" | ||
| decorator = self._create_decorator_with_params({ | ||
| 'custom_debug_image_name': None, | ||
| 'custom_debug_image_entrypoint_command': None, | ||
| 'resource_group_name': 'rg', | ||
| 'container_app_name': 'app', | ||
| 'revision_name': 'rev', | ||
| 'replica_name': 'replica', | ||
| 'container_name': 'container', | ||
| 'command': '/bin/bash', | ||
| }) | ||
|
|
||
| cmd_mock = mock.MagicMock() | ||
| mock_response = mock.MagicMock() | ||
| mock_response.json.return_value = {"status": "ok"} | ||
| with mock.patch.object(decorator, '_get_url', return_value='https://example.com/debug'), \ | ||
| mock.patch.object(decorator, '_get_auth_token', return_value='token'), \ | ||
| mock.patch('azext_containerapp.containerapp_debug_command_decorator.send_raw_request', return_value=mock_response), \ | ||
| mock.patch('azext_containerapp.containerapp_debug_command_decorator.transform_debug_command_output', return_value={"status": "ok"}): | ||
| # Should not raise | ||
| decorator.execute_Command(cmd_mock) | ||
|
|
||
| def test_getter_methods(self): | ||
| """Getter methods should return correct param values.""" | ||
| decorator = self._create_decorator_with_params({ | ||
| 'custom_debug_image_name': 'ubuntu:22.04', | ||
| 'custom_debug_image_entrypoint_command': '/bin/bash', | ||
| }) | ||
|
|
||
| self.assertEqual(decorator.get_argument_custom_debug_image_name(), 'ubuntu:22.04') | ||
| self.assertEqual(decorator.get_argument_custom_debug_image_entrypoint_command(), '/bin/bash') | ||
|
|
||
| def test_getter_methods_return_none_when_not_set(self): | ||
| """Getter methods should return None when params not provided.""" | ||
| decorator = self._create_decorator_with_params({}) | ||
|
|
||
| self.assertIsNone(decorator.get_argument_custom_debug_image_name()) | ||
| self.assertIsNone(decorator.get_argument_custom_debug_image_entrypoint_command()) | ||
|
|
||
|
|
||
| class TestValidateDebugCustomImageRequiresCommand(unittest.TestCase): | ||
| """Validate that --image/--entrypoint require --command.""" | ||
|
|
||
| def _make_namespace(self, **kwargs): | ||
| ns = mock.MagicMock() | ||
| ns.debug_command = kwargs.get('debug_command', None) | ||
| ns.custom_debug_image_name = kwargs.get('custom_debug_image_name', None) | ||
| ns.custom_debug_image_entrypoint_command = kwargs.get('custom_debug_image_entrypoint_command', None) | ||
| ns.revision = kwargs.get('revision', 'rev') | ||
| ns.replica = kwargs.get('replica', 'replica') | ||
| ns.container = kwargs.get('container', 'container') | ||
| ns.name = 'test-app' | ||
| ns.resource_group_name = 'test-rg' | ||
| return ns | ||
|
|
||
| @mock.patch('azext_containerapp._validators._set_debug_defaults') | ||
| def test_image_without_command_raises(self, mock_defaults): | ||
| from azext_containerapp._validators import validate_debug | ||
| ns = self._make_namespace(custom_debug_image_name='ubuntu:22.04') | ||
| with self.assertRaises(ValidationError) as ctx: | ||
| validate_debug(mock.MagicMock(), ns) | ||
| self.assertIn("--image", str(ctx.exception)) | ||
|
|
||
| @mock.patch('azext_containerapp._validators._set_debug_defaults') | ||
| def test_entrypoint_without_command_raises(self, mock_defaults): | ||
| from azext_containerapp._validators import validate_debug | ||
| ns = self._make_namespace(custom_debug_image_entrypoint_command='/bin/bash') | ||
| with self.assertRaises(ValidationError) as ctx: | ||
| validate_debug(mock.MagicMock(), ns) | ||
| self.assertIn("--image", str(ctx.exception)) | ||
|
|
||
| @mock.patch('azext_containerapp._validators._set_debug_defaults') | ||
| @mock.patch('azext_containerapp._validators._validate_revision_exists') | ||
| @mock.patch('azext_containerapp._validators._validate_replica_exists') | ||
| @mock.patch('azext_containerapp._validators._validate_container_exists') | ||
| def test_image_with_command_passes(self, mock_cont, mock_rep, mock_rev, mock_defaults): | ||
| from azext_containerapp._validators import validate_debug | ||
| ns = self._make_namespace( | ||
| debug_command='/bin/bash', | ||
| custom_debug_image_name='ubuntu:22.04', | ||
| ) | ||
| validate_debug(mock.MagicMock(), ns) # should not raise | ||
|
|
||
| @mock.patch('azext_containerapp._validators._set_debug_defaults') | ||
| def test_entrypoint_without_image_raises(self, mock_defaults): | ||
| """--entrypoint without --image should raise even when --command is set.""" | ||
| from azext_containerapp._validators import validate_debug | ||
| ns = self._make_namespace( | ||
| debug_command='/bin/bash', | ||
| custom_debug_image_entrypoint_command='/bin/bash', | ||
| ) | ||
| with self.assertRaises(ValidationError) as ctx: | ||
| validate_debug(mock.MagicMock(), ns) | ||
| self.assertIn("--entrypoint requires --image", str(ctx.exception)) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in commit b978fd8: added unit tests in
tests/latest/test_containerapp_debug_unit.pycovering URL building (4), command-decorator behavior (4), and thevalidate_debugvalidator (4) including the newtest_entrypoint_without_image_raisesfailure case. All 12 unit tests pass locally. Happy to add a recordedtest_containerapp_scenario.pycase as well if preferred over unit-level coverage.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in commit b978fd8: added unit tests in
tests/latest/test_containerapp_debug_unit.pycovering URL building (4), command-decorator behavior (4), and thevalidate_debugvalidator (4) including the newtest_entrypoint_without_image_raisesfailure case. All 12 unit tests pass locally. Happy to add a recordedtest_containerapp_scenario.pycase as well if preferred over unit-level coverage.