@@ -10,8 +10,8 @@ import (
1010 "testing"
1111
1212 "github.com/Azure/azure-sdk-for-go/sdk/azcore"
13-
1413 "github.com/stretchr/testify/assert"
14+ "sigs.k8s.io/cloud-provider-azure/pkg/azclient"
1515
1616 "github.com/netapp/trident/utils/errors"
1717)
@@ -1324,3 +1324,153 @@ func TestValidateCloudConfiguration_InvalidURL(t *testing.T) {
13241324 })
13251325 }
13261326}
1327+
1328+ // ////////////////////////////////////////////////////////////////////////////
1329+ // Tests for GetAzureCredential
1330+ // ////////////////////////////////////////////////////////////////////////////
1331+
1332+ // Helper function to create a test ClientConfig
1333+ func createTestClientConfig (cloudConfig * CloudConfiguration ) ClientConfig {
1334+ return ClientConfig {
1335+ TenantID : "test-tenant-id" ,
1336+ CloudConfig : cloudConfig ,
1337+ AzureAuthConfig : azclient.AzureAuthConfig {},
1338+ }
1339+ }
1340+
1341+ func TestGetAzureCredential_NoCloudConfig (t * testing.T ) {
1342+ // Test with nil cloud config - should default to AzurePublic
1343+ config := createTestClientConfig (nil )
1344+
1345+ // Validate cloud configuration
1346+ cloudConfig , err := ValidateCloudConfiguration (config .CloudConfig )
1347+ assert .NoError (t , err , "ValidateCloudConfiguration should not error with nil config" )
1348+
1349+ _ , err = GetAzureCredential (config , cloudConfig )
1350+ // Since we don't have actual Azure credentials, we expect this to succeed in creating
1351+ // the auth provider structure, even though it won't return a valid credential
1352+ // The important thing is it doesn't error on cloud configuration validation
1353+ assert .NoError (t , err , "GetAzureCredential should not error with nil cloud config" )
1354+ }
1355+
1356+ func TestGetAzureCredential_EmptyCloudConfig (t * testing.T ) {
1357+ // Test with empty cloud config - should default to AzurePublic
1358+ config := createTestClientConfig (& CloudConfiguration {})
1359+
1360+ // Validate cloud configuration
1361+ cloudConfig , err := ValidateCloudConfiguration (config .CloudConfig )
1362+ assert .NoError (t , err , "ValidateCloudConfiguration should not error with empty config" )
1363+
1364+ _ , err = GetAzureCredential (config , cloudConfig )
1365+ assert .NoError (t , err , "GetAzureCredential should not error with empty cloud config" )
1366+ }
1367+
1368+ func TestGetAzureCredential_NamedClouds (t * testing.T ) {
1369+ tests := []struct {
1370+ name string
1371+ cloudName string
1372+ }{
1373+ {
1374+ name : "AzurePublic" ,
1375+ cloudName : "AzurePublic" ,
1376+ },
1377+ {
1378+ name : "AzureChina" ,
1379+ cloudName : "AzureChina" ,
1380+ },
1381+ {
1382+ name : "AzureGovernment" ,
1383+ cloudName : "AzureGovernment" ,
1384+ },
1385+ }
1386+
1387+ for _ , tt := range tests {
1388+ t .Run (tt .name , func (t * testing.T ) {
1389+ config := createTestClientConfig (& CloudConfiguration {
1390+ CloudName : tt .cloudName ,
1391+ })
1392+
1393+ // Validate cloud configuration
1394+ cloudConfig , err := ValidateCloudConfiguration (config .CloudConfig )
1395+ assert .NoError (t , err , "ValidateCloudConfiguration should not error with valid cloud name: %s" , tt .cloudName )
1396+
1397+ _ , err = GetAzureCredential (config , cloudConfig )
1398+ assert .NoError (t , err , "GetAzureCredential should not error with valid named cloud: %s" , tt .cloudName )
1399+ })
1400+ }
1401+ }
1402+
1403+ func TestGetAzureCredential_CustomCloud (t * testing.T ) {
1404+ config := createTestClientConfig (& CloudConfiguration {
1405+ ADAuthorityHost : "https://login.custom.cloud/" ,
1406+ Audience : "https://management.custom.cloud" ,
1407+ Endpoint : "https://management.custom.cloud" ,
1408+ })
1409+
1410+ // Validate cloud configuration
1411+ cloudConfig , err := ValidateCloudConfiguration (config .CloudConfig )
1412+ assert .NoError (t , err , "ValidateCloudConfiguration should not error with valid custom config" )
1413+
1414+ // Note: This test will fail with a network error because the custom cloud URL is fake.
1415+ // This is expected behavior - validation of custom cloud URLs happens when the auth
1416+ // provider attempts to connect to the endpoint.
1417+ _ , err = GetAzureCredential (config , cloudConfig )
1418+ // We expect an error due to network failure, not a validation error
1419+ assert .Error (t , err , "GetAzureCredential should error when custom cloud endpoint is unreachable" )
1420+ assert .Contains (t , err .Error (), "error creating azure auth provider" )
1421+ }
1422+
1423+ func TestGetAzureCredential_InvalidCloudName (t * testing.T ) {
1424+ config := createTestClientConfig (& CloudConfiguration {
1425+ CloudName : "InvalidCloudName" ,
1426+ })
1427+
1428+ // Validate cloud configuration - should fail for invalid cloud name
1429+ _ , err := ValidateCloudConfiguration (config .CloudConfig )
1430+ assert .Error (t , err , "ValidateCloudConfiguration should error with invalid cloud name" )
1431+ assert .Contains (t , err .Error (), "unknown cloudName" )
1432+ }
1433+
1434+ func TestGetAzureCredential_IncompleteCustomConfig (t * testing.T ) {
1435+ config := createTestClientConfig (& CloudConfiguration {
1436+ ADAuthorityHost : "https://login.custom.cloud/" ,
1437+ Audience : "https://management.custom.cloud" ,
1438+ // Missing Endpoint - should cause validation error
1439+ })
1440+
1441+ // Validate cloud configuration - should fail for incomplete custom config
1442+ _ , err := ValidateCloudConfiguration (config .CloudConfig )
1443+ assert .Error (t , err , "ValidateCloudConfiguration should error with incomplete custom config" )
1444+ assert .Contains (t , err .Error (), "adAuthorityHost, audience, and endpoint are all required" )
1445+ }
1446+
1447+ func TestGetAzureCredential_MutuallyExclusiveConfig (t * testing.T ) {
1448+ config := createTestClientConfig (& CloudConfiguration {
1449+ CloudName : "AzurePublic" ,
1450+ ADAuthorityHost : "https://login.custom.cloud/" ,
1451+ Audience : "https://management.custom.cloud" ,
1452+ Endpoint : "https://management.custom.cloud" ,
1453+ })
1454+
1455+ // Validate cloud configuration - should fail for mutually exclusive config
1456+ _ , err := ValidateCloudConfiguration (config .CloudConfig )
1457+ assert .Error (t , err , "ValidateCloudConfiguration should error with mutually exclusive config" )
1458+ assert .Contains (t , err .Error (), "mutually exclusive" )
1459+ }
1460+
1461+ func TestGetAzureCredential_InvalidURLInCustomConfig (t * testing.T ) {
1462+ config := createTestClientConfig (& CloudConfiguration {
1463+ ADAuthorityHost : "https://login.custom.cloud/" ,
1464+ Audience : "https://management.custom.cloud" ,
1465+ Endpoint : "https://management.custom.cloud" ,
1466+ })
1467+
1468+ // Validate cloud configuration - should succeed (url.Parse is permissive)
1469+ cloudConfig , err := ValidateCloudConfiguration (config .CloudConfig )
1470+ assert .NoError (t , err , "ValidateCloudConfiguration accepts syntactically valid URLs" )
1471+
1472+ // The actual network error will occur when trying to use the credential
1473+ _ , err = GetAzureCredential (config , cloudConfig )
1474+ assert .Error (t , err , "GetAzureCredential should error when custom cloud endpoint is unreachable" )
1475+ assert .Contains (t , err .Error (), "error creating azure auth provider" )
1476+ }
0 commit comments