Debian Codegraph @

plugins/meta/portmap/chain_test.go

raw symbols SCIP
1 // Copyright 2017-2018 CNI authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package main
16
17 import (
18 "fmt"
19 "math/rand"
20 "runtime"
21 "sync"
22
23 "github.com/coreos/go-iptables/iptables"
24 . "github.com/onsi/ginkgo/v2"
25 . "github.com/onsi/gomega"
26
27 "github.com/containernetworking/plugins/pkg/ns"
28 "github.com/containernetworking/plugins/pkg/testutils"
29 )
30
31 const TABLE = "filter" // We'll monkey around here
32
33 // TODO: run these tests in a new namespace
34 var _ = Describe("chain tests", func() {
35 var testChain chain
36 var ipt *iptables.IPTables
37 var testNs ns.NetNS
38 var cleanup func()
39
40 beforeEach := func() {
41 // Save a reference to the original namespace,
42 // Add a new NS
43 currNs, err := ns.GetCurrentNS()
44 Expect(err).NotTo(HaveOccurred())
45
46 testNs, err = testutils.NewNS()
47 Expect(err).NotTo(HaveOccurred())
48
49 tlChainName := fmt.Sprintf("cni-test-%d", rand.Intn(10000000))
50 chainName := fmt.Sprintf("cni-test-%d", rand.Intn(10000000))
51
52 testChain = chain{
53 table: TABLE,
54 name: chainName,
55 entryChains: []string{tlChainName},
56 entryRules: [][]string{{"-d", "203.0.113.1"}},
57 rules: [][]string{
58 {"-m", "comment", "--comment", "test 1", "-j", "RETURN"},
59 {"-m", "comment", "--comment", "test 2", "-j", "RETURN"},
60 },
61 }
62
63 ipt, err = iptables.NewWithProtocol(iptables.ProtocolIPv4)
64 Expect(err).NotTo(HaveOccurred())
65
66 runtime.LockOSThread()
67 err = testNs.Set()
68 Expect(err).NotTo(HaveOccurred())
69
70 err = ipt.ClearChain(TABLE, tlChainName) // This will create the chain
71 if err != nil {
72 currNs.Set()
73 Expect(err).NotTo(HaveOccurred())
74 }
75
76 cleanup = func() {
77 if ipt == nil {
78 return
79 }
80 ipt.ClearChain(TABLE, testChain.name)
81 ipt.ClearChain(TABLE, tlChainName)
82 ipt.DeleteChain(TABLE, testChain.name)
83 ipt.DeleteChain(TABLE, tlChainName)
84 currNs.Set()
85 }
86 }
87
88 It("creates and destroys a chain", func() {
89 beforeEach()
90 defer cleanup()
91
92 tlChainName := testChain.entryChains[0]
93
94 // add an extra rule to the test chain to make sure it's not touched
95 err := ipt.Append(TABLE, tlChainName, "-m", "comment", "--comment",
96 "canary value", "-j", "ACCEPT")
97 Expect(err).NotTo(HaveOccurred())
98
99 // Create the chain
100 err = testChain.setup(ipt)
101 Expect(err).NotTo(HaveOccurred())
102
103 // Verify the chain exists
104 ok := false
105 chains, err := ipt.ListChains(TABLE)
106 Expect(err).NotTo(HaveOccurred())
107 for _, chain := range chains {
108 if chain == testChain.name {
109 ok = true
110 break
111 }
112 }
113 if !ok {
114 Fail("Could not find created chain")
115 }
116
117 // Check that the entry rule was created
118 haveRules, err := ipt.List(TABLE, tlChainName)
119 Expect(err).NotTo(HaveOccurred())
120 Expect(haveRules).To(Equal([]string{
121 "-N " + tlChainName,
122 "-A " + tlChainName + ` -m comment --comment "canary value" -j ACCEPT`,
123 "-A " + tlChainName + " -d 203.0.113.1/32 -j " + testChain.name,
124 }))
125
126 // Check that the chain and rule was created
127 haveRules, err = ipt.List(TABLE, testChain.name)
128 Expect(err).NotTo(HaveOccurred())
129 Expect(haveRules).To(Equal([]string{
130 "-N " + testChain.name,
131 "-A " + testChain.name + ` -m comment --comment "test 1" -j RETURN`,
132 "-A " + testChain.name + ` -m comment --comment "test 2" -j RETURN`,
133 }))
134
135 err = testChain.teardown(ipt)
136 Expect(err).NotTo(HaveOccurred())
137
138 tlRules, err := ipt.List(TABLE, tlChainName)
139 Expect(err).NotTo(HaveOccurred())
140 Expect(tlRules).To(Equal([]string{
141 "-N " + tlChainName,
142 "-A " + tlChainName + ` -m comment --comment "canary value" -j ACCEPT`,
143 }))
144
145 chains, err = ipt.ListChains(TABLE)
146 Expect(err).NotTo(HaveOccurred())
147 for _, chain := range chains {
148 if chain == testChain.name {
149 Fail("chain was not deleted")
150 }
151 }
152 })
153
154 It("creates chains idempotently", func() {
155 beforeEach()
156 defer cleanup()
157
158 err := testChain.setup(ipt)
159 Expect(err).NotTo(HaveOccurred())
160
161 // Create it again!
162 err = testChain.setup(ipt)
163 Expect(err).NotTo(HaveOccurred())
164
165 // Make sure there are only two rules
166 // (the first rule is an -N because go-iptables
167 rules, err := ipt.List(TABLE, testChain.name)
168 Expect(err).NotTo(HaveOccurred())
169
170 Expect(rules).To(HaveLen(3))
171 })
172
173 It("deletes chains idempotently", func() {
174 beforeEach()
175 defer cleanup()
176
177 err := testChain.setup(ipt)
178 Expect(err).NotTo(HaveOccurred())
179
180 err = testChain.teardown(ipt)
181 Expect(err).NotTo(HaveOccurred())
182
183 chains, err := ipt.ListChains(TABLE)
184 Expect(err).NotTo(HaveOccurred())
185 for _, chain := range chains {
186 if chain == testChain.name {
187 Fail("Chain was not deleted")
188 }
189 }
190
191 err = testChain.teardown(ipt)
192 Expect(err).NotTo(HaveOccurred())
193 chains, err = ipt.ListChains(TABLE)
194 Expect(err).NotTo(HaveOccurred())
195 for _, chain := range chains {
196 if chain == testChain.name {
197 Fail("Chain was not deleted")
198 }
199 }
200 })
201
202 It("deletes chains idempotently in parallel", func() {
203 beforeEach()
204 defer cleanup()
205 // number of parallel executions
206 N := 10
207 var wg sync.WaitGroup
208 err := testChain.setup(ipt)
209 Expect(err).NotTo(HaveOccurred())
210 errCh := make(chan error, N)
211 for i := 0; i < N; i++ {
212 wg.Add(1)
213 go func() {
214 defer wg.Done()
215 // teardown chain
216 errCh <- testNs.Do(func(ns.NetNS) error {
217 return testChain.teardown(ipt)
218 })
219 }()
220 }
221 wg.Wait()
222 close(errCh)
223 for err := range errCh {
224 Expect(err).NotTo(HaveOccurred())
225 }
226
227 chains, err := ipt.ListChains(TABLE)
228 Expect(err).NotTo(HaveOccurred())
229 for _, chain := range chains {
230 if chain == testChain.name {
231 Fail("Chain was not deleted")
232 }
233 }
234 })
235 })