@@ -24,49 +24,186 @@ import (
24
24
storage "k8s.io/api/storage/v1"
25
25
)
26
26
27
- func NewStorageClass (params map [string ]string ) storage.StorageClass {
28
- return storage.StorageClass {
29
- Parameters : params ,
27
+ func NewStorageClass (params map [string ]string , allowedTopologies []v1.TopologySelectorTerm ) * storage.StorageClass {
28
+ return & storage.StorageClass {
29
+ Parameters : params ,
30
+ AllowedTopologies : allowedTopologies ,
30
31
}
31
32
}
32
33
33
- func TestTranslatePDInTreeVolumeOptionsToCSI (t * testing.T ) {
34
+ func TestTranslatePDInTreeStorageClassToCSI (t * testing.T ) {
34
35
g := NewGCEPersistentDiskCSITranslator ()
35
36
36
37
tcs := []struct {
37
38
name string
38
- options storage.StorageClass
39
- expOptions storage.StorageClass
39
+ options * storage.StorageClass
40
+ expOptions * storage.StorageClass
41
+ expErr bool
40
42
}{
41
43
{
42
44
name : "nothing special" ,
43
- options : NewStorageClass (map [string ]string {"foo" : "bar" }),
44
- expOptions : NewStorageClass (map [string ]string {"foo" : "bar" }),
45
+ options : NewStorageClass (map [string ]string {"foo" : "bar" }, nil ),
46
+ expOptions : NewStorageClass (map [string ]string {"foo" : "bar" }, nil ),
45
47
},
46
48
{
47
49
name : "fstype" ,
48
- options : NewStorageClass (map [string ]string {"fstype" : "myfs" }),
49
- expOptions : NewStorageClass (map [string ]string {"csi.storage.k8s.io/fstype" : "myfs" }),
50
+ options : NewStorageClass (map [string ]string {"fstype" : "myfs" }, nil ),
51
+ expOptions : NewStorageClass (map [string ]string {"csi.storage.k8s.io/fstype" : "myfs" }, nil ),
50
52
},
51
53
{
52
54
name : "empty params" ,
53
- options : NewStorageClass (map [string ]string {}),
54
- expOptions : NewStorageClass (map [string ]string {}),
55
+ options : NewStorageClass (map [string ]string {}, nil ),
56
+ expOptions : NewStorageClass (map [string ]string {}, nil ),
57
+ },
58
+ {
59
+ name : "zone" ,
60
+ options : NewStorageClass (map [string ]string {"zone" : "foo" }, nil ),
61
+ expOptions : NewStorageClass (map [string ]string {}, generateToplogySelectors (GCEPDTopologyKey , []string {"foo" })),
62
+ },
63
+ {
64
+ name : "zones" ,
65
+ options : NewStorageClass (map [string ]string {"zones" : "foo,bar,baz" }, nil ),
66
+ expOptions : NewStorageClass (map [string ]string {}, generateToplogySelectors (GCEPDTopologyKey , []string {"foo" , "bar" , "baz" })),
67
+ },
68
+ {
69
+ name : "some normal topology" ,
70
+ options : NewStorageClass (map [string ]string {}, generateToplogySelectors (GCEPDTopologyKey , []string {"foo" })),
71
+ expOptions : NewStorageClass (map [string ]string {}, generateToplogySelectors (GCEPDTopologyKey , []string {"foo" })),
72
+ },
73
+ {
74
+ name : "some translated topology" ,
75
+ options : NewStorageClass (map [string ]string {}, generateToplogySelectors (v1 .LabelZoneFailureDomain , []string {"foo" })),
76
+ expOptions : NewStorageClass (map [string ]string {}, generateToplogySelectors (GCEPDTopologyKey , []string {"foo" })),
77
+ },
78
+ {
79
+ name : "zone and topology" ,
80
+ options : NewStorageClass (map [string ]string {"zone" : "foo" }, generateToplogySelectors (GCEPDTopologyKey , []string {"foo" })),
81
+ expErr : true ,
55
82
},
56
83
}
57
84
58
85
for _ , tc := range tcs {
59
86
t .Logf ("Testing %v" , tc .name )
60
- gotOptions , err := g .TranslateInTreeVolumeOptionsToCSI (tc .options )
61
- if err != nil {
87
+ gotOptions , err := g .TranslateInTreeStorageClassToCSI (tc .options )
88
+ if err != nil && ! tc . expErr {
62
89
t .Errorf ("Did not expect error but got: %v" , err )
63
90
}
91
+ if err == nil && tc .expErr {
92
+ t .Errorf ("Expected error, but did not get one." )
93
+ }
64
94
if ! reflect .DeepEqual (gotOptions , tc .expOptions ) {
65
95
t .Errorf ("Got parameters: %v, expected :%v" , gotOptions , tc .expOptions )
66
96
}
67
97
}
68
98
}
69
99
100
+ func TestTranslateAllowedTopologies (t * testing.T ) {
101
+ testCases := []struct {
102
+ name string
103
+ topology []v1.TopologySelectorTerm
104
+ expectedToplogy []v1.TopologySelectorTerm
105
+ expErr bool
106
+ }{
107
+ {
108
+ name : "no translation" ,
109
+ topology : generateToplogySelectors (GCEPDTopologyKey , []string {"foo" , "bar" }),
110
+ expectedToplogy : []v1.TopologySelectorTerm {
111
+ {
112
+ MatchLabelExpressions : []v1.TopologySelectorLabelRequirement {
113
+ {
114
+ Key : GCEPDTopologyKey ,
115
+ Values : []string {"foo" , "bar" },
116
+ },
117
+ },
118
+ },
119
+ },
120
+ },
121
+ {
122
+ name : "translate" ,
123
+ topology : []v1.TopologySelectorTerm {
124
+ {
125
+ MatchLabelExpressions : []v1.TopologySelectorLabelRequirement {
126
+ {
127
+ Key : "failure-domain.beta.kubernetes.io/zone" ,
128
+ Values : []string {"foo" , "bar" },
129
+ },
130
+ },
131
+ },
132
+ },
133
+ expectedToplogy : []v1.TopologySelectorTerm {
134
+ {
135
+ MatchLabelExpressions : []v1.TopologySelectorLabelRequirement {
136
+ {
137
+ Key : GCEPDTopologyKey ,
138
+ Values : []string {"foo" , "bar" },
139
+ },
140
+ },
141
+ },
142
+ },
143
+ },
144
+ {
145
+ name : "combo" ,
146
+ topology : []v1.TopologySelectorTerm {
147
+ {
148
+ MatchLabelExpressions : []v1.TopologySelectorLabelRequirement {
149
+ {
150
+ Key : "failure-domain.beta.kubernetes.io/zone" ,
151
+ Values : []string {"foo" , "bar" },
152
+ },
153
+ {
154
+ Key : GCEPDTopologyKey ,
155
+ Values : []string {"boo" , "baz" },
156
+ },
157
+ },
158
+ },
159
+ },
160
+ expectedToplogy : []v1.TopologySelectorTerm {
161
+ {
162
+ MatchLabelExpressions : []v1.TopologySelectorLabelRequirement {
163
+ {
164
+ Key : GCEPDTopologyKey ,
165
+ Values : []string {"foo" , "bar" },
166
+ },
167
+ {
168
+ Key : GCEPDTopologyKey ,
169
+ Values : []string {"boo" , "baz" },
170
+ },
171
+ },
172
+ },
173
+ },
174
+ },
175
+ {
176
+ name : "some other key" ,
177
+ topology : []v1.TopologySelectorTerm {
178
+ {
179
+ MatchLabelExpressions : []v1.TopologySelectorLabelRequirement {
180
+ {
181
+ Key : "test" ,
182
+ Values : []string {"foo" , "bar" },
183
+ },
184
+ },
185
+ },
186
+ },
187
+ expErr : true ,
188
+ },
189
+ }
190
+
191
+ for _ , tc := range testCases {
192
+ t .Logf ("Running test: %v" , tc .name )
193
+ gotTop , err := translateAllowedTopologies (tc .topology )
194
+ if err != nil && ! tc .expErr {
195
+ t .Errorf ("Did not expect an error, got: %v" , err )
196
+ }
197
+ if err == nil && tc .expErr {
198
+ t .Errorf ("Expected an error but did not get one" )
199
+ }
200
+
201
+ if ! reflect .DeepEqual (gotTop , tc .expectedToplogy ) {
202
+ t .Errorf ("Expected topology: %v, but got: %v" , tc .expectedToplogy , gotTop )
203
+ }
204
+ }
205
+ }
206
+
70
207
func TestBackwardCompatibleAccessModes (t * testing.T ) {
71
208
testCases := []struct {
72
209
name string
0 commit comments