csi_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. "fmt"
  16. "math/rand"
  17. "os"
  18. "path"
  19. "path/filepath"
  20. "testing"
  21. "time"
  22. api "k8s.io/api/core/v1"
  23. storage "k8s.io/api/storage/v1"
  24. meta "k8s.io/apimachinery/pkg/apis/meta/v1"
  25. "k8s.io/apimachinery/pkg/types"
  26. "k8s.io/apimachinery/pkg/util/wait"
  27. "k8s.io/apimachinery/pkg/watch"
  28. utilfeature "k8s.io/apiserver/pkg/util/feature"
  29. "k8s.io/client-go/informers"
  30. fakeclient "k8s.io/client-go/kubernetes/fake"
  31. utiltesting "k8s.io/client-go/util/testing"
  32. featuregatetesting "k8s.io/component-base/featuregate/testing"
  33. "k8s.io/kubernetes/pkg/features"
  34. "k8s.io/kubernetes/pkg/volume"
  35. volumetest "k8s.io/kubernetes/pkg/volume/testing"
  36. )
  37. // TestCSI_VolumeAll runs a close approximation of volume workflow
  38. // based on operations from the volume manager/reconciler/operation executor
  39. func TestCSI_VolumeAll(t *testing.T) {
  40. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIInlineVolume, true)()
  41. tests := []struct {
  42. name string
  43. specName string
  44. driver string
  45. volName string
  46. specFunc func(specName, driver, volName string) *volume.Spec
  47. podFunc func() *api.Pod
  48. isInline bool
  49. shouldFail bool
  50. }{
  51. {
  52. name: "PersistentVolume",
  53. specName: "pv2",
  54. driver: "simple-driver",
  55. volName: "vol2",
  56. specFunc: func(specName, driver, volName string) *volume.Spec {
  57. return volume.NewSpecFromPersistentVolume(makeTestPV(specName, 20, driver, volName), false)
  58. },
  59. podFunc: func() *api.Pod {
  60. podUID := types.UID(fmt.Sprintf("%08X", rand.Uint64()))
  61. return &api.Pod{ObjectMeta: meta.ObjectMeta{UID: podUID, Namespace: testns}}
  62. },
  63. },
  64. {
  65. name: "ephermeral inline",
  66. driver: "inline-driver-1",
  67. volName: "test.vol2",
  68. specFunc: func(specName, driver, volName string) *volume.Spec {
  69. return volume.NewSpecFromVolume(makeTestVol(specName, driver))
  70. },
  71. podFunc: func() *api.Pod {
  72. podUID := types.UID(fmt.Sprintf("%08X", rand.Uint64()))
  73. return &api.Pod{ObjectMeta: meta.ObjectMeta{UID: podUID, Namespace: testns}}
  74. },
  75. isInline: true,
  76. },
  77. {
  78. name: "missing spec",
  79. specName: "pv2",
  80. driver: "simple-driver",
  81. volName: "vol2",
  82. specFunc: func(specName, driver, volName string) *volume.Spec {
  83. return nil
  84. },
  85. podFunc: func() *api.Pod {
  86. podUID := types.UID(fmt.Sprintf("%08X", rand.Uint64()))
  87. return &api.Pod{ObjectMeta: meta.ObjectMeta{UID: podUID, Namespace: testns}}
  88. },
  89. shouldFail: true,
  90. },
  91. {
  92. name: "incompete spec",
  93. specName: "pv2",
  94. driver: "simple-driver",
  95. volName: "vol2",
  96. specFunc: func(specName, driver, volName string) *volume.Spec {
  97. return &volume.Spec{ReadOnly: true}
  98. },
  99. podFunc: func() *api.Pod {
  100. podUID := types.UID(fmt.Sprintf("%08X", rand.Uint64()))
  101. return &api.Pod{ObjectMeta: meta.ObjectMeta{UID: podUID, Namespace: testns}}
  102. },
  103. shouldFail: true,
  104. },
  105. }
  106. for _, test := range tests {
  107. t.Run(test.name, func(t *testing.T) {
  108. tmpDir, err := utiltesting.MkTmpdir("csi-test")
  109. if err != nil {
  110. t.Fatalf("can't create temp dir: %v", err)
  111. }
  112. defer os.RemoveAll(tmpDir)
  113. client := fakeclient.NewSimpleClientset()
  114. fakeWatcher := watch.NewRaceFreeFake()
  115. factory := informers.NewSharedInformerFactory(client, csiResyncPeriod)
  116. factory.Start(wait.NeverStop)
  117. host := volumetest.NewFakeVolumeHostWithCSINodeName(
  118. tmpDir,
  119. client,
  120. nil,
  121. "csi-node",
  122. factory.Storage().V1beta1().CSIDrivers().Lister(),
  123. )
  124. plugMgr := &volume.VolumePluginMgr{}
  125. plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, host)
  126. csiClient := setupClient(t, true)
  127. volSpec := test.specFunc(test.specName, test.driver, test.volName)
  128. pod := test.podFunc()
  129. attachName := getAttachmentName(test.volName, test.driver, string(host.GetNodeName()))
  130. t.Log("csiTest.VolumeAll starting...")
  131. // *************** Attach/Mount volume resources ****************//
  132. // attach volume
  133. t.Log("csiTest.VolumeAll Attaching volume...")
  134. attachPlug, err := plugMgr.FindAttachablePluginBySpec(volSpec)
  135. if err != nil {
  136. if !test.shouldFail {
  137. t.Fatalf("csiTest.VolumeAll PluginManager.FindAttachablePluginBySpec failed: %v", err)
  138. } else {
  139. t.Log("csiTest.VolumeAll failed: ", err)
  140. return
  141. }
  142. }
  143. if test.isInline && attachPlug != nil {
  144. t.Fatal("csiTest.VolumeAll AttachablePlugin found with ephemeral volume")
  145. }
  146. if !test.isInline && attachPlug == nil {
  147. t.Fatal("csiTest.VolumeAll AttachablePlugin not found with PV")
  148. }
  149. var devicePath string
  150. if attachPlug != nil {
  151. t.Log("csiTest.VolumeAll attacher.Attach starting")
  152. var volAttacher volume.Attacher
  153. volAttacher, err := attachPlug.NewAttacher()
  154. if err != nil {
  155. t.Fatal("csiTest.VolumeAll failed to create new attacher: ", err)
  156. }
  157. // creates VolumeAttachment and blocks until it is marked attached (done by external attacher)
  158. go func(spec *volume.Spec, nodeName types.NodeName) {
  159. attachID, err := volAttacher.Attach(spec, nodeName)
  160. if err != nil {
  161. t.Fatalf("csiTest.VolumeAll attacher.Attach failed: %s", err)
  162. }
  163. t.Logf("csiTest.VolumeAll got attachID %s", attachID)
  164. }(volSpec, host.GetNodeName())
  165. // Simulates external-attacher and marks VolumeAttachment.Status.Attached = true
  166. markVolumeAttached(t, host.GetKubeClient(), fakeWatcher, attachName, storage.VolumeAttachmentStatus{Attached: true})
  167. devicePath, err = volAttacher.WaitForAttach(volSpec, "", pod, 500*time.Millisecond)
  168. if err != nil {
  169. t.Fatal("csiTest.VolumeAll attacher.WaitForAttach failed:", err)
  170. }
  171. if devicePath != attachName {
  172. t.Fatalf("csiTest.VolumeAll attacher.WaitForAttach got unexpected value %s", devicePath)
  173. }
  174. t.Log("csiTest.VolumeAll attacher.WaitForAttach succeeded OK, attachment ID:", devicePath)
  175. } else {
  176. t.Log("csiTest.VolumeAll volume attacher not found, skipping attachment")
  177. }
  178. // Mount Device
  179. t.Log("csiTest.VolumeAll Mouting device...")
  180. devicePlug, err := plugMgr.FindDeviceMountablePluginBySpec(volSpec)
  181. if err != nil {
  182. t.Fatalf("csiTest.VolumeAll PluginManager.FindDeviceMountablePluginBySpec failed: %v", err)
  183. }
  184. if test.isInline && devicePlug != nil {
  185. t.Fatal("csiTest.VolumeAll DeviceMountablePlugin found with ephemeral volume")
  186. }
  187. if !test.isInline && devicePlug == nil {
  188. t.Fatal("csiTest.VolumeAll DeviceMountablePlugin not found with PV")
  189. }
  190. var devMounter volume.DeviceMounter
  191. if devicePlug != nil {
  192. devMounter, err = devicePlug.NewDeviceMounter()
  193. if err != nil {
  194. t.Fatal("csiTest.VolumeAll failed to create new device mounter: ", err)
  195. }
  196. }
  197. if devMounter != nil {
  198. csiDevMounter := devMounter.(*csiAttacher)
  199. csiDevMounter.csiClient = csiClient
  200. devMountPath, err := csiDevMounter.GetDeviceMountPath(volSpec)
  201. if err != nil {
  202. t.Fatalf("csiTest.VolumeAll deviceMounter.GetdeviceMountPath failed %s", err)
  203. }
  204. if err := csiDevMounter.MountDevice(volSpec, devicePath, devMountPath); err != nil {
  205. t.Fatalf("csiTest.VolumeAll deviceMounter.MountDevice failed: %v", err)
  206. }
  207. t.Log("csiTest.VolumeAll device mounted at path:", devMountPath)
  208. } else {
  209. t.Log("csiTest.VolumeAll DeviceMountablePlugin not found, skipping deviceMounter.MountDevice")
  210. }
  211. // mount volume
  212. t.Log("csiTest.VolumeAll Mouting volume...")
  213. volPlug, err := plugMgr.FindPluginBySpec(volSpec)
  214. if err != nil || volPlug == nil {
  215. t.Fatalf("csiTest.VolumeAll PluginMgr.FindPluginBySpec failed: %v", err)
  216. }
  217. if volPlug == nil {
  218. t.Fatalf("csiTest.VolumeAll volumePlugin is nil")
  219. }
  220. if !volPlug.CanSupport(volSpec) {
  221. t.Fatal("csiTest.VolumeAll volumePlugin.CanSupport returned false")
  222. }
  223. mounter, err := volPlug.NewMounter(volSpec, pod, volume.VolumeOptions{})
  224. if err != nil || mounter == nil {
  225. t.Fatalf("csiTest.VolumeAll volPlugin.NewMounter is nil or error: %s", err)
  226. }
  227. if err := mounter.CanMount(); err != nil {
  228. t.Fatal("csiTest.VolumeAll mounter.CanMount failed, skipping mount")
  229. }
  230. var fsGroup *int64
  231. if pod.Spec.SecurityContext != nil && pod.Spec.SecurityContext.FSGroup != nil {
  232. fsGroup = pod.Spec.SecurityContext.FSGroup
  233. }
  234. csiMounter := mounter.(*csiMountMgr)
  235. csiMounter.csiClient = csiClient
  236. var mounterArgs volume.MounterArgs
  237. mounterArgs.FsGroup = fsGroup
  238. if err := csiMounter.SetUp(mounterArgs); err != nil {
  239. t.Fatalf("csiTest.VolumeAll mounter.Setup(fsGroup) failed: %s", err)
  240. }
  241. t.Log("csiTest.VolumeAll mounter.Setup(fsGroup) done OK")
  242. dataFile := filepath.Join(path.Dir(mounter.GetPath()), volDataFileName)
  243. if _, err := os.Stat(dataFile); err != nil {
  244. t.Fatalf("csiTest.VolumeAll meatadata JSON file not found: %s", dataFile)
  245. }
  246. t.Log("csiTest.VolumeAll JSON datafile generated OK:", dataFile)
  247. // ******** Volume Reconstruction ************* //
  248. volPath := path.Dir(csiMounter.GetPath())
  249. t.Log("csiTest.VolumeAll entering plugin.ConstructVolumeSpec for path", volPath)
  250. spec, err := volPlug.ConstructVolumeSpec(test.volName, volPath)
  251. if err != nil {
  252. t.Fatalf("csiTest.VolumeAll plugin.ConstructVolumeSpec failed: %s", err)
  253. } else {
  254. if spec == nil {
  255. t.Fatalf("csiTest.VolumeAll plugin.ConstructVolumeSpec returned nil spec")
  256. } else {
  257. volSpec = spec
  258. if test.isInline {
  259. if volSpec.Volume == nil || volSpec.Volume.CSI == nil {
  260. t.Fatal("csiTest.VolumeAll reconstruction of ephemeral volumeSpec missing CSI Volume source")
  261. }
  262. if volSpec.Volume.CSI.Driver == "" {
  263. t.Fatal("csiTest.VolumeAll reconstruction ephemral volume missing driver name")
  264. }
  265. } else {
  266. if volSpec.PersistentVolume == nil || volSpec.PersistentVolume.Spec.CSI == nil {
  267. t.Fatal("csiTest.VolumeAll reconstruction of volumeSpec missing CSI PersistentVolume source")
  268. }
  269. csi := volSpec.PersistentVolume.Spec.CSI
  270. if csi.Driver == "" {
  271. t.Fatal("csiTest.VolumeAll reconstruction of PV missing driver name")
  272. }
  273. if csi.VolumeHandle == "" {
  274. t.Fatal("csiTest.VolumeAll reconstruction of PV missing volume handle")
  275. }
  276. }
  277. }
  278. }
  279. // ************* Teardown everything **************** //
  280. t.Log("csiTest.VolumeAll Tearing down...")
  281. // unmount volume
  282. t.Log("csiTest.VolumeAll Unmouting volume...")
  283. volPlug, err = plugMgr.FindPluginBySpec(volSpec)
  284. if err != nil || volPlug == nil {
  285. t.Fatalf("csiTest.VolumeAll PluginMgr.FindPluginBySpec failed: %v", err)
  286. }
  287. if volPlug == nil {
  288. t.Fatalf("csiTest.VolumeAll volumePlugin is nil")
  289. }
  290. mounter, err = volPlug.NewMounter(volSpec, pod, volume.VolumeOptions{})
  291. if err != nil || mounter == nil {
  292. t.Fatalf("csiTest.VolumeAll volPlugin.NewMounter is nil or error: %s", err)
  293. }
  294. unmounter, err := volPlug.NewUnmounter(test.specName, pod.GetUID())
  295. if err != nil {
  296. t.Fatal("csiTest.VolumeAll volumePlugin.NewUnmounter failed:", err)
  297. }
  298. csiUnmounter := unmounter.(*csiMountMgr)
  299. csiUnmounter.csiClient = csiClient
  300. if err := csiUnmounter.TearDownAt(mounter.GetPath()); err != nil {
  301. t.Fatal("csiTest.VolumeAll unmounter.TearDownAt failed:", err)
  302. }
  303. t.Log("csiTest.VolumeAll unmounter.TearDownAt done OK for dir:", mounter.GetPath())
  304. // unmount device
  305. t.Log("csiTest.VolumeAll Unmouting device...")
  306. devicePlug, err = plugMgr.FindDeviceMountablePluginBySpec(volSpec)
  307. if err != nil {
  308. t.Fatalf("csiTest.VolumeAll failed to create mountable device plugin: %s", err)
  309. }
  310. if test.isInline && devicePlug != nil {
  311. t.Fatal("csiTest.VolumeAll DeviceMountablePlugin found with ephemeral volume")
  312. }
  313. if !test.isInline && devicePlug == nil {
  314. t.Fatal("csiTest.VolumeAll DeviceMountablePlugin not found with PV")
  315. }
  316. var devUnmounter volume.DeviceUnmounter
  317. if devicePlug != nil {
  318. t.Log("csiTest.VolumeAll found DeviceMountablePlugin, entering device unmouting ...")
  319. devMounter, err = devicePlug.NewDeviceMounter()
  320. if err != nil {
  321. t.Fatal("csiTest.VolumeAll failed to create new device mounter: ", err)
  322. }
  323. devUnmounter, err = devicePlug.NewDeviceUnmounter()
  324. if err != nil {
  325. t.Fatal("csiTest.VolumeAll failed to create new device unmounter: ", err)
  326. }
  327. if devMounter != nil && devUnmounter != nil {
  328. csiDevMounter := devMounter.(*csiAttacher)
  329. csiDevUnmounter := devUnmounter.(*csiAttacher)
  330. csiDevUnmounter.csiClient = csiClient
  331. devMountPath, err := csiDevMounter.GetDeviceMountPath(volSpec)
  332. if err != nil {
  333. t.Fatalf("csiTest.VolumeAll deviceMounter.GetdeviceMountPath failed %s", err)
  334. }
  335. if err := csiDevUnmounter.UnmountDevice(devMountPath); err != nil {
  336. t.Fatalf("csiTest.VolumeAll deviceMounter.UnmountDevice failed: %s", err)
  337. }
  338. t.Log("csiTest.VolumeAll deviceUmounter.UnmountDevice done OK for path", devMountPath)
  339. }
  340. } else {
  341. t.Log("csiTest.VolumeAll DeviceMountablePluginBySpec did not find a plugin, skipping unmounting.")
  342. }
  343. // detach volume
  344. t.Log("csiTest.VolumeAll Detaching volume...")
  345. attachPlug, err = plugMgr.FindAttachablePluginBySpec(volSpec)
  346. if err != nil {
  347. t.Fatalf("csiTest.VolumeAll PluginManager.FindAttachablePluginBySpec failed: %v", err)
  348. }
  349. if test.isInline && attachPlug != nil {
  350. t.Fatal("csiTest.VolumeAll AttachablePlugin found with ephemeral volume")
  351. }
  352. if !test.isInline && attachPlug == nil {
  353. t.Fatal("csiTest.VolumeAll AttachablePlugin not found with PV")
  354. }
  355. if attachPlug != nil {
  356. volDetacher, err := attachPlug.NewDetacher()
  357. if err != nil {
  358. t.Fatal("csiTest.VolumeAll failed to create new detacher: ", err)
  359. }
  360. t.Log("csiTest.VolumeAll preparing detacher.Detach...")
  361. volName, err := volPlug.GetVolumeName(volSpec)
  362. if err != nil {
  363. t.Fatal("csiTest.VolumeAll volumePlugin.GetVolumeName failed:", err)
  364. }
  365. csiDetacher := volDetacher.(*csiAttacher)
  366. csiDetacher.csiClient = csiClient
  367. if err := csiDetacher.Detach(volName, host.GetNodeName()); err != nil {
  368. t.Fatal("csiTest.VolumeAll detacher.Detach failed:", err)
  369. }
  370. t.Log("csiTest.VolumeAll detacher.Detach succeeded for volume", volName)
  371. } else {
  372. t.Log("csiTest.VolumeAll attachable plugin not found for plugin.Detach call, skipping")
  373. }
  374. })
  375. }
  376. }