csi_util_test.go 4.1 KB

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