csi_block_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. Copyright 2018 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. "context"
  16. "errors"
  17. "fmt"
  18. "os"
  19. "path/filepath"
  20. "testing"
  21. api "k8s.io/api/core/v1"
  22. "k8s.io/api/storage/v1beta1"
  23. meta "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  25. utilfeature "k8s.io/apiserver/pkg/util/feature"
  26. fakeclient "k8s.io/client-go/kubernetes/fake"
  27. featuregatetesting "k8s.io/component-base/featuregate/testing"
  28. "k8s.io/kubernetes/pkg/features"
  29. "k8s.io/kubernetes/pkg/volume"
  30. )
  31. func prepareBlockMapperTest(plug *csiPlugin, specVolumeName string, t *testing.T) (*csiBlockMapper, *volume.Spec, *api.PersistentVolume, error) {
  32. registerFakePlugin(testDriver, "endpoint", []string{"1.0.0"}, t)
  33. pv := makeTestPV(specVolumeName, 10, testDriver, testVol)
  34. spec := volume.NewSpecFromPersistentVolume(pv, pv.Spec.PersistentVolumeSource.CSI.ReadOnly)
  35. mapper, err := plug.NewBlockVolumeMapper(
  36. spec,
  37. &api.Pod{ObjectMeta: meta.ObjectMeta{UID: testPodUID, Namespace: testns}},
  38. volume.VolumeOptions{},
  39. )
  40. if err != nil {
  41. return nil, nil, nil, fmt.Errorf("Failed to make a new Mapper: %v", err)
  42. }
  43. csiMapper := mapper.(*csiBlockMapper)
  44. return csiMapper, spec, pv, nil
  45. }
  46. func prepareBlockUnmapperTest(plug *csiPlugin, specVolumeName string, t *testing.T) (*csiBlockMapper, *volume.Spec, *api.PersistentVolume, error) {
  47. registerFakePlugin(testDriver, "endpoint", []string{"1.0.0"}, t)
  48. pv := makeTestPV(specVolumeName, 10, testDriver, testVol)
  49. spec := volume.NewSpecFromPersistentVolume(pv, pv.Spec.PersistentVolumeSource.CSI.ReadOnly)
  50. // save volume data
  51. dir := getVolumeDeviceDataDir(pv.ObjectMeta.Name, plug.host)
  52. if err := os.MkdirAll(dir, 0755); err != nil && !os.IsNotExist(err) {
  53. t.Errorf("failed to create dir [%s]: %v", dir, err)
  54. }
  55. if err := saveVolumeData(
  56. dir,
  57. volDataFileName,
  58. map[string]string{
  59. volDataKey.specVolID: pv.ObjectMeta.Name,
  60. volDataKey.driverName: testDriver,
  61. volDataKey.volHandle: testVol,
  62. },
  63. ); err != nil {
  64. t.Fatalf("failed to save volume data: %v", err)
  65. }
  66. unmapper, err := plug.NewBlockVolumeUnmapper(pv.ObjectMeta.Name, testPodUID)
  67. if err != nil {
  68. t.Fatalf("failed to make a new Unmapper: %v", err)
  69. }
  70. csiUnmapper := unmapper.(*csiBlockMapper)
  71. csiUnmapper.csiClient = setupClient(t, true)
  72. return csiUnmapper, spec, pv, nil
  73. }
  74. func TestBlockMapperGetGlobalMapPath(t *testing.T) {
  75. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIBlockVolume, true)()
  76. plug, tmpDir := newTestPlugin(t, nil)
  77. defer os.RemoveAll(tmpDir)
  78. // TODO (vladimirvivien) specName with slashes will not work
  79. testCases := []struct {
  80. name string
  81. specVolumeName string
  82. path string
  83. }{
  84. {
  85. name: "simple specName",
  86. specVolumeName: "spec-0",
  87. path: filepath.Join(tmpDir, fmt.Sprintf("plugins/kubernetes.io/csi/volumeDevices/%s/%s", "spec-0", "dev")),
  88. },
  89. {
  90. name: "specName with dots",
  91. specVolumeName: "test.spec.1",
  92. path: filepath.Join(tmpDir, fmt.Sprintf("plugins/kubernetes.io/csi/volumeDevices/%s/%s", "test.spec.1", "dev")),
  93. },
  94. }
  95. for _, tc := range testCases {
  96. t.Logf("test case: %s", tc.name)
  97. csiMapper, spec, _, err := prepareBlockMapperTest(plug, tc.specVolumeName, t)
  98. if err != nil {
  99. t.Fatalf("Failed to make a new Mapper: %v", err)
  100. }
  101. path, err := csiMapper.GetGlobalMapPath(spec)
  102. if err != nil {
  103. t.Errorf("mapper GetGlobalMapPath failed: %v", err)
  104. }
  105. if tc.path != path {
  106. t.Errorf("expecting path %s, got %s", tc.path, path)
  107. }
  108. }
  109. }
  110. func TestBlockMapperGetStagingPath(t *testing.T) {
  111. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIBlockVolume, true)()
  112. plug, tmpDir := newTestPlugin(t, nil)
  113. defer os.RemoveAll(tmpDir)
  114. testCases := []struct {
  115. name string
  116. specVolumeName string
  117. path string
  118. }{
  119. {
  120. name: "simple specName",
  121. specVolumeName: "spec-0",
  122. path: filepath.Join(tmpDir, fmt.Sprintf("plugins/kubernetes.io/csi/volumeDevices/staging/%s", "spec-0")),
  123. },
  124. {
  125. name: "specName with dots",
  126. specVolumeName: "test.spec.1",
  127. path: filepath.Join(tmpDir, fmt.Sprintf("plugins/kubernetes.io/csi/volumeDevices/staging/%s", "test.spec.1")),
  128. },
  129. }
  130. for _, tc := range testCases {
  131. t.Logf("test case: %s", tc.name)
  132. csiMapper, _, _, err := prepareBlockMapperTest(plug, tc.specVolumeName, t)
  133. if err != nil {
  134. t.Fatalf("Failed to make a new Mapper: %v", err)
  135. }
  136. path := csiMapper.getStagingPath()
  137. if tc.path != path {
  138. t.Errorf("expecting path %s, got %s", tc.path, path)
  139. }
  140. }
  141. }
  142. func TestBlockMapperGetPublishPath(t *testing.T) {
  143. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIBlockVolume, true)()
  144. plug, tmpDir := newTestPlugin(t, nil)
  145. defer os.RemoveAll(tmpDir)
  146. testCases := []struct {
  147. name string
  148. specVolumeName string
  149. path string
  150. }{
  151. {
  152. name: "simple specName",
  153. specVolumeName: "spec-0",
  154. path: filepath.Join(tmpDir, fmt.Sprintf("plugins/kubernetes.io/csi/volumeDevices/publish/%s/%s", "spec-0", testPodUID)),
  155. },
  156. {
  157. name: "specName with dots",
  158. specVolumeName: "test.spec.1",
  159. path: filepath.Join(tmpDir, fmt.Sprintf("plugins/kubernetes.io/csi/volumeDevices/publish/%s/%s", "test.spec.1", testPodUID)),
  160. },
  161. }
  162. for _, tc := range testCases {
  163. t.Logf("test case: %s", tc.name)
  164. csiMapper, _, _, err := prepareBlockMapperTest(plug, tc.specVolumeName, t)
  165. if err != nil {
  166. t.Fatalf("Failed to make a new Mapper: %v", err)
  167. }
  168. path := csiMapper.getPublishPath()
  169. if tc.path != path {
  170. t.Errorf("expecting path %s, got %s", tc.path, path)
  171. }
  172. }
  173. }
  174. func TestBlockMapperGetDeviceMapPath(t *testing.T) {
  175. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIBlockVolume, true)()
  176. plug, tmpDir := newTestPlugin(t, nil)
  177. defer os.RemoveAll(tmpDir)
  178. testCases := []struct {
  179. name string
  180. specVolumeName string
  181. path string
  182. }{
  183. {
  184. name: "simple specName",
  185. specVolumeName: "spec-0",
  186. path: filepath.Join(tmpDir, fmt.Sprintf("pods/%s/volumeDevices/kubernetes.io~csi", testPodUID)),
  187. },
  188. {
  189. name: "specName with dots",
  190. specVolumeName: "test.spec.1",
  191. path: filepath.Join(tmpDir, fmt.Sprintf("pods/%s/volumeDevices/kubernetes.io~csi", testPodUID)),
  192. },
  193. }
  194. for _, tc := range testCases {
  195. t.Logf("test case: %s", tc.name)
  196. csiMapper, _, _, err := prepareBlockMapperTest(plug, tc.specVolumeName, t)
  197. if err != nil {
  198. t.Fatalf("Failed to make a new Mapper: %v", err)
  199. }
  200. path, volName := csiMapper.GetPodDeviceMapPath()
  201. if tc.path != path {
  202. t.Errorf("expecting path %s, got %s", tc.path, path)
  203. }
  204. if tc.specVolumeName != volName {
  205. t.Errorf("expecting volName %s, got %s", tc.specVolumeName, volName)
  206. }
  207. }
  208. }
  209. func TestBlockMapperSetupDevice(t *testing.T) {
  210. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIBlockVolume, true)()
  211. plug, tmpDir := newTestPlugin(t, nil)
  212. defer os.RemoveAll(tmpDir)
  213. csiMapper, _, pv, err := prepareBlockMapperTest(plug, "test-pv", t)
  214. if err != nil {
  215. t.Fatalf("Failed to make a new Mapper: %v", err)
  216. }
  217. pvName := pv.GetName()
  218. nodeName := string(plug.host.GetNodeName())
  219. csiMapper.csiClient = setupClient(t, true)
  220. attachID := getAttachmentName(csiMapper.volumeID, string(csiMapper.driverName), string(nodeName))
  221. attachment := makeTestAttachment(attachID, nodeName, pvName)
  222. attachment.Status.Attached = true
  223. _, err = csiMapper.k8s.StorageV1().VolumeAttachments().Create(context.TODO(), attachment, metav1.CreateOptions{})
  224. if err != nil {
  225. t.Fatalf("failed to setup VolumeAttachment: %v", err)
  226. }
  227. t.Log("created attachement ", attachID)
  228. err = csiMapper.SetUpDevice()
  229. if err != nil {
  230. t.Fatalf("mapper failed to SetupDevice: %v", err)
  231. }
  232. // Check if NodeStageVolume staged to the right path
  233. stagingPath := csiMapper.getStagingPath()
  234. svols := csiMapper.csiClient.(*fakeCsiDriverClient).nodeClient.GetNodeStagedVolumes()
  235. svol, ok := svols[csiMapper.volumeID]
  236. if !ok {
  237. t.Error("csi server may not have received NodeStageVolume call")
  238. }
  239. if svol.Path != stagingPath {
  240. t.Errorf("csi server expected device path %s, got %s", stagingPath, svol.Path)
  241. }
  242. }
  243. func TestBlockMapperSetupDeviceError(t *testing.T) {
  244. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIBlockVolume, true)()
  245. plug, tmpDir := newTestPlugin(t, nil)
  246. defer os.RemoveAll(tmpDir)
  247. csiMapper, _, pv, err := prepareBlockMapperTest(plug, "test-pv", t)
  248. if err != nil {
  249. t.Fatalf("Failed to make a new Mapper: %v", err)
  250. }
  251. pvName := pv.GetName()
  252. nodeName := string(plug.host.GetNodeName())
  253. csiMapper.csiClient = setupClient(t, true)
  254. fClient := csiMapper.csiClient.(*fakeCsiDriverClient)
  255. fClient.nodeClient.SetNextError(errors.New("mock final error"))
  256. attachID := getAttachmentName(csiMapper.volumeID, string(csiMapper.driverName), string(nodeName))
  257. attachment := makeTestAttachment(attachID, nodeName, pvName)
  258. attachment.Status.Attached = true
  259. _, err = csiMapper.k8s.StorageV1().VolumeAttachments().Create(context.Background(), attachment, metav1.CreateOptions{})
  260. if err != nil {
  261. t.Fatalf("failed to setup VolumeAttachment: %v", err)
  262. }
  263. t.Log("created attachement ", attachID)
  264. err = csiMapper.SetUpDevice()
  265. if err == nil {
  266. t.Fatal("mapper unexpectedly succeeded")
  267. }
  268. // Check that all directories have been cleaned
  269. // Check that all metadata / staging / publish directories were deleted
  270. dataDir := getVolumeDeviceDataDir(pv.ObjectMeta.Name, plug.host)
  271. if _, err := os.Stat(dataDir); err == nil {
  272. t.Errorf("volume publish data directory %s was not deleted", dataDir)
  273. }
  274. devDir := getVolumeDeviceDataDir(pv.ObjectMeta.Name, plug.host)
  275. if _, err := os.Stat(devDir); err == nil {
  276. t.Errorf("volume publish device directory %s was not deleted", devDir)
  277. }
  278. stagingPath := csiMapper.getStagingPath()
  279. if _, err := os.Stat(stagingPath); err == nil {
  280. t.Errorf("volume staging path %s was not deleted", stagingPath)
  281. }
  282. }
  283. func TestBlockMapperMapPodDevice(t *testing.T) {
  284. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIBlockVolume, true)()
  285. plug, tmpDir := newTestPlugin(t, nil)
  286. defer os.RemoveAll(tmpDir)
  287. csiMapper, _, pv, err := prepareBlockMapperTest(plug, "test-pv", t)
  288. if err != nil {
  289. t.Fatalf("Failed to make a new Mapper: %v", err)
  290. }
  291. pvName := pv.GetName()
  292. nodeName := string(plug.host.GetNodeName())
  293. csiMapper.csiClient = setupClient(t, true)
  294. attachID := getAttachmentName(csiMapper.volumeID, string(csiMapper.driverName), nodeName)
  295. attachment := makeTestAttachment(attachID, nodeName, pvName)
  296. attachment.Status.Attached = true
  297. _, err = csiMapper.k8s.StorageV1().VolumeAttachments().Create(context.Background(), attachment, metav1.CreateOptions{})
  298. if err != nil {
  299. t.Fatalf("failed to setup VolumeAttachment: %v", err)
  300. }
  301. t.Log("created attachement ", attachID)
  302. // Map device to global and pod device map path
  303. path, err := csiMapper.MapPodDevice()
  304. if err != nil {
  305. t.Fatalf("mapper failed to GetGlobalMapPath: %v", err)
  306. }
  307. // Check if NodePublishVolume published to the right path
  308. pvols := csiMapper.csiClient.(*fakeCsiDriverClient).nodeClient.GetNodePublishedVolumes()
  309. pvol, ok := pvols[csiMapper.volumeID]
  310. if !ok {
  311. t.Error("csi server may not have received NodePublishVolume call")
  312. }
  313. publishPath := csiMapper.getPublishPath()
  314. if pvol.Path != publishPath {
  315. t.Errorf("csi server expected path %s, got %s", publishPath, pvol.Path)
  316. }
  317. if path != publishPath {
  318. t.Errorf("csi server expected path %s, but MapPodDevice returned %s", publishPath, path)
  319. }
  320. }
  321. func TestBlockMapperMapPodDeviceNotSupportAttach(t *testing.T) {
  322. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIBlockVolume, true)()
  323. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIDriverRegistry, true)()
  324. fakeClient := fakeclient.NewSimpleClientset()
  325. attachRequired := false
  326. fakeDriver := &v1beta1.CSIDriver{
  327. ObjectMeta: meta.ObjectMeta{
  328. Name: testDriver,
  329. },
  330. Spec: v1beta1.CSIDriverSpec{
  331. AttachRequired: &attachRequired,
  332. },
  333. }
  334. _, err := fakeClient.StorageV1beta1().CSIDrivers().Create(context.TODO(), fakeDriver, metav1.CreateOptions{})
  335. if err != nil {
  336. t.Fatalf("Failed to create a fakeDriver: %v", err)
  337. }
  338. // after the driver is created, create the plugin. newTestPlugin waits for the informer to sync,
  339. // such that csiMapper.SetUpDevice below sees the VolumeAttachment object in the lister.
  340. plug, tmpDir := newTestPlugin(t, fakeClient)
  341. defer os.RemoveAll(tmpDir)
  342. csiMapper, _, _, err := prepareBlockMapperTest(plug, "test-pv", t)
  343. if err != nil {
  344. t.Fatalf("Failed to make a new Mapper: %v", err)
  345. }
  346. csiMapper.csiClient = setupClient(t, true)
  347. // Map device to global and pod device map path
  348. path, err := csiMapper.MapPodDevice()
  349. if err != nil {
  350. t.Fatalf("mapper failed to GetGlobalMapPath: %v", err)
  351. }
  352. publishPath := csiMapper.getPublishPath()
  353. if path != publishPath {
  354. t.Errorf("path %s and %s doesn't match", path, publishPath)
  355. }
  356. }
  357. func TestBlockMapperTearDownDevice(t *testing.T) {
  358. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIBlockVolume, true)()
  359. plug, tmpDir := newTestPlugin(t, nil)
  360. defer os.RemoveAll(tmpDir)
  361. _, spec, pv, err := prepareBlockMapperTest(plug, "test-pv", t)
  362. if err != nil {
  363. t.Fatalf("Failed to make a new Mapper: %v", err)
  364. }
  365. // save volume data
  366. dir := getVolumeDeviceDataDir(pv.ObjectMeta.Name, plug.host)
  367. if err := os.MkdirAll(dir, 0755); err != nil && !os.IsNotExist(err) {
  368. t.Errorf("failed to create dir [%s]: %v", dir, err)
  369. }
  370. if err := saveVolumeData(
  371. dir,
  372. volDataFileName,
  373. map[string]string{
  374. volDataKey.specVolID: pv.ObjectMeta.Name,
  375. volDataKey.driverName: testDriver,
  376. volDataKey.volHandle: testVol,
  377. },
  378. ); err != nil {
  379. t.Fatalf("failed to save volume data: %v", err)
  380. }
  381. unmapper, err := plug.NewBlockVolumeUnmapper(pv.ObjectMeta.Name, testPodUID)
  382. if err != nil {
  383. t.Fatalf("failed to make a new Unmapper: %v", err)
  384. }
  385. csiUnmapper := unmapper.(*csiBlockMapper)
  386. csiUnmapper.csiClient = setupClient(t, true)
  387. globalMapPath, err := csiUnmapper.GetGlobalMapPath(spec)
  388. if err != nil {
  389. t.Fatalf("unmapper failed to GetGlobalMapPath: %v", err)
  390. }
  391. err = csiUnmapper.TearDownDevice(globalMapPath, "/dev/test")
  392. if err != nil {
  393. t.Fatal(err)
  394. }
  395. // ensure csi client call and node unpblished
  396. pubs := csiUnmapper.csiClient.(*fakeCsiDriverClient).nodeClient.GetNodePublishedVolumes()
  397. if _, ok := pubs[csiUnmapper.volumeID]; ok {
  398. t.Error("csi server may not have received NodeUnpublishVolume call")
  399. }
  400. // ensure csi client call and node unstaged
  401. vols := csiUnmapper.csiClient.(*fakeCsiDriverClient).nodeClient.GetNodeStagedVolumes()
  402. if _, ok := vols[csiUnmapper.volumeID]; ok {
  403. t.Error("csi server may not have received NodeUnstageVolume call")
  404. }
  405. }
  406. func TestVolumeSetupTeardown(t *testing.T) {
  407. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIBlockVolume, true)()
  408. // Follow volume setup + teardown sequences at top of cs_block.go and set up / clean up one CSI block device.
  409. // Focus on testing that there were no leftover files present after the cleanup.
  410. plug, tmpDir := newTestPlugin(t, nil)
  411. defer os.RemoveAll(tmpDir)
  412. csiMapper, spec, pv, err := prepareBlockMapperTest(plug, "test-pv", t)
  413. if err != nil {
  414. t.Fatalf("Failed to make a new Mapper: %v", err)
  415. }
  416. pvName := pv.GetName()
  417. nodeName := string(plug.host.GetNodeName())
  418. csiMapper.csiClient = setupClient(t, true)
  419. attachID := getAttachmentName(csiMapper.volumeID, string(csiMapper.driverName), string(nodeName))
  420. attachment := makeTestAttachment(attachID, nodeName, pvName)
  421. attachment.Status.Attached = true
  422. _, err = csiMapper.k8s.StorageV1().VolumeAttachments().Create(context.TODO(), attachment, metav1.CreateOptions{})
  423. if err != nil {
  424. t.Fatalf("failed to setup VolumeAttachment: %v", err)
  425. }
  426. t.Log("created attachement ", attachID)
  427. err = csiMapper.SetUpDevice()
  428. if err != nil {
  429. t.Fatalf("mapper failed to SetupDevice: %v", err)
  430. }
  431. // Check if NodeStageVolume staged to the right path
  432. stagingPath := csiMapper.getStagingPath()
  433. svols := csiMapper.csiClient.(*fakeCsiDriverClient).nodeClient.GetNodeStagedVolumes()
  434. svol, ok := svols[csiMapper.volumeID]
  435. if !ok {
  436. t.Error("csi server may not have received NodeStageVolume call")
  437. }
  438. if svol.Path != stagingPath {
  439. t.Errorf("csi server expected device path %s, got %s", stagingPath, svol.Path)
  440. }
  441. path, err := csiMapper.MapPodDevice()
  442. if err != nil {
  443. t.Fatalf("mapper failed to GetGlobalMapPath: %v", err)
  444. }
  445. pvols := csiMapper.csiClient.(*fakeCsiDriverClient).nodeClient.GetNodePublishedVolumes()
  446. pvol, ok := pvols[csiMapper.volumeID]
  447. if !ok {
  448. t.Error("csi server may not have received NodePublishVolume call")
  449. }
  450. publishPath := csiMapper.getPublishPath()
  451. if pvol.Path != publishPath {
  452. t.Errorf("csi server expected path %s, got %s", publishPath, pvol.Path)
  453. }
  454. if path != publishPath {
  455. t.Errorf("csi server expected path %s, but MapPodDevice returned %s", publishPath, path)
  456. }
  457. unmapper, err := plug.NewBlockVolumeUnmapper(pv.ObjectMeta.Name, testPodUID)
  458. if err != nil {
  459. t.Fatalf("failed to make a new Unmapper: %v", err)
  460. }
  461. csiUnmapper := unmapper.(*csiBlockMapper)
  462. csiUnmapper.csiClient = csiMapper.csiClient
  463. globalMapPath, err := csiUnmapper.GetGlobalMapPath(spec)
  464. if err != nil {
  465. t.Fatalf("unmapper failed to GetGlobalMapPath: %v", err)
  466. }
  467. err = csiUnmapper.UnmapPodDevice()
  468. if err != nil {
  469. t.Errorf("unmapper failed to call UnmapPodDevice: %v", err)
  470. }
  471. // GenerateUnmapDeviceFunc uses "" as pod UUID, it is global operation over all pods that used the volume
  472. unmapper, err = plug.NewBlockVolumeUnmapper(pv.ObjectMeta.Name, "")
  473. if err != nil {
  474. t.Fatalf("failed to make a new Unmapper: %v", err)
  475. }
  476. csiUnmapper = unmapper.(*csiBlockMapper)
  477. csiUnmapper.csiClient = csiMapper.csiClient
  478. err = csiUnmapper.TearDownDevice(globalMapPath, "/dev/test")
  479. if err != nil {
  480. t.Fatal(err)
  481. }
  482. pubs := csiUnmapper.csiClient.(*fakeCsiDriverClient).nodeClient.GetNodePublishedVolumes()
  483. if _, ok := pubs[csiUnmapper.volumeID]; ok {
  484. t.Error("csi server may not have received NodeUnpublishVolume call")
  485. }
  486. vols := csiUnmapper.csiClient.(*fakeCsiDriverClient).nodeClient.GetNodeStagedVolumes()
  487. if _, ok := vols[csiUnmapper.volumeID]; ok {
  488. t.Error("csi server may not have received NodeUnstageVolume call")
  489. }
  490. // Check that all metadata / staging / publish directories were deleted
  491. dataDir := getVolumeDeviceDataDir(pv.ObjectMeta.Name, plug.host)
  492. if _, err := os.Stat(dataDir); err == nil {
  493. t.Errorf("volume publish data directory %s was not deleted", dataDir)
  494. }
  495. devDir := getVolumeDeviceDataDir(pv.ObjectMeta.Name, plug.host)
  496. if _, err := os.Stat(devDir); err == nil {
  497. t.Errorf("volume publish device directory %s was not deleted", devDir)
  498. }
  499. if _, err := os.Stat(publishPath); err == nil {
  500. t.Errorf("volume publish path %s was not deleted", publishPath)
  501. }
  502. publishDir := filepath.Dir(publishPath)
  503. if _, err := os.Stat(publishDir); err == nil {
  504. t.Errorf("volume publish parent directory %s was not deleted", publishDir)
  505. }
  506. if _, err := os.Stat(stagingPath); err == nil {
  507. t.Errorf("volume staging path %s was not deleted", stagingPath)
  508. }
  509. }