Skip to content

Commit 6a52aac

Browse files
committed
add a mock test
1 parent 6a1b839 commit 6a52aac

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/azure-cli/azure/cli/command_modules/util/_help.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@
9797
type: command
9898
short-summary: Create a sandboxed what-if simulation of Azure CLI scripts to visualize infrastructure changes before execution.
9999
examples:
100-
- name: Simulate a what-if scenario for a resource group deletion
100+
- name: Simulate a what-if scenario for a provided script
101101
text: az what-if --script-path "/path/to/your/script.sh"
102102
- name: Simulate a what-if scenario for a specific subscription
103-
text: az what-if --script-path "/path/to/your/script.sh" --subscription "MySubscription"
103+
text: az what-if --script-path "/path/to/your/script.sh" --subscription 00000000-0000-0000-0000-000000000000
104104
"""
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

Comments
 (0)