@@ -127,6 +127,20 @@ func makeFakeExecWithTimeout(withTimeout bool, output []byte, err error) func(st
127
127
}
128
128
}
129
129
130
+ func marshalDeviceInfo (d * deviceInfo ) string {
131
+ var output string
132
+ pkNames := map [string ]string {}
133
+ for _ , device := range * d {
134
+ for _ , child := range device .Children {
135
+ pkNames [child .Name ] = device .Name
136
+ }
137
+ }
138
+ for _ , device := range * d {
139
+ output += fmt .Sprintf ("%s %s %s %s %s %s %s\n " , device .Name , device .Name , pkNames [device .Name ], device .Hctl , device .Type , device .Transport , device .Size )
140
+ }
141
+ return output
142
+ }
143
+
130
144
func TestExecCommandHelper (t * testing.T ) {
131
145
if os .Getenv ("GO_WANT_HELPER_PROCESS" ) != "1" {
132
146
return
@@ -579,16 +593,15 @@ func Test_getMultipathDevice(t *testing.T) {
579
593
}
580
594
581
595
func Test_lsblk (t * testing.T ) {
582
- sda := Device {Name : "sda" , Children : []Device {{}}}
583
-
584
- sdaOutput , err := json .Marshal (deviceInfo {BlockDevices : []Device {sda }})
585
- assert .New (t ).Nil (err , "could not setup test" )
596
+ sda1 := Device {Name : "sda1" }
597
+ sda := Device {Name : "sda" , Children : []Device {sda1 }}
598
+ sdaOutput := marshalDeviceInfo (& deviceInfo {sda , sda1 })
586
599
587
600
tests := map [string ]struct {
588
601
devicePaths []string
589
602
strict bool
590
603
mockedStdout string
591
- mockedDevices [] Device
604
+ mockedDevices deviceInfo
592
605
mockedExitStatus int
593
606
wantErr bool
594
607
}{
@@ -603,7 +616,7 @@ func Test_lsblk(t *testing.T) {
603
616
mockedExitStatus : 32 ,
604
617
wantErr : true ,
605
618
},
606
- "InvalidJson " : {
619
+ "InvalidOutput " : {
607
620
mockedStdout : "{" ,
608
621
mockedExitStatus : 0 ,
609
622
wantErr : true ,
@@ -636,7 +649,7 @@ func Test_lsblk(t *testing.T) {
636
649
assert .NotNil (err )
637
650
} else {
638
651
assert .NotNil (deviceInfo )
639
- assert .Equal (tt .mockedDevices , deviceInfo . BlockDevices )
652
+ assert .Equal (tt .mockedDevices , deviceInfo )
640
653
assert .Nil (err )
641
654
}
642
655
})
@@ -654,17 +667,17 @@ func TestConnectorPersistance(t *testing.T) {
654
667
PasswordIn : "fake password in" ,
655
668
}
656
669
childDevice := Device {
657
- Name : "child name" ,
658
- Hctl : "child hctl" ,
659
- Type : "child type" ,
660
- Transport : "child transport" ,
670
+ Name : "child- name" ,
671
+ Hctl : "child- hctl" ,
672
+ Type : "child- type" ,
673
+ Transport : "child- transport" ,
661
674
}
662
675
device := Device {
663
- Name : "device name" ,
664
- Hctl : "device hctl" ,
676
+ Name : "device- name" ,
677
+ Hctl : "device- hctl" ,
665
678
Children : []Device {childDevice },
666
- Type : "device type" ,
667
- Transport : "device transport" ,
679
+ Type : "device- type" ,
680
+ Transport : "device- transport" ,
668
681
}
669
682
c := Connector {
670
683
VolumeName : "fake volume name" ,
@@ -687,32 +700,32 @@ func TestConnectorPersistance(t *testing.T) {
687
700
devicesByPath [device .GetPath ()] = & device
688
701
689
702
defer gostub .Stub (& execCommand , func (name string , arg ... string ) * exec.Cmd {
690
- blockDevices := [] Device {}
703
+ blockDevices := deviceInfo {}
691
704
for _ , path := range arg [3 :] {
692
705
blockDevices = append (blockDevices , * devicesByPath [path ])
693
706
}
694
707
695
- out , err := json .Marshal (deviceInfo {BlockDevices : blockDevices })
696
- assert .Nil (err , "could not setup test" )
708
+ out := marshalDeviceInfo (& blockDevices )
697
709
return makeFakeExecCommand (0 , string (out ))(name , arg ... )
698
710
}).Reset ()
699
711
700
712
defer gostub .Stub (& execCommand , func (cmd string , args ... string ) * exec.Cmd {
701
- mockedDevice := device
702
- if args [3 ] == "/dev/child name" {
703
- mockedDevice = childDevice
713
+ devInfo := & deviceInfo { device , childDevice }
714
+ if args [3 ] == "/dev/child- name" {
715
+ devInfo = & deviceInfo { childDevice }
704
716
}
705
717
706
- mockedOutput , err := json .Marshal (deviceInfo {BlockDevices : []Device {mockedDevice }})
707
- assert .Nil (err , "could not setup test" )
708
-
718
+ mockedOutput := marshalDeviceInfo (devInfo )
709
719
return makeFakeExecCommand (0 , string (mockedOutput ))(cmd , args ... )
710
720
}).Reset ()
711
721
712
722
c .Persist ("/tmp/connector.json" )
713
723
c2 , err := GetConnectorFromFile ("/tmp/connector.json" )
714
724
assert .Nil (err )
715
- assert .Equal (c , * c2 )
725
+ assert .NotNil (c2 )
726
+ if c2 != nil {
727
+ assert .Equal (c , * c2 )
728
+ }
716
729
717
730
err = c .Persist ("/tmp" )
718
731
assert .NotNil (err )
0 commit comments