Debian Codegraph @

pkg/types/100/types_test.go

raw
1 // Copyright 2016 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 types100_test
16
17 import (
18 "encoding/json"
19 "io"
20 "net"
21 "os"
22
23 . "github.com/onsi/ginkgo/v2"
24 . "github.com/onsi/gomega"
25
26 "github.com/containernetworking/cni/pkg/types"
27 current "github.com/containernetworking/cni/pkg/types/100"
28 )
29
30 func testResult() *current.Result {
31 ipv4, err := types.ParseCIDR("1.2.3.30/24")
32 Expect(err).NotTo(HaveOccurred())
33 Expect(ipv4).NotTo(BeNil())
34
35 routegwv4, routev4, err := net.ParseCIDR("15.5.6.8/24")
36 Expect(err).NotTo(HaveOccurred())
37 Expect(routev4).NotTo(BeNil())
38 Expect(routegwv4).NotTo(BeNil())
39
40 ipv6, err := types.ParseCIDR("abcd:1234:ffff::cdde/64")
41 Expect(err).NotTo(HaveOccurred())
42 Expect(ipv6).NotTo(BeNil())
43
44 routegwv6, routev6, err := net.ParseCIDR("1111:dddd::aaaa/80")
45 Expect(err).NotTo(HaveOccurred())
46 Expect(routev6).NotTo(BeNil())
47 Expect(routegwv6).NotTo(BeNil())
48 // Set every field of the struct to ensure source compatibility
49 return &current.Result{
50 CNIVersion: current.ImplementedSpecVersion,
51 Interfaces: []*current.Interface{
52 {
53 Name: "eth0",
54 Mac: "00:11:22:33:44:55",
55 Mtu: 1500,
56 Sandbox: "/proc/3553/ns/net",
57 PciID: "8086:9a01",
58 SocketPath: "/path/to/vhost/fd",
59 },
60 },
61 IPs: []*current.IPConfig{
62 {
63 Interface: current.Int(0),
64 Address: *ipv4,
65 Gateway: net.ParseIP("1.2.3.1"),
66 },
67 {
68 Interface: current.Int(0),
69 Address: *ipv6,
70 Gateway: net.ParseIP("abcd:1234:ffff::1"),
71 },
72 },
73 Routes: []*types.Route{
74 {Dst: *routev4, GW: routegwv4},
75 {Dst: *routev6, GW: routegwv6},
76 },
77 DNS: types.DNS{
78 Nameservers: []string{"1.2.3.4", "1::cafe"},
79 Domain: "acompany.com",
80 Search: []string{"somedomain.com", "otherdomain.net"},
81 Options: []string{"foo", "bar"},
82 },
83 }
84 }
85
86 var _ = Describe("Current types operations", func() {
87 It("correctly encodes a 1.1.0 Result", func() {
88 res := testResult()
89
90 // Redirect stdout to capture JSON result
91 oldStdout := os.Stdout
92 r, w, err := os.Pipe()
93 Expect(err).NotTo(HaveOccurred())
94
95 os.Stdout = w
96 err = res.Print()
97 w.Close()
98 Expect(err).NotTo(HaveOccurred())
99
100 // parse the result
101 out, err := io.ReadAll(r)
102 os.Stdout = oldStdout
103 Expect(err).NotTo(HaveOccurred())
104
105 Expect(string(out)).To(MatchJSON(`{
106 "cniVersion": "` + current.ImplementedSpecVersion + `",
107 "interfaces": [
108 {
109 "name": "eth0",
110 "mac": "00:11:22:33:44:55",
111 "mtu": 1500,
112 "sandbox": "/proc/3553/ns/net",
113 "pciID": "8086:9a01",
114 "socketPath": "/path/to/vhost/fd"
115 }
116 ],
117 "ips": [
118 {
119 "interface": 0,
120 "address": "1.2.3.30/24",
121 "gateway": "1.2.3.1"
122 },
123 {
124 "interface": 0,
125 "address": "abcd:1234:ffff::cdde/64",
126 "gateway": "abcd:1234:ffff::1"
127 }
128 ],
129 "routes": [
130 {
131 "dst": "15.5.6.0/24",
132 "gw": "15.5.6.8"
133 },
134 {
135 "dst": "1111:dddd::/80",
136 "gw": "1111:dddd::aaaa"
137 }
138 ],
139 "dns": {
140 "nameservers": [
141 "1.2.3.4",
142 "1::cafe"
143 ],
144 "domain": "acompany.com",
145 "search": [
146 "somedomain.com",
147 "otherdomain.net"
148 ],
149 "options": [
150 "foo",
151 "bar"
152 ]
153 }
154 }`))
155 })
156
157 It("correctly converts a 1.0.0 Result to 1.1.0", func() {
158 tr, err := testResult().GetAsVersion("1.0.0")
159 Expect(err).NotTo(HaveOccurred())
160
161 trv1, ok := tr.(*current.Result)
162 Expect(ok).To(BeTrue())
163
164 // 1.0.0 and 1.1.0 should be the same except for CNI version
165 Expect(trv1.CNIVersion).To(Equal("1.0.0"))
166
167 // If we convert 1.0.0 back to 1.1.0 it should be identical
168 trv11, err := trv1.GetAsVersion("1.1.0")
169 Expect(err).NotTo(HaveOccurred())
170 Expect(trv11).To(Equal(testResult()))
171 })
172
173 It("correctly encodes a 0.1.0 Result", func() {
174 res, err := testResult().GetAsVersion("0.1.0")
175 Expect(err).NotTo(HaveOccurred())
176
177 // Redirect stdout to capture JSON result
178 oldStdout := os.Stdout
179 r, w, err := os.Pipe()
180 Expect(err).NotTo(HaveOccurred())
181
182 os.Stdout = w
183 err = res.Print()
184 w.Close()
185 Expect(err).NotTo(HaveOccurred())
186
187 // parse the result
188 out, err := io.ReadAll(r)
189 os.Stdout = oldStdout
190 Expect(err).NotTo(HaveOccurred())
191
192 Expect(string(out)).To(MatchJSON(`{
193 "cniVersion": "0.1.0",
194 "ip4": {
195 "ip": "1.2.3.30/24",
196 "gateway": "1.2.3.1",
197 "routes": [
198 {
199 "dst": "15.5.6.0/24",
200 "gw": "15.5.6.8"
201 }
202 ]
203 },
204 "ip6": {
205 "ip": "abcd:1234:ffff::cdde/64",
206 "gateway": "abcd:1234:ffff::1",
207 "routes": [
208 {
209 "dst": "1111:dddd::/80",
210 "gw": "1111:dddd::aaaa"
211 }
212 ]
213 },
214 "dns": {
215 "nameservers": [
216 "1.2.3.4",
217 "1::cafe"
218 ],
219 "domain": "acompany.com",
220 "search": [
221 "somedomain.com",
222 "otherdomain.net"
223 ],
224 "options": [
225 "foo",
226 "bar"
227 ]
228 }
229 }`))
230 })
231
232 It("correctly encodes a 0.4.0 Result", func() {
233 res, err := testResult().GetAsVersion("0.4.0")
234 Expect(err).NotTo(HaveOccurred())
235
236 // Redirect stdout to capture JSON result
237 oldStdout := os.Stdout
238 r, w, err := os.Pipe()
239 Expect(err).NotTo(HaveOccurred())
240
241 os.Stdout = w
242 err = res.Print()
243 w.Close()
244 Expect(err).NotTo(HaveOccurred())
245
246 // parse the result
247 out, err := io.ReadAll(r)
248 os.Stdout = oldStdout
249 Expect(err).NotTo(HaveOccurred())
250
251 Expect(string(out)).To(MatchJSON(`{
252 "cniVersion": "0.4.0",
253 "interfaces": [
254 {
255 "name": "eth0",
256 "mac": "00:11:22:33:44:55",
257 "sandbox": "/proc/3553/ns/net"
258 }
259 ],
260 "ips": [
261 {
262 "interface": 0,
263 "version": "4",
264 "address": "1.2.3.30/24",
265 "gateway": "1.2.3.1"
266 },
267 {
268 "interface": 0,
269 "version": "6",
270 "address": "abcd:1234:ffff::cdde/64",
271 "gateway": "abcd:1234:ffff::1"
272 }
273 ],
274 "routes": [
275 {
276 "dst": "15.5.6.0/24",
277 "gw": "15.5.6.8"
278 },
279 {
280 "dst": "1111:dddd::/80",
281 "gw": "1111:dddd::aaaa"
282 }
283 ],
284 "dns": {
285 "nameservers": [
286 "1.2.3.4",
287 "1::cafe"
288 ],
289 "domain": "acompany.com",
290 "search": [
291 "somedomain.com",
292 "otherdomain.net"
293 ],
294 "options": [
295 "foo",
296 "bar"
297 ]
298 }
299 }`))
300 })
301
302 It("correctly marshals and unmarshals interface index 0", func() {
303 ipc := &current.IPConfig{
304 Interface: current.Int(0),
305 Address: net.IPNet{
306 IP: net.ParseIP("10.1.2.3"),
307 Mask: net.IPv4Mask(255, 255, 255, 0),
308 },
309 }
310
311 jsonBytes, err := json.Marshal(ipc)
312 Expect(err).NotTo(HaveOccurred())
313 Expect(jsonBytes).To(MatchJSON(`{
314 "interface": 0,
315 "address": "10.1.2.3/24"
316 }`))
317
318 recovered := &current.IPConfig{}
319 Expect(json.Unmarshal(jsonBytes, recovered)).To(Succeed())
320 Expect(recovered).To(Equal(ipc))
321 })
322
323 Context("when unmarshalling json fails", func() {
324 It("returns an error", func() {
325 recovered := &current.IPConfig{}
326 err := json.Unmarshal([]byte(`{"address": 5}`), recovered)
327 Expect(err).To(MatchError(HavePrefix("json: cannot unmarshal")))
328 })
329 })
330
331 It("correctly marshals a missing interface index", func() {
332 ipc := &current.IPConfig{
333 Address: net.IPNet{
334 IP: net.ParseIP("10.1.2.3"),
335 Mask: net.IPv4Mask(255, 255, 255, 0),
336 },
337 }
338
339 json, err := json.Marshal(ipc)
340 Expect(err).NotTo(HaveOccurred())
341 Expect(json).To(MatchJSON(`{
342 "address": "10.1.2.3/24"
343 }`))
344 })
345 })