@@ -18,6 +18,7 @@ package csi
18
18
19
19
import (
20
20
"context"
21
+ "errors"
21
22
"fmt"
22
23
"os"
23
24
"path/filepath"
@@ -282,7 +283,7 @@ func TestBlockMapperSetupDevice(t *testing.T) {
282
283
}
283
284
}
284
285
285
- func TestBlockMapperMapPodDevice (t * testing.T ) {
286
+ func TestBlockMapperSetupDeviceError (t * testing.T ) {
286
287
defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .CSIBlockVolume , true )()
287
288
288
289
plug , tmpDir := newTestPlugin (t , nil )
@@ -297,11 +298,59 @@ func TestBlockMapperMapPodDevice(t *testing.T) {
297
298
nodeName := string (plug .host .GetNodeName ())
298
299
299
300
csiMapper .csiClient = setupClient (t , true )
301
+ fClient := csiMapper .csiClient .(* fakeCsiDriverClient )
302
+ fClient .nodeClient .SetNextError (errors .New ("mock final error" ))
300
303
301
304
attachID := getAttachmentName (csiMapper .volumeID , string (csiMapper .driverName ), string (nodeName ))
302
305
attachment := makeTestAttachment (attachID , nodeName , pvName )
303
306
attachment .Status .Attached = true
304
- _ , err = csiMapper .k8s .StorageV1 ().VolumeAttachments ().Create (context .TODO (), attachment , metav1.CreateOptions {})
307
+ _ , err = csiMapper .k8s .StorageV1 ().VolumeAttachments ().Create (context .Background (), attachment , metav1.CreateOptions {})
308
+ if err != nil {
309
+ t .Fatalf ("failed to setup VolumeAttachment: %v" , err )
310
+ }
311
+ t .Log ("created attachement " , attachID )
312
+
313
+ err = csiMapper .SetUpDevice ()
314
+ if err == nil {
315
+ t .Fatal ("mapper unexpectedly succeeded" )
316
+ }
317
+
318
+ // Check that all directories have been cleaned
319
+ // Check that all metadata / staging / publish directories were deleted
320
+ dataDir := getVolumeDeviceDataDir (pv .ObjectMeta .Name , plug .host )
321
+ if _ , err := os .Stat (dataDir ); err == nil {
322
+ t .Errorf ("volume publish data directory %s was not deleted" , dataDir )
323
+ }
324
+ devDir := getVolumeDeviceDataDir (pv .ObjectMeta .Name , plug .host )
325
+ if _ , err := os .Stat (devDir ); err == nil {
326
+ t .Errorf ("volume publish device directory %s was not deleted" , devDir )
327
+ }
328
+ stagingPath := csiMapper .getStagingPath ()
329
+ if _ , err := os .Stat (stagingPath ); err == nil {
330
+ t .Errorf ("volume staging path %s was not deleted" , stagingPath )
331
+ }
332
+ }
333
+
334
+ func TestBlockMapperMapPodDevice (t * testing.T ) {
335
+ defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .CSIBlockVolume , true )()
336
+
337
+ plug , tmpDir := newTestPlugin (t , nil )
338
+ defer os .RemoveAll (tmpDir )
339
+
340
+ csiMapper , _ , pv , err := prepareBlockMapperTest (plug , "test-pv" , t )
341
+ if err != nil {
342
+ t .Fatalf ("Failed to make a new Mapper: %v" , err )
343
+ }
344
+
345
+ pvName := pv .GetName ()
346
+ nodeName := string (plug .host .GetNodeName ())
347
+
348
+ csiMapper .csiClient = setupClient (t , true )
349
+
350
+ attachID := getAttachmentName (csiMapper .volumeID , string (csiMapper .driverName ), nodeName )
351
+ attachment := makeTestAttachment (attachID , nodeName , pvName )
352
+ attachment .Status .Attached = true
353
+ _ , err = csiMapper .k8s .StorageV1 ().VolumeAttachments ().Create (context .Background (), attachment , metav1.CreateOptions {})
305
354
if err != nil {
306
355
t .Fatalf ("failed to setup VolumeAttachment: %v" , err )
307
356
}
@@ -430,3 +479,123 @@ func TestBlockMapperTearDownDevice(t *testing.T) {
430
479
t .Error ("csi server may not have received NodeUnstageVolume call" )
431
480
}
432
481
}
482
+
483
+ func TestVolumeSetupTeardown (t * testing.T ) {
484
+ defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .CSIBlockVolume , true )()
485
+
486
+ // Follow volume setup + teardown sequences at top of cs_block.go and set up / clean up one CSI block device.
487
+ // Focus on testing that there were no leftover files present after the cleanup.
488
+
489
+ plug , tmpDir := newTestPlugin (t , nil )
490
+ defer os .RemoveAll (tmpDir )
491
+
492
+ csiMapper , spec , pv , err := prepareBlockMapperTest (plug , "test-pv" , t )
493
+ if err != nil {
494
+ t .Fatalf ("Failed to make a new Mapper: %v" , err )
495
+ }
496
+
497
+ pvName := pv .GetName ()
498
+ nodeName := string (plug .host .GetNodeName ())
499
+
500
+ csiMapper .csiClient = setupClient (t , true )
501
+
502
+ attachID := getAttachmentName (csiMapper .volumeID , string (csiMapper .driverName ), string (nodeName ))
503
+ attachment := makeTestAttachment (attachID , nodeName , pvName )
504
+ attachment .Status .Attached = true
505
+ _ , err = csiMapper .k8s .StorageV1 ().VolumeAttachments ().Create (context .TODO (), attachment , metav1.CreateOptions {})
506
+ if err != nil {
507
+ t .Fatalf ("failed to setup VolumeAttachment: %v" , err )
508
+ }
509
+ t .Log ("created attachement " , attachID )
510
+
511
+ err = csiMapper .SetUpDevice ()
512
+ if err != nil {
513
+ t .Fatalf ("mapper failed to SetupDevice: %v" , err )
514
+ }
515
+ // Check if NodeStageVolume staged to the right path
516
+ stagingPath := csiMapper .getStagingPath ()
517
+ svols := csiMapper .csiClient .(* fakeCsiDriverClient ).nodeClient .GetNodeStagedVolumes ()
518
+ svol , ok := svols [csiMapper .volumeID ]
519
+ if ! ok {
520
+ t .Error ("csi server may not have received NodeStageVolume call" )
521
+ }
522
+ if svol .Path != stagingPath {
523
+ t .Errorf ("csi server expected device path %s, got %s" , stagingPath , svol .Path )
524
+ }
525
+
526
+ path , err := csiMapper .MapPodDevice ()
527
+ if err != nil {
528
+ t .Fatalf ("mapper failed to GetGlobalMapPath: %v" , err )
529
+ }
530
+ pvols := csiMapper .csiClient .(* fakeCsiDriverClient ).nodeClient .GetNodePublishedVolumes ()
531
+ pvol , ok := pvols [csiMapper .volumeID ]
532
+ if ! ok {
533
+ t .Error ("csi server may not have received NodePublishVolume call" )
534
+ }
535
+ publishPath := csiMapper .getPublishPath ()
536
+ if pvol .Path != publishPath {
537
+ t .Errorf ("csi server expected path %s, got %s" , publishPath , pvol .Path )
538
+ }
539
+ if path != publishPath {
540
+ t .Errorf ("csi server expected path %s, but MapPodDevice returned %s" , publishPath , path )
541
+ }
542
+
543
+ unmapper , err := plug .NewBlockVolumeUnmapper (pv .ObjectMeta .Name , testPodUID )
544
+ if err != nil {
545
+ t .Fatalf ("failed to make a new Unmapper: %v" , err )
546
+ }
547
+
548
+ csiUnmapper := unmapper .(* csiBlockMapper )
549
+ csiUnmapper .csiClient = csiMapper .csiClient
550
+
551
+ globalMapPath , err := csiUnmapper .GetGlobalMapPath (spec )
552
+ if err != nil {
553
+ t .Fatalf ("unmapper failed to GetGlobalMapPath: %v" , err )
554
+ }
555
+
556
+ err = csiUnmapper .UnmapPodDevice ()
557
+ if err != nil {
558
+ t .Errorf ("unmapper failed to call UnmapPodDevice: %v" , err )
559
+ }
560
+
561
+ // GenerateUnmapDeviceFunc uses "" as pod UUID, it is global operation over all pods that used the volume
562
+ unmapper , err = plug .NewBlockVolumeUnmapper (pv .ObjectMeta .Name , "" )
563
+ if err != nil {
564
+ t .Fatalf ("failed to make a new Unmapper: %v" , err )
565
+ }
566
+ csiUnmapper = unmapper .(* csiBlockMapper )
567
+ csiUnmapper .csiClient = csiMapper .csiClient
568
+
569
+ err = csiUnmapper .TearDownDevice (globalMapPath , "/dev/test" )
570
+ if err != nil {
571
+ t .Fatal (err )
572
+ }
573
+ pubs := csiUnmapper .csiClient .(* fakeCsiDriverClient ).nodeClient .GetNodePublishedVolumes ()
574
+ if _ , ok := pubs [csiUnmapper .volumeID ]; ok {
575
+ t .Error ("csi server may not have received NodeUnpublishVolume call" )
576
+ }
577
+ vols := csiUnmapper .csiClient .(* fakeCsiDriverClient ).nodeClient .GetNodeStagedVolumes ()
578
+ if _ , ok := vols [csiUnmapper .volumeID ]; ok {
579
+ t .Error ("csi server may not have received NodeUnstageVolume call" )
580
+ }
581
+
582
+ // Check that all metadata / staging / publish directories were deleted
583
+ dataDir := getVolumeDeviceDataDir (pv .ObjectMeta .Name , plug .host )
584
+ if _ , err := os .Stat (dataDir ); err == nil {
585
+ t .Errorf ("volume publish data directory %s was not deleted" , dataDir )
586
+ }
587
+ devDir := getVolumeDeviceDataDir (pv .ObjectMeta .Name , plug .host )
588
+ if _ , err := os .Stat (devDir ); err == nil {
589
+ t .Errorf ("volume publish device directory %s was not deleted" , devDir )
590
+ }
591
+ if _ , err := os .Stat (publishPath ); err == nil {
592
+ t .Errorf ("volume publish path %s was not deleted" , publishPath )
593
+ }
594
+ publishDir := filepath .Dir (publishPath )
595
+ if _ , err := os .Stat (publishDir ); err == nil {
596
+ t .Errorf ("volume publish parent directory %s was not deleted" , publishDir )
597
+ }
598
+ if _ , err := os .Stat (stagingPath ); err == nil {
599
+ t .Errorf ("volume staging path %s was not deleted" , stagingPath )
600
+ }
601
+ }
0 commit comments