Skip to content

Commit 265c63d

Browse files
Add more infrastructure tests
1 parent 89ee816 commit 265c63d

File tree

1 file changed

+375
-0
lines changed

1 file changed

+375
-0
lines changed

tests/python/test_infrastructures.py

Lines changed: 375 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2616,3 +2616,378 @@ def test_infrastructure_with_different_indices(mock_utils):
26162616
rg_location='eastus'
26172617
)
26182618
assert infra.index == index
2619+
2620+
2621+
def test_appgw_apim_pe_create_keyvault_success(mock_utils, mock_az):
2622+
"""Test KeyVault creation for AppGwApimPeInfrastructure."""
2623+
infra = infrastructures.AppGwApimPeInfrastructure(
2624+
rg_location='eastus',
2625+
index=1
2626+
)
2627+
2628+
mock_az.resource_exists.return_value = False
2629+
mock_az.run.return_value = Mock(success=True)
2630+
2631+
result = infra._create_keyvault('test-kv')
2632+
assert isinstance(result, bool)
2633+
2634+
2635+
def test_appgw_apim_create_keyvault_success(mock_utils, mock_az):
2636+
"""Test KeyVault creation for AppGwApimInfrastructure."""
2637+
infra = infrastructures.AppGwApimInfrastructure(
2638+
rg_location='eastus',
2639+
index=1
2640+
)
2641+
2642+
mock_az.resource_exists.return_value = False
2643+
mock_az.run.return_value = Mock(success=True)
2644+
2645+
result = infra._create_keyvault('test-kv')
2646+
assert isinstance(result, bool)
2647+
2648+
2649+
def test_appgw_apim_pe_create_certificate_success(mock_utils, mock_az):
2650+
"""Test certificate creation for AppGwApimPeInfrastructure."""
2651+
infra = infrastructures.AppGwApimPeInfrastructure(
2652+
rg_location='eastus',
2653+
index=1
2654+
)
2655+
2656+
mock_az.run.return_value = Mock(success=True)
2657+
2658+
result = infra._create_keyvault_certificate('test-kv')
2659+
assert isinstance(result, bool)
2660+
2661+
2662+
def test_appgw_apim_create_certificate_success(mock_utils, mock_az):
2663+
"""Test certificate creation for AppGwApimInfrastructure."""
2664+
infra = infrastructures.AppGwApimInfrastructure(
2665+
rg_location='eastus',
2666+
index=1
2667+
)
2668+
2669+
mock_az.run.return_value = Mock(success=True)
2670+
2671+
result = infra._create_keyvault_certificate('test-kv')
2672+
assert isinstance(result, bool)
2673+
2674+
2675+
def test_afd_apim_aca_approve_private_links_multiple(mock_utils, mock_az):
2676+
"""Test private link approval with multiple connections."""
2677+
infra = infrastructures.AfdApimAcaInfrastructure(
2678+
rg_location='eastus',
2679+
index=1
2680+
)
2681+
2682+
mock_output = Mock()
2683+
mock_output.success = True
2684+
mock_output.getJson.return_value = [
2685+
{'name': 'conn1', 'properties': {'privateLinkServiceConnectionState': {'status': 'Pending'}}},
2686+
{'name': 'conn2', 'properties': {'privateLinkServiceConnectionState': {'status': 'Pending'}}}
2687+
]
2688+
mock_az.run.return_value = mock_output
2689+
2690+
result = infra._approve_private_link_connections('/subscriptions/test/resourceGroups/test/providers/Microsoft.ApiManagement/service/test')
2691+
2692+
assert isinstance(result, bool)
2693+
2694+
2695+
def test_appgw_apim_pe_approve_private_links_multiple(mock_utils, mock_az):
2696+
"""Test private link approval for AppGwApimPeInfrastructure with multiple connections."""
2697+
infra = infrastructures.AppGwApimPeInfrastructure(
2698+
rg_location='eastus',
2699+
index=1
2700+
)
2701+
2702+
mock_output = Mock()
2703+
mock_output.success = True
2704+
mock_output.getJson.return_value = [
2705+
{'name': 'conn1', 'properties': {'privateLinkServiceConnectionState': {'status': 'Pending'}}},
2706+
{'name': 'conn2', 'properties': {'privateLinkServiceConnectionState': {'status': 'Pending'}}}
2707+
]
2708+
mock_az.run.return_value = mock_output
2709+
2710+
result = infra._approve_private_link_connections('/subscriptions/test/resourceGroups/test/providers/Microsoft.ApiManagement/service/test')
2711+
2712+
assert isinstance(result, bool)
2713+
2714+
2715+
def test_afd_apim_aca_disable_public_access_success(mock_utils, mock_az):
2716+
"""Test disabling public access for AfdApimAcaInfrastructure."""
2717+
infra = infrastructures.AfdApimAcaInfrastructure(
2718+
rg_location='eastus',
2719+
index=1
2720+
)
2721+
2722+
mock_az.run.return_value = Mock(success=True)
2723+
2724+
result = infra._disable_apim_public_access()
2725+
2726+
assert isinstance(result, bool)
2727+
2728+
2729+
def test_appgw_apim_pe_disable_public_access_success(mock_utils, mock_az):
2730+
"""Test disabling public access for AppGwApimPeInfrastructure."""
2731+
infra = infrastructures.AppGwApimPeInfrastructure(
2732+
rg_location='eastus',
2733+
index=1
2734+
)
2735+
2736+
mock_az.run.return_value = Mock(success=True)
2737+
2738+
result = infra._disable_apim_public_access()
2739+
2740+
assert isinstance(result, bool)
2741+
2742+
2743+
def test_afd_apim_aca_verify_connectivity_with_retry(mock_utils, mock_az):
2744+
"""Test connectivity verification with retries for AfdApimAcaInfrastructure."""
2745+
infra = infrastructures.AfdApimAcaInfrastructure(
2746+
rg_location='eastus',
2747+
index=1
2748+
)
2749+
2750+
with patch('infrastructures.requests.get') as mock_requests:
2751+
mock_requests.return_value.status_code = 200
2752+
2753+
result = infra._verify_apim_connectivity('https://test-apim.azure-api.net')
2754+
2755+
assert isinstance(result, bool)
2756+
2757+
2758+
def test_appgw_apim_pe_verify_connectivity_with_retry(mock_utils, mock_az):
2759+
"""Test connectivity verification for AppGwApimPeInfrastructure."""
2760+
infra = infrastructures.AppGwApimPeInfrastructure(
2761+
rg_location='eastus',
2762+
index=1
2763+
)
2764+
2765+
with patch('infrastructures.requests.get') as mock_requests:
2766+
mock_requests.return_value.status_code = 200
2767+
2768+
result = infra._verify_apim_connectivity('https://test-apim.azure-api.net')
2769+
2770+
assert isinstance(result, bool)
2771+
2772+
2773+
def test_afd_apim_aca_define_bicep_parameters_complete(mock_utils):
2774+
"""Test complete bicep parameter definition for AfdApimAcaInfrastructure."""
2775+
pf = PolicyFragment('custom-pf', '<policy></policy>', 'Custom')
2776+
api = API('custom-api', 'Custom', '/custom', 'Custom', '<policy></policy>')
2777+
2778+
infra = infrastructures.AfdApimAcaInfrastructure(
2779+
rg_location='eastus',
2780+
index=1,
2781+
infra_pfs=[pf],
2782+
infra_apis=[api]
2783+
)
2784+
2785+
infra._define_policy_fragments()
2786+
infra._define_apis()
2787+
params = infra._define_bicep_parameters()
2788+
2789+
assert 'resourceSuffix' in params
2790+
assert 'apimSku' in params
2791+
assert 'apis' in params
2792+
assert 'policyFragments' in params
2793+
assert len(params['apis']['value']) > 1
2794+
assert len(params['policyFragments']['value']) > 6
2795+
2796+
2797+
def test_appgw_apim_pe_define_bicep_parameters_complete(mock_utils):
2798+
"""Test complete bicep parameter definition for AppGwApimPeInfrastructure."""
2799+
infra = infrastructures.AppGwApimPeInfrastructure(
2800+
rg_location='eastus',
2801+
index=1
2802+
)
2803+
2804+
infra._define_policy_fragments()
2805+
infra._define_apis()
2806+
params = infra._define_bicep_parameters()
2807+
2808+
assert 'resourceSuffix' in params
2809+
assert 'apimSku' in params
2810+
assert 'apis' in params
2811+
assert 'policyFragments' in params
2812+
2813+
2814+
def test_appgw_apim_define_bicep_parameters_complete(mock_utils):
2815+
"""Test complete bicep parameter definition for AppGwApimInfrastructure."""
2816+
infra = infrastructures.AppGwApimInfrastructure(
2817+
rg_location='eastus',
2818+
index=1
2819+
)
2820+
2821+
infra._define_policy_fragments()
2822+
infra._define_apis()
2823+
params = infra._define_bicep_parameters()
2824+
2825+
assert 'resourceSuffix' in params
2826+
assert 'apimSku' in params
2827+
assert 'apis' in params
2828+
assert 'policyFragments' in params
2829+
2830+
2831+
def test_apim_aca_verify_infrastructure_specific_checks(mock_utils, mock_az):
2832+
"""Test ApimAcaInfrastructure specific verification checks."""
2833+
infra = infrastructures.ApimAcaInfrastructure(
2834+
rg_location='eastus',
2835+
index=1
2836+
)
2837+
2838+
mock_az.does_resource_group_exist.return_value = True
2839+
mock_az.does_apim_exist.return_value = True
2840+
2841+
result = infra._verify_infrastructure_specific('test-rg')
2842+
assert isinstance(result, bool)
2843+
2844+
2845+
def test_infrastructure_deployment_with_all_skus(mock_utils):
2846+
"""Test infrastructure creation with all available SKUs."""
2847+
skus = [APIM_SKU.DEVELOPER, APIM_SKU.BASIC, APIM_SKU.STANDARD, APIM_SKU.PREMIUM,
2848+
APIM_SKU.BASICV2, APIM_SKU.STANDARDV2, APIM_SKU.PREMIUMV2]
2849+
2850+
for sku in skus:
2851+
infra = infrastructures.Infrastructure(
2852+
infra=INFRASTRUCTURE.SIMPLE_APIM,
2853+
index=1,
2854+
rg_location='eastus',
2855+
apim_sku=sku
2856+
)
2857+
infra._define_policy_fragments()
2858+
infra._define_apis()
2859+
infra._define_bicep_parameters()
2860+
assert infra.bicep_parameters['apimSku']['value'] == sku.value
2861+
2862+
2863+
def test_infrastructure_bicep_parameters_structure(mock_utils):
2864+
"""Test that bicep parameters have correct structure."""
2865+
infra = infrastructures.Infrastructure(
2866+
infra=INFRASTRUCTURE.SIMPLE_APIM,
2867+
index=1,
2868+
rg_location='eastus'
2869+
)
2870+
2871+
infra._define_policy_fragments()
2872+
infra._define_apis()
2873+
params = infra._define_bicep_parameters()
2874+
2875+
# Verify structure
2876+
assert isinstance(params, dict)
2877+
for _key, value in params.items():
2878+
assert 'value' in value
2879+
assert isinstance(value['value'], (str, list))
2880+
2881+
2882+
def test_infrastructure_policy_fragments_combining(mock_utils):
2883+
"""Test combining base and custom policy fragments."""
2884+
pf1 = PolicyFragment('custom-1', '<policy></policy>', 'Custom 1')
2885+
pf2 = PolicyFragment('custom-2', '<policy></policy>', 'Custom 2')
2886+
2887+
infra = infrastructures.Infrastructure(
2888+
infra=INFRASTRUCTURE.SIMPLE_APIM,
2889+
index=1,
2890+
rg_location='eastus',
2891+
infra_pfs=[pf1, pf2]
2892+
)
2893+
2894+
pfs = infra._define_policy_fragments()
2895+
2896+
# Should have 6 base + 2 custom
2897+
assert len(pfs) == 8
2898+
assert infra.base_pfs
2899+
assert any(pf.name == 'custom-1' for pf in pfs)
2900+
assert any(pf.name == 'custom-2' for pf in pfs)
2901+
2902+
2903+
def test_infrastructure_apis_combining(mock_utils):
2904+
"""Test combining base and custom APIs."""
2905+
api1 = API('custom-1', 'Custom 1', '/c1', 'Custom 1', '<policy></policy>')
2906+
api2 = API('custom-2', 'Custom 2', '/c2', 'Custom 2', '<policy></policy>')
2907+
2908+
infra = infrastructures.Infrastructure(
2909+
infra=INFRASTRUCTURE.SIMPLE_APIM,
2910+
index=1,
2911+
rg_location='eastus',
2912+
infra_apis=[api1, api2]
2913+
)
2914+
2915+
apis = infra._define_apis()
2916+
2917+
# Should have 1 base (hello-world) + 2 custom
2918+
assert len(apis) == 3
2919+
assert infra.base_apis
2920+
assert any(api.name == 'custom-1' for api in apis)
2921+
assert any(api.name == 'custom-2' for api in apis)
2922+
assert infra.apis[0].name == 'hello-world'
2923+
2924+
2925+
def test_infrastructure_with_no_custom_components(mock_utils):
2926+
"""Test infrastructure with empty custom component lists."""
2927+
infra = infrastructures.Infrastructure(
2928+
infra=INFRASTRUCTURE.SIMPLE_APIM,
2929+
index=1,
2930+
rg_location='eastus',
2931+
infra_apis=[],
2932+
infra_pfs=[]
2933+
)
2934+
2935+
apis = infra._define_apis()
2936+
pfs = infra._define_policy_fragments()
2937+
2938+
# Only base components
2939+
assert len(apis) == 1
2940+
assert len(pfs) == 6
2941+
2942+
2943+
def test_infrastructure_network_mode_with_custom_components(mock_utils):
2944+
"""Test infrastructure with network mode and custom components."""
2945+
pf = PolicyFragment('test-pf', '<policy></policy>', 'Test')
2946+
api = API('test-api', 'Test API', '/test', 'Test', '<policy></policy>')
2947+
2948+
for network_mode in [APIMNetworkMode.PUBLIC, APIMNetworkMode.INTERNAL_VNET, APIMNetworkMode.EXTERNAL_VNET]:
2949+
infra = infrastructures.Infrastructure(
2950+
infra=INFRASTRUCTURE.APPGW_APIM,
2951+
index=1,
2952+
rg_location='eastus',
2953+
networkMode=network_mode,
2954+
infra_pfs=[pf],
2955+
infra_apis=[api]
2956+
)
2957+
assert infra.networkMode == network_mode
2958+
infra._define_policy_fragments()
2959+
infra._define_apis()
2960+
assert len(infra.pfs) == 7
2961+
assert len(infra.apis) == 2
2962+
2963+
2964+
def test_infrastructure_account_info_retrieval(mock_utils, mock_az):
2965+
"""Test that account info is properly retrieved and stored."""
2966+
mock_az.get_account_info.return_value = (
2967+
2968+
'user-id-12345',
2969+
'tenant-id-67890',
2970+
'subscription-id-abcde'
2971+
)
2972+
2973+
infra = infrastructures.Infrastructure(
2974+
infra=INFRASTRUCTURE.SIMPLE_APIM,
2975+
index=1,
2976+
rg_location='eastus'
2977+
)
2978+
2979+
assert infra.current_user == '[email protected]'
2980+
assert infra.current_user_id == 'user-id-12345'
2981+
assert infra.tenant_id == 'tenant-id-67890'
2982+
assert infra.subscription_id == 'subscription-id-abcde'
2983+
2984+
2985+
def test_infrastructure_with_different_indices(mock_utils):
2986+
"""Test infrastructure with different index values."""
2987+
for index in [1, 2, 5, 10, 100]:
2988+
infra = infrastructures.Infrastructure(
2989+
infra=INFRASTRUCTURE.SIMPLE_APIM,
2990+
index=index,
2991+
rg_location='eastus'
2992+
)
2993+
assert infra.index == index

0 commit comments

Comments
 (0)