| 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 utils |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "math/rand" |
| 20 | "runtime" |
| 21 | |
| 22 | "github.com/coreos/go-iptables/iptables" |
| 23 | . "github.com/onsi/ginkgo/v2" |
| 24 | . "github.com/onsi/gomega" |
| 25 | |
| 26 | "github.com/containernetworking/plugins/pkg/ns" |
| 27 | "github.com/containernetworking/plugins/pkg/testutils" |
| 28 | ) |
| 29 | |
| 30 | const TABLE = "filter" // We'll monkey around here |
| 31 | |
| 32 | var _ = Describe("chain tests", func() { |
| 33 | var testChain string |
| 34 | var ipt *iptables.IPTables |
| 35 | var cleanup func() |
| 36 | |
| 37 | BeforeEach(func() { |
| 38 | // Save a reference to the original namespace, |
| 39 | // Add a new NS |
| 40 | currNs, err := ns.GetCurrentNS() |
| 41 | Expect(err).NotTo(HaveOccurred()) |
| 42 | |
| 43 | testNs, err := testutils.NewNS() |
| 44 | Expect(err).NotTo(HaveOccurred()) |
| 45 | |
| 46 | testChain = fmt.Sprintf("cni-test-%d", rand.Intn(10000000)) |
| 47 | |
| 48 | ipt, err = iptables.NewWithProtocol(iptables.ProtocolIPv4) |
| 49 | Expect(err).NotTo(HaveOccurred()) |
| 50 | |
| 51 | runtime.LockOSThread() |
| 52 | err = testNs.Set() |
| 53 | Expect(err).NotTo(HaveOccurred()) |
| 54 | |
| 55 | cleanup = func() { |
| 56 | if ipt == nil { |
| 57 | return |
| 58 | } |
| 59 | ipt.ClearChain(TABLE, testChain) |
| 60 | ipt.DeleteChain(TABLE, testChain) |
| 61 | currNs.Set() |
| 62 | } |
| 63 | }) |
| 64 | |
| 65 | AfterEach(func() { |
| 66 | cleanup() |
| 67 | }) |
| 68 | |
| 69 | Describe("EnsureChain", func() { |
| 70 | It("creates chains idempotently", func() { |
| 71 | err := EnsureChain(ipt, TABLE, testChain) |
| 72 | Expect(err).NotTo(HaveOccurred()) |
| 73 | |
| 74 | // Create it again! |
| 75 | err = EnsureChain(ipt, TABLE, testChain) |
| 76 | Expect(err).NotTo(HaveOccurred()) |
| 77 | }) |
| 78 | }) |
| 79 | |
| 80 | Describe("DeleteChain", func() { |
| 81 | It("delete chains idempotently", func() { |
| 82 | // Create chain |
| 83 | err := EnsureChain(ipt, TABLE, testChain) |
| 84 | Expect(err).NotTo(HaveOccurred()) |
| 85 | |
| 86 | // Delete chain |
| 87 | err = DeleteChain(ipt, TABLE, testChain) |
| 88 | Expect(err).NotTo(HaveOccurred()) |
| 89 | |
| 90 | // Delete it again! |
| 91 | err = DeleteChain(ipt, TABLE, testChain) |
| 92 | Expect(err).NotTo(HaveOccurred()) |
| 93 | }) |
| 94 | }) |
| 95 | }) |