Skip to content

Commit ce0a423

Browse files
committed
test(iptables): deflake TestRestoreAllWaitOldIptablesRestore
Signed-off-by: knight42 <[email protected]>
1 parent b25af8e commit ce0a423

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

pkg/util/iptables/iptables_test.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ import (
3535
fakeexec "k8s.io/utils/exec/testing"
3636
)
3737

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+
}
3943

4044
func testIPTablesVersionCmds(t *testing.T, protocol Protocol) {
4145
version := " v1.4.22"
@@ -934,8 +938,8 @@ func TestRestoreAll(t *testing.T) {
934938
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
935939
},
936940
}
937-
runner := newInternal(&fexec, ProtocolIPv4, TestLockfilePath)
938-
defer os.Remove(TestLockfilePath)
941+
lockPath14x, lockPath16x := getLockPaths()
942+
runner := newInternal(&fexec, ProtocolIPv4, lockPath14x, lockPath16x)
939943

940944
err := runner.RestoreAll([]byte{}, NoFlushTables, RestoreCounters)
941945
if err != nil {
@@ -975,8 +979,8 @@ func TestRestoreAllWait(t *testing.T) {
975979
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
976980
},
977981
}
978-
runner := newInternal(&fexec, ProtocolIPv4, TestLockfilePath)
979-
defer os.Remove(TestLockfilePath)
982+
lockPath14x, lockPath16x := getLockPaths()
983+
runner := newInternal(&fexec, ProtocolIPv4, lockPath14x, lockPath16x)
980984

981985
err := runner.RestoreAll([]byte{}, NoFlushTables, RestoreCounters)
982986
if err != nil {
@@ -1020,8 +1024,8 @@ func TestRestoreAllWaitOldIptablesRestore(t *testing.T) {
10201024
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
10211025
},
10221026
}
1023-
runner := newInternal(&fexec, ProtocolIPv4, TestLockfilePath)
1024-
defer os.Remove(TestLockfilePath)
1027+
lockPath14x, lockPath16x := getLockPaths()
1028+
runner := newInternal(&fexec, ProtocolIPv4, lockPath14x, lockPath16x)
10251029

10261030
err := runner.RestoreAll([]byte{}, NoFlushTables, RestoreCounters)
10271031
if err != nil {
@@ -1065,24 +1069,23 @@ func TestRestoreAllGrabNewLock(t *testing.T) {
10651069
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
10661070
},
10671071
}
1068-
1069-
runner := newInternal(&fexec, ProtocolIPv4, TestLockfilePath)
1070-
defer os.Remove(TestLockfilePath)
1072+
lockPath14x, lockPath16x := getLockPaths()
1073+
runner := newInternal(&fexec, ProtocolIPv4, lockPath14x, lockPath16x)
10711074

10721075
// 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)
10741077
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)
10761079
}
10771080
defer runLock.Close()
10781081

10791082
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)
10811084
}
10821085

10831086
err = runner.RestoreAll([]byte{}, NoFlushTables, RestoreCounters)
10841087
if err == nil {
1085-
t.Errorf("expected failure, got success instead")
1088+
t.Fatal("expected failure, got success instead")
10861089
}
10871090
if !strings.Contains(err.Error(), "failed to acquire new iptables lock: timed out waiting for the condition") {
10881091
t.Errorf("expected timeout error, got %v", err)
@@ -1107,22 +1110,21 @@ func TestRestoreAllGrabOldLock(t *testing.T) {
11071110
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
11081111
},
11091112
}
1110-
1111-
runner := newInternal(&fexec, ProtocolIPv4, TestLockfilePath)
1112-
defer os.Remove(TestLockfilePath)
1113+
lockPath14x, lockPath16x := getLockPaths()
1114+
runner := newInternal(&fexec, ProtocolIPv4, lockPath14x, lockPath16x)
11131115

11141116
var runLock *net.UnixListener
11151117
// Grab the abstract @xtables socket, will retry if the socket exists
11161118
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"})
11181120
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)
11201122
return false, nil
11211123
}
11221124
return true, nil
11231125
})
11241126
if err != nil {
1125-
t.Fatal("Timed out locking @xtables")
1127+
t.Fatalf("Timed out locking %s", lockPath14x)
11261128
}
11271129
if runLock == nil {
11281130
t.Fatal("Unexpected nil runLock")
@@ -1132,7 +1134,7 @@ func TestRestoreAllGrabOldLock(t *testing.T) {
11321134

11331135
err = runner.RestoreAll([]byte{}, NoFlushTables, RestoreCounters)
11341136
if err == nil {
1135-
t.Errorf("expected failure, got success instead")
1137+
t.Fatal("expected failure, got success instead")
11361138
}
11371139
if !strings.Contains(err.Error(), "failed to acquire old iptables lock: timed out waiting for the condition") {
11381140
t.Errorf("expected timeout error, got %v", err)
@@ -1160,8 +1162,8 @@ func TestRestoreAllWaitBackportedIptablesRestore(t *testing.T) {
11601162
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
11611163
},
11621164
}
1163-
runner := newInternal(&fexec, ProtocolIPv4, TestLockfilePath)
1164-
defer os.Remove(TestLockfilePath)
1165+
lockPath14x, lockPath16x := getLockPaths()
1166+
runner := newInternal(&fexec, ProtocolIPv4, lockPath14x, lockPath16x)
11651167

11661168
err := runner.RestoreAll([]byte{}, NoFlushTables, RestoreCounters)
11671169
if err != nil {

0 commit comments

Comments
 (0)