|
| 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 json |
| 7 | +import unittest |
| 8 | +from types import SimpleNamespace |
| 9 | +from unittest import mock |
| 10 | + |
| 11 | +from azure.core.exceptions import HttpResponseError |
| 12 | +from azure.core.pipeline.transport import HttpRequest, RequestsTransportResponse |
| 13 | +from knack.util import CLIError |
| 14 | +from requests import Response |
| 15 | + |
| 16 | +from azure.cli.core.azclierror import DeploymentError, InvalidTemplateError |
| 17 | +from azure.cli.core.commands.arm import handle_template_based_exception |
| 18 | + |
| 19 | + |
| 20 | +class TestTemplateBasedException(unittest.TestCase): |
| 21 | + |
| 22 | + @staticmethod |
| 23 | + def _sdk_error(error=None, message=None, consumed=True): |
| 24 | + response = Response() |
| 25 | + response.status_code = 400 |
| 26 | + response.reason = 'Bad Request' |
| 27 | + response.headers['Content-Type'] = 'application/json' |
| 28 | + response._content = json.dumps({'error': error} if error else {}).encode('utf-8') |
| 29 | + transport_response = RequestsTransportResponse( |
| 30 | + HttpRequest('PUT', 'https://management.azure.com/deployments/test'), response) |
| 31 | + exception = HttpResponseError(response=transport_response, message=message) |
| 32 | + if consumed: |
| 33 | + # Match a streamed requests response whose body was read without caching it. |
| 34 | + response._content = False |
| 35 | + response._content_consumed = True |
| 36 | + return exception |
| 37 | + |
| 38 | + def test_template_exception_consumed_sku_error(self): |
| 39 | + message = ( |
| 40 | + "The requested VM size Standard_B2s is currently not available in location eastus. " |
| 41 | + "Please try another size or deploy to a different location or different zone.") |
| 42 | + exception = self._sdk_error({'code': 'SkuNotAvailable', 'message': message}) |
| 43 | + self.assertIsNone(exception.inner_exception) |
| 44 | + with self.assertRaisesRegex(RuntimeError, 'already consumed'): |
| 45 | + _ = exception.response.internal_response.text |
| 46 | + |
| 47 | + with self.assertRaises(DeploymentError) as raised: |
| 48 | + handle_template_based_exception(exception) |
| 49 | + self.assertEqual(str(raised.exception), exception.message) |
| 50 | + self.assertIn('SkuNotAvailable', str(raised.exception)) |
| 51 | + self.assertIn(message, str(raised.exception)) |
| 52 | + |
| 53 | + def test_template_exception_consumed_sku_errors_across_regions_and_sizes(self): |
| 54 | + for region in ('eastus', 'westeurope', 'northeurope'): |
| 55 | + for size in ('Standard_B2s', 'Standard_B2ms', 'Standard_D2s_v3', |
| 56 | + 'Standard_D2s_v5', 'Standard_B1s'): |
| 57 | + with self.subTest(region=region, size=size): |
| 58 | + message = ( |
| 59 | + 'The requested VM size {} is currently not available in location {}. ' |
| 60 | + 'Please try another size or deploy to a different location or different zone.' |
| 61 | + ).format(size, region) |
| 62 | + exception = self._sdk_error({'code': 'SkuNotAvailable', 'message': message}) |
| 63 | + self.assertIsNone(exception.inner_exception) |
| 64 | + |
| 65 | + with self.assertRaises(DeploymentError) as raised: |
| 66 | + handle_template_based_exception(exception) |
| 67 | + self.assertEqual(str(raised.exception), exception.message) |
| 68 | + self.assertIn('SkuNotAvailable', str(raised.exception)) |
| 69 | + self.assertIn(message, str(raised.exception)) |
| 70 | + |
| 71 | + def test_template_exception_consumed_quota_error(self): |
| 72 | + message = ( |
| 73 | + 'Operation could not be completed as it results in exceeding approved ' |
| 74 | + 'standardDSv5Family Cores quota. Location: eastus, Current Limit: 0, Current Usage: 0, ' |
| 75 | + 'Additional Required: 2. Please request a quota increase.') |
| 76 | + exception = self._sdk_error({'code': 'QuotaExceeded', 'message': message}) |
| 77 | + |
| 78 | + with self.assertRaises(DeploymentError) as raised: |
| 79 | + handle_template_based_exception(exception) |
| 80 | + self.assertEqual(str(raised.exception), exception.message) |
| 81 | + self.assertIn('QuotaExceeded', str(raised.exception)) |
| 82 | + self.assertIn(message, str(raised.exception)) |
| 83 | + |
| 84 | + def test_template_exception_consumed_nested_error(self): |
| 85 | + message = 'At least one resource deployment operation failed.' |
| 86 | + detail_message = 'Allocation failed. Please retry later or use a different VM size.' |
| 87 | + exception = self._sdk_error({ |
| 88 | + 'code': 'DeploymentFailed', |
| 89 | + 'message': message, |
| 90 | + 'details': [{ |
| 91 | + 'code': 'ResourceDeploymentFailure', |
| 92 | + 'message': 'The resource write operation failed.', |
| 93 | + 'details': [{'code': 'AllocationFailed', 'message': detail_message}] |
| 94 | + }] |
| 95 | + }) |
| 96 | + |
| 97 | + with self.assertRaises(DeploymentError) as raised: |
| 98 | + handle_template_based_exception(exception) |
| 99 | + self.assertEqual(str(raised.exception), exception.message) |
| 100 | + for expected in ('DeploymentFailed', message, 'ResourceDeploymentFailure', |
| 101 | + 'The resource write operation failed.', 'AllocationFailed', detail_message): |
| 102 | + self.assertIn(expected, str(raised.exception)) |
| 103 | + |
| 104 | + def test_template_exception_consumed_invalid_template(self): |
| 105 | + message = 'The template deployment is not valid.' |
| 106 | + exception = self._sdk_error({'code': 'InvalidTemplateDeployment', 'message': message}) |
| 107 | + |
| 108 | + with self.assertRaises(InvalidTemplateError) as raised: |
| 109 | + handle_template_based_exception(exception) |
| 110 | + self.assertIn('InvalidTemplateDeployment', str(raised.exception)) |
| 111 | + self.assertIn(message, str(raised.exception)) |
| 112 | + |
| 113 | + def test_template_exception_consumed_message_without_parsed_error(self): |
| 114 | + message = '(QuotaExceeded) Current Limit: 0, Current Usage: 0, Additional Required: 2.' |
| 115 | + exception = self._sdk_error(message=message) |
| 116 | + self.assertIsNone(exception.error) |
| 117 | + |
| 118 | + with self.assertRaises(DeploymentError) as raised: |
| 119 | + handle_template_based_exception(exception) |
| 120 | + self.assertEqual(str(raised.exception), message) |
| 121 | + |
| 122 | + def test_template_exception_sdk_error_without_response(self): |
| 123 | + message = 'Deployment failed before a response was available.' |
| 124 | + exception = HttpResponseError(message=message) |
| 125 | + |
| 126 | + with self.assertRaises(DeploymentError) as raised: |
| 127 | + handle_template_based_exception(exception) |
| 128 | + self.assertEqual(str(raised.exception), message) |
| 129 | + |
| 130 | + def test_template_exception_sdk_error_does_not_reread_response(self): |
| 131 | + exception = self._sdk_error({'code': 'SkuNotAvailable', 'message': 'Try another VM size.'}) |
| 132 | + with mock.patch.object(exception.response, 'text', side_effect=AssertionError('Body reread')) as text: |
| 133 | + with self.assertRaises(DeploymentError) as raised: |
| 134 | + handle_template_based_exception(exception) |
| 135 | + text.assert_not_called() |
| 136 | + self.assertEqual(str(raised.exception), exception.message) |
| 137 | + |
| 138 | + def test_template_exception_unconsumed_error(self): |
| 139 | + message = 'The deployment conflicts with an existing operation.' |
| 140 | + exception = self._sdk_error({'code': 'Conflict', 'message': message}, consumed=False) |
| 141 | + |
| 142 | + with self.assertRaises(DeploymentError) as raised: |
| 143 | + handle_template_based_exception(exception) |
| 144 | + self.assertIn('Conflict', str(raised.exception)) |
| 145 | + self.assertIn(message, str(raised.exception)) |
| 146 | + |
| 147 | + def test_template_exception_inner_error_preserved(self): |
| 148 | + exception = self._sdk_error({'code': 'DeploymentFailed', 'message': 'Outer failure.'}) |
| 149 | + exception.inner_exception = SimpleNamespace(error=SimpleNamespace(message='Inner failure.')) |
| 150 | + |
| 151 | + with self.assertRaises(CLIError) as raised: |
| 152 | + handle_template_based_exception(exception) |
| 153 | + self.assertIs(type(raised.exception), CLIError) |
| 154 | + self.assertEqual(str(raised.exception), 'Inner failure.') |
| 155 | + |
| 156 | + def test_template_exception_legacy_response_preserved(self): |
| 157 | + for code, error_type in [('DeploymentFailed', DeploymentError), |
| 158 | + ('InvalidTemplateDeployment', InvalidTemplateError)]: |
| 159 | + with self.subTest(code=code): |
| 160 | + exception = SimpleNamespace( |
| 161 | + inner_exception=None, |
| 162 | + error=SimpleNamespace(code=code), |
| 163 | + response=SimpleNamespace(internal_response=SimpleNamespace(text='Original response body.'))) |
| 164 | + |
| 165 | + with self.assertRaises(error_type) as raised: |
| 166 | + handle_template_based_exception(exception) |
| 167 | + self.assertEqual(str(raised.exception), 'Original response body.') |
| 168 | + |
| 169 | + def test_template_exception_unrelated_error_preserved(self): |
| 170 | + exception = ValueError('Unrelated VM error.') |
| 171 | + |
| 172 | + with self.assertRaises(CLIError) as raised: |
| 173 | + handle_template_based_exception(exception) |
| 174 | + self.assertIs(type(raised.exception), CLIError) |
| 175 | + self.assertEqual(str(raised.exception), 'Unrelated VM error.') |
0 commit comments