|
| 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