cephfs_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 cephfs
  14. import (
  15. "os"
  16. "path/filepath"
  17. "testing"
  18. "k8s.io/api/core/v1"
  19. "k8s.io/apimachinery/pkg/types"
  20. utiltesting "k8s.io/client-go/util/testing"
  21. "k8s.io/kubernetes/pkg/util/mount"
  22. "k8s.io/kubernetes/pkg/volume"
  23. volumetest "k8s.io/kubernetes/pkg/volume/testing"
  24. )
  25. func TestCanSupport(t *testing.T) {
  26. tmpDir, err := utiltesting.MkTmpdir("cephTest")
  27. if err != nil {
  28. t.Fatalf("can't make a temp dir: %v", err)
  29. }
  30. defer os.RemoveAll(tmpDir)
  31. plugMgr := volume.VolumePluginMgr{}
  32. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
  33. plug, err := plugMgr.FindPluginByName("kubernetes.io/cephfs")
  34. if err != nil {
  35. t.Errorf("Can't find the plugin by name")
  36. }
  37. if plug.GetPluginName() != "kubernetes.io/cephfs" {
  38. t.Errorf("Wrong name: %s", plug.GetPluginName())
  39. }
  40. if plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{}}}) {
  41. t.Errorf("Expected false")
  42. }
  43. if !plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{CephFS: &v1.CephFSVolumeSource{}}}}) {
  44. t.Errorf("Expected true")
  45. }
  46. }
  47. func TestPlugin(t *testing.T) {
  48. tmpDir, err := utiltesting.MkTmpdir("cephTest")
  49. if err != nil {
  50. t.Fatalf("can't make a temp dir: %v", err)
  51. }
  52. defer os.RemoveAll(tmpDir)
  53. plugMgr := volume.VolumePluginMgr{}
  54. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
  55. plug, err := plugMgr.FindPluginByName("kubernetes.io/cephfs")
  56. if err != nil {
  57. t.Errorf("Can't find the plugin by name")
  58. }
  59. spec := &v1.Volume{
  60. Name: "vol1",
  61. VolumeSource: v1.VolumeSource{
  62. CephFS: &v1.CephFSVolumeSource{
  63. Monitors: []string{"a", "b"},
  64. User: "user",
  65. SecretRef: nil,
  66. SecretFile: "/etc/ceph/user.secret",
  67. },
  68. },
  69. }
  70. mounter, err := plug.(*cephfsPlugin).newMounterInternal(volume.NewSpecFromVolume(spec), types.UID("poduid"), &mount.FakeMounter{}, "secrets")
  71. if err != nil {
  72. t.Errorf("Failed to make a new Mounter: %v", err)
  73. }
  74. if mounter == nil {
  75. t.Errorf("Got a nil Mounter")
  76. }
  77. volumePath := mounter.GetPath()
  78. volpath := filepath.Join(tmpDir, "pods/poduid/volumes/kubernetes.io~cephfs/vol1")
  79. if volumePath != volpath {
  80. t.Errorf("Got unexpected path: %s", volumePath)
  81. }
  82. if err := mounter.SetUp(volume.MounterArgs{}); err != nil {
  83. t.Errorf("Expected success, got: %v", err)
  84. }
  85. if _, err := os.Stat(volumePath); err != nil {
  86. if os.IsNotExist(err) {
  87. t.Errorf("SetUp() failed, volume path not created: %s", volumePath)
  88. } else {
  89. t.Errorf("SetUp() failed: %v", err)
  90. }
  91. }
  92. unmounter, err := plug.(*cephfsPlugin).newUnmounterInternal("vol1", types.UID("poduid"), &mount.FakeMounter{})
  93. if err != nil {
  94. t.Errorf("Failed to make a new Unmounter: %v", err)
  95. }
  96. if unmounter == nil {
  97. t.Errorf("Got a nil Unmounter")
  98. }
  99. if err := unmounter.TearDown(); err != nil {
  100. t.Errorf("Expected success, got: %v", err)
  101. }
  102. if _, err := os.Stat(volumePath); err == nil {
  103. t.Errorf("TearDown() failed, volume path still exists: %s", volumePath)
  104. } else if !os.IsNotExist(err) {
  105. t.Errorf("TearDown() failed: %v", err)
  106. }
  107. }
  108. func TestConstructVolumeSpec(t *testing.T) {
  109. tmpDir, err := utiltesting.MkTmpdir("cephTest")
  110. if err != nil {
  111. t.Fatalf("Can't make a temp dir: %v", err)
  112. }
  113. defer os.RemoveAll(tmpDir)
  114. plugMgr := volume.VolumePluginMgr{}
  115. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
  116. plug, err := plugMgr.FindPluginByName("kubernetes.io/cephfs")
  117. if err != nil {
  118. t.Errorf("can't find cephfs plugin by name")
  119. }
  120. cephfsSpec, err := plug.(*cephfsPlugin).ConstructVolumeSpec("cephfsVolume", "/cephfsVolume/")
  121. if cephfsSpec.Name() != "cephfsVolume" {
  122. t.Errorf("Get wrong cephfs spec name, got: %s", cephfsSpec.Name())
  123. }
  124. }
  125. type testcase struct {
  126. name string
  127. defaultNs string
  128. spec *volume.Spec
  129. // Expected return of the test
  130. expectedName string
  131. expectedNs string
  132. expectedError error
  133. }
  134. func TestGetSecretNameAndNamespaceForPV(t *testing.T) {
  135. tests := []testcase{
  136. {
  137. name: "persistent volume source",
  138. defaultNs: "default",
  139. spec: &volume.Spec{
  140. PersistentVolume: &v1.PersistentVolume{
  141. Spec: v1.PersistentVolumeSpec{
  142. PersistentVolumeSource: v1.PersistentVolumeSource{
  143. CephFS: &v1.CephFSPersistentVolumeSource{
  144. Monitors: []string{"a", "b"},
  145. User: "user",
  146. SecretRef: &v1.SecretReference{
  147. Name: "name",
  148. Namespace: "ns",
  149. },
  150. SecretFile: "/etc/ceph/user.secret",
  151. },
  152. },
  153. },
  154. },
  155. },
  156. expectedName: "name",
  157. expectedNs: "ns",
  158. expectedError: nil,
  159. },
  160. {
  161. name: "persistent volume source without namespace",
  162. defaultNs: "default",
  163. spec: &volume.Spec{
  164. PersistentVolume: &v1.PersistentVolume{
  165. Spec: v1.PersistentVolumeSpec{
  166. PersistentVolumeSource: v1.PersistentVolumeSource{
  167. CephFS: &v1.CephFSPersistentVolumeSource{
  168. Monitors: []string{"a", "b"},
  169. User: "user",
  170. SecretRef: &v1.SecretReference{
  171. Name: "name",
  172. },
  173. SecretFile: "/etc/ceph/user.secret",
  174. },
  175. },
  176. },
  177. },
  178. },
  179. expectedName: "name",
  180. expectedNs: "default",
  181. expectedError: nil,
  182. },
  183. {
  184. name: "pod volume source",
  185. defaultNs: "default",
  186. spec: &volume.Spec{
  187. Volume: &v1.Volume{
  188. VolumeSource: v1.VolumeSource{
  189. CephFS: &v1.CephFSVolumeSource{
  190. Monitors: []string{"a", "b"},
  191. User: "user",
  192. SecretRef: &v1.LocalObjectReference{
  193. Name: "name",
  194. },
  195. SecretFile: "/etc/ceph/user.secret",
  196. },
  197. },
  198. },
  199. },
  200. expectedName: "name",
  201. expectedNs: "default",
  202. expectedError: nil,
  203. },
  204. }
  205. for _, testcase := range tests {
  206. resultName, resultNs, err := getSecretNameAndNamespace(testcase.spec, testcase.defaultNs)
  207. if err != testcase.expectedError || resultName != testcase.expectedName || resultNs != testcase.expectedNs {
  208. t.Errorf("%s failed: expected err=%v ns=%q name=%q, got %v/%q/%q", testcase.name, testcase.expectedError, testcase.expectedNs, testcase.expectedName,
  209. err, resultNs, resultName)
  210. }
  211. }
  212. }
  213. func TestGetAccessModes(t *testing.T) {
  214. tmpDir, err := utiltesting.MkTmpdir("cephfs_test")
  215. if err != nil {
  216. t.Fatalf("error creating temp dir: %v", err)
  217. }
  218. defer os.RemoveAll(tmpDir)
  219. plugMgr := volume.VolumePluginMgr{}
  220. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(tmpDir, nil, nil))
  221. plug, err := plugMgr.FindPersistentPluginByName("kubernetes.io/cephfs")
  222. if err != nil {
  223. t.Errorf("Can't find the plugin by name")
  224. }
  225. modes := plug.GetAccessModes()
  226. for _, v := range modes {
  227. if !volumetest.ContainsAccessMode(modes, v) {
  228. t.Errorf("Expected AccessModeTypes: %s", v)
  229. }
  230. }
  231. }