Skip to content

Commit

Permalink
Fix error message
Browse files Browse the repository at this point in the history
  • Loading branch information
prajwalv-netapp authored Jan 17, 2024
1 parent e8a7ca1 commit b4bff61
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
8 changes: 2 additions & 6 deletions storage_drivers/azure/azure_anf.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ const (
nfsVersion3 = "3"
nfsVersion4 = "4"
nfsVersion41 = "4.1"

DefaultConfigurationFilePath = "/etc/kubernetes/azure.json"
)

var (
Expand Down Expand Up @@ -609,12 +607,10 @@ func (d *NASStorageDriver) initializeAzureSDKClient(
// Azure managed identity
// If cloud provider is set to 'Azure' and cloud identity is not provided during the installation,
// we read the contents of AZURE_CREDENTIAL_FILE to initialize the ANF driver.
if config.ClientSecret == "" && config.ClientID == "" {
if config.ClientSecret == "" && config.ClientID == "" && os.Getenv("AZURE_CREDENTIAL_FILE") != "" {
credFilePath := os.Getenv("AZURE_CREDENTIAL_FILE")
if credFilePath == "" {
credFilePath = DefaultConfigurationFilePath
}
Logc(ctx).WithField("credFilePath", credFilePath).Info("Using Azure credential config file.")

credFile, err := os.ReadFile(credFilePath)
if err != nil {
return errors.New("error reading from azure config file: " + err.Error())
Expand Down
6 changes: 2 additions & 4 deletions storage_drivers/azure/azure_anf_subvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,12 +595,10 @@ func (d *NASBlockStorageDriver) initializeAzureSDKClient(
// Azure managed identity
// If cloud provider is set to 'Azure' and cloud identity is not provided during the installation,
// we read the contents of AZURE_CREDENTIAL_FILE to initialize the ANF Subvolume driver.
if config.ClientSecret == "" && config.ClientID == "" {
if config.ClientSecret == "" && config.ClientID == "" && os.Getenv("AZURE_CREDENTIAL_FILE") != "" {
credFilePath := os.Getenv("AZURE_CREDENTIAL_FILE")
if credFilePath == "" {
credFilePath = DefaultConfigurationFilePath
}
Logc(ctx).WithField("credFilePath", credFilePath).Info("Using Azure credential config file.")

credFile, err := os.ReadFile(credFilePath)
if err != nil {
return errors.New("error reading from azure config file: " + err.Error())
Expand Down
16 changes: 16 additions & 0 deletions storage_drivers/azure/azure_anf_subvolume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,17 @@ func TestSubvolumeInitialize_NOClientID_NOClientSecret(t *testing.T) {
func TestSubvolumeInitialize_NOClientID_NOClientSecret_Error_ReadingFile(t *testing.T) {
commonConfig, _ := getStructsForSubvolumeInitialize()

configFile, _ := os.Getwd()

envVariable := map[string]string{
"AZURE_CREDENTIAL_FILE": configFile + "azure.json",
}

// Set required environment variable for testing
for key, value := range envVariable {
_ = os.Setenv(key, value)
}

configJSON := `
{
"version": 1,
Expand All @@ -802,6 +813,11 @@ func TestSubvolumeInitialize_NOClientID_NOClientSecret_Error_ReadingFile(t *test

assert.Error(t, result, "initialized")
assert.False(t, driver.Initialized(), "initialized")

// Unset environment variable
for key := range envVariable {
_ = os.Unsetenv(key)
}
}

func TestSubvolumeInitialize_NOClientID_NOClientSecret_Error_JSONUnmarshal(t *testing.T) {
Expand Down
26 changes: 21 additions & 5 deletions storage_drivers/azure/azure_anf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,17 @@ func TestInitialize_NOClientID_NOClientSecret_Error_ReadingFile(t *testing.T) {
DebugTraceFlags: debugTraceFlags,
}

configFile, _ := os.Getwd()

envVariable := map[string]string{
"AZURE_CREDENTIAL_FILE": configFile + "azure.json",
}

// Set required environment variable for testing
for key, value := range envVariable {
_ = os.Setenv(key, value)
}

configJSON := `
{
"version": 1,
Expand All @@ -565,6 +576,11 @@ func TestInitialize_NOClientID_NOClientSecret_Error_ReadingFile(t *testing.T) {

assert.Error(t, result, "initialize did not fail")
assert.False(t, driver.Initialized(), "initialized")

// Unset environment variable
for key := range envVariable {
_ = os.Unsetenv(key)
}
}

func TestInitialize_NOClientID_NOClientSecret_Error_JSONUnmarshal(t *testing.T) {
Expand Down Expand Up @@ -603,15 +619,15 @@ func TestInitialize_NOClientID_NOClientSecret_Error_JSONUnmarshal(t *testing.T)
_ = os.WriteFile(envVariable["AZURE_CREDENTIAL_FILE"], []byte(configFileContent), os.ModePerm)

configJSON := `
{
{
"version": 1,
"storageDriverName": "azure-netapp-files",
"serviceLevel": "Premium",
"debugTraceFlags": {"method": true, "api": true, "discovery": true},
"storageDriverName": "azure-netapp-files",
"serviceLevel": "Premium",
"debugTraceFlags": {"method": true, "api": true, "discovery": true},
"capacityPools": ["RG1/NA1/CP1", "RG1/NA1/CP2"],
"virtualNetwork": "VN1",
"subnet": "RG1/VN1/SN1"
}`
}`

_, driver := newMockANFDriver(t)

Expand Down

0 comments on commit b4bff61

Please sign in to comment.