-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathpatches.py
105 lines (76 loc) · 4.04 KB
/
patches.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
from .scenario_tests import mock_in_unit_test
from .scenario_tests.const import MOCKED_SUBSCRIPTION_ID, MOCKED_TENANT_ID
from .exceptions import CliExecutionError
MOCKED_USER_NAME = '[email protected]'
def patch_progress_controller(unit_test):
def _mock_pass(*args, **kwargs): # pylint: disable=unused-argument
pass
mock_in_unit_test(
unit_test, 'azure.cli.core.commands.progress.ProgressHook.update', _mock_pass)
mock_in_unit_test(
unit_test, 'azure.cli.core.commands.progress.ProgressHook.add', _mock_pass)
mock_in_unit_test(
unit_test, 'azure.cli.core.commands.progress.ProgressHook.end', _mock_pass)
def patch_main_exception_handler(unit_test):
from vcr.errors import CannotOverwriteExistingCassetteException
def _handle_main_exception(ex, *args, **kwargs): # pylint: disable=unused-argument
if isinstance(ex, CannotOverwriteExistingCassetteException):
# This exception usually caused by a no match HTTP request. This is a product error
# that is caused by change of SDK invocation.
raise ex
raise CliExecutionError(ex)
mock_in_unit_test(unit_test, 'azure.cli.core.util.handle_exception', _handle_main_exception)
def patch_load_cached_subscriptions(unit_test):
def _handle_load_cached_subscription(*args, **kwargs): # pylint: disable=unused-argument
return [
{
"id": MOCKED_SUBSCRIPTION_ID,
"state": "Enabled",
"name": "Example",
"tenantId": MOCKED_TENANT_ID,
"isDefault": True,
"environmentName": "AzureCloud",
"user": {
"name": MOCKED_USER_NAME,
"type": "user"
}
}
]
mock_in_unit_test(unit_test,
'azure.cli.core._profile.Profile.load_cached_subscriptions',
_handle_load_cached_subscription)
def patch_retrieve_token_for_user(unit_test):
def get_user_credential_mock(*args, **kwargs):
class UserCredentialMock:
def __init__(self, *args, **kwargs):
super().__init__()
def get_token(self, *scopes, **kwargs): # pylint: disable=unused-argument
# Old Track 2 SDKs are no longer supported. https://github.com/Azure/azure-cli/pull/29690
assert len(scopes) == 1, "'scopes' must contain only one element."
from azure.core.credentials import AccessToken
import time
fake_raw_token = 'top-secret-token-for-you'
now = int(time.time())
return AccessToken(fake_raw_token, now + 3600)
return UserCredentialMock()
mock_in_unit_test(unit_test, 'azure.cli.core.auth.identity.Identity.get_user_credential', get_user_credential_mock)
def patch_long_run_operation_delay(unit_test):
def _shortcut_long_run_operation(*args, **kwargs): # pylint: disable=unused-argument
return
mock_in_unit_test(unit_test,
'azure.mgmt.core.polling.arm_polling.ARMPolling._delay',
_shortcut_long_run_operation)
mock_in_unit_test(unit_test,
'azure.cli.core.commands.LongRunningOperation._delay',
_shortcut_long_run_operation)
def patch_get_current_system_username(unit_test):
def _get_current_system_username(*args, **kwargs): # pylint: disable=unused-argument
from .utilities import create_random_name
return create_random_name(prefix='example_')
mock_in_unit_test(unit_test,
'azure.cli.core.local_context._get_current_system_username',
_get_current_system_username)