Merge pull request #51869 from cofyc/fix_persistent_rbd

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>..

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

**What this PR does / why we need it**:

Embedded struct `volume.MetricProvider` is capitalized and should be omitted in JSON marshalling.   It's also a unmarshalable struct, will cause error in `RBDUtil.loadRBD`. 

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #

**Special notes for your reviewer**:

It's a bug I introduced in https://github.com/kubernetes/kubernetes/pull/48486. It's my bad, sorry about that.

**Release note**:

```release-note
```
This commit is contained in:
Kubernetes Submit Queue 2017-09-20 07:50:41 -07:00 committed by GitHub
commit 3fa6761bd1
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)
}
}
}