@@ -29,6 +29,7 @@ import (
29
29
"testing"
30
30
"time"
31
31
32
+ v1 "k8s.io/api/core/v1"
32
33
"k8s.io/apimachinery/pkg/util/sets"
33
34
utilversion "k8s.io/apimachinery/pkg/util/version"
34
35
"k8s.io/apimachinery/pkg/util/wait"
@@ -208,6 +209,141 @@ func TestNew(t *testing.T) {
208
209
}
209
210
}
210
211
212
+ func TestNewDualStack (t * testing.T ) {
213
+ testCases := []struct {
214
+ name string
215
+ commands []testCommand
216
+ ipv4 bool
217
+ ipv6 bool
218
+ }{
219
+ {
220
+ name : "both available" ,
221
+ commands : []testCommand {
222
+ {
223
+ // ipv4 creation
224
+ command : "iptables --version" ,
225
+ action : func () ([]byte , []byte , error ) { return []byte ("iptables v1.8.0" ), nil , nil },
226
+ },
227
+ {
228
+ // ipv4 Present()
229
+ command : "iptables -w 5 -W 100000 -S POSTROUTING -t nat" ,
230
+ action : func () ([]byte , []byte , error ) { return nil , nil , nil },
231
+ },
232
+ {
233
+ // ipv6 creation
234
+ command : "ip6tables --version" ,
235
+ action : func () ([]byte , []byte , error ) { return []byte ("iptables v1.8.0" ), nil , nil },
236
+ },
237
+ {
238
+ // ipv6 Present()
239
+ command : "ip6tables -w 5 -W 100000 -S POSTROUTING -t nat" ,
240
+ action : func () ([]byte , []byte , error ) { return nil , nil , nil },
241
+ },
242
+ },
243
+ ipv4 : true ,
244
+ ipv6 : true ,
245
+ },
246
+ {
247
+ name : "ipv4 available, ipv6 not installed" ,
248
+ commands : []testCommand {
249
+ {
250
+ // ipv4 creation
251
+ command : "iptables --version" ,
252
+ action : func () ([]byte , []byte , error ) { return []byte ("iptables v1.8.0" ), nil , nil },
253
+ },
254
+ {
255
+ // ipv4 Present()
256
+ command : "iptables -w 5 -W 100000 -S POSTROUTING -t nat" ,
257
+ action : func () ([]byte , []byte , error ) { return nil , nil , nil },
258
+ },
259
+ {
260
+ // ipv6 creation
261
+ command : "ip6tables --version" ,
262
+ action : func () ([]byte , []byte , error ) { return nil , nil , fmt .Errorf ("no such file or directory" ) },
263
+ },
264
+ {
265
+ // ipv6 Present()
266
+ command : "ip6tables -S POSTROUTING -t nat" ,
267
+ action : func () ([]byte , []byte , error ) { return nil , nil , fmt .Errorf ("no such file or directory" ) },
268
+ },
269
+ },
270
+ ipv4 : true ,
271
+ ipv6 : false ,
272
+ },
273
+ {
274
+ name : "ipv4 available, ipv6 disabled" ,
275
+ commands : []testCommand {
276
+ {
277
+ // ipv4 creation
278
+ command : "iptables --version" ,
279
+ action : func () ([]byte , []byte , error ) { return []byte ("iptables v1.8.0" ), nil , nil },
280
+ },
281
+ {
282
+ // ipv4 Present()
283
+ command : "iptables -w 5 -W 100000 -S POSTROUTING -t nat" ,
284
+ action : func () ([]byte , []byte , error ) { return nil , nil , nil },
285
+ },
286
+ {
287
+ // ipv6 creation
288
+ command : "ip6tables --version" ,
289
+ action : func () ([]byte , []byte , error ) { return []byte ("iptables v1.8.0" ), nil , nil },
290
+ },
291
+ {
292
+ // ipv6 Present()
293
+ command : "ip6tables -w 5 -W 100000 -S POSTROUTING -t nat" ,
294
+ action : func () ([]byte , []byte , error ) { return nil , nil , fmt .Errorf ("ipv6 is broken" ) },
295
+ },
296
+ },
297
+ ipv4 : true ,
298
+ ipv6 : false ,
299
+ },
300
+ {
301
+ name : "no iptables support" ,
302
+ commands : []testCommand {
303
+ {
304
+ // ipv4 creation
305
+ command : "iptables --version" ,
306
+ action : func () ([]byte , []byte , error ) { return nil , nil , fmt .Errorf ("no such file or directory" ) },
307
+ },
308
+ {
309
+ // ipv4 Present()
310
+ command : "iptables -S POSTROUTING -t nat" ,
311
+ action : func () ([]byte , []byte , error ) { return nil , nil , fmt .Errorf ("no such file or directory" ) },
312
+ },
313
+ {
314
+ // ipv6 creation
315
+ command : "ip6tables --version" ,
316
+ action : func () ([]byte , []byte , error ) { return nil , nil , fmt .Errorf ("no such file or directory" ) },
317
+ },
318
+ {
319
+ // ipv6 Present()
320
+ command : "ip6tables -S POSTROUTING -t nat" ,
321
+ action : func () ([]byte , []byte , error ) { return nil , nil , fmt .Errorf ("no such file or directory" ) },
322
+ },
323
+ },
324
+ ipv4 : false ,
325
+ ipv6 : false ,
326
+ },
327
+ }
328
+
329
+ for _ , tc := range testCases {
330
+ t .Run (tc .name , func (t * testing.T ) {
331
+ fexec := fakeExecForCommands (tc .commands )
332
+ runners := newDualStackInternal (fexec )
333
+
334
+ if tc .ipv4 && runners [v1 .IPv4Protocol ] == nil {
335
+ t .Errorf ("Expected ipv4 runner, got nil" )
336
+ } else if ! tc .ipv4 && runners [v1 .IPv4Protocol ] != nil {
337
+ t .Errorf ("Expected no ipv4 runner, got one" )
338
+ }
339
+ if tc .ipv6 && runners [v1 .IPv6Protocol ] == nil {
340
+ t .Errorf ("Expected ipv6 runner, got nil" )
341
+ } else if ! tc .ipv6 && runners [v1 .IPv6Protocol ] != nil {
342
+ t .Errorf ("Expected no ipv6 runner, got one" )
343
+ }
344
+ })
345
+ }
346
+ }
211
347
func testEnsureChain (t * testing.T , protocol Protocol ) {
212
348
fcmd := fakeexec.FakeCmd {
213
349
CombinedOutputScript : []fakeexec.FakeAction {
0 commit comments