This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathcontroller_test.go
1117 lines (984 loc) · 52.7 KB
/
controller_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2017 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package controller_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"encoding/json"
"fmt"
ctl "github.com/IBM/ubiquity-k8s/controller"
k8sresources "github.com/IBM/ubiquity-k8s/resources"
"github.com/IBM/ubiquity/fakes"
"github.com/IBM/ubiquity/resources"
)
var _ = Describe("Controller", func() {
var (
fakeClient *fakes.FakeStorageClient
controller *ctl.Controller
fakeExec *fakes.FakeExecutor
fakeMounterFactory *fakes.FakeMounterFactory
fakeMounter *fakes.FakeMounter
ubiquityConfig resources.UbiquityPluginConfig
dat map[string]interface{}
mountPoint string
)
BeforeEach(func() {
fakeExec = new(fakes.FakeExecutor)
ubiquityConfig = resources.UbiquityPluginConfig{}
fakeClient = new(fakes.FakeStorageClient)
fakeMounterFactory = new(fakes.FakeMounterFactory)
fakeMounter = new(fakes.FakeMounter)
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
byt := []byte(`{"Wwn":"fake"}`)
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
mountPoint = "/tmp/kubelet/pods/1f94f1d9-8f36-11e8-b227-005056a4d4cb/volumes/ibm~ubiquity-k8s-flex/pvc-123"
})
Context(".Init", func() {
It("does not error when init is successful", func() {
initResponse := controller.Init(ubiquityConfig)
Expect(initResponse.Status).To(Equal("Success"))
Expect(initResponse.Message).To(Equal("Plugin init successfully"))
Expect(initResponse.Device).To(Equal(""))
})
Context(".Attach", func() {
It("fails when Attach fails to get volume details", func() {
fakeClient.GetVolumeReturns(resources.Volume{}, fmt.Errorf("GetVolume error"))
opts := map[string]string{}
host := "fakehost"
AttachRequest := k8sresources.FlexVolumeAttachRequest{"vol1", host, opts, "version", resources.RequestContext{}}
attachResponse := controller.Attach(AttachRequest)
Expect(attachResponse.Status).To(Equal("Failure"))
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
})
It("Attach should return Not supported for SpectrumScale backend", func() {
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "spectrum-scale", Mountpoint: "fake"}, nil)
opts := map[string]string{}
host := "fakehost"
AttachRequest := k8sresources.FlexVolumeAttachRequest{"vol1", host, opts, "version", resources.RequestContext{}}
attachResponse := controller.Attach(AttachRequest)
Expect(attachResponse.Status).To(Equal("Not supported"))
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
})
})
//Context(".Attach", func() {
//
// It("fails when attachRequest does not have volumeName", func() {
// fakeClient.GetVolumeReturns(resources.Volume{}, fmt.Errorf("GetVolume error"))
// attachRequest := map[string]string{"Filesystem": "gpfs1", "Size": "200m", "Fileset": "fs1", "Path": "myPath"}
// attachResponse := controller.Attach(attachRequest)
// Expect(attachResponse.Status).To(Equal("Failure"))
// Expect(fakeClient.GetVolumeCallCount()).To(Equal(0))
// })
//
// It("fails when client fails to fetch volume", func() {
// fakeClient.GetVolumeReturns(resources.Volume{}, fmt.Errorf("GetVolume error"))
// attachRequest := map[string]string{"volumeName": "vol1", "Filesystem": "gpfs1", "Size": "200m", "Fileset": "fs1", "Path": "myPath"}
// attachResponse := controller.Attach(attachRequest)
// Expect(attachResponse.Status).To(Equal("Failure"))
// Expect(attachResponse.Message).To(Equal("Failed checking volume, call create before attach"))
// Expect(attachResponse.Device).To(Equal("vol1"))
// })
//
// It("Succeeds when volume exists", func() {
// fakeClient.GetVolumeReturns(resources.Volume{}, nil)
// attachRequest := map[string]string{"volumeName": "vol1", "Filesystem": "gpfs1", "Size": "200m", "Fileset": "fs1", "Path": "myPath"}
// attachResponse := controller.Attach(attachRequest)
// Expect(attachResponse.Status).To(Equal("Success"))
// Expect(attachResponse.Message).To(Equal("Volume already attached"))
// Expect(attachResponse.Device).To(Equal("vol1"))
// Expect(fakeClient.CreateVolumeCallCount()).To(Equal(0))
// })
//})
//
//Context(".Detach", func() {
// It("does not error when existing volume name is given", func() {
// fakeClient.RemoveVolumeReturns(nil)
// detachRequest := resources.FlexVolumeDetachRequest{Name: "vol1"}
// detachResponse := controller.Detach(detachRequest)
// Expect(detachResponse.Status).To(Equal("Success"))
// Expect(detachResponse.Message).To(Equal("Volume detached successfully"))
// Expect(detachResponse.Device).To(Equal("vol1"))
// Expect(fakeClient.RemoveVolumeCallCount()).To(Equal(1))
// })
//
// It("error when client fails to detach volume", func() {
// err := fmt.Errorf("error detaching volume")
// fakeClient.RemoveVolumeReturns(err)
// detachRequest := resources.FlexVolumeDetachRequest{Name: "vol1"}
// detachResponse := controller.Detach(detachRequest)
// Expect(detachResponse.Status).To(Equal("Failure"))
// Expect(detachResponse.Message).To(Equal(fmt.Sprintf("Failed to detach volume %#v", err)))
// Expect(detachResponse.Device).To(Equal("vol1"))
// Expect(fakeClient.RemoveVolumeCallCount()).To(Equal(1))
// })
})
Context(".Mount", func() {
var (
extraParams map[string]interface{}
controller *ctl.Controller
)
BeforeEach(func(){
extraParams = make(map[string]interface{})
extraParams["pv"] = "volume_name"
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
})
It("should fail if k8s version < 1.6 (doMount)", func() {
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: "fake", MountDevice: "pv1", Opts: map[string]string{"Wwn": "fake"}, Version: k8sresources.KubernetesVersion_1_5}
mountResponse := controller.Mount(mountRequest)
Expect(mountResponse.Message).To(MatchRegexp(ctl.SupportK8sVesion))
Expect(mountResponse.Status).To(Equal(ctl.FlexFailureStr))
Expect(mountResponse.Device).To(Equal(""))
})
It("should fail in GetVolume if volume not in ubiqutiyDB (doMount)", func() {
fakeClient.GetVolumeReturns(resources.Volume{}, fmt.Errorf("error not found in DB"))
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: mountPoint, MountDevice: "pv1", Opts: map[string]string{}}
mountResponse := controller.Mount(mountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(mountResponse.Message).To(MatchRegexp(".*error not found in DB"))
Expect(mountResponse.Status).To(Equal(ctl.FlexFailureStr))
Expect(mountResponse.Device).To(Equal(""))
})
It("should fail if cannot get mounter for the PV (doMount, GetMounterPerBackend)", func() {
errstr := "ERROR backend"
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "XXX", Mountpoint: "fake"}, nil)
fakeMounterFactory.GetMounterPerBackendReturns(fakeMounter, fmt.Errorf(errstr))
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: mountPoint, MountDevice: "pv1", Opts: map[string]string{}}
mountResponse := controller.Mount(mountRequest)
Expect(mountResponse.Device).To(Equal(""))
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeMounterFactory.GetMounterPerBackendCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(0))
Expect(mountResponse.Message).To(MatchRegexp(errstr))
Expect(mountResponse.Status).To(Equal(ctl.FlexFailureStr))
})
It("should fail to prepareUbiquityMountRequest if GetVolumeConfig failed (doMount)", func() {
errstr := "error GetVolumeConfig"
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "aaaa", Mountpoint: "fake"}, nil)
byt := []byte(`{"":""}`)
var dat map[string]interface{}
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
fakeClient.GetVolumeConfigReturns(dat, fmt.Errorf(errstr))
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: "/pod/pv1", MountDevice: "pv1", Opts: map[string]string{}}
mountResponse := controller.Mount(mountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeMounterFactory.GetMounterPerBackendCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(1))
Expect(fakeMounter.MountCallCount()).To(Equal(0))
Expect(mountResponse.Message).To(MatchRegexp(errstr))
Expect(mountResponse.Status).To(Equal(ctl.FlexFailureStr))
})
It("should fail to prepareUbiquityMountRequest if GetVolumeConfig does not contain Wwn key (doMount)", func() {
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "scbe", Mountpoint: "fake"}, nil)
byt := []byte(`{"fake":"fake"}`)
var dat map[string]interface{}
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
fakeClient.GetVolumeConfigReturns(dat, nil)
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: "/pod/pv1", MountDevice: "pv1", Opts: map[string]string{}}
mountResponse := controller.Mount(mountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeMounterFactory.GetMounterPerBackendCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(1))
Expect(fakeMounter.MountCallCount()).To(Equal(0))
Expect(mountResponse.Message).To(Equal(ctl.MissingWwnMountRequestErrorStr))
Expect(mountResponse.Status).To(Equal(ctl.FlexFailureStr))
})
It("should fail to prepareUbiquityMountRequest if GetVolumeConfig does not contain mountpoint for spectrumscale backend", func() {
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "spectrum-scale", Mountpoint: "fake"}, nil)
byt := []byte(`{"fake":"fake"}`)
var dat map[string]interface{}
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
fakeClient.GetVolumeConfigReturns(dat, nil)
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: "/pod/pv1", MountDevice: "pv1", Opts: map[string]string{}}
mountResponse := controller.Mount(mountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeMounterFactory.GetMounterPerBackendCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(1))
Expect(fakeMounter.MountCallCount()).To(Equal(0))
Expect(mountResponse.Message).To(Equal(ctl.SpectrumScaleMissingMntPtVolumeErrorStr+" volume=[pv1]"))
Expect(mountResponse.Status).To(Equal(ctl.FlexFailureStr))
})
It("should fail to prepareUbiquityMountRequest if GetVolumeConfig does not contain valid backend", func() {
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "fake", Mountpoint: "fake"}, nil)
byt := []byte(`{"fake":"fake"}`)
var dat map[string]interface{}
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
fakeClient.GetVolumeConfigReturns(dat, nil)
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: "/pod/pv1", MountDevice: "pv1", Opts: map[string]string{}}
mountResponse := controller.Mount(mountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeMounterFactory.GetMounterPerBackendCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(1))
Expect(fakeMounter.MountCallCount()).To(Equal(0))
Expect(mountResponse.Message).To(Equal(ctl.PvBackendNotSupportedErrorStr+" backend=[fake]"))
Expect(mountResponse.Status).To(Equal(ctl.FlexFailureStr))
})
It("should fail if mounter.Mount failed (doMount)", func() {
errstr := "TODO set error in mounter"
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "scbe", Mountpoint: "fake"}, nil)
byt := []byte(`{"Wwn":"fake"}`)
var dat map[string]interface{}
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
fakeClient.GetVolumeConfigReturns(dat, nil)
fakeMounter.MountReturns("fake device", fmt.Errorf(errstr))
fakeMounterFactory.GetMounterPerBackendReturns(fakeMounter, nil)
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: mountPoint, MountDevice: "pv1", Opts: map[string]string{"Wwn": "fake"}}
mountResponse := controller.Mount(mountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(1))
Expect(fakeMounter.MountCallCount()).To(Equal(1))
Expect(fakeExec.LstatCallCount()).To(Equal(0))
Expect(mountResponse.Message).To(Equal(errstr))
Expect(mountResponse.Status).To(Equal(ctl.FlexFailureStr))
})
It("should fail to Mount if fail to lstat k8s-mountpoint (doAfterMount)", func() {
errstr := "fakerror"
errstrObj := fmt.Errorf(errstr)
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "scbe", Mountpoint: "fake"}, nil)
fakeClient.GetVolumeConfigReturns(dat, nil)
fakeMounter.MountReturns("/ubiquity/wwn1", nil)
fakeMounterFactory.GetMounterPerBackendReturns(fakeMounter, nil)
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
fakeExec.LstatReturns(nil, errstrObj)
fakeExec.IsNotExistReturns(false)
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: mountPoint, MountDevice: "pv1", Opts: map[string]string{"Wwn": "fake"}, Version: k8sresources.KubernetesVersion_1_6OrLater}
mountResponse := controller.Mount(mountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(1))
Expect(fakeMounter.MountCallCount()).To(Equal(1))
Expect(fakeExec.LstatCallCount()).To(Equal(1))
Expect(fakeExec.IsNotExistCallCount()).To(Equal(1))
Expect(fakeExec.IsNotExistArgsForCall(0)).To(Equal(errstrObj))
Expect(fakeExec.IsDirCallCount()).To(Equal(0))
Expect(mountResponse.Message).To(Equal(errstr))
Expect(mountResponse.Status).To(Equal(ctl.FlexFailureStr))
})
It("should fail to Mount k8s-mountpoint dir not exist(idempotent) but and failed to slink (doAfterMount)", func() {
errstr := "fakerror"
errstrObj := fmt.Errorf(errstr)
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "scbe", Mountpoint: "fake"}, nil)
fakeClient.GetVolumeConfigReturns(dat, nil)
fakeMounter.MountReturns("/ubiquity/wwn1", nil)
fakeMounterFactory.GetMounterPerBackendReturns(fakeMounter, nil)
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
fakeExec.LstatReturns(nil, errstrObj)
fakeExec.IsNotExistReturns(true)
fakeExec.SymlinkReturns(errstrObj)
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: mountPoint, MountDevice: "pv1", Opts: map[string]string{"Wwn": "fake"}, Version: k8sresources.KubernetesVersion_1_6OrLater}
mountResponse := controller.Mount(mountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(1))
Expect(fakeMounter.MountCallCount()).To(Equal(1))
Expect(fakeExec.LstatCallCount()).To(Equal(1))
Expect(fakeExec.IsNotExistCallCount()).To(Equal(1))
Expect(fakeExec.IsNotExistArgsForCall(0)).To(Equal(errstrObj))
Expect(fakeExec.IsDirCallCount()).To(Equal(0))
Expect(mountResponse.Message).To(Equal(errstr))
Expect(mountResponse.Status).To(Equal(ctl.FlexFailureStr))
})
It("should succeed to Mount k8s-mountpoint dir not exist(idempotent) and slink succeed (doAfterMount)", func() {
errstr := "fakerror"
errstrObj := fmt.Errorf(errstr)
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "scbe", Mountpoint: "fake"}, nil)
fakeClient.GetVolumeConfigReturns(dat, nil)
mountpoint := "/ubiquity/wwn1"
fakeMounter.MountReturns(mountpoint, nil)
fakeMounterFactory.GetMounterPerBackendReturns(fakeMounter, nil)
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
fakeExec.LstatReturns(nil, errstrObj)
fakeExec.IsNotExistReturns(true)
fakeExec.SymlinkReturns(nil)
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: mountPoint, MountDevice: "pv1", Opts: map[string]string{"Wwn": "fake"}, Version: k8sresources.KubernetesVersion_1_6OrLater}
mountResponse := controller.Mount(mountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(1))
Expect(fakeMounter.MountCallCount()).To(Equal(1))
Expect(fakeExec.LstatCallCount()).To(Equal(1))
Expect(fakeExec.IsNotExistCallCount()).To(Equal(1))
Expect(fakeExec.SymlinkCallCount()).To(Equal(1))
Expect(fakeExec.IsDirCallCount()).To(Equal(0))
Expect(mountResponse.Message).To(Equal(""))
Expect(mountResponse.Status).To(Equal(ctl.FlexSuccessStr))
Expect(mountResponse.Device).To(Equal(""))
})
It("should fail to Mount because fail to Remove the k8s-mountpoint dir (doAfterMount)", func() {
errstr := "fakerror"
errstrObj := fmt.Errorf(errstr)
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "scbe", Mountpoint: "fake"}, nil)
fakeClient.GetVolumeConfigReturns(dat, nil)
fakeMounter.MountReturns("/ubiquity/wwn1", nil)
fakeMounterFactory.GetMounterPerBackendReturns(fakeMounter, nil)
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: mountPoint, MountDevice: "pv1", Opts: map[string]string{"Wwn": "fake"}, Version: k8sresources.KubernetesVersion_1_6OrLater}
fakeExec.LstatReturns(nil, nil)
fakeExec.IsDirReturns(true)
fakeExec.RemoveReturns(errstrObj)
mountResponse := controller.Mount(mountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(1))
Expect(fakeMounter.MountCallCount()).To(Equal(1))
Expect(fakeExec.LstatCallCount()).To(Equal(1))
Expect(fakeExec.IsNotExistCallCount()).To(Equal(0))
Expect(fakeExec.IsDirCallCount()).To(Equal(1))
Expect(fakeExec.RemoveCallCount()).To(Equal(1))
Expect(fakeExec.SymlinkCallCount()).To(Equal(0))
Expect(mountResponse.Message).To(MatchRegexp(ctl.FailRemovePVorigDirErrorStr))
Expect(mountResponse.Status).To(Equal(ctl.FlexFailureStr))
})
It("should fail to Mount because fail to create Symlink after Remove the k8s-mountpoint dir (doAfterMount)", func() {
errstr := "fakerror"
errstrObj := fmt.Errorf(errstr)
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "scbe", Mountpoint: "fake"}, nil)
fakeClient.GetVolumeConfigReturns(dat, nil)
fakeMounter.MountReturns("/ubiquity/wwn1", nil)
fakeMounterFactory.GetMounterPerBackendReturns(fakeMounter, nil)
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: mountPoint, MountDevice: "pv1", Opts: map[string]string{"Wwn": "fake"}, Version: k8sresources.KubernetesVersion_1_6OrLater}
fakeExec.LstatReturns(nil, nil)
fakeExec.IsDirReturns(true)
fakeExec.RemoveReturns(nil)
fakeExec.SymlinkReturns(errstrObj)
mountResponse := controller.Mount(mountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(1))
Expect(fakeMounter.MountCallCount()).To(Equal(1))
Expect(fakeExec.LstatCallCount()).To(Equal(1))
Expect(fakeExec.IsNotExistCallCount()).To(Equal(0))
Expect(fakeExec.IsDirCallCount()).To(Equal(1))
Expect(fakeExec.RemoveCallCount()).To(Equal(1))
Expect(fakeExec.SymlinkCallCount()).To(Equal(1))
Expect(fakeExec.IsSlinkCallCount()).To(Equal(0))
Expect(mountResponse.Message).To(MatchRegexp(errstr))
Expect(mountResponse.Status).To(Equal(ctl.FlexFailureStr))
})
It("should fail to Mount because fail to create Symlink after Remove the k8s-mountpoint dir (doAfterMount)", func() {
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "scbe", Mountpoint: "fake"}, nil)
fakeClient.GetVolumeConfigReturns(dat, nil)
fakeMounter.MountReturns("/ubiquity/wwn1", nil)
fakeMounterFactory.GetMounterPerBackendReturns(fakeMounter, nil)
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: mountPoint, MountDevice: "pv1", Opts: map[string]string{"Wwn": "fake"}, Version: k8sresources.KubernetesVersion_1_6OrLater}
fakeExec.LstatReturns(nil, nil)
fakeExec.IsDirReturns(true)
fakeExec.RemoveReturns(nil)
fakeExec.SymlinkReturns(nil)
mountResponse := controller.Mount(mountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(1))
Expect(fakeMounter.MountCallCount()).To(Equal(1))
Expect(fakeExec.LstatCallCount()).To(Equal(1))
Expect(fakeExec.IsNotExistCallCount()).To(Equal(0))
Expect(fakeExec.IsDirCallCount()).To(Equal(1))
Expect(fakeExec.RemoveCallCount()).To(Equal(1))
Expect(fakeExec.SymlinkCallCount()).To(Equal(1))
Expect(fakeExec.IsSlinkCallCount()).To(Equal(0))
Expect(mountResponse.Message).To(Equal(""))
Expect(mountResponse.Status).To(Equal(ctl.FlexSuccessStr))
})
It("should fail to Mount because fail to EvalSymlinks (idempotent) (doAfterMount)", func() {
errstr := "fakerror"
errstrObj := fmt.Errorf(errstr)
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "scbe", Mountpoint: "fake"}, nil)
fakeClient.GetVolumeConfigReturns(dat, nil)
fakeMounter.MountReturns("/ubiquity/wwn1", nil)
fakeMounterFactory.GetMounterPerBackendReturns(fakeMounter, nil)
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: mountPoint, MountDevice: "pv1", Opts: map[string]string{"Wwn": "fake"}, Version: k8sresources.KubernetesVersion_1_6OrLater}
fakeExec.LstatReturns(nil, nil)
fakeExec.IsDirReturns(false)
fakeExec.IsSlinkReturns(true)
fakeExec.EvalSymlinksReturns("", errstrObj)
mountResponse := controller.Mount(mountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(1))
Expect(fakeMounter.MountCallCount()).To(Equal(1))
Expect(fakeExec.LstatCallCount()).To(Equal(1))
Expect(fakeExec.IsNotExistCallCount()).To(Equal(0))
Expect(fakeExec.IsDirCallCount()).To(Equal(1))
Expect(fakeExec.IsSlinkCallCount()).To(Equal(1))
Expect(fakeExec.EvalSymlinksCallCount()).To(Equal(1))
Expect(mountResponse.Message).To(Equal(errstr))
Expect(mountResponse.Status).To(Equal(ctl.FlexFailureStr))
})
It("should succeed to Mount after k8s-mountpoint is already slink(idempotent) and point to the right mountpath (doAfterMount)", func() {
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "scbe", Mountpoint: "fake"}, nil)
fakeClient.GetVolumeConfigReturns(dat, nil)
mountPath := "/ubiquity/wwn1"
fakeMounter.MountReturns(mountPath, nil)
fakeMounterFactory.GetMounterPerBackendReturns(fakeMounter, nil)
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: mountPoint, MountDevice: "pv1", Opts: map[string]string{"Wwn": "fake"}, Version: k8sresources.KubernetesVersion_1_6OrLater}
fakeExec.LstatReturns(nil, nil)
fakeExec.IsDirReturns(false)
fakeExec.IsSlinkReturns(true)
fakeExec.EvalSymlinksReturns(mountPath, nil)
mountResponse := controller.Mount(mountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(1))
Expect(fakeMounter.MountCallCount()).To(Equal(1))
Expect(fakeExec.LstatCallCount()).To(Equal(1))
Expect(fakeExec.IsNotExistCallCount()).To(Equal(0))
Expect(fakeExec.IsDirCallCount()).To(Equal(1))
Expect(fakeExec.IsSlinkCallCount()).To(Equal(1))
Expect(fakeExec.EvalSymlinksCallCount()).To(Equal(1))
Expect(mountResponse.Message).To(Equal(""))
Expect(mountResponse.Status).To(Equal(ctl.FlexSuccessStr))
Expect(mountResponse.Device).To(Equal(""))
})
It("should fail to Mount after k8s-mountpoint is already slink(idempotent) but point to wrong mountpath (doAfterMount)", func() {
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "scbe", Mountpoint: "fake"}, nil)
fakeClient.GetVolumeConfigReturns(dat, nil)
mountPath := "/ubiquity/wwn1"
fakeMounter.MountReturns(mountPath, nil)
fakeMounterFactory.GetMounterPerBackendReturns(fakeMounter, nil)
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: mountPoint, MountDevice: "pv1", Opts: map[string]string{"Wwn": "fake"}, Version: k8sresources.KubernetesVersion_1_6OrLater}
fakeExec.LstatReturns(nil, nil)
fakeExec.IsDirReturns(false)
fakeExec.IsSlinkReturns(true)
badMountPointForSlink := mountPath + "/bad"
fakeExec.EvalSymlinksReturns(badMountPointForSlink, nil)
mountResponse := controller.Mount(mountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(1))
Expect(fakeMounter.MountCallCount()).To(Equal(1))
Expect(fakeExec.LstatCallCount()).To(Equal(1))
Expect(fakeExec.IsNotExistCallCount()).To(Equal(0))
Expect(fakeExec.IsDirCallCount()).To(Equal(1))
Expect(fakeExec.IsSlinkCallCount()).To(Equal(1))
Expect(fakeExec.EvalSymlinksCallCount()).To(Equal(1))
Expect(mountResponse.Message).To(MatchRegexp(ctl.WrongSlinkErrorStr))
Expect(mountResponse.Status).To(Equal(ctl.FlexFailureStr))
})
It("should fail to Mount when k8s-mountpoint exist but not as dir nor as slink (idempotent) (doAfterMount)", func() {
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "scbe", Mountpoint: "fake"}, nil)
fakeClient.GetVolumeConfigReturns(dat, nil)
mountPath := "/ubiquity/wwn1"
fakeMounter.MountReturns(mountPath, nil)
fakeMounterFactory.GetMounterPerBackendReturns(fakeMounter, nil)
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: mountPoint, MountDevice: "pv1", Opts: map[string]string{"Wwn": "fake"}, Version: k8sresources.KubernetesVersion_1_6OrLater}
fakeExec.LstatReturns(nil, nil)
fakeExec.IsDirReturns(false)
fakeExec.IsSlinkReturns(false)
mountResponse := controller.Mount(mountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(1))
Expect(fakeMounter.MountCallCount()).To(Equal(1))
Expect(fakeExec.LstatCallCount()).To(Equal(1))
Expect(fakeExec.IsNotExistCallCount()).To(Equal(0))
Expect(fakeExec.IsDirCallCount()).To(Equal(1))
Expect(fakeExec.IsSlinkCallCount()).To(Equal(1))
Expect(fakeExec.EvalSymlinksCallCount()).To(Equal(0))
Expect(mountResponse.Message).To(MatchRegexp(ctl.K8sPVDirectoryIsNotDirNorSlinkErrorStr))
Expect(mountResponse.Status).To(Equal(ctl.FlexFailureStr))
})
It("should fail to Mount when there are errors from checking other slinks (idempotent) (doMount)", func() {
err := fmt.Errorf("an Error has occured")
fakeExec.GetGlobFilesReturns(nil, err)
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "scbe", Mountpoint: "fake"}, nil)
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: mountPoint, MountDevice: "pv1", Opts: map[string]string{"Wwn": "fake"}, Version: k8sresources.KubernetesVersion_1_6OrLater}
mountResponse := controller.Mount(mountRequest)
Expect(mountResponse.Message).To(Equal(err.Error()))
Expect(mountResponse.Status).To(Equal(ctl.FlexFailureStr))
Expect(fakeExec.GetGlobFilesCallCount()).To(Equal(1))
})
})
Context(".Unmount", func() {
It("should fail in GetVolume returns error that is not about volume not found in DB (Unmount)", func() {
fakeClient.GetVolumeReturns(resources.Volume{}, fmt.Errorf("just error"))
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "/k8s-dir-pod/pv1"}
mountResponse := controller.Unmount(unmountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(mountResponse.Message).To(MatchRegexp(".*just error"))
Expect(mountResponse.Status).To(Equal(ctl.FlexFailureStr))
})
It("should return success if try to unmount PV that is not exist, idempotent issue (Unmount)", func() {
fakeClient.GetVolumeReturns(resources.Volume{}, &resources.VolumeNotFoundError{VolName: "pv1"})
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "/k8s-dir-pod/pv1"}
mountResponse := controller.Unmount(unmountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(mountResponse.Message).To(MatchRegexp(".*" + resources.VolumeNotFoundErrorMsg))
Expect(mountResponse.Status).To(Equal(ctl.FlexSuccessStr))
})
It("should fail if cannot get mounter for the PV (doMount, GetMounterPerBackend)", func() {
errstr := "ERROR backend"
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "XXX", Mountpoint: "fake"}, nil)
fakeMounterFactory.GetMounterPerBackendReturns(fakeMounter, fmt.Errorf(errstr))
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "/k8s-dir-pod/pv1"}
response := controller.Unmount(unmountRequest)
Expect(response.Device).To(Equal(""))
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeMounterFactory.GetMounterPerBackendCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(0))
Expect(response.Message).To(MatchRegexp(errstr))
Expect(response.Status).To(Equal(ctl.FlexFailureStr))
})
It("should fail to prepareUbiquityMountRequest if GetVolumeConfig failed (doMount)", func() {
errstr := "fakeError"
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "aaaa", Mountpoint: "fake"}, nil)
byt := []byte(`{"":""}`)
var dat map[string]interface{}
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
fakeClient.GetVolumeConfigReturns(dat, fmt.Errorf(errstr))
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "/pod/pv1"}
response := controller.Unmount(unmountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeMounterFactory.GetMounterPerBackendCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(1))
Expect(fakeMounter.UnmountCallCount()).To(Equal(0))
Expect(response.Message).To(MatchRegexp(errstr))
Expect(response.Status).To(Equal(ctl.FlexFailureStr))
})
It("should fail to doUnmount because volume has unknown backend type", func() {
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: "aaaa", Mountpoint: "fake"}, nil)
byt := []byte(`{"":""}`)
var dat map[string]interface{}
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
fakeClient.GetVolumeConfigReturns(dat, nil)
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "/pod/pv1"}
response := controller.Unmount(unmountRequest)
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeMounterFactory.GetMounterPerBackendCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(1))
Expect(fakeMounter.UnmountCallCount()).To(Equal(0))
Expect(response.Message).To(MatchRegexp(ctl.PvBackendNotSupportedErrorStr))
Expect(response.Status).To(Equal(ctl.FlexFailureStr))
})
Context(".Unmount checkSlinkBeforeUmount", func() {
var (
errstrObj error
errstr string
wwn string
)
BeforeEach(func() {
errstr = "fakerror"
errstrObj = fmt.Errorf(errstr)
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: resources.SCBE, Mountpoint: "fake"}, nil)
wwn = "fake"
byt := []byte(`{"Wwn":"fake"}`) // TODO should use the wwn var inside
var dat map[string]interface{}
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
fakeClient.GetVolumeConfigReturns(dat, nil)
})
AfterEach(func() {
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeMounterFactory.GetMounterPerBackendCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(1))
Expect(fakeMounter.UnmountCallCount()).To(Equal(0))
Expect(fakeExec.LstatCallCount()).To(Equal(1))
})
// Start with all the failures in this function
It("should fail because slink lstat error diff from not exist", func() {
fakeExec.LstatReturns(nil, errstrObj)
fakeExec.IsNotExistReturns(false)
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "/pod/pv1"}
response := controller.Unmount(unmountRequest)
Expect(fakeExec.IsNotExistCallCount()).To(Equal(1))
Expect(response.Message).To(MatchRegexp(errstr))
Expect(response.Status).To(Equal(ctl.FlexFailureStr))
})
It("should fail because its an real slink but cannot eval it", func() {
fakeExec.LstatReturns(nil, nil)
fakeExec.IsSlinkReturns(true)
fakeExec.EvalSymlinksReturns("", errstrObj)
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "/pod/pv1"}
response := controller.Unmount(unmountRequest)
Expect(fakeExec.IsSlinkCallCount()).To(Equal(1))
Expect(fakeExec.EvalSymlinksCallCount()).To(Equal(1))
Expect(response.Message).To(MatchRegexp(errstr))
Expect(response.Status).To(Equal(ctl.FlexFailureStr))
})
It("should fail because its an real slink but cannot eval it", func() {
fakeExec.LstatReturns(nil, nil)
fakeExec.IsSlinkReturns(true)
fakeExec.EvalSymlinksReturns("/fake/evalOfSlink", nil)
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "/pod/pv1"}
response := controller.Unmount(unmountRequest)
Expect(fakeExec.IsSlinkCallCount()).To(Equal(1))
Expect(fakeExec.EvalSymlinksCallCount()).To(Equal(1))
Expect(response.Message).To(MatchRegexp(ctl.WrongSlinkErrorStr))
Expect(response.Status).To(Equal(ctl.FlexFailureStr))
})
It("should fail because slink exist but its not slink", func() {
fakeExec.LstatReturns(nil, nil)
fakeExec.IsSlinkReturns(false)
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "/pod/pv1"}
response := controller.Unmount(unmountRequest)
Expect(fakeExec.IsSlinkCallCount()).To(Equal(1))
Expect(fakeExec.EvalSymlinksCallCount()).To(Equal(0))
Expect(response.Message).To(MatchRegexp(ctl.K8sPVDirectoryIsNotSlinkErrorStr))
Expect(response.Status).To(Equal(ctl.FlexFailureStr))
})
})
Context(".Unmount checkSlinkBeforeUmount succeed with slink exist - so check slink removal and mounter.Mount", func() {
var (
errstrObj error
errstr string
wwn string
)
BeforeEach(func() {
errstr = "fakerror"
errstrObj = fmt.Errorf(errstr)
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: resources.SCBE, Mountpoint: "fake"}, nil)
wwn = "fake"
byt := []byte(`{"Wwn":"fake"}`) // TODO should use the wwn var inside
var dat map[string]interface{}
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
fakeClient.GetVolumeConfigReturns(dat, nil)
fakeExec.LstatReturns(nil, nil)
fakeExec.IsSlinkReturns(true)
})
AfterEach(func() {
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeMounterFactory.GetMounterPerBackendCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(1))
Expect(fakeExec.LstatCallCount()).To(Equal(1))
Expect(fakeExec.IsSlinkCallCount()).To(Equal(1))
Expect(fakeExec.EvalSymlinksCallCount()).To(Equal(1))
Expect(fakeMounter.UnmountCallCount()).To(Equal(1))
})
It("should fail unmount because mounter.Unmount failed", func() {
fakeExec.EvalSymlinksReturns("/ubiquity/"+wwn, nil)
fakeMounter.UnmountReturns(errstrObj)
fakeMounterFactory.GetMounterPerBackendReturns(fakeMounter, nil)
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "/pod/pv1"}
response := controller.Unmount(unmountRequest)
Expect(response.Message).To(MatchRegexp(errstr))
Expect(response.Status).To(Equal(ctl.FlexFailureStr))
})
It("should fail unmount because the removal of slink failed (reminder in this stage the slink exist so it fail due to a real issue of the rm)", func() {
fakeExec.EvalSymlinksReturns("/ubiquity/"+wwn, nil)
fakeExec.RemoveReturns(errstrObj)
fakeMounter.UnmountReturns(nil)
fakeMounterFactory.GetMounterPerBackendReturns(fakeMounter, nil)
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "/pod/pv1"}
response := controller.Unmount(unmountRequest)
Expect(response.Message).To(MatchRegexp(errstr))
Expect(response.Status).To(Equal(ctl.FlexFailureStr))
})
//TODO continue from here!!!!!!!!!!!! to add UT for slink not exist at all. and then continue the flow for after mounter.Unmount
})
Context(".Unmount check good path of doUnmount till hit the first error in doLegacyDetach", func() {
var (
errstrObj error
errstr string
wwn string
)
BeforeEach(func() {
errstr = "fakerror"
errstrObj = fmt.Errorf(errstr)
fakeClient.GetVolumeReturns(resources.Volume{Name: "pv1", Backend: resources.SCBE, Mountpoint: "fake"}, nil)
wwn = "fake"
byt := []byte(`{"Wwn":"fake"}`) // TODO should use the wwn var inside
var dat map[string]interface{}
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
fakeClient.GetVolumeConfigReturnsOnCall(0, dat, nil)
fakeClient.GetVolumeConfigReturnsOnCall(1, dat, errstrObj) // the first fail in doLegacyDetach
})
AfterEach(func() {
fmt.Println("11111111111111111111")
Expect(fakeClient.GetVolumeCallCount()).To(Equal(1))
Expect(fakeMounterFactory.GetMounterPerBackendCallCount()).To(Equal(1))
Expect(fakeClient.GetVolumeConfigCallCount()).To(Equal(2)) // first in doUnmount and second is in the legacy
})
It("should succeed doUnmount with slink exist(means doing mounter.Unmount) but fail during doLegacyDetach", func() {
fakeExec.EvalSymlinksReturns("/ubiquity/"+wwn, nil)
fakeMounter.UnmountReturns(nil)
fakeMounterFactory.GetMounterPerBackendReturns(fakeMounter, nil)
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
fakeExec.IsSlinkReturns(true)
fakeExec.LstatReturns(nil, nil)
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "/pod/pv1"}
response := controller.Unmount(unmountRequest)
Expect(fakeExec.LstatCallCount()).To(Equal(1))
Expect(fakeExec.EvalSymlinksCallCount()).To(Equal(1))
Expect(fakeMounter.UnmountCallCount()).To(Equal(1))
Expect(response.Message).To(MatchRegexp(errstr))
Expect(response.Status).To(Equal(ctl.FlexFailureStr))
})
It("should succeed doUnmount because slink not exist (no need to run mounter.Unmount) but then lets fail it during doLegacyDetach", func() {
fakeExec.EvalSymlinksReturns("/ubiquity/"+wwn, nil)
fakeMounter.UnmountReturns(nil)
fakeMounterFactory.GetMounterPerBackendReturns(fakeMounter, nil)
controller = ctl.NewControllerWithClient(testLogger, ubiquityConfig, fakeClient, fakeExec, fakeMounterFactory)
fakeExec.IsSlinkReturns(true)
fakeExec.LstatReturns(nil, fmt.Errorf("error because file not exist")) // simulate idempotent with no slink exist
fakeExec.IsNotExistReturns(true) // simulate idempotent with no slink exist
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "/pod/pv1"}
response := controller.Unmount(unmountRequest)
Expect(response.Message).To(MatchRegexp(errstr))
Expect(response.Status).To(Equal(ctl.FlexFailureStr))
})
// So now we actually finish all unit tests of Unmount before calling doLegacyDetach
// From now we need to start test the doLegacyDetach aspects.
})
})
/*
Context(".Mount", func() {
AfterEach(func() {
err := os.RemoveAll("/tmp/test/mnt1")
Expect(err).ToNot(HaveOccurred())
})
It("does not error when volume exists and is not currently mounted", func() {
fakeClient.AttachReturns("/tmp/test/mnt1", nil)
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: "/tmp/test/mnt2", MountDevice: "vol1", Opts: map[string]string{}}
mountResponse := controller.Mount(mountRequest)
Expect(mountResponse.Message).To(Equal("Volume mounted successfully to /tmp/test/mnt1"))
Expect(mountResponse.Status).To(Equal("Success"))
Expect(mountResponse.Device).To(Equal(""))
Expect(fakeClient.AttachCallCount()).To(Equal(1))
})
It("errors when volume exists and client fails to mount it", func() {
err := fmt.Errorf("failed to mount volume")
fakeClient.AttachReturns("", err)
mountRequest := k8sresources.FlexVolumeMountRequest{MountPath: "some-mountpath", MountDevice: "vol1", Opts: map[string]string{}}
mountResponse := controller.Mount(mountRequest)
Expect(mountResponse.Status).To(Equal("Failure"))
Expect(mountResponse.Message).To(MatchRegexp(err.Error()))
Expect(mountResponse.Device).To(Equal(""))
Expect(fakeClient.AttachCallCount()).To(Equal(1))
})
})
Context(".Unmount", func() {
var volumes []resources.Volume
It("succeeds when volume exists and is currently mounted", func() {
fakeExec.EvalSymlinksReturns("/path/gpfs/fs/mountpoint", nil)
fakeClient.DetachReturns(nil)
volume := resources.Volume{Name: "vol1", Mountpoint: "some-mountpoint"}
volumes = []resources.Volume{volume}
fakeClient.ListVolumesReturns(volumes, nil)
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "some-mountpoint"}
unmountResponse := controller.Unmount(unmountRequest)
Expect(unmountResponse.Status).To(Equal("Success"))
Expect(unmountResponse.Message).To(Equal("Volume unmounted successfully"))
Expect(unmountResponse.Device).To(Equal(""))
Expect(fakeClient.DetachCallCount()).To(Equal(1))
Expect(fakeClient.ListVolumesCallCount()).To(Equal(1))
})
It("errors when client fails to get volume related to the mountpoint", func() {
err := fmt.Errorf("failed to get fileset")
fakeClient.ListVolumesReturns(volumes, err)
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "some-mountpoint"}
unmountResponse := controller.Unmount(unmountRequest)
Expect(unmountResponse.Status).To(Equal("Failure"))
Expect(unmountResponse.Message).To(MatchRegexp(err.Error()))
Expect(unmountResponse.Device).To(Equal(""))
Expect(fakeClient.ListVolumesCallCount()).To(Equal(1))
Expect(fakeClient.DetachCallCount()).To(Equal(0))
})
It("errors when volume does not exist", func() {
volumes = []resources.Volume{}
fakeClient.ListVolumesReturns(volumes, nil)
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "some-mountpoint"}
unmountResponse := controller.Unmount(unmountRequest)
Expect(unmountResponse.Status).To(Equal("Failure"))
Expect(unmountResponse.Message).To(MatchRegexp("Volume not found"))
Expect(unmountResponse.Device).To(Equal(""))
Expect(fakeClient.ListVolumesCallCount()).To(Equal(1))
Expect(fakeClient.DetachCallCount()).To(Equal(0))
})
It("errors when volume exists and client fails to unmount it", func() {
err := fmt.Errorf("error detaching the volume")
volume := resources.Volume{Name: "vol1", Mountpoint: "some-mountpoint"}
volumes = []resources.Volume{volume}
fakeClient.ListVolumesReturns(volumes, nil)
fakeClient.DetachReturns(err)
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "some-mountpoint"}
unmountResponse := controller.Unmount(unmountRequest)
Expect(unmountResponse.Status).To(Equal("Failure"))
Expect(unmountResponse.Message).To(MatchRegexp(err.Error()))
Expect(unmountResponse.Device).To(Equal(""))
Expect(fakeClient.ListVolumesCallCount()).To(Equal(1))
Expect(fakeClient.DetachCallCount()).To(Equal(1))
})
It("should fail to umount if mountpoint is not slink", func() {
errMsg := fmt.Errorf("not a link")
fakeExec.EvalSymlinksReturns("", errMsg)
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "some-mountpoint"}
unmountResponse := controller.Unmount(unmountRequest)
Expect(unmountResponse.Status).To(Equal("Failure"))
Expect(unmountResponse.Message).To(MatchRegexp(errMsg.Error()))
Expect(unmountResponse.Device).To(Equal(""))
})
It("should fail to umount if detach failed", func() {
errMsg := fmt.Errorf("error")
realMountPoint := fmt.Sprintf(resources.PathToMountUbiquityBlockDevices, "fakeWWN")
fakeExec.EvalSymlinksReturns(realMountPoint, nil)
fakeClient.DetachReturns(errMsg)
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "/k8s/podid/some/pvname"}
unmountResponse := controller.Unmount(unmountRequest)
Expect(unmountResponse.Status).To(Equal("Failure"))
Expect(unmountResponse.Message).To(MatchRegexp(errMsg.Error()))
Expect(unmountResponse.Device).To(Equal(""))
detachRequest := fakeClient.DetachArgsForCall(0)
Expect(detachRequest.Name).To(Equal("pvname"))
})
It("should fail to umount if detach failed", func() {
errMsg := fmt.Errorf("error")
realMountPoint := fmt.Sprintf(resources.PathToMountUbiquityBlockDevices, "fakeWWN")
fakeExec.EvalSymlinksReturns(realMountPoint, nil)
fakeClient.DetachReturns(errMsg)
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "/k8s/podid/some/pvname"}
unmountResponse := controller.Unmount(unmountRequest)
Expect(unmountResponse.Status).To(Equal("Failure"))
Expect(unmountResponse.Message).To(MatchRegexp(errMsg.Error()))
Expect(unmountResponse.Device).To(Equal(""))
detachRequest := fakeClient.DetachArgsForCall(0)
Expect(detachRequest.Name).To(Equal("pvname"))
})
It("should fail to umount if fail to remove the slink", func() {
errMsg := fmt.Errorf("error")
realMountPoint := fmt.Sprintf(resources.PathToMountUbiquityBlockDevices, "fakeWWN")
fakeExec.EvalSymlinksReturns(realMountPoint, nil)
fakeClient.DetachReturns(nil)
fakeExec.RemoveReturns(errMsg)
unmountRequest := k8sresources.FlexVolumeUnmountRequest{MountPath: "/k8s/podid/some/pvname"}
unmountResponse := controller.Unmount(unmountRequest)
Expect(unmountResponse.Status).To(Equal("Failure"))
Expect(unmountResponse.Message).To(MatchRegexp(errMsg.Error()))
Expect(unmountResponse.Device).To(Equal(""))