cleanup: use different file name for testing

For clusterMappingConfigFile using different
file name so that multiple unit test cases can
work without any data race.

Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
This commit is contained in:
Madhu Rajanna 2021-08-09 15:18:15 +05:30 committed by mergify[bot]
parent 3c85219962
commit 6cc37f0a17

View File

@ -88,13 +88,11 @@ func TestGetClusterMappingInfo(t *testing.T) {
clusterID string clusterID string
mappingFilecontent []byte mappingFilecontent []byte
expectedData *[]ClusterMappingInfo expectedData *[]ClusterMappingInfo
createMappingConfigFile bool
expectErr bool expectErr bool
}{ }{
{ {
name: "mapping file not found", name: "mapping file not found",
clusterID: "site-a-clusterid", clusterID: "site-a-clusterid",
createMappingConfigFile: false,
mappingFilecontent: []byte{}, mappingFilecontent: []byte{},
expectedData: nil, expectedData: nil,
expectErr: false, expectErr: false,
@ -102,7 +100,6 @@ func TestGetClusterMappingInfo(t *testing.T) {
{ {
name: "mapping file found with empty data", name: "mapping file found with empty data",
clusterID: "site-a-clusterid", clusterID: "site-a-clusterid",
createMappingConfigFile: true,
mappingFilecontent: []byte{}, mappingFilecontent: []byte{},
expectedData: nil, expectedData: nil,
expectErr: false, expectErr: false,
@ -110,7 +107,6 @@ func TestGetClusterMappingInfo(t *testing.T) {
{ {
name: "cluster-id mapping not found", name: "cluster-id mapping not found",
clusterID: "site-a-clusterid", clusterID: "site-a-clusterid",
createMappingConfigFile: true,
mappingFilecontent: mappingFileContent, mappingFilecontent: mappingFileContent,
expectedData: nil, expectedData: nil,
expectErr: false, expectErr: false,
@ -118,7 +114,6 @@ func TestGetClusterMappingInfo(t *testing.T) {
{ {
name: "site2-storage cluster-id mapping", name: "site2-storage cluster-id mapping",
clusterID: clusterIDOfSite2, clusterID: clusterIDOfSite2,
createMappingConfigFile: true,
mappingFilecontent: mappingFileContent, mappingFilecontent: mappingFileContent,
expectedData: &expectedSite2Data, expectedData: &expectedSite2Data,
expectErr: false, expectErr: false,
@ -126,7 +121,6 @@ func TestGetClusterMappingInfo(t *testing.T) {
{ {
name: "site1-storage cluster-id mapping", name: "site1-storage cluster-id mapping",
clusterID: clusterIDOfSite1, clusterID: clusterIDOfSite1,
createMappingConfigFile: true,
mappingFilecontent: mappingFileContent, mappingFilecontent: mappingFileContent,
expectedData: &expectedSite1To2Data, expectedData: &expectedSite1To2Data,
expectErr: false, expectErr: false,
@ -134,31 +128,29 @@ func TestGetClusterMappingInfo(t *testing.T) {
{ {
name: "site3-storage cluster-id mapping", name: "site3-storage cluster-id mapping",
clusterID: clusterIDOfSite3, clusterID: clusterIDOfSite3,
createMappingConfigFile: true,
mappingFilecontent: mappingFileContent, mappingFilecontent: mappingFileContent,
expectedData: &expectedSite3To2Data, expectedData: &expectedSite3To2Data,
expectErr: false, expectErr: false,
}, },
} }
for _, tt := range tests { for i, tt := range tests {
tt := tt currentI := i
t.Run(tt.name, func(t *testing.T) { currentTT := tt
t.Run(currentTT.name, func(t *testing.T) {
t.Parallel() t.Parallel()
if tt.createMappingConfigFile { clusterMappingConfigFile = fmt.Sprintf("%s/mapping-%d.json", mappingBasePath, currentI)
clusterMappingConfigFile = fmt.Sprintf("%s/mapping.json", mappingBasePath) if len(currentTT.mappingFilecontent) != 0 {
} err = ioutil.WriteFile(clusterMappingConfigFile, currentTT.mappingFilecontent, 0o600)
if len(tt.mappingFilecontent) != 0 {
err = ioutil.WriteFile(clusterMappingConfigFile, tt.mappingFilecontent, 0o600)
if err != nil { if err != nil {
t.Errorf("GetClusterMappingInfo() error = %v", err) t.Errorf("GetClusterMappingInfo() error = %v", err)
} }
} }
data, mErr := GetClusterMappingInfo(tt.clusterID) data, mErr := GetClusterMappingInfo(currentTT.clusterID)
if (mErr != nil) != tt.expectErr { if (mErr != nil) != currentTT.expectErr {
t.Errorf("GetClusterMappingInfo() error = %v, expected Error %v", mErr, tt.expectErr) t.Errorf("GetClusterMappingInfo() error = %v, expected Error %v", mErr, currentTT.expectErr)
} }
if !reflect.DeepEqual(data, tt.expectedData) { if !reflect.DeepEqual(data, currentTT.expectedData) {
t.Errorf("GetClusterMappingInfo() = %v, expected data %v", data, tt.expectedData) t.Errorf("GetClusterMappingInfo() = %v, expected data %v", data, currentTT.expectedData)
} }
}) })
} }