@@ -17,7 +17,6 @@ limitations under the License.
17
17
package validation
18
18
19
19
import (
20
- "fmt"
21
20
"reflect"
22
21
"strings"
23
22
"testing"
@@ -29,230 +28,6 @@ import (
29
28
"k8s.io/kubernetes/pkg/features"
30
29
)
31
30
32
- func TestValidatePodSCTP (t * testing.T ) {
33
- objectWithValue := func () * api.Pod {
34
- return & api.Pod {
35
- Spec : api.PodSpec {
36
- Containers : []api.Container {{Name : "container1" , Image : "testimage" , Ports : []api.ContainerPort {{ContainerPort : 80 , Protocol : api .ProtocolSCTP }}}},
37
- InitContainers : []api.Container {{Name : "container2" , Image : "testimage" , Ports : []api.ContainerPort {{ContainerPort : 90 , Protocol : api .ProtocolSCTP }}}},
38
- },
39
- }
40
- }
41
- objectWithoutValue := func () * api.Pod {
42
- return & api.Pod {
43
- Spec : api.PodSpec {
44
- Containers : []api.Container {{Name : "container1" , Image : "testimage" , Ports : []api.ContainerPort {{ContainerPort : 80 , Protocol : api .ProtocolTCP }}}},
45
- InitContainers : []api.Container {{Name : "container2" , Image : "testimage" , Ports : []api.ContainerPort {{ContainerPort : 90 , Protocol : api .ProtocolTCP }}}},
46
- },
47
- }
48
- }
49
-
50
- objectInfo := []struct {
51
- description string
52
- hasValue bool
53
- object func () * api.Pod
54
- }{
55
- {
56
- description : "has value" ,
57
- hasValue : true ,
58
- object : objectWithValue ,
59
- },
60
- {
61
- description : "does not have value" ,
62
- hasValue : false ,
63
- object : objectWithoutValue ,
64
- },
65
- {
66
- description : "is nil" ,
67
- hasValue : false ,
68
- object : func () * api.Pod { return nil },
69
- },
70
- }
71
-
72
- for _ , enabled := range []bool {true , false } {
73
- for _ , oldPodInfo := range objectInfo {
74
- for _ , newPodInfo := range objectInfo {
75
- oldPodHasValue , oldPod := oldPodInfo .hasValue , oldPodInfo .object ()
76
- newPodHasValue , newPod := newPodInfo .hasValue , newPodInfo .object ()
77
- if newPod == nil {
78
- continue
79
- }
80
-
81
- t .Run (fmt .Sprintf ("feature enabled=%v, old object %v, new object %v" , enabled , oldPodInfo .description , newPodInfo .description ), func (t * testing.T ) {
82
- defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .SCTPSupport , enabled )()
83
- errs := ValidateConditionalPod (newPod , oldPod , nil )
84
- // objects should never be changed
85
- if ! reflect .DeepEqual (oldPod , oldPodInfo .object ()) {
86
- t .Errorf ("old object changed: %v" , diff .ObjectReflectDiff (oldPod , oldPodInfo .object ()))
87
- }
88
- if ! reflect .DeepEqual (newPod , newPodInfo .object ()) {
89
- t .Errorf ("new object changed: %v" , diff .ObjectReflectDiff (newPod , newPodInfo .object ()))
90
- }
91
-
92
- switch {
93
- case enabled || oldPodHasValue || ! newPodHasValue :
94
- if len (errs ) > 0 {
95
- t .Errorf ("unexpected errors: %v" , errs )
96
- }
97
- default :
98
- if len (errs ) != 2 {
99
- t .Errorf ("expected 2 errors, got %v" , errs )
100
- }
101
- }
102
- })
103
- }
104
- }
105
- }
106
- }
107
-
108
- func TestValidateServiceSCTP (t * testing.T ) {
109
- objectWithValue := func () * api.Service {
110
- return & api.Service {
111
- Spec : api.ServiceSpec {
112
- Ports : []api.ServicePort {{Protocol : api .ProtocolSCTP }},
113
- },
114
- }
115
- }
116
- objectWithoutValue := func () * api.Service {
117
- return & api.Service {
118
- Spec : api.ServiceSpec {
119
- Ports : []api.ServicePort {{Protocol : api .ProtocolTCP }},
120
- },
121
- }
122
- }
123
-
124
- objectInfo := []struct {
125
- description string
126
- hasValue bool
127
- object func () * api.Service
128
- }{
129
- {
130
- description : "has value" ,
131
- hasValue : true ,
132
- object : objectWithValue ,
133
- },
134
- {
135
- description : "does not have value" ,
136
- hasValue : false ,
137
- object : objectWithoutValue ,
138
- },
139
- {
140
- description : "is nil" ,
141
- hasValue : false ,
142
- object : func () * api.Service { return nil },
143
- },
144
- }
145
-
146
- for _ , enabled := range []bool {true , false } {
147
- for _ , oldServiceInfo := range objectInfo {
148
- for _ , newServiceInfo := range objectInfo {
149
- oldServiceHasValue , oldService := oldServiceInfo .hasValue , oldServiceInfo .object ()
150
- newServiceHasValue , newService := newServiceInfo .hasValue , newServiceInfo .object ()
151
- if newService == nil {
152
- continue
153
- }
154
-
155
- t .Run (fmt .Sprintf ("feature enabled=%v, old object %v, new object %v" , enabled , oldServiceInfo .description , newServiceInfo .description ), func (t * testing.T ) {
156
- defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .SCTPSupport , enabled )()
157
- errs := ValidateConditionalService (newService , oldService , []api.IPFamily {api .IPv4Protocol })
158
- // objects should never be changed
159
- if ! reflect .DeepEqual (oldService , oldServiceInfo .object ()) {
160
- t .Errorf ("old object changed: %v" , diff .ObjectReflectDiff (oldService , oldServiceInfo .object ()))
161
- }
162
- if ! reflect .DeepEqual (newService , newServiceInfo .object ()) {
163
- t .Errorf ("new object changed: %v" , diff .ObjectReflectDiff (newService , newServiceInfo .object ()))
164
- }
165
-
166
- switch {
167
- case enabled || oldServiceHasValue || ! newServiceHasValue :
168
- if len (errs ) > 0 {
169
- t .Errorf ("unexpected errors: %v" , errs )
170
- }
171
- default :
172
- if len (errs ) != 1 {
173
- t .Errorf ("expected 1 error, got %v" , errs )
174
- }
175
- }
176
- })
177
- }
178
- }
179
- }
180
- }
181
-
182
- func TestValidateEndpointsSCTP (t * testing.T ) {
183
- objectWithValue := func () * api.Endpoints {
184
- return & api.Endpoints {
185
- Subsets : []api.EndpointSubset {
186
- {Ports : []api.EndpointPort {{Protocol : api .ProtocolSCTP }}},
187
- },
188
- }
189
- }
190
- objectWithoutValue := func () * api.Endpoints {
191
- return & api.Endpoints {
192
- Subsets : []api.EndpointSubset {
193
- {Ports : []api.EndpointPort {{Protocol : api .ProtocolTCP }}},
194
- },
195
- }
196
- }
197
-
198
- objectInfo := []struct {
199
- description string
200
- hasValue bool
201
- object func () * api.Endpoints
202
- }{
203
- {
204
- description : "has value" ,
205
- hasValue : true ,
206
- object : objectWithValue ,
207
- },
208
- {
209
- description : "does not have value" ,
210
- hasValue : false ,
211
- object : objectWithoutValue ,
212
- },
213
- {
214
- description : "is nil" ,
215
- hasValue : false ,
216
- object : func () * api.Endpoints { return nil },
217
- },
218
- }
219
-
220
- for _ , enabled := range []bool {true , false } {
221
- for _ , oldEndpointsInfo := range objectInfo {
222
- for _ , newEndpointsInfo := range objectInfo {
223
- oldEndpointsHasValue , oldEndpoints := oldEndpointsInfo .hasValue , oldEndpointsInfo .object ()
224
- newEndpointsHasValue , newEndpoints := newEndpointsInfo .hasValue , newEndpointsInfo .object ()
225
- if newEndpoints == nil {
226
- continue
227
- }
228
-
229
- t .Run (fmt .Sprintf ("feature enabled=%v, old object %v, new object %v" , enabled , oldEndpointsInfo .description , newEndpointsInfo .description ), func (t * testing.T ) {
230
- defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .SCTPSupport , enabled )()
231
- errs := ValidateConditionalEndpoints (newEndpoints , oldEndpoints )
232
- // objects should never be changed
233
- if ! reflect .DeepEqual (oldEndpoints , oldEndpointsInfo .object ()) {
234
- t .Errorf ("old object changed: %v" , diff .ObjectReflectDiff (oldEndpoints , oldEndpointsInfo .object ()))
235
- }
236
- if ! reflect .DeepEqual (newEndpoints , newEndpointsInfo .object ()) {
237
- t .Errorf ("new object changed: %v" , diff .ObjectReflectDiff (newEndpoints , newEndpointsInfo .object ()))
238
- }
239
-
240
- switch {
241
- case enabled || oldEndpointsHasValue || ! newEndpointsHasValue :
242
- if len (errs ) > 0 {
243
- t .Errorf ("unexpected errors: %v" , errs )
244
- }
245
- default :
246
- if len (errs ) != 1 {
247
- t .Errorf ("expected 1 error, got %v" , errs )
248
- }
249
- }
250
- })
251
- }
252
- }
253
- }
254
- }
255
-
256
31
func TestValidateServiceIPFamily (t * testing.T ) {
257
32
ipv4 := api .IPv4Protocol
258
33
ipv6 := api .IPv6Protocol
0 commit comments