11package policies
22
33import (
4- "errors"
54 "fmt"
65 "sort"
76 "strings"
@@ -45,8 +44,6 @@ Chain AZURE-NPM-INGRESS (1 references)
4544`
4645)
4746
48- var errKernelVersion = errors .New ("kernel error" )
49-
5047func TestStaleChainsForceLock (t * testing.T ) {
5148 testChains := []string {}
5249 for i := 0 ; i < 100000 ; i ++ {
@@ -59,7 +56,6 @@ func TestStaleChainsForceLock(t *testing.T) {
5956 ioshim := common .NewMockIOShim (calls )
6057 // don't verify calls because there shouldn't be as many commands as we create if forceLock works properly
6158 pMgr := NewPolicyManager (ioshim , ipsetConfig )
62- util .SetIptablesToNft ()
6359
6460 start := make (chan struct {}, 1 )
6561 done := make (chan struct {}, 1 )
@@ -146,7 +142,6 @@ func TestCleanupChainsSuccess(t *testing.T) {
146142 ioshim := common .NewMockIOShim (calls )
147143 defer ioshim .VerifyCalls (t , calls )
148144 pMgr := NewPolicyManager (ioshim , ipsetConfig )
149- util .SetIptablesToNft ()
150145
151146 pMgr .staleChains .add (testChain1 )
152147 pMgr .staleChains .add (testChain2 )
@@ -165,7 +160,6 @@ func TestCleanupChainsFailure(t *testing.T) {
165160 ioshim := common .NewMockIOShim (calls )
166161 defer ioshim .VerifyCalls (t , calls )
167162 pMgr := NewPolicyManager (ioshim , ipsetConfig )
168- util .SetIptablesToNft ()
169163
170164 pMgr .staleChains .add (testChain1 )
171165 pMgr .staleChains .add (testChain2 )
@@ -484,7 +478,6 @@ func TestBootupLinux(t *testing.T) {
484478 ioshim := common .NewMockIOShim (tt .calls )
485479 defer ioshim .VerifyCalls (t , tt .calls )
486480 pMgr := NewPolicyManager (ioshim , ipsetConfig )
487- util .SetIptablesToNft ()
488481 err := pMgr .bootupAfterDetectAndCleanup ()
489482 if tt .wantErr {
490483 require .Error (t , err )
@@ -773,7 +766,6 @@ func TestPositionAzureChainJumpRule(t *testing.T) {
773766 PlaceAzureChainFirst : tt .placeAzureChainFirst ,
774767 }
775768 pMgr := NewPolicyManager (ioshim , cfg )
776- util .SetIptablesToNft ()
777769
778770 err := pMgr .positionAzureChainJumpRule ()
779771 if tt .wantErr {
@@ -866,7 +858,6 @@ func TestChainLineNumber(t *testing.T) {
866858 ioshim := common .NewMockIOShim (tt .calls )
867859 defer ioshim .VerifyCalls (t , tt .calls )
868860 pMgr := NewPolicyManager (ioshim , ipsetConfig )
869- util .SetIptablesToNft ()
870861
871862 lineNum , err := pMgr .chainLineNumber (testChainName )
872863 if tt .wantErr {
@@ -903,10 +894,7 @@ func stringsToMap(items []string) map[string]struct{} {
903894func TestDetectIptablesVersion (t * testing.T ) {
904895 type args struct {
905896 name string
906- kernelVersion int
907- kernelVersionErr error
908897 calls []testutils.TestCmd
909- expectedErr bool
910898 expectedIptablesVersion string
911899 }
912900
@@ -919,7 +907,6 @@ func TestDetectIptablesVersion(t *testing.T) {
919907 ExitCode : 0 ,
920908 },
921909 },
922- expectedErr : false ,
923910 expectedIptablesVersion : util .IptablesNft ,
924911 },
925912 {
@@ -934,7 +921,6 @@ func TestDetectIptablesVersion(t *testing.T) {
934921 ExitCode : 0 ,
935922 },
936923 },
937- expectedErr : false ,
938924 expectedIptablesVersion : util .IptablesNft ,
939925 },
940926 {
@@ -953,36 +939,10 @@ func TestDetectIptablesVersion(t *testing.T) {
953939 ExitCode : 0 ,
954940 },
955941 },
956- expectedErr : false ,
957942 expectedIptablesVersion : util .IptablesLegacy ,
958943 },
959944 {
960- name : "nft and legacy both fail: kernel version >= 5" ,
961- kernelVersion : 5 ,
962- calls : []testutils.TestCmd {
963- {
964- Cmd : []string {"iptables-nft" , "-w" , "60" , "-L" , "KUBE-IPTABLES-HINT" , "-t" , "mangle" , "-n" },
965- ExitCode : 2 ,
966- },
967- {
968- Cmd : []string {"iptables-nft" , "-w" , "60" , "-L" , "KUBE-KUBELET-CANARY" , "-t" , "mangle" , "-n" },
969- ExitCode : 2 ,
970- },
971- {
972- Cmd : []string {"iptables" , "-w" , "60" , "-L" , "KUBE-IPTABLES-HINT" , "-t" , "mangle" , "-n" },
973- ExitCode : 2 ,
974- },
975- {
976- Cmd : []string {"iptables" , "-w" , "60" , "-L" , "KUBE-KUBELET-CANARY" , "-t" , "mangle" , "-n" },
977- ExitCode : 2 ,
978- },
979- },
980- expectedErr : false ,
981- expectedIptablesVersion : util .IptablesNft ,
982- },
983- {
984- name : "no kube chains: kernel version < 5" ,
985- kernelVersion : 4 ,
945+ name : "no kube chains: default nft" ,
986946 calls : []testutils.TestCmd {
987947 {
988948 Cmd : []string {"iptables-nft" , "-w" , "60" , "-L" , "KUBE-IPTABLES-HINT" , "-t" , "mangle" , "-n" },
@@ -1001,64 +961,50 @@ func TestDetectIptablesVersion(t *testing.T) {
1001961 ExitCode : 1 ,
1002962 },
1003963 },
1004- expectedErr : false ,
1005- expectedIptablesVersion : util .IptablesLegacy ,
964+ expectedIptablesVersion : util .IptablesNft ,
1006965 },
1007966 {
1008- name : "no kube chains: kernel version error" ,
1009- kernelVersionErr : errKernelVersion ,
967+ name : "nft and legacy both fail: default nft" ,
1010968 calls : []testutils.TestCmd {
1011969 {
1012970 Cmd : []string {"iptables-nft" , "-w" , "60" , "-L" , "KUBE-IPTABLES-HINT" , "-t" , "mangle" , "-n" },
1013- ExitCode : 1 ,
971+ ExitCode : 2 ,
1014972 },
1015973 {
1016974 Cmd : []string {"iptables-nft" , "-w" , "60" , "-L" , "KUBE-KUBELET-CANARY" , "-t" , "mangle" , "-n" },
1017- ExitCode : 1 ,
975+ ExitCode : 2 ,
1018976 },
1019977 {
1020978 Cmd : []string {"iptables" , "-w" , "60" , "-L" , "KUBE-IPTABLES-HINT" , "-t" , "mangle" , "-n" },
1021- ExitCode : 1 ,
979+ ExitCode : 2 ,
1022980 },
1023981 {
1024982 Cmd : []string {"iptables" , "-w" , "60" , "-L" , "KUBE-KUBELET-CANARY" , "-t" , "mangle" , "-n" },
1025- ExitCode : 1 ,
983+ ExitCode : 2 ,
1026984 },
1027985 },
1028- expectedErr : true ,
986+ expectedIptablesVersion : util . IptablesNft ,
1029987 },
1030988 }
1031989
1032990 for _ , tt := range tests {
1033991 tt := tt
1034992
1035- if tt .name != "no kube chains: kernel version error" {
1036- continue
1037- }
1038-
1039993 t .Run (tt .name , func (t * testing.T ) {
1040994
1041995 metrics .InitializeAll ()
1042996
1043997 ioshim := common .NewMockIOShim (tt .calls )
1044998 defer ioshim .VerifyCalls (t , tt .calls )
1045999 cfg := & PolicyManagerCfg {
1046- debug : true ,
1047- debugKernelVersion : tt .kernelVersion ,
1048- debugKernelVersionErr : tt .kernelVersionErr ,
1049- NodeIP : "6.7.8.9" ,
1050- PolicyMode : IPSetPolicyMode ,
1051- PlaceAzureChainFirst : util .PlaceAzureChainFirst ,
1000+ NodeIP : "6.7.8.9" ,
1001+ PolicyMode : IPSetPolicyMode ,
1002+ PlaceAzureChainFirst : util .PlaceAzureChainFirst ,
10521003 }
10531004 pMgr := NewPolicyManager (ioshim , cfg )
1054- err := pMgr .detectIptablesVersion ()
1005+ pMgr .detectIptablesVersion ()
10551006
1056- if tt .expectedErr {
1057- require .Error (t , err )
1058- } else {
1059- require .NoError (t , err )
1060- require .Equal (t , tt .expectedIptablesVersion , util .Iptables )
1061- }
1007+ require .Equal (t , tt .expectedIptablesVersion , util .Iptables )
10621008 })
10631009 }
10641010}
@@ -1268,19 +1214,19 @@ func TestCleanupOtherChains(t *testing.T) {
12681214 },
12691215 {
12701216 Cmd : []string {"iptables" , "-w" , "60" , "-F" , "AZURE-NPM" },
1271- ExitCode : 1 ,
1217+ ExitCode : 2 ,
12721218 },
12731219 {
12741220 Cmd : []string {"iptables" , "-w" , "60" , "-F" , "AZURE-NPM-INGRESS" },
1275- ExitCode : 1 ,
1221+ ExitCode : 2 ,
12761222 },
12771223 {
12781224 Cmd : []string {"iptables" , "-w" , "60" , "-X" , "AZURE-NPM" },
1279- ExitCode : 1 ,
1225+ ExitCode : 2 ,
12801226 },
12811227 {
12821228 Cmd : []string {"iptables" , "-w" , "60" , "-X" , "AZURE-NPM-INGRESS" },
1283- ExitCode : 1 ,
1229+ ExitCode : 2 ,
12841230 },
12851231 },
12861232 expectedErr : false ,
@@ -1402,6 +1348,10 @@ func TestCleanupOtherChains(t *testing.T) {
14021348 util .SetIptablesToNft ()
14031349 } else {
14041350 util .SetIptablesToLegacy ()
1351+ // set back to default
1352+ defer func () {
1353+ util .SetIptablesToNft ()
1354+ }()
14051355 }
14061356
14071357 err := pMgr .cleanupOtherIptables ()
0 commit comments