2019-04-22 21:35:39 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 ceph-csi authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2022-03-31 13:29:33 +00:00
|
|
|
"encoding/json"
|
2019-04-22 21:35:39 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2021-07-13 12:21:05 +00:00
|
|
|
var (
|
|
|
|
basePath = "./test_artifacts"
|
|
|
|
csiClusters = "csi-clusters.json"
|
|
|
|
pathToConfig = basePath + "/" + csiClusters
|
|
|
|
clusterID1 = "test1"
|
|
|
|
clusterID2 = "test2"
|
|
|
|
)
|
2019-04-22 21:35:39 +00:00
|
|
|
|
|
|
|
func cleanupTestData() {
|
|
|
|
os.RemoveAll(basePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCSIConfig(t *testing.T) {
|
2021-06-02 09:55:53 +00:00
|
|
|
t.Parallel()
|
2019-04-22 21:35:39 +00:00
|
|
|
var err error
|
|
|
|
var data string
|
|
|
|
var content string
|
|
|
|
|
|
|
|
defer cleanupTestData()
|
|
|
|
|
2021-07-13 12:21:05 +00:00
|
|
|
err = os.MkdirAll(basePath, 0o700)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test setup error %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TEST: Should fail as clusterid file is missing
|
|
|
|
_, err = Mons(pathToConfig, clusterID1)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Failed: expected error due to missing config")
|
|
|
|
}
|
|
|
|
|
|
|
|
data = ""
|
2022-01-21 09:28:27 +00:00
|
|
|
err = os.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test setup error %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TEST: Should fail as file is empty
|
|
|
|
content, err = Mons(pathToConfig, clusterID1)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Failed: want (%s), got (%s)", data, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
data = "[{\"clusterIDBad\":\"" + clusterID2 + "\",\"monitors\":[\"mon1\",\"mon2\",\"mon3\"]}]"
|
2022-01-21 09:28:27 +00:00
|
|
|
err = os.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test setup error %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TEST: Should fail as clusterID data is malformed
|
|
|
|
content, err = Mons(pathToConfig, clusterID2)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Failed: want (%s), got (%s)", data, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
data = "[{\"clusterID\":\"" + clusterID2 + "\",\"monitorsBad\":[\"mon1\",\"mon2\",\"mon3\"]}]"
|
2022-01-21 09:28:27 +00:00
|
|
|
err = os.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test setup error %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TEST: Should fail as monitors key is incorrect/missing
|
|
|
|
content, err = Mons(pathToConfig, clusterID2)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Failed: want (%s), got (%s)", data, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
data = "[{\"clusterID\":\"" + clusterID2 + "\",\"monitors\":[\"mon1\",2,\"mon3\"]}]"
|
2022-01-21 09:28:27 +00:00
|
|
|
err = os.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test setup error %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TEST: Should fail as monitor data is malformed
|
|
|
|
content, err = Mons(pathToConfig, clusterID2)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Failed: want (%s), got (%s)", data, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
data = "[{\"clusterID\":\"" + clusterID2 + "\",\"monitors\":[\"mon1\",\"mon2\",\"mon3\"]}]"
|
2022-01-21 09:28:27 +00:00
|
|
|
err = os.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test setup error %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TEST: Should fail as clusterID is not present in config
|
|
|
|
content, err = Mons(pathToConfig, clusterID1)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Failed: want (%s), got (%s)", data, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TEST: Should pass as clusterID is present in config
|
|
|
|
content, err = Mons(pathToConfig, clusterID2)
|
|
|
|
if err != nil || content != "mon1,mon2,mon3" {
|
|
|
|
t.Errorf("Failed: want (%s), got (%s) (%v)", "mon1,mon2,mon3", content, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data = "[{\"clusterID\":\"" + clusterID2 + "\",\"monitors\":[\"mon1\",\"mon2\",\"mon3\"]}," +
|
|
|
|
"{\"clusterID\":\"" + clusterID1 + "\",\"monitors\":[\"mon4\",\"mon5\",\"mon6\"]}]"
|
2022-01-21 09:28:27 +00:00
|
|
|
err = os.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600)
|
2019-04-22 21:35:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test setup error %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TEST: Should pass as clusterID is present in config
|
|
|
|
content, err = Mons(pathToConfig, clusterID1)
|
|
|
|
if err != nil || content != "mon4,mon5,mon6" {
|
|
|
|
t.Errorf("Failed: want (%s), got (%s) (%v)", "mon4,mon5,mon6", content, err)
|
|
|
|
}
|
2021-09-16 05:34:06 +00:00
|
|
|
|
|
|
|
data = "[{\"clusterID\":\"" + clusterID2 + "\",\"monitors\":[\"mon1\",\"mon2\",\"mon3\"]}," +
|
|
|
|
"{\"clusterID\":\"" + clusterID1 + "\",\"monitors\":[\"mon4\",\"mon5\",\"mon6\"]}]"
|
2022-01-21 09:28:27 +00:00
|
|
|
err = os.WriteFile(basePath+"/"+csiClusters, []byte(data), 0o600)
|
2021-09-16 05:34:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test setup error %s", err)
|
|
|
|
}
|
2019-04-22 21:35:39 +00:00
|
|
|
}
|
2022-03-31 13:29:33 +00:00
|
|
|
|
2022-04-18 05:57:04 +00:00
|
|
|
func TestGetRBDNetNamespaceFilePath(t *testing.T) {
|
2022-03-31 13:29:33 +00:00
|
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
clusterID string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
2022-04-18 05:57:04 +00:00
|
|
|
name: "get RBD NetNamespaceFilePath for cluster-1",
|
2022-03-31 13:29:33 +00:00
|
|
|
clusterID: "cluster-1",
|
|
|
|
want: "/var/lib/kubelet/plugins/rbd.ceph.csi.com/cluster1-net",
|
|
|
|
},
|
|
|
|
{
|
2022-04-18 05:57:04 +00:00
|
|
|
name: "get RBD NetNamespaceFilePath for cluster-2",
|
2022-03-31 13:29:33 +00:00
|
|
|
clusterID: "cluster-2",
|
|
|
|
want: "/var/lib/kubelet/plugins/rbd.ceph.csi.com/cluster2-net",
|
|
|
|
},
|
|
|
|
{
|
2022-04-18 05:57:04 +00:00
|
|
|
name: "when RBD NetNamespaceFilePath is empty",
|
2022-03-31 13:29:33 +00:00
|
|
|
clusterID: "cluster-3",
|
|
|
|
want: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
csiConfig := []ClusterInfo{
|
|
|
|
{
|
2022-04-18 05:57:04 +00:00
|
|
|
ClusterID: "cluster-1",
|
|
|
|
Monitors: []string{"ip-1", "ip-2"},
|
|
|
|
RBD: struct {
|
|
|
|
NetNamespaceFilePath string `json:"netNamespaceFilePath"`
|
2022-04-18 06:08:52 +00:00
|
|
|
RadosNamespace string `json:"radosNamespace"`
|
2022-04-18 05:57:04 +00:00
|
|
|
}{
|
|
|
|
NetNamespaceFilePath: "/var/lib/kubelet/plugins/rbd.ceph.csi.com/cluster1-net",
|
|
|
|
},
|
2022-03-31 13:29:33 +00:00
|
|
|
},
|
|
|
|
{
|
2022-04-18 05:57:04 +00:00
|
|
|
ClusterID: "cluster-2",
|
|
|
|
Monitors: []string{"ip-3", "ip-4"},
|
|
|
|
RBD: struct {
|
|
|
|
NetNamespaceFilePath string `json:"netNamespaceFilePath"`
|
2022-04-18 06:08:52 +00:00
|
|
|
RadosNamespace string `json:"radosNamespace"`
|
2022-04-18 05:57:04 +00:00
|
|
|
}{
|
|
|
|
NetNamespaceFilePath: "/var/lib/kubelet/plugins/rbd.ceph.csi.com/cluster2-net",
|
|
|
|
},
|
2022-03-31 13:29:33 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ClusterID: "cluster-3",
|
|
|
|
Monitors: []string{"ip-5", "ip-6"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
csiConfigFileContent, err := json.Marshal(csiConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to marshal csi config info %v", err)
|
|
|
|
}
|
|
|
|
tmpConfPath := t.TempDir() + "/ceph-csi.json"
|
|
|
|
err = os.WriteFile(tmpConfPath, csiConfigFileContent, 0o600)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to write %s file content: %v", CsiConfigFile, err)
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
ts := tt
|
|
|
|
t.Run(ts.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2022-04-18 05:57:04 +00:00
|
|
|
got, err := GetRBDNetNamespaceFilePath(tmpConfPath, ts.clusterID)
|
2022-03-31 13:29:33 +00:00
|
|
|
if err != nil {
|
2022-04-18 05:57:04 +00:00
|
|
|
t.Errorf("GetRBDNetNamespaceFilePath() error = %v", err)
|
2022-03-31 13:29:33 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got != ts.want {
|
2022-04-18 05:57:04 +00:00
|
|
|
t.Errorf("GetRBDNetNamespaceFilePath() = %v, want %v", got, ts.want)
|
2022-03-31 13:29:33 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|