flexvolume_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. Copyright 2015 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 flexvolume
  14. import (
  15. "bytes"
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "testing"
  20. "text/template"
  21. "k8s.io/api/core/v1"
  22. utiltesting "k8s.io/client-go/util/testing"
  23. "k8s.io/kubernetes/pkg/volume"
  24. volumetest "k8s.io/kubernetes/pkg/volume/testing"
  25. "k8s.io/utils/exec"
  26. )
  27. const execScriptTempl1 = `#!/usr/bin/env bash
  28. if [ "$1" == "init" -a $# -eq 1 ]; then
  29. echo -n '{
  30. "status": "Success"
  31. }'
  32. exit 0
  33. fi
  34. PATH=$2
  35. if [ "$1" == "attach" -a $# -eq 2 ]; then
  36. echo -n '{
  37. "device": "{{.DevicePath}}",
  38. "status": "Success"
  39. }'
  40. exit 0
  41. elif [ "$1" == "detach" -a $# -eq 2 ]; then
  42. echo -n '{
  43. "status": "Success"
  44. }'
  45. exit 0
  46. elif [ "$1" == "getvolumename" -a $# -eq 4 ]; then
  47. echo -n '{
  48. "status": "Success",
  49. "volume": "fakevolume"
  50. }'
  51. exit 0
  52. elif [ "$1" == "isattached" -a $# -eq 2 ]; then
  53. echo -n '{
  54. "status": "Success",
  55. "attached": true
  56. }'
  57. exit 0
  58. fi
  59. echo -n '{
  60. "status": "Not supported"
  61. }'
  62. exit 1
  63. # Direct the arguments to a file to be tested against later
  64. echo -n $@ &> {{.OutputFile}}
  65. `
  66. const execScriptTempl2 = `#!/usr/bin/env bash
  67. if [ "$1" == "init" -a $# -eq 1 ]; then
  68. echo -n '{
  69. "status": "Success"
  70. }'
  71. exit 0
  72. fi
  73. if [ "$1" == "getvolumename" -a $# -eq 2 ]; then
  74. echo -n '{
  75. "status": "Success",
  76. "volumeName": "fakevolume"
  77. }'
  78. exit 0
  79. elif [ "$1" == "mount" -a $# -eq 4 ]; then
  80. PATH=$2
  81. /bin/mkdir -p $PATH
  82. if [ $? -ne 0 ]; then
  83. echo -n '{
  84. "status": "Failure",
  85. "reason": "Failed to create $PATH"
  86. }'
  87. exit 1
  88. fi
  89. echo -n '{
  90. "status": "Success"
  91. }'
  92. exit 0
  93. elif [ "$1" == "unmount" -a $# -eq 2 ]; then
  94. PATH=$2
  95. /bin/rm -r $PATH
  96. if [ $? -ne 0 ]; then
  97. echo -n '{
  98. "status": "Failure",
  99. "reason": "Failed to cleanup $PATH"
  100. }'
  101. exit 1
  102. fi
  103. echo -n '{
  104. "status": "Success"
  105. }'
  106. exit 0
  107. fi
  108. echo -n '{
  109. "status": "Not Supported"
  110. }'
  111. exit 1
  112. # Direct the arguments to a file to be tested against later
  113. echo -n $@ &> {{.OutputFile}}
  114. `
  115. func installPluginUnderTest(t *testing.T, vendorName, plugName, tmpDir string, execScriptTempl string, execTemplateData *map[string]interface{}) {
  116. vendoredName := plugName
  117. if vendorName != "" {
  118. vendoredName = fmt.Sprintf("%s~%s", vendorName, plugName)
  119. }
  120. pluginDir := filepath.Join(tmpDir, vendoredName)
  121. err := os.MkdirAll(pluginDir, 0777)
  122. if err != nil {
  123. t.Errorf("Failed to create plugin: %v", err)
  124. }
  125. pluginExec := filepath.Join(pluginDir, plugName)
  126. f, err := os.Create(pluginExec)
  127. if err != nil {
  128. t.Errorf("Failed to install plugin")
  129. }
  130. err = f.Chmod(0777)
  131. if err != nil {
  132. t.Errorf("Failed to set exec perms on plugin")
  133. }
  134. if execTemplateData == nil {
  135. execTemplateData = &map[string]interface{}{
  136. "DevicePath": "/dev/sdx",
  137. "OutputFile": filepath.Join(pluginDir, plugName+".out"),
  138. }
  139. }
  140. tObj := template.Must(template.New("test").Parse(execScriptTempl))
  141. buf := &bytes.Buffer{}
  142. if err := tObj.Execute(buf, *execTemplateData); err != nil {
  143. t.Errorf("Error in executing script template - %v", err)
  144. }
  145. execScript := buf.String()
  146. _, err = f.WriteString(execScript)
  147. if err != nil {
  148. t.Errorf("Failed to write plugin exec")
  149. }
  150. f.Close()
  151. }
  152. func TestCanSupport(t *testing.T) {
  153. tmpDir, err := utiltesting.MkTmpdir("flexvolume_test")
  154. if err != nil {
  155. t.Fatalf("error creating temp dir: %v", err)
  156. }
  157. defer os.RemoveAll(tmpDir)
  158. plugMgr := volume.VolumePluginMgr{}
  159. runner := exec.New()
  160. installPluginUnderTest(t, "kubernetes.io", "fakeAttacher", tmpDir, execScriptTempl1, nil)
  161. plugMgr.InitPlugins(nil, GetDynamicPluginProber(tmpDir, runner), volumetest.NewFakeVolumeHost("fake", nil, nil))
  162. plugin, err := plugMgr.FindPluginByName("flexvolume-kubernetes.io/fakeAttacher")
  163. if err != nil {
  164. t.Fatalf("Can't find the plugin by name")
  165. }
  166. if plugin.GetPluginName() != "flexvolume-kubernetes.io/fakeAttacher" {
  167. t.Errorf("Wrong name: %s", plugin.GetPluginName())
  168. }
  169. if !plugin.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{FlexVolume: &v1.FlexVolumeSource{Driver: "kubernetes.io/fakeAttacher"}}}}) {
  170. t.Errorf("Expected true")
  171. }
  172. if !plugin.CanSupport(&volume.Spec{PersistentVolume: &v1.PersistentVolume{Spec: v1.PersistentVolumeSpec{PersistentVolumeSource: v1.PersistentVolumeSource{FlexVolume: &v1.FlexPersistentVolumeSource{Driver: "kubernetes.io/fakeAttacher"}}}}}) {
  173. t.Errorf("Expected true")
  174. }
  175. if plugin.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{}}}) {
  176. t.Errorf("Expected false")
  177. }
  178. }
  179. func TestGetAccessModes(t *testing.T) {
  180. tmpDir, err := utiltesting.MkTmpdir("flexvolume_test")
  181. if err != nil {
  182. t.Fatalf("error creating temp dir: %v", err)
  183. }
  184. defer os.RemoveAll(tmpDir)
  185. plugMgr := volume.VolumePluginMgr{}
  186. runner := exec.New()
  187. installPluginUnderTest(t, "kubernetes.io", "fakeAttacher", tmpDir, execScriptTempl1, nil)
  188. plugMgr.InitPlugins(nil, GetDynamicPluginProber(tmpDir, runner), volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
  189. plugin, err := plugMgr.FindPersistentPluginByName("flexvolume-kubernetes.io/fakeAttacher")
  190. if err != nil {
  191. t.Fatalf("Can't find the plugin by name")
  192. }
  193. if !volumetest.ContainsAccessMode(plugin.GetAccessModes(), v1.ReadWriteOnce) || !volumetest.ContainsAccessMode(plugin.GetAccessModes(), v1.ReadOnlyMany) {
  194. t.Errorf("Expected two AccessModeTypes: %s and %s", v1.ReadWriteOnce, v1.ReadOnlyMany)
  195. }
  196. }