From 5fc08c33ee2370c7e1e5fd37d7c1c9d1e6cea916 Mon Sep 17 00:00:00 2001 From: Cristian Magherusan-Stanciu Date: Sun, 19 Jul 2026 21:42:14 +0200 Subject: [PATCH] test(providers/azure): make IsConfigured guards deterministic (closes #1465) The three affected tests wrapped assertions in `if !p.IsConfigured()` around a bare `&AzureProvider{}`. With no credProvider injected, IsConfigured() fell through to the real DefaultAzureCredential lookup, which succeeds on virtually any machine, so the guarded negative-path assertions silently never ran. Inject the existing mockCredentialProvider configured to fail so IsConfigured() deterministically returns false, and assert unconditionally instead of behind an if-guard. --- providers/azure/provider_test.go | 58 +++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/providers/azure/provider_test.go b/providers/azure/provider_test.go index 432c9d86c..0071b9473 100644 --- a/providers/azure/provider_test.go +++ b/providers/azure/provider_test.go @@ -300,14 +300,20 @@ func TestAzureProvider_IsConfigured(t *testing.T) { } func TestAzureProvider_GetCredentials_NotConfigured(t *testing.T) { - // Test GetCredentials when Azure is not configured + // Test GetCredentials when Azure is not configured. Inject a mock + // credential provider that fails so IsConfigured() deterministically + // returns false, instead of falling through to the real + // DefaultAzureCredential lookup. p := &AzureProvider{} - // If IsConfigured returns false, GetCredentials should return error - if !p.IsConfigured() { - _, err := p.GetCredentials() - assert.Error(t, err) - assert.Contains(t, err.Error(), "azure provider is not configured") - } + p.SetCredentialProvider(&mockCredentialProvider{ + cred: nil, + err: errors.New("no credentials"), + }) + require.False(t, p.IsConfigured()) + + _, err := p.GetCredentials() + assert.Error(t, err) + assert.Contains(t, err.Error(), "azure provider is not configured") } func TestAzureProvider_ValidateCredentials(t *testing.T) { @@ -375,13 +381,20 @@ func TestAzureProvider_ValidateCredentials(t *testing.T) { } func TestAzureProvider_GetServiceClient_NotConfigured(t *testing.T) { - // Test GetServiceClient when Azure is not configured + // Test GetServiceClient when Azure is not configured. Inject a mock + // credential provider that fails so IsConfigured() deterministically + // returns false, instead of falling through to the real + // DefaultAzureCredential lookup. p := &AzureProvider{} - if !p.IsConfigured() { - _, err := p.GetServiceClient(context.Background(), common.ServiceCompute, "eastus") - assert.Error(t, err) - assert.Contains(t, err.Error(), "azure provider is not configured") - } + p.SetCredentialProvider(&mockCredentialProvider{ + cred: nil, + err: errors.New("no credentials"), + }) + require.False(t, p.IsConfigured()) + + _, err := p.GetServiceClient(context.Background(), common.ServiceCompute, "eastus") + assert.Error(t, err) + assert.Contains(t, err.Error(), "azure provider is not configured") } func TestAzureProvider_GetServiceClient_UnsupportedService(t *testing.T) { @@ -429,13 +442,20 @@ func TestAzureProvider_GetServiceClient_AllServiceTypes(t *testing.T) { } func TestAzureProvider_GetRecommendationsClient_NotConfigured(t *testing.T) { - // Test GetRecommendationsClient when Azure is not configured + // Test GetRecommendationsClient when Azure is not configured. Inject a + // mock credential provider that fails so IsConfigured() deterministically + // returns false, instead of falling through to the real + // DefaultAzureCredential lookup. p := &AzureProvider{} - if !p.IsConfigured() { - _, err := p.GetRecommendationsClient(context.Background()) - assert.Error(t, err) - assert.Contains(t, err.Error(), "azure provider is not configured") - } + p.SetCredentialProvider(&mockCredentialProvider{ + cred: nil, + err: errors.New("no credentials"), + }) + require.False(t, p.IsConfigured()) + + _, err := p.GetRecommendationsClient(context.Background()) + assert.Error(t, err) + assert.Contains(t, err.Error(), "azure provider is not configured") } func TestAzureProvider_GetRecommendationsClient(t *testing.T) {