Debian Codegraph @

plugins/meta/portmap/portmap_test.go

raw symbols SCIP
1 // Copyright 2017 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
20 . "github.com/onsi/ginkgo/v2"
21 . "github.com/onsi/gomega"
22
23 "github.com/containernetworking/cni/pkg/types"
24 )
25
26 var _ = Describe("portmapping configuration", func() {
27 for _, ver := range []string{"0.3.0", "0.3.1", "0.4.0", "1.0.0"} {
28 // Redefine ver inside for scope so real value is picked up by each dynamically defined It()
29 // See Gingkgo's "Patterns for dynamically generating tests" documentation.
30 ver := ver
31
32 Context("config parsing", func() {
33 It(fmt.Sprintf("[%s] correctly parses an ADD config", ver), func() {
34 configBytes := []byte(fmt.Sprintf(`{
35 "name": "test",
36 "type": "portmap",
37 "cniVersion": "%s",
38 "backend": "iptables",
39 "runtimeConfig": {
40 "portMappings": [
41 { "hostPort": 8080, "containerPort": 80, "protocol": "tcp"},
42 { "hostPort": 8081, "containerPort": 81, "protocol": "udp"}
43 ]
44 },
45 "snat": false,
46 "conditionsV4": ["-s", "1.2.3.4"],
47 "conditionsV6": ["!", "-s", "12::34"],
48 "prevResult": {
49 "interfaces": [
50 {"name": "host"},
51 {"name": "container", "sandbox":"netns"}
52 ],
53 "ips": [
54 {
55 "version": "4",
56 "address": "10.0.0.1/24",
57 "gateway": "10.0.0.1",
58 "interface": 0
59 },
60 {
61 "version": "6",
62 "address": "2001:db8:1::2/64",
63 "gateway": "2001:db8:1::1",
64 "interface": 1
65 },
66 {
67 "version": "4",
68 "address": "10.0.0.2/24",
69 "gateway": "10.0.0.1",
70 "interface": 1
71 }
72 ]
73 }
74 }`, ver))
75 c, _, err := parseConfig(configBytes, "container")
76 Expect(err).NotTo(HaveOccurred())
77 Expect(c.CNIVersion).To(Equal(ver))
78 Expect(c.ConditionsV4).To(Equal(&[]string{"-s", "1.2.3.4"}))
79 Expect(c.ConditionsV6).To(Equal(&[]string{"!", "-s", "12::34"}))
80 fvar := false
81 Expect(c.SNAT).To(Equal(&fvar))
82 Expect(c.Name).To(Equal("test"))
83
84 n, err := types.ParseCIDR("10.0.0.2/24")
85 Expect(err).NotTo(HaveOccurred())
86 Expect(c.ContIPv4).To(Equal(*n))
87 n, err = types.ParseCIDR("2001:db8:1::2/64")
88 Expect(err).NotTo(HaveOccurred())
89 Expect(c.ContIPv6).To(Equal(*n))
90 })
91
92 It(fmt.Sprintf("[%s] correctly parses a DEL config", ver), func() {
93 // When called with DEL, neither runtimeConfig nor prevResult may be specified
94 configBytes := []byte(fmt.Sprintf(`{
95 "name": "test",
96 "type": "portmap",
97 "cniVersion": "%s",
98 "backend": "iptables",
99 "snat": false,
100 "conditionsV4": ["-s", "1.2.3.4"],
101 "conditionsV6": ["-s", "12::34"]
102 }`, ver))
103 c, _, err := parseConfig(configBytes, "container")
104 Expect(err).NotTo(HaveOccurred())
105 Expect(c.CNIVersion).To(Equal(ver))
106 Expect(c.ConditionsV4).To(Equal(&[]string{"-s", "1.2.3.4"}))
107 Expect(c.ConditionsV6).To(Equal(&[]string{"-s", "12::34"}))
108 fvar := false
109 Expect(c.SNAT).To(Equal(&fvar))
110 Expect(c.Name).To(Equal("test"))
111 })
112
113 It(fmt.Sprintf("[%s] fails with invalid mappings", ver), func() {
114 configBytes := []byte(fmt.Sprintf(`{
115 "name": "test",
116 "type": "portmap",
117 "cniVersion": "%s",
118 "backend": "iptables",
119 "snat": false,
120 "conditionsV4": ["-s", "1.2.3.4"],
121 "conditionsV6": ["-s", "12::34"],
122 "runtimeConfig": {
123 "portMappings": [
124 { "hostPort": 0, "containerPort": 80, "protocol": "tcp"}
125 ]
126 }
127 }`, ver))
128 _, _, err := parseConfig(configBytes, "container")
129 Expect(err).To(MatchError("Invalid host port number: 0"))
130 })
131
132 It(fmt.Sprintf("[%s] defaults to iptables when backend is not specified", ver), func() {
133 // "defaults to iptables" is only true if iptables is installed
134 // (or if neither iptables nor nftables is installed), but the
135 // other unit tests would fail if iptables wasn't installed, so
136 // we know it must be.
137 configBytes := []byte(fmt.Sprintf(`{
138 "name": "test",
139 "type": "portmap",
140 "cniVersion": "%s"
141 }`, ver))
142 c, _, err := parseConfig(configBytes, "container")
143 Expect(err).NotTo(HaveOccurred())
144 Expect(c.CNIVersion).To(Equal(ver))
145 Expect(c.Backend).To(Equal(&iptablesBackend))
146 Expect(c.Name).To(Equal("test"))
147 })
148
149 It(fmt.Sprintf("[%s] uses nftables if requested", ver), func() {
150 configBytes := []byte(fmt.Sprintf(`{
151 "name": "test",
152 "type": "portmap",
153 "cniVersion": "%s",
154 "backend": "nftables"
155 }`, ver))
156 c, _, err := parseConfig(configBytes, "container")
157 Expect(err).NotTo(HaveOccurred())
158 Expect(c.CNIVersion).To(Equal(ver))
159 Expect(c.Backend).To(Equal(&nftablesBackend))
160 Expect(c.Name).To(Equal("test"))
161 })
162
163 It(fmt.Sprintf("[%s] allows nftables conditions if nftables is requested", ver), func() {
164 configBytes := []byte(fmt.Sprintf(`{
165 "name": "test",
166 "type": "portmap",
167 "cniVersion": "%s",
168 "backend": "nftables",
169 "conditionsV4": ["ip", "saddr", "1.2.3.4"],
170 "conditionsV6": ["ip6", "saddr", "12::34"]
171 }`, ver))
172 c, _, err := parseConfig(configBytes, "container")
173 Expect(err).NotTo(HaveOccurred())
174 Expect(c.CNIVersion).To(Equal(ver))
175 Expect(c.Backend).To(Equal(&nftablesBackend))
176 Expect(c.ConditionsV4).To(Equal(&[]string{"ip", "saddr", "1.2.3.4"}))
177 Expect(c.ConditionsV6).To(Equal(&[]string{"ip6", "saddr", "12::34"}))
178 Expect(c.Name).To(Equal("test"))
179 })
180
181 It(fmt.Sprintf("[%s] rejects nftables options with 'backend: iptables'", ver), func() {
182 configBytes := []byte(fmt.Sprintf(`{
183 "name": "test",
184 "type": "portmap",
185 "cniVersion": "%s",
186 "backend": "iptables",
187 "conditionsV4": ["ip", "saddr", "1.2.3.4"],
188 "conditionsV6": ["ip6", "saddr", "12::34"]
189 }`, ver))
190 _, _, err := parseConfig(configBytes, "container")
191 Expect(err).To(MatchError("iptables backend was requested but configuration contains nftables-specific options [conditionsV4 conditionsV6]"))
192 })
193
194 It(fmt.Sprintf("[%s] rejects iptables options with 'backend: nftables'", ver), func() {
195 configBytes := []byte(fmt.Sprintf(`{
196 "name": "test",
197 "type": "portmap",
198 "cniVersion": "%s",
199 "backend": "nftables",
200 "externalSetMarkChain": "KUBE-MARK-MASQ",
201 "conditionsV4": ["-s", "1.2.3.4"],
202 "conditionsV6": ["-s", "12::34"]
203 }`, ver))
204 _, _, err := parseConfig(configBytes, "container")
205 Expect(err).To(MatchError("nftables backend was requested but configuration contains iptables-specific options [externalSetMarkChain conditionsV4 conditionsV6]"))
206 })
207
208 It(fmt.Sprintf("[%s] does not fail on missing prevResult interface index", ver), func() {
209 configBytes := []byte(fmt.Sprintf(`{
210 "name": "test",
211 "type": "portmap",
212 "cniVersion": "%s",
213 "runtimeConfig": {
214 "portMappings": [
215 { "hostPort": 8080, "containerPort": 80, "protocol": "tcp"}
216 ]
217 },
218 "conditionsV4": ["-s", "1.2.3.4"],
219 "prevResult": {
220 "interfaces": [
221 {"name": "host"}
222 ],
223 "ips": [
224 {
225 "version": "4",
226 "address": "10.0.0.1/24",
227 "gateway": "10.0.0.1"
228 }
229 ]
230 }
231 }`, ver))
232 _, _, err := parseConfig(configBytes, "container")
233 Expect(err).NotTo(HaveOccurred())
234 })
235 })
236 }
237 })