|
| 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 os |
| 8 | +from unittest.mock import patch, Mock |
| 9 | +from azure.cli.testsdk import ScenarioTest |
| 10 | + |
| 11 | +TEST_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 12 | + |
| 13 | + |
| 14 | +class WhatIfTest(ScenarioTest): |
| 15 | + |
| 16 | + def setUp(self): |
| 17 | + super().setUp() |
| 18 | + self.test_script_path = os.path.join(TEST_DIR, 'test_whatif_script.sh') |
| 19 | + |
| 20 | + @patch('azure.cli.core.util.send_raw_request') |
| 21 | + def test_what_if_command_success(self, mock_send_raw_request): |
| 22 | + mock_response = Mock() |
| 23 | + mock_response.json.return_value = { |
| 24 | + "what_if_result": { |
| 25 | + "changes": [ |
| 26 | + { |
| 27 | + "changeType": "Create", |
| 28 | + "resourceId": "/subscriptions/test/resourceGroups/myrg/providers/Microsoft.Compute/virtualMachines/MyVM_01", |
| 29 | + "before": None, |
| 30 | + "after": { |
| 31 | + "name": "MyVM_01", |
| 32 | + "type": "Microsoft.Compute/virtualMachines", |
| 33 | + "location": "eastus" |
| 34 | + } |
| 35 | + } |
| 36 | + ], |
| 37 | + "potential_changes": [], |
| 38 | + "diagnostics": [] |
| 39 | + } |
| 40 | + } |
| 41 | + mock_send_raw_request.return_value = mock_response |
| 42 | + result = self.cmd('az what-if --script-path "{}" --no-pretty-print'.format(self.test_script_path)) |
| 43 | + output = result.get_output_in_json() |
| 44 | + self.assertIsInstance(output, dict) |
| 45 | + self.assertIn("changes", output) |
| 46 | + self.assertEqual(len(output["changes"]), 1) |
| 47 | + self.assertEqual(output["changes"][0]["changeType"], "Create") |
| 48 | + mock_send_raw_request.assert_called_once() |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == '__main__': |
| 52 | + unittest.main() |
0 commit comments