|
| 1 | +# -------------------------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | +# -------------------------------------------------------------------------------------------- |
| 5 | + |
| 6 | +import unittest |
| 7 | +import json |
| 8 | +import tempfile |
| 9 | +import os |
| 10 | +from unittest.mock import MagicMock, patch |
| 11 | +from azext_fleet.custom import get_update_run_strategy |
| 12 | +from azext_fleet.vendored_sdks.v2025_04_01_preview.models import UpdateRunStrategy, UpdateStage, UpdateGroup |
| 13 | +from azure.cli.core.azclierror import ( |
| 14 | + InvalidArgumentValueError, |
| 15 | +) |
| 16 | + |
| 17 | +class TestStagesJsonHandling(unittest.TestCase): |
| 18 | + """Test inline JSON support for --stages argument in fleet commands.""" |
| 19 | + |
| 20 | + def setUp(self): |
| 21 | + """Set up test data and mock objects.""" |
| 22 | + self.test_data = { |
| 23 | + "stages": [ |
| 24 | + { |
| 25 | + "name": "stage1", |
| 26 | + "groups": [ |
| 27 | + {"name": "group1"}, |
| 28 | + {"name": "group2"} |
| 29 | + ], |
| 30 | + "afterStageWaitInSeconds": 3600 |
| 31 | + } |
| 32 | + ] |
| 33 | + } |
| 34 | + |
| 35 | + # Mock cmd object that provides get_models method |
| 36 | + self.mock_cmd = MagicMock() |
| 37 | + |
| 38 | + # Set up get_models to return our mock classes |
| 39 | + def mock_get_models(model_name, **kwargs): |
| 40 | + if model_name == "UpdateGroup": |
| 41 | + return UpdateGroup |
| 42 | + elif model_name == "UpdateStage": |
| 43 | + return UpdateStage |
| 44 | + elif model_name == "UpdateRunStrategy": |
| 45 | + return UpdateRunStrategy |
| 46 | + else: |
| 47 | + return MagicMock() |
| 48 | + |
| 49 | + self.mock_cmd.get_models = mock_get_models |
| 50 | + |
| 51 | + def test_file_path_stages(self): |
| 52 | + """Test that file paths for stages work correctly with get_update_run_strategy.""" |
| 53 | + # Create a temporary file with test data |
| 54 | + with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: |
| 55 | + json.dump(self.test_data, f) |
| 56 | + temp_file_path = f.name |
| 57 | + |
| 58 | + try: |
| 59 | + # Test the actual function |
| 60 | + result = get_update_run_strategy(self.mock_cmd, "fleet_update_runs", temp_file_path) |
| 61 | + |
| 62 | + # Verify the returned strategy |
| 63 | + self.assertIsNotNone(result) |
| 64 | + self.assertIsInstance(result, UpdateRunStrategy) |
| 65 | + self.assertEqual(len(result.stages), 1) |
| 66 | + |
| 67 | + # Verify first stage |
| 68 | + stage = result.stages[0] |
| 69 | + self.assertIsInstance(stage, UpdateStage) |
| 70 | + self.assertEqual(stage.name, "stage1") |
| 71 | + self.assertEqual(stage.after_stage_wait_in_seconds, 3600) |
| 72 | + self.assertEqual(len(stage.groups), 2) |
| 73 | + |
| 74 | + # Verify groups |
| 75 | + self.assertIsInstance(stage.groups[0], UpdateGroup) |
| 76 | + self.assertEqual(stage.groups[0].name, "group1") |
| 77 | + self.assertEqual(stage.groups[1].name, "group2") |
| 78 | + |
| 79 | + finally: |
| 80 | + # Clean up |
| 81 | + os.unlink(temp_file_path) |
| 82 | + |
| 83 | + def test_inline_json_stages(self): |
| 84 | + """Test that inline JSON strings work correctly with get_update_run_strategy.""" |
| 85 | + inline_json = json.dumps(self.test_data) |
| 86 | + |
| 87 | + # Test the actual function |
| 88 | + result = get_update_run_strategy(self.mock_cmd, "fleet_update_runs", inline_json) |
| 89 | + |
| 90 | + # Verify the returned strategy |
| 91 | + self.assertIsNotNone(result) |
| 92 | + self.assertIsInstance(result, UpdateRunStrategy) |
| 93 | + self.assertEqual(len(result.stages), 1) |
| 94 | + |
| 95 | + # Verify first stage |
| 96 | + stage = result.stages[0] |
| 97 | + self.assertIsInstance(stage, UpdateStage) |
| 98 | + self.assertEqual(stage.name, "stage1") |
| 99 | + self.assertEqual(stage.after_stage_wait_in_seconds, 3600) |
| 100 | + self.assertEqual(len(stage.groups), 2) |
| 101 | + |
| 102 | + # Verify groups |
| 103 | + self.assertIsInstance(stage.groups[0], UpdateGroup) |
| 104 | + self.assertEqual(stage.groups[0].name, "group1") |
| 105 | + self.assertEqual(stage.groups[1].name, "group2") |
| 106 | + |
| 107 | + def test_inline_json_minimal_stages(self): |
| 108 | + """Test inline JSON with minimal required fields.""" |
| 109 | + minimal_data = { |
| 110 | + "stages": [ |
| 111 | + { |
| 112 | + "name": "minimal-stage", |
| 113 | + "groups": [ |
| 114 | + {"name": "minimal-group"} |
| 115 | + ] |
| 116 | + } |
| 117 | + ] |
| 118 | + } |
| 119 | + |
| 120 | + inline_json = json.dumps(minimal_data) |
| 121 | + result = get_update_run_strategy(self.mock_cmd, "fleet_update_runs", inline_json) |
| 122 | + |
| 123 | + # Verify minimal structure works |
| 124 | + self.assertIsNotNone(result) |
| 125 | + self.assertEqual(len(result.stages), 1) |
| 126 | + |
| 127 | + stage = result.stages[0] |
| 128 | + self.assertEqual(stage.name, "minimal-stage") |
| 129 | + self.assertEqual(stage.after_stage_wait_in_seconds, 0) # Should default to 0 |
| 130 | + self.assertEqual(len(stage.groups), 1) |
| 131 | + self.assertEqual(stage.groups[0].name, "minimal-group") |
| 132 | + |
| 133 | + def test_complex_stages_structure(self): |
| 134 | + """Test more complex stages structure with multiple stages and groups.""" |
| 135 | + complex_data = { |
| 136 | + "stages": [ |
| 137 | + { |
| 138 | + "name": "stage1", |
| 139 | + "groups": [ |
| 140 | + {"name": "group1"}, |
| 141 | + {"name": "group2"} |
| 142 | + ], |
| 143 | + "afterStageWaitInSeconds": 1800 |
| 144 | + }, |
| 145 | + { |
| 146 | + "name": "stage2", |
| 147 | + "groups": [ |
| 148 | + {"name": "group3"} |
| 149 | + ], |
| 150 | + "afterStageWaitInSeconds": 3600 |
| 151 | + } |
| 152 | + ] |
| 153 | + } |
| 154 | + |
| 155 | + inline_json = json.dumps(complex_data) |
| 156 | + result = get_update_run_strategy(self.mock_cmd, "fleet_update_runs", inline_json) |
| 157 | + |
| 158 | + # Verify complex structure |
| 159 | + self.assertIsNotNone(result) |
| 160 | + self.assertEqual(len(result.stages), 2) |
| 161 | + |
| 162 | + # Verify first stage |
| 163 | + stage1 = result.stages[0] |
| 164 | + self.assertEqual(stage1.name, "stage1") |
| 165 | + self.assertEqual(stage1.after_stage_wait_in_seconds, 1800) |
| 166 | + self.assertEqual(len(stage1.groups), 2) |
| 167 | + self.assertEqual(stage1.groups[0].name, "group1") |
| 168 | + self.assertEqual(stage1.groups[1].name, "group2") |
| 169 | + |
| 170 | + # Verify second stage |
| 171 | + stage2 = result.stages[1] |
| 172 | + self.assertEqual(stage2.name, "stage2") |
| 173 | + self.assertEqual(stage2.after_stage_wait_in_seconds, 3600) |
| 174 | + self.assertEqual(len(stage2.groups), 1) |
| 175 | + self.assertEqual(stage2.groups[0].name, "group3") |
| 176 | + |
| 177 | + def test_none_stages_returns_none(self): |
| 178 | + """Test that None stages input returns None.""" |
| 179 | + result = get_update_run_strategy(self.mock_cmd, "fleet_update_runs", None) |
| 180 | + self.assertIsNone(result) |
| 181 | + |
| 182 | + def test_invalid_json_raises_error(self): |
| 183 | + """Test that invalid JSON strings raise appropriate errors.""" |
| 184 | + invalid_json = '{"stages": [{"name": "test", invalid_syntax}]}' |
| 185 | + |
| 186 | + # Should raise an error when parsing invalid JSON |
| 187 | + with self.assertRaises(InvalidArgumentValueError): |
| 188 | + get_update_run_strategy(self.mock_cmd, "fleet_update_runs", invalid_json) |
| 189 | + |
| 190 | + |
| 191 | +if __name__ == "__main__": |
| 192 | + unittest.main() |
0 commit comments