generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 61
/
volume_test.go
149 lines (128 loc) · 3.58 KB
/
volume_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
package volume
import (
"context"
"fmt"
"testing"
volumeapi "github.com/kubernetes-csi/csi-proxy/v2/pkg/volume/hostapi"
)
type fakeVolumeAPI struct {
diskVolMap map[uint32][]string
}
var _ volumeapi.HostAPI = &fakeVolumeAPI{}
func (volumeAPI *fakeVolumeAPI) Fill(diskToVolMapIn map[uint32][]string) {
for d, v := range diskToVolMapIn {
volumeAPI.diskVolMap[d] = v
}
}
func (volumeAPI *fakeVolumeAPI) ListVolumesOnDisk(diskNumber uint32, partitionNumber uint32) (volumeIDs []string, err error) {
v := volumeAPI.diskVolMap[diskNumber]
if v == nil {
return nil, fmt.Errorf("returning error for %d list", diskNumber)
}
return v, nil
}
func (volumeAPI *fakeVolumeAPI) MountVolume(volumeID, path string) error {
return nil
}
func (volumeAPI *fakeVolumeAPI) UnmountVolume(volumeID, path string) error {
return nil
}
func (volumeAPI *fakeVolumeAPI) IsVolumeFormatted(volumeID string) (bool, error) {
return true, nil
}
func (volumeAPI *fakeVolumeAPI) FormatVolume(volumeID string) error {
return nil
}
func (volumeAPI *fakeVolumeAPI) ResizeVolume(volumeID string, size int64) error {
return nil
}
func (volumeAPI *fakeVolumeAPI) GetDiskNumberFromVolumeID(volumeID string) (uint32, error) {
return 0, nil
}
func (volumeAPI *fakeVolumeAPI) GetVolumeIDFromTargetPath(mount string) (string, error) {
return "id", nil
}
func (volumeAPI *fakeVolumeAPI) GetClosestVolumeIDFromTargetPath(mount string) (string, error) {
return "id", nil
}
func (volumeAPI *fakeVolumeAPI) GetVolumeStats(volumeID string) (int64, int64, error) {
return -1, -1, nil
}
func (volumeAPI *fakeVolumeAPI) WriteVolumeCache(volumeID string) error {
return nil
}
func TestListVolumesOnDisk(t *testing.T) {
testCases := []struct {
name string
inputDiskNumber uint32
expectedVolumeIDs []string
isErrorExpected bool
expectedError error
}{
{
name: "return two volumeIDs",
inputDiskNumber: 1,
expectedVolumeIDs: []string{"volumeID1", "volumeID2"},
isErrorExpected: false,
expectedError: nil,
},
{
name: "return one volumeIDs",
inputDiskNumber: 2,
expectedVolumeIDs: []string{"volumeID3"},
isErrorExpected: false,
expectedError: nil,
},
{
name: "return error",
inputDiskNumber: 3,
expectedVolumeIDs: nil,
isErrorExpected: true,
expectedError: fmt.Errorf("returning error for 3 list"),
},
}
diskToVolMap := map[uint32][]string{
1: {"volumeID1", "volumeID2"},
2: {"volumeID3"},
}
volAPI := &fakeVolumeAPI{
diskVolMap: make(map[uint32][]string),
}
volAPI.Fill(diskToVolMap)
client, err := New(volAPI)
if err != nil {
t.Fatalf("Volume server could not be initialized: %v", err)
}
for _, tc := range testCases {
t.Logf("test case: %s", tc.name)
listInput := &ListVolumesOnDiskRequest{
DiskNumber: tc.inputDiskNumber,
}
volumeListResponse, err := client.ListVolumesOnDisk(context.TODO(), listInput)
if tc.isErrorExpected {
if tc.expectedError.Error() != err.Error() {
t.Fatalf("Expected error: %v. Got error: %v", tc.expectedError, err)
}
} else {
if err != nil {
t.Fatalf("Error %v not expected", err)
}
expectedVolumeIDMap := make(map[string]int)
for _, j := range tc.expectedVolumeIDs {
expectedVolumeIDMap[j] = 0
}
for _, i := range volumeListResponse.VolumeIDs {
if _, found := expectedVolumeIDMap[i]; found == true {
expectedVolumeIDMap[i]++
} else {
t.Fatalf("Found unexpected volume: %s", i)
}
}
for k, v := range expectedVolumeIDMap {
if v != 1 {
t.Fatalf("Volume: %s count: %d", k, v)
}
}
}
}
}