-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[fleet] add support for inline json to --stages argument in CLI #9000
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
22a4e50
Initial plan
Copilot 0297053
Implement inline JSON support for --stages argument
Copilot 8ddde4e
Replace unit tests to actually test get_update_run_strategy function
Copilot 89f99c1
Move imports to top of file and consolidate azure.cli.core.util imports
Copilot 07bb1e6
Update test_stages_json.py
serbrech 1a3b3a4
remove unused json import
serbrech 2111cac
Update version to 1.6.1 and add changelog entry for --stages argument…
Copilot c153c6a
fix tests
serbrech 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| import unittest | ||
| import json | ||
| import tempfile | ||
| import os | ||
| from unittest.mock import MagicMock, patch | ||
| from azext_fleet.custom import get_update_run_strategy | ||
| from azext_fleet.vendored_sdks.v2025_04_01_preview.models import UpdateRunStrategy, UpdateStage, UpdateGroup | ||
| from azure.cli.core.azclierror import ( | ||
| InvalidArgumentValueError, | ||
| ) | ||
|
|
||
| class TestStagesJsonHandling(unittest.TestCase): | ||
| """Test inline JSON support for --stages argument in fleet commands.""" | ||
|
|
||
| def setUp(self): | ||
| """Set up test data and mock objects.""" | ||
| self.test_data = { | ||
| "stages": [ | ||
| { | ||
| "name": "stage1", | ||
| "groups": [ | ||
| {"name": "group1"}, | ||
| {"name": "group2"} | ||
| ], | ||
| "afterStageWaitInSeconds": 3600 | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| # Mock cmd object that provides get_models method | ||
| self.mock_cmd = MagicMock() | ||
|
|
||
| # Set up get_models to return our mock classes | ||
| def mock_get_models(model_name, **kwargs): | ||
| if model_name == "UpdateGroup": | ||
| return UpdateGroup | ||
| elif model_name == "UpdateStage": | ||
| return UpdateStage | ||
| elif model_name == "UpdateRunStrategy": | ||
| return UpdateRunStrategy | ||
| else: | ||
| return MagicMock() | ||
|
|
||
| self.mock_cmd.get_models = mock_get_models | ||
|
|
||
| def test_file_path_stages(self): | ||
| """Test that file paths for stages work correctly with get_update_run_strategy.""" | ||
| # Create a temporary file with test data | ||
| with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: | ||
| json.dump(self.test_data, f) | ||
| temp_file_path = f.name | ||
|
|
||
| try: | ||
| # Test the actual function | ||
| result = get_update_run_strategy(self.mock_cmd, "fleet_update_runs", temp_file_path) | ||
|
|
||
| # Verify the returned strategy | ||
| self.assertIsNotNone(result) | ||
| self.assertIsInstance(result, UpdateRunStrategy) | ||
| self.assertEqual(len(result.stages), 1) | ||
|
|
||
| # Verify first stage | ||
| stage = result.stages[0] | ||
| self.assertIsInstance(stage, UpdateStage) | ||
| self.assertEqual(stage.name, "stage1") | ||
| self.assertEqual(stage.after_stage_wait_in_seconds, 3600) | ||
| self.assertEqual(len(stage.groups), 2) | ||
|
|
||
| # Verify groups | ||
| self.assertIsInstance(stage.groups[0], UpdateGroup) | ||
| self.assertEqual(stage.groups[0].name, "group1") | ||
| self.assertEqual(stage.groups[1].name, "group2") | ||
|
|
||
| finally: | ||
| # Clean up | ||
| os.unlink(temp_file_path) | ||
|
|
||
| def test_inline_json_stages(self): | ||
| """Test that inline JSON strings work correctly with get_update_run_strategy.""" | ||
| inline_json = json.dumps(self.test_data) | ||
|
|
||
| # Test the actual function | ||
| result = get_update_run_strategy(self.mock_cmd, "fleet_update_runs", inline_json) | ||
|
|
||
| # Verify the returned strategy | ||
| self.assertIsNotNone(result) | ||
| self.assertIsInstance(result, UpdateRunStrategy) | ||
| self.assertEqual(len(result.stages), 1) | ||
|
|
||
| # Verify first stage | ||
| stage = result.stages[0] | ||
| self.assertIsInstance(stage, UpdateStage) | ||
| self.assertEqual(stage.name, "stage1") | ||
| self.assertEqual(stage.after_stage_wait_in_seconds, 3600) | ||
| self.assertEqual(len(stage.groups), 2) | ||
|
|
||
| # Verify groups | ||
| self.assertIsInstance(stage.groups[0], UpdateGroup) | ||
| self.assertEqual(stage.groups[0].name, "group1") | ||
| self.assertEqual(stage.groups[1].name, "group2") | ||
|
|
||
| def test_inline_json_minimal_stages(self): | ||
| """Test inline JSON with minimal required fields.""" | ||
| minimal_data = { | ||
| "stages": [ | ||
| { | ||
| "name": "minimal-stage", | ||
| "groups": [ | ||
| {"name": "minimal-group"} | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| inline_json = json.dumps(minimal_data) | ||
| result = get_update_run_strategy(self.mock_cmd, "fleet_update_runs", inline_json) | ||
|
|
||
| # Verify minimal structure works | ||
| self.assertIsNotNone(result) | ||
| self.assertEqual(len(result.stages), 1) | ||
|
|
||
| stage = result.stages[0] | ||
| self.assertEqual(stage.name, "minimal-stage") | ||
| self.assertEqual(stage.after_stage_wait_in_seconds, 0) # Should default to 0 | ||
| self.assertEqual(len(stage.groups), 1) | ||
| self.assertEqual(stage.groups[0].name, "minimal-group") | ||
|
|
||
| def test_complex_stages_structure(self): | ||
| """Test more complex stages structure with multiple stages and groups.""" | ||
| complex_data = { | ||
| "stages": [ | ||
| { | ||
| "name": "stage1", | ||
| "groups": [ | ||
| {"name": "group1"}, | ||
| {"name": "group2"} | ||
| ], | ||
| "afterStageWaitInSeconds": 1800 | ||
| }, | ||
| { | ||
| "name": "stage2", | ||
| "groups": [ | ||
| {"name": "group3"} | ||
| ], | ||
| "afterStageWaitInSeconds": 3600 | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| inline_json = json.dumps(complex_data) | ||
| result = get_update_run_strategy(self.mock_cmd, "fleet_update_runs", inline_json) | ||
|
|
||
| # Verify complex structure | ||
| self.assertIsNotNone(result) | ||
| self.assertEqual(len(result.stages), 2) | ||
|
|
||
| # Verify first stage | ||
| stage1 = result.stages[0] | ||
| self.assertEqual(stage1.name, "stage1") | ||
| self.assertEqual(stage1.after_stage_wait_in_seconds, 1800) | ||
| self.assertEqual(len(stage1.groups), 2) | ||
| self.assertEqual(stage1.groups[0].name, "group1") | ||
| self.assertEqual(stage1.groups[1].name, "group2") | ||
|
|
||
| # Verify second stage | ||
| stage2 = result.stages[1] | ||
| self.assertEqual(stage2.name, "stage2") | ||
| self.assertEqual(stage2.after_stage_wait_in_seconds, 3600) | ||
| self.assertEqual(len(stage2.groups), 1) | ||
| self.assertEqual(stage2.groups[0].name, "group3") | ||
|
|
||
| def test_none_stages_returns_none(self): | ||
| """Test that None stages input returns None.""" | ||
| result = get_update_run_strategy(self.mock_cmd, "fleet_update_runs", None) | ||
| self.assertIsNone(result) | ||
|
|
||
| def test_invalid_json_raises_error(self): | ||
| """Test that invalid JSON strings raise appropriate errors.""" | ||
| invalid_json = '{"stages": [{"name": "test", invalid_syntax}]}' | ||
|
|
||
| # Should raise an error when parsing invalid JSON | ||
| with self.assertRaises(InvalidArgumentValueError): | ||
| get_update_run_strategy(self.mock_cmd, "fleet_update_runs", invalid_json) | ||
|
|
||
|
|
||
| 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.
Uh oh!
There was an error while loading. Please reload this page.