Skip to content

Commit 195e1e7

Browse files
committed
libnetwork: prefer DeferCleanup to AfterEach in tests
In Ginkgo, there might be some unexpected behavior when mising AfterEach and DeferCleanup (see [1], [2]), so let's switch to DeferCleanup. [1]: containers/podman@3c0176b2d0dd3 [2]: onsi/ginkgo#1360 Reported-by: Paul Holzinger <[email protected]> Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent f710c98 commit 195e1e7

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

Diff for: libnetwork/cni/config_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ var _ = Describe("Config", func() {
3131
logBuffer = bytes.Buffer{}
3232
logrus.SetOutput(&logBuffer)
3333
logrus.SetLevel(logrus.InfoLevel)
34+
DeferCleanup(func() {
35+
logrus.SetLevel(logrus.InfoLevel)
36+
})
3437
})
3538

3639
JustBeforeEach(func() {
@@ -41,10 +44,6 @@ var _ = Describe("Config", func() {
4144
}
4245
})
4346

44-
AfterEach(func() {
45-
logrus.SetLevel(logrus.InfoLevel)
46-
})
47-
4847
Context("basic network config tests", func() {
4948
It("check default network config exists", func() {
5049
networks, err := libpodNet.NetworkList()

Diff for: libnetwork/cni/run_test.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,24 @@ var _ = Describe("run CNI", func() {
8686
if err != nil {
8787
Fail("Failed to create netns")
8888
}
89+
DeferCleanup(func() {
90+
_ = netns.UnmountNS(netNSTest.Path())
91+
_ = netNSTest.Close()
92+
})
8993

9094
netNSContainer, err = netns.NewNS()
9195
if err != nil {
9296
Fail("Failed to create netns")
9397
}
98+
DeferCleanup(func() {
99+
_ = netns.UnmountNS(netNSContainer.Path())
100+
_ = netNSContainer.Close()
101+
})
102+
94103
logrus.SetLevel(logrus.WarnLevel)
104+
DeferCleanup(func() {
105+
logrus.SetLevel(logrus.InfoLevel)
106+
})
95107
})
96108

97109
JustBeforeEach(func() {
@@ -102,16 +114,6 @@ var _ = Describe("run CNI", func() {
102114
}
103115
})
104116

105-
AfterEach(func() {
106-
logrus.SetLevel(logrus.InfoLevel)
107-
108-
_ = netns.UnmountNS(netNSTest.Path())
109-
_ = netNSTest.Close()
110-
111-
_ = netns.UnmountNS(netNSContainer.Path())
112-
_ = netNSContainer.Close()
113-
})
114-
115117
Context("network setup test", func() {
116118
It("run with default config", func() {
117119
runTest(func() {

Diff for: libnetwork/netavark/run_test.go

+18-12
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,23 @@ var _ = Describe("run netavark", func() {
6161
Skip("NETAVARK_BINARY not set skip run tests")
6262
}
6363

64-
// set the logrus settings
65-
logrus.SetLevel(logrus.TraceLevel)
66-
// disable extra quotes so we can easily copy the netavark command
67-
logrus.SetFormatter(&logrus.TextFormatter{DisableQuote: true})
68-
logrus.SetOutput(os.Stderr)
6964
// The tests need root privileges.
7065
// Technically we could work around that by using user namespaces and
7166
// the rootless cni code but this is to much work to get it right for a unit test.
7267
if unshare.IsRootless() {
7368
Skip("this test needs to be run as root")
7469
}
7570

71+
// set the logrus settings
72+
logrus.SetLevel(logrus.TraceLevel)
73+
// disable extra quotes so we can easily copy the netavark command
74+
logrus.SetFormatter(&logrus.TextFormatter{DisableQuote: true})
75+
logrus.SetOutput(os.Stderr)
76+
DeferCleanup(func() {
77+
logrus.SetFormatter(&logrus.TextFormatter{})
78+
logrus.SetLevel(logrus.InfoLevel)
79+
})
80+
7681
t := GinkgoT()
7782
confDir = t.TempDir()
7883

@@ -81,12 +86,19 @@ var _ = Describe("run netavark", func() {
8186
if err != nil {
8287
Fail("Failed to create netns")
8388
}
89+
DeferCleanup(func() {
90+
_ = netns.UnmountNS(netNSTest.Path())
91+
_ = netNSTest.Close()
92+
})
8493

8594
netNSContainer, err = netns.NewNS()
8695
if err != nil {
8796
Fail("Failed to create netns")
8897
}
89-
98+
DeferCleanup(func() {
99+
_ = netns.UnmountNS(netNSContainer.Path())
100+
_ = netNSContainer.Close()
101+
})
90102
// Force iptables driver, firewalld is broken inside the extra
91103
// namespace because it still connects to firewalld on the host.
92104
t.Setenv("NETAVARK_FW", "iptables")
@@ -103,12 +115,6 @@ var _ = Describe("run netavark", func() {
103115
AfterEach(func() {
104116
logrus.SetFormatter(&logrus.TextFormatter{})
105117
logrus.SetLevel(logrus.InfoLevel)
106-
107-
_ = netns.UnmountNS(netNSTest.Path())
108-
_ = netNSTest.Close()
109-
110-
_ = netns.UnmountNS(netNSContainer.Path())
111-
_ = netNSContainer.Close()
112118
})
113119

114120
It("test basic setup", func() {

0 commit comments

Comments
 (0)