@@ -35,7 +35,11 @@ import (
35
35
fakeexec "k8s.io/utils/exec/testing"
36
36
)
37
37
38
- const TestLockfilePath = "xtables.lock"
38
+ func getLockPaths () (string , string ) {
39
+ lock14x := fmt .Sprintf ("@xtables-%d" , time .Now ().Nanosecond ())
40
+ lock16x := fmt .Sprintf ("xtables-%d.lock" , time .Now ().Nanosecond ())
41
+ return lock14x , lock16x
42
+ }
39
43
40
44
func testIPTablesVersionCmds (t * testing.T , protocol Protocol ) {
41
45
version := " v1.4.22"
@@ -934,8 +938,8 @@ func TestRestoreAll(t *testing.T) {
934
938
func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
935
939
},
936
940
}
937
- runner := newInternal ( & fexec , ProtocolIPv4 , TestLockfilePath )
938
- defer os . Remove ( TestLockfilePath )
941
+ lockPath14x , lockPath16x := getLockPaths ( )
942
+ runner := newInternal ( & fexec , ProtocolIPv4 , lockPath14x , lockPath16x )
939
943
940
944
err := runner .RestoreAll ([]byte {}, NoFlushTables , RestoreCounters )
941
945
if err != nil {
@@ -975,8 +979,8 @@ func TestRestoreAllWait(t *testing.T) {
975
979
func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
976
980
},
977
981
}
978
- runner := newInternal ( & fexec , ProtocolIPv4 , TestLockfilePath )
979
- defer os . Remove ( TestLockfilePath )
982
+ lockPath14x , lockPath16x := getLockPaths ( )
983
+ runner := newInternal ( & fexec , ProtocolIPv4 , lockPath14x , lockPath16x )
980
984
981
985
err := runner .RestoreAll ([]byte {}, NoFlushTables , RestoreCounters )
982
986
if err != nil {
@@ -1020,8 +1024,8 @@ func TestRestoreAllWaitOldIptablesRestore(t *testing.T) {
1020
1024
func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
1021
1025
},
1022
1026
}
1023
- runner := newInternal ( & fexec , ProtocolIPv4 , TestLockfilePath )
1024
- defer os . Remove ( TestLockfilePath )
1027
+ lockPath14x , lockPath16x := getLockPaths ( )
1028
+ runner := newInternal ( & fexec , ProtocolIPv4 , lockPath14x , lockPath16x )
1025
1029
1026
1030
err := runner .RestoreAll ([]byte {}, NoFlushTables , RestoreCounters )
1027
1031
if err != nil {
@@ -1065,24 +1069,23 @@ func TestRestoreAllGrabNewLock(t *testing.T) {
1065
1069
func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
1066
1070
},
1067
1071
}
1068
-
1069
- runner := newInternal (& fexec , ProtocolIPv4 , TestLockfilePath )
1070
- defer os .Remove (TestLockfilePath )
1072
+ lockPath14x , lockPath16x := getLockPaths ()
1073
+ runner := newInternal (& fexec , ProtocolIPv4 , lockPath14x , lockPath16x )
1071
1074
1072
1075
// Grab the /run lock and ensure the RestoreAll fails
1073
- runLock , err := os .OpenFile (TestLockfilePath , os .O_CREATE , 0600 )
1076
+ runLock , err := os .OpenFile (lockPath16x , os .O_CREATE , 0600 )
1074
1077
if err != nil {
1075
- t .Fatalf ("expected to open %s, got %v" , TestLockfilePath , err )
1078
+ t .Fatalf ("expected to open %s, got %v" , lockPath16x , err )
1076
1079
}
1077
1080
defer runLock .Close ()
1078
1081
1079
1082
if err := grabIptablesFileLock (runLock ); err != nil {
1080
- t .Errorf ("expected to lock %s, got %v" , TestLockfilePath , err )
1083
+ t .Errorf ("expected to lock %s, got %v" , lockPath16x , err )
1081
1084
}
1082
1085
1083
1086
err = runner .RestoreAll ([]byte {}, NoFlushTables , RestoreCounters )
1084
1087
if err == nil {
1085
- t .Errorf ("expected failure, got success instead" )
1088
+ t .Fatal ("expected failure, got success instead" )
1086
1089
}
1087
1090
if ! strings .Contains (err .Error (), "failed to acquire new iptables lock: timed out waiting for the condition" ) {
1088
1091
t .Errorf ("expected timeout error, got %v" , err )
@@ -1107,22 +1110,21 @@ func TestRestoreAllGrabOldLock(t *testing.T) {
1107
1110
func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
1108
1111
},
1109
1112
}
1110
-
1111
- runner := newInternal (& fexec , ProtocolIPv4 , TestLockfilePath )
1112
- defer os .Remove (TestLockfilePath )
1113
+ lockPath14x , lockPath16x := getLockPaths ()
1114
+ runner := newInternal (& fexec , ProtocolIPv4 , lockPath14x , lockPath16x )
1113
1115
1114
1116
var runLock * net.UnixListener
1115
1117
// Grab the abstract @xtables socket, will retry if the socket exists
1116
1118
err := wait .PollImmediate (time .Second , wait .ForeverTestTimeout , func () (done bool , err error ) {
1117
- runLock , err = net .ListenUnix ("unix" , & net.UnixAddr {Name : "@xtables" , Net : "unix" })
1119
+ runLock , err = net .ListenUnix ("unix" , & net.UnixAddr {Name : lockPath14x , Net : "unix" })
1118
1120
if err != nil {
1119
- t .Logf ("Failed to lock @xtables : %v, will retry." , err )
1121
+ t .Logf ("Failed to lock %s : %v, will retry." , lockPath14x , err )
1120
1122
return false , nil
1121
1123
}
1122
1124
return true , nil
1123
1125
})
1124
1126
if err != nil {
1125
- t .Fatal ("Timed out locking @xtables" )
1127
+ t .Fatalf ("Timed out locking %s" , lockPath14x )
1126
1128
}
1127
1129
if runLock == nil {
1128
1130
t .Fatal ("Unexpected nil runLock" )
@@ -1132,7 +1134,7 @@ func TestRestoreAllGrabOldLock(t *testing.T) {
1132
1134
1133
1135
err = runner .RestoreAll ([]byte {}, NoFlushTables , RestoreCounters )
1134
1136
if err == nil {
1135
- t .Errorf ("expected failure, got success instead" )
1137
+ t .Fatal ("expected failure, got success instead" )
1136
1138
}
1137
1139
if ! strings .Contains (err .Error (), "failed to acquire old iptables lock: timed out waiting for the condition" ) {
1138
1140
t .Errorf ("expected timeout error, got %v" , err )
@@ -1160,8 +1162,8 @@ func TestRestoreAllWaitBackportedIptablesRestore(t *testing.T) {
1160
1162
func (cmd string , args ... string ) exec.Cmd { return fakeexec .InitFakeCmd (& fcmd , cmd , args ... ) },
1161
1163
},
1162
1164
}
1163
- runner := newInternal ( & fexec , ProtocolIPv4 , TestLockfilePath )
1164
- defer os . Remove ( TestLockfilePath )
1165
+ lockPath14x , lockPath16x := getLockPaths ( )
1166
+ runner := newInternal ( & fexec , ProtocolIPv4 , lockPath14x , lockPath16x )
1165
1167
1166
1168
err := runner .RestoreAll ([]byte {}, NoFlushTables , RestoreCounters )
1167
1169
if err != nil {
0 commit comments