diff --git a/internal/util/cluster_mapping.go b/internal/util/cluster_mapping.go index 51af34f9a..173f5126e 100644 --- a/internal/util/cluster_mapping.go +++ b/internal/util/cluster_mapping.go @@ -65,9 +65,9 @@ type ClusterMappingInfo struct { // ... // }] -func readClusterMappingInfo() (*[]ClusterMappingInfo, error) { +func readClusterMappingInfo(filename string) (*[]ClusterMappingInfo, error) { var info []ClusterMappingInfo - content, err := ioutil.ReadFile(clusterMappingConfigFile) + content, err := ioutil.ReadFile(filename) // #nosec:G304, file inclusion via variable. if err != nil { err = fmt.Errorf("error fetching clusterID mapping %w", err) @@ -83,11 +83,11 @@ func readClusterMappingInfo() (*[]ClusterMappingInfo, error) { return &info, nil } -// GetClusterMappingInfo returns corresponding cluster details like clusterID's -// poolID,fscID lists read from configfile. -func GetClusterMappingInfo(clusterID string) (*[]ClusterMappingInfo, error) { +// getClusterMappingInfo returns corresponding cluster details like clusterID's +// poolID,fscID lists read from 'filename'. +func getClusterMappingInfo(clusterID, filename string) (*[]ClusterMappingInfo, error) { var mappingInfo []ClusterMappingInfo - info, err := readClusterMappingInfo() + info, err := readClusterMappingInfo(filename) if err != nil { // discard not found error as this file is expected to be created by // the admin in case of failover. @@ -114,3 +114,9 @@ func GetClusterMappingInfo(clusterID string) (*[]ClusterMappingInfo, error) { return &mappingInfo, nil } + +// GetClusterMappingInfo returns corresponding cluster details like clusterID's +// poolID,fscID lists read from configfile. +func GetClusterMappingInfo(clusterID string) (*[]ClusterMappingInfo, error) { + return getClusterMappingInfo(clusterID, clusterMappingConfigFile) +} diff --git a/internal/util/cluster_mapping_test.go b/internal/util/cluster_mapping_test.go index 92aa2da5c..2a85ed9ef 100644 --- a/internal/util/cluster_mapping_test.go +++ b/internal/util/cluster_mapping_test.go @@ -138,19 +138,19 @@ func TestGetClusterMappingInfo(t *testing.T) { currentTT := tt t.Run(currentTT.name, func(t *testing.T) { t.Parallel() - clusterMappingConfigFile = fmt.Sprintf("%s/mapping-%d.json", mappingBasePath, currentI) + mappingConfigFile := fmt.Sprintf("%s/mapping-%d.json", mappingBasePath, currentI) if len(currentTT.mappingFilecontent) != 0 { - err = ioutil.WriteFile(clusterMappingConfigFile, currentTT.mappingFilecontent, 0o600) + err = ioutil.WriteFile(mappingConfigFile, currentTT.mappingFilecontent, 0o600) if err != nil { - t.Errorf("GetClusterMappingInfo() error = %v", err) + t.Errorf("failed to write to %q, error = %v", mappingConfigFile, err) } } - data, mErr := GetClusterMappingInfo(currentTT.clusterID) + data, mErr := getClusterMappingInfo(currentTT.clusterID, mappingConfigFile) if (mErr != nil) != currentTT.expectErr { - t.Errorf("GetClusterMappingInfo() error = %v, expected Error %v", mErr, currentTT.expectErr) + t.Errorf("getClusterMappingInfo() error = %v, expected Error %v", mErr, currentTT.expectErr) } if !reflect.DeepEqual(data, currentTT.expectedData) { - t.Errorf("GetClusterMappingInfo() = %v, expected data %v", data, currentTT.expectedData) + t.Errorf("getClusterMappingInfo() = %v, expected data %v", data, currentTT.expectedData) } }) }