Skip to content

Commit b138e6d

Browse files
author
Vladimir Vivien
committed
CSI - Surfaces klog flags; common tests aggregation
1 parent 9e83e6d commit b138e6d

File tree

5 files changed

+150
-112
lines changed

5 files changed

+150
-112
lines changed

pkg/volume/csi/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ go_test(
5050
"csi_drivers_store_test.go",
5151
"csi_mounter_test.go",
5252
"csi_plugin_test.go",
53+
"csi_util_test.go",
5354
"expander_test.go",
5455
],
5556
embed = [":go_default_library"],

pkg/volume/csi/csi_attacher_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,9 @@ func TestAttacherWithCSIDriver(t *testing.T) {
334334
for _, test := range tests {
335335
t.Run(test.name, func(t *testing.T) {
336336
fakeClient := fakeclient.NewSimpleClientset(
337-
getCSIDriver("not-attachable", nil, &bFalse),
338-
getCSIDriver("attachable", nil, &bTrue),
339-
getCSIDriver("nil", nil, nil),
337+
getTestCSIDriver("not-attachable", nil, &bFalse),
338+
getTestCSIDriver("attachable", nil, &bTrue),
339+
getTestCSIDriver("nil", nil, nil),
340340
)
341341
plug, fakeWatcher, tmpDir, _ := newTestWatchPlugin(t, fakeClient)
342342
defer os.RemoveAll(tmpDir)
@@ -418,9 +418,9 @@ func TestAttacherWaitForVolumeAttachmentWithCSIDriver(t *testing.T) {
418418
for _, test := range tests {
419419
t.Run(test.name, func(t *testing.T) {
420420
fakeClient := fakeclient.NewSimpleClientset(
421-
getCSIDriver("not-attachable", nil, &bFalse),
422-
getCSIDriver("attachable", nil, &bTrue),
423-
getCSIDriver("nil", nil, nil),
421+
getTestCSIDriver("not-attachable", nil, &bFalse),
422+
getTestCSIDriver("attachable", nil, &bTrue),
423+
getTestCSIDriver("nil", nil, nil),
424424
)
425425
plug, tmpDir := newTestPlugin(t, fakeClient)
426426
defer os.RemoveAll(tmpDir)

pkg/volume/csi/csi_mounter_test.go

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ limitations under the License.
1717
package csi
1818

1919
import (
20-
"bytes"
21-
"encoding/json"
2220
"fmt"
23-
"io/ioutil"
2421
"math/rand"
2522
"os"
2623
"path"
@@ -30,7 +27,6 @@ import (
3027

3128
api "k8s.io/api/core/v1"
3229
storage "k8s.io/api/storage/v1"
33-
storagev1beta1 "k8s.io/api/storage/v1beta1"
3430
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
3531
"k8s.io/apimachinery/pkg/types"
3632
utilfeature "k8s.io/apiserver/pkg/util/feature"
@@ -147,9 +143,9 @@ func MounterSetUpTests(t *testing.T, podInfoEnabled bool) {
147143
t.Run(test.name, func(t *testing.T) {
148144
klog.Infof("Starting test %s", test.name)
149145
fakeClient := fakeclient.NewSimpleClientset(
150-
getCSIDriver("no-info", &noPodMountInfo, nil),
151-
getCSIDriver("info", &currentPodInfoMount, nil),
152-
getCSIDriver("nil", nil, nil),
146+
getTestCSIDriver("no-info", &noPodMountInfo, nil),
147+
getTestCSIDriver("info", &currentPodInfoMount, nil),
148+
getTestCSIDriver("nil", nil, nil),
153149
)
154150
plug, tmpDir := newTestPlugin(t, fakeClient)
155151
defer os.RemoveAll(tmpDir)
@@ -695,63 +691,3 @@ func TestUnmounterTeardown(t *testing.T) {
695691
}
696692

697693
}
698-
699-
func TestSaveVolumeData(t *testing.T) {
700-
plug, tmpDir := newTestPlugin(t, nil)
701-
defer os.RemoveAll(tmpDir)
702-
testCases := []struct {
703-
name string
704-
data map[string]string
705-
shouldFail bool
706-
}{
707-
{name: "test with data ok", data: map[string]string{"key0": "val0", "_key1": "val1", "key2": "val2"}},
708-
{name: "test with data ok 2 ", data: map[string]string{"_key0_": "val0", "&key1": "val1", "key2": "val2"}},
709-
}
710-
711-
for i, tc := range testCases {
712-
t.Logf("test case: %s", tc.name)
713-
specVolID := fmt.Sprintf("spec-volid-%d", i)
714-
mountDir := path.Join(getTargetPath(testPodUID, specVolID, plug.host), "/mount")
715-
if err := os.MkdirAll(mountDir, 0755); err != nil && !os.IsNotExist(err) {
716-
t.Errorf("failed to create dir [%s]: %v", mountDir, err)
717-
}
718-
719-
err := saveVolumeData(path.Dir(mountDir), volDataFileName, tc.data)
720-
721-
if !tc.shouldFail && err != nil {
722-
t.Errorf("unexpected failure: %v", err)
723-
}
724-
// did file get created
725-
dataDir := getTargetPath(testPodUID, specVolID, plug.host)
726-
file := path.Join(dataDir, volDataFileName)
727-
if _, err := os.Stat(file); err != nil {
728-
t.Errorf("failed to create data dir: %v", err)
729-
}
730-
731-
// validate content
732-
data, err := ioutil.ReadFile(file)
733-
if !tc.shouldFail && err != nil {
734-
t.Errorf("failed to read data file: %v", err)
735-
}
736-
737-
jsonData := new(bytes.Buffer)
738-
if err := json.NewEncoder(jsonData).Encode(tc.data); err != nil {
739-
t.Errorf("failed to encode json: %v", err)
740-
}
741-
if string(data) != jsonData.String() {
742-
t.Errorf("expecting encoded data %v, got %v", string(data), jsonData)
743-
}
744-
}
745-
}
746-
747-
func getCSIDriver(name string, podInfoMount *bool, attachable *bool) *storagev1beta1.CSIDriver {
748-
return &storagev1beta1.CSIDriver{
749-
ObjectMeta: meta.ObjectMeta{
750-
Name: name,
751-
},
752-
Spec: storagev1beta1.CSIDriverSpec{
753-
PodInfoOnMount: podInfoMount,
754-
AttachRequired: attachable,
755-
},
756-
}
757-
}

pkg/volume/csi/csi_plugin_test.go

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"testing"
2626

2727
api "k8s.io/api/core/v1"
28-
"k8s.io/apimachinery/pkg/api/resource"
2928
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
3029
"k8s.io/apimachinery/pkg/types"
3130
"k8s.io/apimachinery/pkg/util/wait"
@@ -86,42 +85,6 @@ func newTestPlugin(t *testing.T, client *fakeclient.Clientset) (*csiPlugin, stri
8685
return csiPlug, tmpDir
8786
}
8887

89-
func makeTestPV(name string, sizeGig int, driverName, volID string) *api.PersistentVolume {
90-
return &api.PersistentVolume{
91-
ObjectMeta: meta.ObjectMeta{
92-
Name: name,
93-
},
94-
Spec: api.PersistentVolumeSpec{
95-
AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce},
96-
Capacity: api.ResourceList{
97-
api.ResourceName(api.ResourceStorage): resource.MustParse(
98-
fmt.Sprintf("%dGi", sizeGig),
99-
),
100-
},
101-
PersistentVolumeSource: api.PersistentVolumeSource{
102-
CSI: &api.CSIPersistentVolumeSource{
103-
Driver: driverName,
104-
VolumeHandle: volID,
105-
ReadOnly: false,
106-
},
107-
},
108-
},
109-
}
110-
}
111-
112-
func makeTestVol(name string, driverName string) *api.Volume {
113-
ro := false
114-
return &api.Volume{
115-
Name: name,
116-
VolumeSource: api.VolumeSource{
117-
CSI: &api.CSIVolumeSource{
118-
Driver: driverName,
119-
ReadOnly: &ro,
120-
},
121-
},
122-
}
123-
}
124-
12588
func registerFakePlugin(pluginName, endpoint string, versions []string, t *testing.T) {
12689
highestSupportedVersions, err := highestSupportedVersion(versions)
12790
if err != nil {
@@ -910,7 +873,7 @@ func TestPluginCanAttach(t *testing.T) {
910873
}
911874

912875
for _, test := range tests {
913-
csiDriver := getCSIDriver(test.driverName, nil, &test.canAttach)
876+
csiDriver := getTestCSIDriver(test.driverName, nil, &test.canAttach)
914877
t.Run(test.name, func(t *testing.T) {
915878
fakeCSIClient := fakeclient.NewSimpleClientset(csiDriver)
916879
plug, tmpDir := newTestPlugin(t, fakeCSIClient)
@@ -971,7 +934,7 @@ func TestPluginFindAttachablePlugin(t *testing.T) {
971934
}
972935
defer os.RemoveAll(tmpDir)
973936

974-
client := fakeclient.NewSimpleClientset(getCSIDriver(test.driverName, nil, &test.canAttach))
937+
client := fakeclient.NewSimpleClientset(getTestCSIDriver(test.driverName, nil, &test.canAttach))
975938
factory := informers.NewSharedInformerFactory(client, csiResyncPeriod)
976939
host := volumetest.NewFakeVolumeHostWithCSINodeName(
977940
tmpDir,

pkg/volume/csi/csi_util_test.go

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package csi
18+
19+
import (
20+
"bytes"
21+
"encoding/json"
22+
"flag"
23+
"fmt"
24+
"io/ioutil"
25+
"os"
26+
"path"
27+
"testing"
28+
29+
api "k8s.io/api/core/v1"
30+
storagev1beta1 "k8s.io/api/storage/v1beta1"
31+
"k8s.io/apimachinery/pkg/api/resource"
32+
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
33+
"k8s.io/klog"
34+
)
35+
36+
// TestMain starting point for all tests.
37+
// Surfaces klog flags by default to enable
38+
// go test -v ./ --args <klog flags>
39+
func TestMain(m *testing.M) {
40+
klog.InitFlags(flag.CommandLine)
41+
os.Exit(m.Run())
42+
}
43+
44+
func makeTestPV(name string, sizeGig int, driverName, volID string) *api.PersistentVolume {
45+
return &api.PersistentVolume{
46+
ObjectMeta: meta.ObjectMeta{
47+
Name: name,
48+
},
49+
Spec: api.PersistentVolumeSpec{
50+
AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce},
51+
Capacity: api.ResourceList{
52+
api.ResourceName(api.ResourceStorage): resource.MustParse(
53+
fmt.Sprintf("%dGi", sizeGig),
54+
),
55+
},
56+
PersistentVolumeSource: api.PersistentVolumeSource{
57+
CSI: &api.CSIPersistentVolumeSource{
58+
Driver: driverName,
59+
VolumeHandle: volID,
60+
ReadOnly: false,
61+
},
62+
},
63+
},
64+
}
65+
}
66+
67+
func makeTestVol(name string, driverName string) *api.Volume {
68+
ro := false
69+
return &api.Volume{
70+
Name: name,
71+
VolumeSource: api.VolumeSource{
72+
CSI: &api.CSIVolumeSource{
73+
Driver: driverName,
74+
ReadOnly: &ro,
75+
},
76+
},
77+
}
78+
}
79+
80+
func getTestCSIDriver(name string, podInfoMount *bool, attachable *bool) *storagev1beta1.CSIDriver {
81+
return &storagev1beta1.CSIDriver{
82+
ObjectMeta: meta.ObjectMeta{
83+
Name: name,
84+
},
85+
Spec: storagev1beta1.CSIDriverSpec{
86+
PodInfoOnMount: podInfoMount,
87+
AttachRequired: attachable,
88+
},
89+
}
90+
}
91+
92+
func TestSaveVolumeData(t *testing.T) {
93+
plug, tmpDir := newTestPlugin(t, nil)
94+
defer os.RemoveAll(tmpDir)
95+
testCases := []struct {
96+
name string
97+
data map[string]string
98+
shouldFail bool
99+
}{
100+
{name: "test with data ok", data: map[string]string{"key0": "val0", "_key1": "val1", "key2": "val2"}},
101+
{name: "test with data ok 2 ", data: map[string]string{"_key0_": "val0", "&key1": "val1", "key2": "val2"}},
102+
}
103+
104+
for i, tc := range testCases {
105+
t.Logf("test case: %s", tc.name)
106+
specVolID := fmt.Sprintf("spec-volid-%d", i)
107+
mountDir := path.Join(getTargetPath(testPodUID, specVolID, plug.host), "/mount")
108+
if err := os.MkdirAll(mountDir, 0755); err != nil && !os.IsNotExist(err) {
109+
t.Errorf("failed to create dir [%s]: %v", mountDir, err)
110+
}
111+
112+
err := saveVolumeData(path.Dir(mountDir), volDataFileName, tc.data)
113+
114+
if !tc.shouldFail && err != nil {
115+
t.Errorf("unexpected failure: %v", err)
116+
}
117+
// did file get created
118+
dataDir := getTargetPath(testPodUID, specVolID, plug.host)
119+
file := path.Join(dataDir, volDataFileName)
120+
if _, err := os.Stat(file); err != nil {
121+
t.Errorf("failed to create data dir: %v", err)
122+
}
123+
124+
// validate content
125+
data, err := ioutil.ReadFile(file)
126+
if !tc.shouldFail && err != nil {
127+
t.Errorf("failed to read data file: %v", err)
128+
}
129+
130+
jsonData := new(bytes.Buffer)
131+
if err := json.NewEncoder(jsonData).Encode(tc.data); err != nil {
132+
t.Errorf("failed to encode json: %v", err)
133+
}
134+
if string(data) != jsonData.String() {
135+
t.Errorf("expecting encoded data %v, got %v", string(data), jsonData)
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)