Browse code

libnetwork: TestUserChain: re-use IPTables instances

The test already creates instances for each ip-version, so let's
re-use them. While changing, also use assert.Check to not fail early.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2023/07/17 01:05:47
Showing 1 changed files
... ...
@@ -11,6 +11,7 @@ import (
11 11
 	"github.com/docker/docker/libnetwork/options"
12 12
 	"github.com/docker/docker/libnetwork/testutils"
13 13
 	"gotest.tools/v3/assert"
14
+	is "gotest.tools/v3/assert/cmp"
14 15
 )
15 16
 
16 17
 const (
... ...
@@ -63,8 +64,8 @@ func TestUserChain(t *testing.T) {
63 63
 			defer c.Stop()
64 64
 
65 65
 			// init. condition, FORWARD chain empty DOCKER-USER not exist
66
-			assert.DeepEqual(t, getRules(t, iptables.IPv4, fwdChainName), []string{"-P FORWARD ACCEPT"})
67
-			assert.DeepEqual(t, getRules(t, iptables.IPv6, fwdChainName), []string{"-P FORWARD ACCEPT"})
66
+			assert.Check(t, is.DeepEqual(getRules(t, iptable4, fwdChainName), []string{"-P FORWARD ACCEPT"}))
67
+			assert.Check(t, is.DeepEqual(getRules(t, iptable6, fwdChainName), []string{"-P FORWARD ACCEPT"}))
68 68
 
69 69
 			if tc.insert {
70 70
 				_, err = iptable4.Raw("-A", fwdChainName, "-j", "DROP")
... ...
@@ -74,11 +75,11 @@ func TestUserChain(t *testing.T) {
74 74
 			}
75 75
 			arrangeUserFilterRule()
76 76
 
77
-			assert.DeepEqual(t, getRules(t, iptables.IPv4, fwdChainName), tc.fwdChain)
78
-			assert.DeepEqual(t, getRules(t, iptables.IPv6, fwdChainName), tc.fwdChain)
77
+			assert.Check(t, is.DeepEqual(getRules(t, iptable4, fwdChainName), tc.fwdChain))
78
+			assert.Check(t, is.DeepEqual(getRules(t, iptable6, fwdChainName), tc.fwdChain))
79 79
 			if tc.userChain != nil {
80
-				assert.DeepEqual(t, getRules(t, iptables.IPv4, usrChainName), tc.userChain)
81
-				assert.DeepEqual(t, getRules(t, iptables.IPv6, usrChainName), tc.userChain)
80
+				assert.Check(t, is.DeepEqual(getRules(t, iptable4, usrChainName), tc.userChain))
81
+				assert.Check(t, is.DeepEqual(getRules(t, iptable6, usrChainName), tc.userChain))
82 82
 			} else {
83 83
 				_, err := iptable4.Raw("-S", usrChainName)
84 84
 				assert.Assert(t, err != nil, "ipv4 chain %v: created unexpectedly", usrChainName)
... ...
@@ -89,9 +90,7 @@ func TestUserChain(t *testing.T) {
89 89
 	}
90 90
 }
91 91
 
92
-func getRules(t *testing.T, ipVer iptables.IPVersion, chain string) []string {
93
-	iptable := iptables.GetIptable(ipVer)
94
-
92
+func getRules(t *testing.T, iptable *iptables.IPTable, chain string) []string {
95 93
 	t.Helper()
96 94
 	output, err := iptable.Raw("-S", chain)
97 95
 	assert.NilError(t, err, "chain %s: failed to get rules", chain)