@@ -45,19 +45,23 @@ func protocolStr(protocol Protocol) string {
45
45
}
46
46
47
47
func testIPTablesVersionCmds (t * testing.T , protocol Protocol ) {
48
- version := " v1.9 .22"
48
+ version := " v1.4 .22"
49
49
iptablesCmd := iptablesCommand (protocol )
50
+ iptablesRestoreCmd := iptablesRestoreCommand (protocol )
50
51
protoStr := protocolStr (protocol )
51
52
52
53
fcmd := fakeexec.FakeCmd {
53
54
CombinedOutputScript : []fakeexec.FakeCombinedOutputAction {
54
55
// iptables version response (for runner instantiation)
55
56
func () ([]byte , error ) { return []byte (iptablesCmd + version ), nil },
57
+ // iptables-restore version response (for runner instantiation)
58
+ func () ([]byte , error ) { return []byte (iptablesRestoreCmd + version ), nil },
56
59
},
57
60
}
58
61
fexec := fakeexec.FakeExec {
59
62
CommandScript : []fakeexec.FakeCommandAction {
60
63
func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
64
+ func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
61
65
},
62
66
}
63
67
runner := New (& fexec , dbus .NewFake (nil , nil ), protocol )
@@ -67,6 +71,11 @@ func testIPTablesVersionCmds(t *testing.T, protocol Protocol) {
67
71
if ! sets .NewString (fcmd .CombinedOutputLog [0 ]... ).HasAll (iptablesCmd , "--version" ) {
68
72
t .Errorf ("%s runner instantiate: Expected cmd '%s --version', Got '%s'" , protoStr , iptablesCmd , fcmd .CombinedOutputLog [0 ])
69
73
}
74
+
75
+ // Check that proper iptables restore version command was used during runner instantiation
76
+ if ! sets .NewString (fcmd .CombinedOutputLog [1 ]... ).HasAll (iptablesRestoreCmd , "--version" ) {
77
+ t .Errorf ("%s runner instantiate: Expected cmd '%s --version', Got '%s'" , protoStr , iptablesRestoreCmd , fcmd .CombinedOutputLog [1 ])
78
+ }
70
79
}
71
80
72
81
func TestIPTablesVersionCmdsIPv4 (t * testing.T ) {
@@ -486,11 +495,13 @@ func TestGetIPTablesHasCheckCommand(t *testing.T) {
486
495
fcmd := fakeexec.FakeCmd {
487
496
CombinedOutputScript : []fakeexec.FakeCombinedOutputAction {
488
497
func () ([]byte , error ) { return []byte (testCase .Version ), nil },
498
+ func () ([]byte , error ) { return []byte (testCase .Version ), nil },
489
499
},
490
500
}
491
501
fexec := fakeexec.FakeExec {
492
502
CommandScript : []fakeexec.FakeCommandAction {
493
503
func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
504
+ func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
494
505
},
495
506
}
496
507
ipt := New (& fexec , nil , ProtocolIpv4 )
@@ -639,6 +650,8 @@ func TestWaitFlagUnavailable(t *testing.T) {
639
650
CombinedOutputScript : []fakeexec.FakeCombinedOutputAction {
640
651
// iptables version check
641
652
func () ([]byte , error ) { return []byte ("iptables v1.4.19" ), nil },
653
+ // iptables-restore version check
654
+ func () ([]byte , error ) { return []byte {}, nil },
642
655
// Success.
643
656
func () ([]byte , error ) { return []byte {}, nil },
644
657
},
@@ -647,6 +660,8 @@ func TestWaitFlagUnavailable(t *testing.T) {
647
660
CommandScript : []fakeexec.FakeCommandAction {
648
661
// iptables version check
649
662
func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
663
+ // iptables-restore version check
664
+ func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
650
665
func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
651
666
},
652
667
}
@@ -656,10 +671,10 @@ func TestWaitFlagUnavailable(t *testing.T) {
656
671
if err != nil {
657
672
t .Errorf ("expected success, got %v" , err )
658
673
}
659
- if fcmd .CombinedOutputCalls != 2 {
660
- t .Errorf ("expected 2 CombinedOutput() calls, got %d" , fcmd .CombinedOutputCalls )
674
+ if fcmd .CombinedOutputCalls != 3 {
675
+ t .Errorf ("expected 3 CombinedOutput() calls, got %d" , fcmd .CombinedOutputCalls )
661
676
}
662
- if sets .NewString (fcmd .CombinedOutputLog [1 ]... ).Has (WaitString ) {
677
+ if sets .NewString (fcmd .CombinedOutputLog [2 ]... ).Has (WaitString ) {
663
678
t .Errorf ("wrong CombinedOutput() log, got %s" , fcmd .CombinedOutputLog [2 ])
664
679
}
665
680
}
@@ -669,6 +684,8 @@ func TestWaitFlagOld(t *testing.T) {
669
684
CombinedOutputScript : []fakeexec.FakeCombinedOutputAction {
670
685
// iptables version check
671
686
func () ([]byte , error ) { return []byte ("iptables v1.4.20" ), nil },
687
+ // iptables-restore version check
688
+ func () ([]byte , error ) { return []byte {}, nil },
672
689
// Success.
673
690
func () ([]byte , error ) { return []byte {}, nil },
674
691
},
@@ -677,6 +694,7 @@ func TestWaitFlagOld(t *testing.T) {
677
694
CommandScript : []fakeexec.FakeCommandAction {
678
695
func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
679
696
func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
697
+ func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
680
698
},
681
699
}
682
700
runner := New (& fexec , dbus .NewFake (nil , nil ), ProtocolIpv4 )
@@ -685,14 +703,14 @@ func TestWaitFlagOld(t *testing.T) {
685
703
if err != nil {
686
704
t .Errorf ("expected success, got %v" , err )
687
705
}
688
- if fcmd .CombinedOutputCalls != 2 {
689
- t .Errorf ("expected 2 CombinedOutput() calls, got %d" , fcmd .CombinedOutputCalls )
706
+ if fcmd .CombinedOutputCalls != 3 {
707
+ t .Errorf ("expected 3 CombinedOutput() calls, got %d" , fcmd .CombinedOutputCalls )
690
708
}
691
- if ! sets .NewString (fcmd .CombinedOutputLog [1 ]... ).HasAll ("iptables" , WaitString ) {
692
- t .Errorf ("wrong CombinedOutput() log, got %s" , fcmd .CombinedOutputLog [1 ])
709
+ if ! sets .NewString (fcmd .CombinedOutputLog [2 ]... ).HasAll ("iptables" , WaitString ) {
710
+ t .Errorf ("wrong CombinedOutput() log, got %s" , fcmd .CombinedOutputLog [2 ])
693
711
}
694
- if sets .NewString (fcmd .CombinedOutputLog [1 ]... ).Has (WaitSecondsValue ) {
695
- t .Errorf ("wrong CombinedOutput() log, got %s" , fcmd .CombinedOutputLog [1 ])
712
+ if sets .NewString (fcmd .CombinedOutputLog [2 ]... ).Has (WaitSecondsValue ) {
713
+ t .Errorf ("wrong CombinedOutput() log, got %s" , fcmd .CombinedOutputLog [2 ])
696
714
}
697
715
}
698
716
@@ -701,6 +719,8 @@ func TestWaitFlagNew(t *testing.T) {
701
719
CombinedOutputScript : []fakeexec.FakeCombinedOutputAction {
702
720
// iptables version check
703
721
func () ([]byte , error ) { return []byte ("iptables v1.4.22" ), nil },
722
+ // iptables-restore version check
723
+ func () ([]byte , error ) { return []byte {}, nil },
704
724
// Success.
705
725
func () ([]byte , error ) { return []byte {}, nil },
706
726
},
@@ -709,6 +729,7 @@ func TestWaitFlagNew(t *testing.T) {
709
729
CommandScript : []fakeexec.FakeCommandAction {
710
730
func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
711
731
func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
732
+ func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
712
733
},
713
734
}
714
735
runner := New (& fexec , dbus .NewFake (nil , nil ), ProtocolIpv4 )
@@ -717,11 +738,11 @@ func TestWaitFlagNew(t *testing.T) {
717
738
if err != nil {
718
739
t .Errorf ("expected success, got %v" , err )
719
740
}
720
- if fcmd .CombinedOutputCalls != 2 {
721
- t .Errorf ("expected 2 CombinedOutput() calls, got %d" , fcmd .CombinedOutputCalls )
741
+ if fcmd .CombinedOutputCalls != 3 {
742
+ t .Errorf ("expected 3 CombinedOutput() calls, got %d" , fcmd .CombinedOutputCalls )
722
743
}
723
- if ! sets .NewString (fcmd .CombinedOutputLog [1 ]... ).HasAll ("iptables" , WaitString , WaitSecondsValue ) {
724
- t .Errorf ("wrong CombinedOutput() log, got %s" , fcmd .CombinedOutputLog [1 ])
744
+ if ! sets .NewString (fcmd .CombinedOutputLog [2 ]... ).HasAll ("iptables" , WaitString , WaitSecondsValue ) {
745
+ t .Errorf ("wrong CombinedOutput() log, got %s" , fcmd .CombinedOutputLog [2 ])
725
746
}
726
747
}
727
748
@@ -736,7 +757,7 @@ func TestReload(t *testing.T) {
736
757
fcmd := fakeexec.FakeCmd {
737
758
CombinedOutputScript : []fakeexec.FakeCombinedOutputAction {
738
759
// iptables version check
739
- func () ([]byte , error ) { return []byte ("iptables v1.4.22 " ), nil },
760
+ func () ([]byte , error ) { return []byte ("iptables v1.6.4 " ), nil },
740
761
741
762
// first reload
742
763
// EnsureChain
@@ -1096,6 +1117,8 @@ func TestRestoreAllWaitOldIptablesRestore(t *testing.T) {
1096
1117
CombinedOutputScript : []fakeexec.FakeCombinedOutputAction {
1097
1118
// iptables version check
1098
1119
func () ([]byte , error ) { return []byte ("iptables v1.4.22" ), nil },
1120
+ // iptables-restore version check
1121
+ func () ([]byte , error ) { return []byte {}, nil },
1099
1122
func () ([]byte , error ) { return []byte {}, nil },
1100
1123
func () ([]byte , error ) { return nil , & fakeexec.FakeExitError {Status : 1 } },
1101
1124
},
@@ -1105,6 +1128,7 @@ func TestRestoreAllWaitOldIptablesRestore(t *testing.T) {
1105
1128
func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
1106
1129
func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
1107
1130
func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
1131
+ func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
1108
1132
},
1109
1133
}
1110
1134
runner := newInternal (& fexec , dbus .NewFake (nil , nil ), ProtocolIpv4 , TestLockfilePath )
@@ -1116,16 +1140,16 @@ func TestRestoreAllWaitOldIptablesRestore(t *testing.T) {
1116
1140
t .Fatalf ("expected success, got %v" , err )
1117
1141
}
1118
1142
1119
- commandSet := sets .NewString (fcmd .CombinedOutputLog [1 ]... )
1143
+ commandSet := sets .NewString (fcmd .CombinedOutputLog [2 ]... )
1120
1144
if ! commandSet .HasAll ("iptables-restore" , "--counters" , "--noflush" ) {
1121
1145
t .Errorf ("wrong CombinedOutput() log, got %s" , fcmd .CombinedOutputLog [2 ])
1122
1146
}
1123
- if commandSet .HasAll (WaitString , WaitSecondsValue ) {
1147
+ if commandSet .HasAll (WaitString ) {
1124
1148
t .Errorf ("wrong CombinedOutput() log (unexpected %s option), got %s" , WaitString , fcmd .CombinedOutputLog [1 ])
1125
1149
}
1126
1150
1127
- if fcmd .CombinedOutputCalls != 2 {
1128
- t .Errorf ("expected 2 CombinedOutput() calls, got %d" , fcmd .CombinedOutputCalls )
1151
+ if fcmd .CombinedOutputCalls != 3 {
1152
+ t .Errorf ("expected 3 CombinedOutput() calls, got %d" , fcmd .CombinedOutputCalls )
1129
1153
}
1130
1154
1131
1155
// Failure.
@@ -1143,11 +1167,14 @@ func TestRestoreAllGrabNewLock(t *testing.T) {
1143
1167
CombinedOutputScript : []fakeexec.FakeCombinedOutputAction {
1144
1168
// iptables version check
1145
1169
func () ([]byte , error ) { return []byte ("iptables v1.4.22" ), nil },
1170
+ // iptables-restore version check
1171
+ func () ([]byte , error ) { return []byte {}, nil },
1146
1172
},
1147
1173
}
1148
1174
fexec := fakeexec.FakeExec {
1149
1175
CommandScript : []fakeexec.FakeCommandAction {
1150
1176
func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
1177
+ func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
1151
1178
},
1152
1179
}
1153
1180
@@ -1183,11 +1210,14 @@ func TestRestoreAllGrabOldLock(t *testing.T) {
1183
1210
CombinedOutputScript : []fakeexec.FakeCombinedOutputAction {
1184
1211
// iptables version check
1185
1212
func () ([]byte , error ) { return []byte ("iptables v1.4.22" ), nil },
1213
+ // iptables-restore version check
1214
+ func () ([]byte , error ) { return []byte {}, nil },
1186
1215
},
1187
1216
}
1188
1217
fexec := fakeexec.FakeExec {
1189
1218
CommandScript : []fakeexec.FakeCommandAction {
1190
1219
func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
1220
+ func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
1191
1221
},
1192
1222
}
1193
1223
@@ -1210,3 +1240,52 @@ func TestRestoreAllGrabOldLock(t *testing.T) {
1210
1240
t .Errorf ("expected timeout error, got %v" , err )
1211
1241
}
1212
1242
}
1243
+
1244
+ // TestRestoreAllWaitBackportedIptablesRestore tests that the "wait" flag is passed
1245
+ // to a seemingly-old-but-actually-new iptables-restore
1246
+ func TestRestoreAllWaitBackportedIptablesRestore (t * testing.T ) {
1247
+ fcmd := fakeexec.FakeCmd {
1248
+ CombinedOutputScript : []fakeexec.FakeCombinedOutputAction {
1249
+ // iptables version check
1250
+ func () ([]byte , error ) { return []byte ("iptables v1.4.22" ), nil },
1251
+ // iptables-restore version check
1252
+ func () ([]byte , error ) { return []byte ("iptables v1.4.22" ), nil },
1253
+ func () ([]byte , error ) { return []byte {}, nil },
1254
+ func () ([]byte , error ) { return nil , & fakeexec.FakeExitError {Status : 1 } },
1255
+ },
1256
+ }
1257
+ fexec := fakeexec.FakeExec {
1258
+ CommandScript : []fakeexec.FakeCommandAction {
1259
+ func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
1260
+ func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
1261
+ func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
1262
+ func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
1263
+ },
1264
+ }
1265
+ runner := newInternal (& fexec , dbus .NewFake (nil , nil ), ProtocolIpv4 , TestLockfilePath )
1266
+ defer os .Remove (TestLockfilePath )
1267
+ defer runner .Destroy ()
1268
+
1269
+ err := runner .RestoreAll ([]byte {}, NoFlushTables , RestoreCounters )
1270
+ if err != nil {
1271
+ t .Fatalf ("expected success, got %v" , err )
1272
+ }
1273
+
1274
+ commandSet := sets .NewString (fcmd .CombinedOutputLog [2 ]... )
1275
+ if ! commandSet .HasAll ("iptables-restore" , "--counters" , "--noflush" ) {
1276
+ t .Errorf ("wrong CombinedOutput() log, got %s" , fcmd .CombinedOutputLog [2 ])
1277
+ }
1278
+ if ! commandSet .HasAll (WaitString ) {
1279
+ t .Errorf ("wrong CombinedOutput() log (expected %s option), got %s" , WaitString , fcmd .CombinedOutputLog [1 ])
1280
+ }
1281
+
1282
+ if fcmd .CombinedOutputCalls != 3 {
1283
+ t .Errorf ("expected 3 CombinedOutput() calls, got %d" , fcmd .CombinedOutputCalls )
1284
+ }
1285
+
1286
+ // Failure.
1287
+ err = runner .Restore (TableNAT , []byte {}, FlushTables , RestoreCounters )
1288
+ if err == nil {
1289
+ t .Errorf ("expected failure" )
1290
+ }
1291
+ }
0 commit comments