RBD Plugin: Omit volume.MetricsProvider field and add some testcases.

pull/6/head
Yecheng Fu 2017-09-03 14:31:01 +00:00
parent 4af900b43f
commit 8fa92e48a8
3 changed files with 88 additions and 2 deletions

View File

@ -40,6 +40,7 @@ go_test(
"//pkg/util/mount:go_default_library",
"//pkg/volume:go_default_library",
"//pkg/volume/testing:go_default_library",
"//vendor/github.com/stretchr/testify/assert:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",

View File

@ -409,8 +409,8 @@ type rbd struct {
mounter *mount.SafeFormatAndMount
exec mount.Exec
// Utility interface that provides API calls to the provider to attach/detach disks.
manager diskManager
volume.MetricsProvider
manager diskManager
volume.MetricsProvider `json:"-"`
}
func (rbd *rbd) GetPath() string {

View File

@ -18,9 +18,13 @@ package rbd
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
@ -244,3 +248,84 @@ func TestPersistentClaimReadOnlyFlag(t *testing.T) {
t.Errorf("Expected true for mounter.IsReadOnly")
}
}
func TestPersistAndLoadRBD(t *testing.T) {
tmpDir, err := utiltesting.MkTmpdir("rbd_test")
if err != nil {
t.Fatalf("error creating temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
testcases := []struct {
rbdMounter rbdMounter
expectedJSONStr string
expectedLoadedRBDMounter rbdMounter
}{
{
rbdMounter{},
`{"Mon":null,"Id":"","Keyring":"","Secret":""}`,
rbdMounter{},
},
{
rbdMounter{
rbd: &rbd{
podUID: "poduid",
Pool: "kube",
Image: "some-test-image",
ReadOnly: false,
MetricsProvider: volume.NewMetricsStatFS("/tmp"),
},
Mon: []string{"127.0.0.1"},
Id: "kube",
Keyring: "",
Secret: "QVFEcTdKdFp4SmhtTFJBQUNwNDI3UnhGRzBvQ1Y0SUJwLy9pRUE9PQ==",
},
`
{
"Pool": "kube",
"Image": "some-test-image",
"ReadOnly": false,
"Mon": ["127.0.0.1"],
"Id": "kube",
"Keyring": "",
"Secret": "QVFEcTdKdFp4SmhtTFJBQUNwNDI3UnhGRzBvQ1Y0SUJwLy9pRUE9PQ=="
}
`,
rbdMounter{
rbd: &rbd{
Pool: "kube",
Image: "some-test-image",
ReadOnly: false,
},
Mon: []string{"127.0.0.1"},
Id: "kube",
Keyring: "",
Secret: "QVFEcTdKdFp4SmhtTFJBQUNwNDI3UnhGRzBvQ1Y0SUJwLy9pRUE9PQ==",
},
},
}
util := &RBDUtil{}
for _, c := range testcases {
err = util.persistRBD(c.rbdMounter, tmpDir)
if err != nil {
t.Errorf("failed to persist rbd: %v, err: %v", c.rbdMounter, err)
}
jsonFile := filepath.Join(tmpDir, "rbd.json")
jsonData, err := ioutil.ReadFile(jsonFile)
if err != nil {
t.Errorf("failed to read json file %s: %v", jsonFile, err)
}
if !assert.JSONEq(t, c.expectedJSONStr, string(jsonData)) {
t.Errorf("json file does not match expected one: %s, should be %s", string(jsonData), c.expectedJSONStr)
}
tmpRBDMounter := rbdMounter{}
err = util.loadRBD(&tmpRBDMounter, tmpDir)
if err != nil {
t.Errorf("faild to load rbd: %v", err)
}
if !reflect.DeepEqual(tmpRBDMounter, c.expectedLoadedRBDMounter) {
t.Errorf("loaded rbd does not equal to expected one: %v, should be %v", tmpRBDMounter, c.rbdMounter)
}
}
}