|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import sys |
| 4 | +from types import ModuleType, SimpleNamespace |
| 5 | + |
| 6 | +from azurefox.clients.factory import build_clients |
| 7 | + |
| 8 | + |
| 9 | +def _install_module(monkeypatch, name: str, *, is_package: bool = False, **attrs) -> ModuleType: |
| 10 | + module = ModuleType(name) |
| 11 | + if is_package: |
| 12 | + module.__path__ = [] |
| 13 | + for attr_name, attr_value in attrs.items(): |
| 14 | + setattr(module, attr_name, attr_value) |
| 15 | + monkeypatch.setitem(sys.modules, name, module) |
| 16 | + return module |
| 17 | + |
| 18 | + |
| 19 | +def _management_client_class(name: str): |
| 20 | + class FakeManagementClient: |
| 21 | + def __init__(self, credential: object, subscription_id: str) -> None: |
| 22 | + self.credential = credential |
| 23 | + self.subscription_id = subscription_id |
| 24 | + |
| 25 | + FakeManagementClient.__name__ = name |
| 26 | + return FakeManagementClient |
| 27 | + |
| 28 | + |
| 29 | +def _install_fake_management_modules( |
| 30 | + monkeypatch, |
| 31 | + *, |
| 32 | + subscription_client_on_resource: bool, |
| 33 | + subscription_client_on_split_module: bool, |
| 34 | +) -> None: |
| 35 | + _install_module(monkeypatch, "azure", is_package=True) |
| 36 | + _install_module(monkeypatch, "azure.mgmt", is_package=True) |
| 37 | + |
| 38 | + subscriptions = [ |
| 39 | + SimpleNamespace(subscription_id="sub-a", display_name="Alpha", state="Enabled"), |
| 40 | + SimpleNamespace(subscription_id="sub-b", display_name="Beta", state="Warned"), |
| 41 | + ] |
| 42 | + |
| 43 | + class FakeSubscriptionClient: |
| 44 | + def __init__(self, credential: object) -> None: |
| 45 | + self.credential = credential |
| 46 | + self.subscriptions = SimpleNamespace(list=lambda: list(subscriptions)) |
| 47 | + |
| 48 | + client_modules = { |
| 49 | + "azure.mgmt.apimanagement": ("ApiManagementClient",), |
| 50 | + "azure.mgmt.authorization": ("AuthorizationManagementClient",), |
| 51 | + "azure.mgmt.automation": ("AutomationClient",), |
| 52 | + "azure.mgmt.compute": ("ComputeManagementClient",), |
| 53 | + "azure.mgmt.containerregistry": ("ContainerRegistryManagementClient",), |
| 54 | + "azure.mgmt.containerservice": ("ContainerServiceClient",), |
| 55 | + "azure.mgmt.keyvault": ("KeyVaultManagementClient",), |
| 56 | + "azure.mgmt.mysqlflexibleservers": ("MySQLManagementClient",), |
| 57 | + "azure.mgmt.network": ("NetworkManagementClient",), |
| 58 | + "azure.mgmt.postgresqlflexibleservers": ("PostgreSQLManagementClient",), |
| 59 | + "azure.mgmt.sql": ("SqlManagementClient",), |
| 60 | + "azure.mgmt.storage": ("StorageManagementClient",), |
| 61 | + "azure.mgmt.web": ("WebSiteManagementClient",), |
| 62 | + } |
| 63 | + for module_name, class_names in client_modules.items(): |
| 64 | + _install_module( |
| 65 | + monkeypatch, |
| 66 | + module_name, |
| 67 | + **{class_name: _management_client_class(class_name) for class_name in class_names}, |
| 68 | + ) |
| 69 | + |
| 70 | + resource_attrs = { |
| 71 | + "ResourceManagementClient": _management_client_class("ResourceManagementClient"), |
| 72 | + } |
| 73 | + if subscription_client_on_resource: |
| 74 | + resource_attrs["SubscriptionClient"] = FakeSubscriptionClient |
| 75 | + _install_module(monkeypatch, "azure.mgmt.resource", is_package=True, **resource_attrs) |
| 76 | + |
| 77 | + if subscription_client_on_split_module: |
| 78 | + _install_module( |
| 79 | + monkeypatch, |
| 80 | + "azure.mgmt.resource.subscriptions", |
| 81 | + SubscriptionClient=FakeSubscriptionClient, |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +def test_build_clients_supports_split_subscription_package(monkeypatch) -> None: |
| 86 | + _install_fake_management_modules( |
| 87 | + monkeypatch, |
| 88 | + subscription_client_on_resource=False, |
| 89 | + subscription_client_on_split_module=True, |
| 90 | + ) |
| 91 | + |
| 92 | + session = SimpleNamespace(credential=object()) |
| 93 | + |
| 94 | + clients = build_clients(session, "sub-b") |
| 95 | + |
| 96 | + assert clients.subscription_id == "sub-b" |
| 97 | + assert clients.subscription.display_name == "Beta" |
| 98 | + assert clients.resource.subscription_id == "sub-b" |
0 commit comments