Debian Codegraph @

pkg/types/types_test.go

raw
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 types_test
16
17 import (
18 "encoding/json"
19 "net"
20
21 . "github.com/onsi/ginkgo/v2"
22 . "github.com/onsi/gomega"
23
24 "github.com/containernetworking/cni/pkg/types"
25 types040 "github.com/containernetworking/cni/pkg/types/040"
26 current "github.com/containernetworking/cni/pkg/types/100"
27 )
28
29 var _ = Describe("Types", func() {
30 Describe("ParseCIDR", func() {
31 DescribeTable("Parse and stringify",
32 func(input, expectedIP string, expectedMask int) {
33 ipn, err := types.ParseCIDR(input)
34 Expect(err).NotTo(HaveOccurred())
35 Expect(ipn.String()).To(Equal(input))
36
37 Expect(ipn.IP.String()).To(Equal(expectedIP))
38 ones, _ := ipn.Mask.Size()
39 Expect(ones).To(Equal(expectedMask))
40 },
41 Entry("ipv4", "1.2.3.4/24", "1.2.3.4", 24),
42 Entry("ipv6", "2001:db8::/32", "2001:db8::", 32),
43 )
44 It("returns an error when given invalid inputs", func() {
45 ipn, err := types.ParseCIDR("1.2.3/45")
46 Expect(ipn).To(BeNil())
47 Expect(err).To(MatchError("invalid CIDR address: 1.2.3/45"))
48 })
49 })
50
51 Describe("custom IPNet type", func() {
52 It("marshals and unmarshals to JSON as a string", func() {
53 ipn := types.IPNet{
54 IP: net.ParseIP("1.2.3.4"),
55 Mask: net.CIDRMask(24, 32),
56 }
57 jsonBytes, err := json.Marshal(ipn)
58 Expect(err).NotTo(HaveOccurred())
59 Expect(jsonBytes).To(MatchJSON(`"1.2.3.4/24"`))
60
61 var unmarshaled types.IPNet
62 Expect(json.Unmarshal(jsonBytes, &unmarshaled)).To(Succeed())
63 Expect(unmarshaled).To(Equal(ipn))
64 })
65
66 Context("when the json data is not syntactically valid", func() {
67 Specify("UnmarshalJSON returns an error", func() {
68 ipn := new(types.IPNet)
69 err := ipn.UnmarshalJSON([]byte("1"))
70 Expect(err).To(MatchError("json: cannot unmarshal number into Go value of type string"))
71 })
72 })
73
74 Context("when the json data is not semantically valid", func() {
75 Specify("UnmarshalJSON returns an error", func() {
76 ipn := new(types.IPNet)
77 err := ipn.UnmarshalJSON([]byte(`"1.2.3.4/99"`))
78 Expect(err).To(MatchError("invalid CIDR address: 1.2.3.4/99"))
79 })
80 })
81 })
82
83 Describe("custom Route type", func() {
84 var example types.Route
85 BeforeEach(func() {
86 example = types.Route{
87 Dst: net.IPNet{
88 IP: net.ParseIP("1.2.3.0"),
89 Mask: net.CIDRMask(24, 32),
90 },
91 GW: net.ParseIP("1.2.3.1"),
92 MTU: 1500,
93 AdvMSS: 1340,
94 Priority: 100,
95 Table: types040.Int(50),
96 Scope: types040.Int(253),
97 }
98 })
99
100 It("marshals and unmarshals to JSON", func() {
101 jsonBytes, err := json.Marshal(example)
102 Expect(err).NotTo(HaveOccurred())
103 Expect(jsonBytes).To(MatchJSON(`{ "dst": "1.2.3.0/24", "gw": "1.2.3.1", "mtu": 1500, "advmss": 1340, "priority": 100, "table": 50, "scope": 253 }`))
104
105 var unmarshaled types.Route
106 Expect(json.Unmarshal(jsonBytes, &unmarshaled)).To(Succeed())
107 Expect(unmarshaled).To(Equal(example))
108 })
109
110 Context("when the json data is not valid", func() {
111 Specify("UnmarshalJSON returns an error", func() {
112 route := new(types.Route)
113 err := route.UnmarshalJSON([]byte(`{ "dst": "1.2.3.0/24", "gw": "1.2.3.x" }`))
114 Expect(err).To(MatchError("invalid IP address: 1.2.3.x"))
115 })
116 })
117
118 It("formats as a string with a hex mask", func() {
119 Expect(example.String()).To(Equal(`{Dst:{IP:1.2.3.0 Mask:ffffff00} GW:1.2.3.1 MTU:1500 AdvMSS:1340 Priority:100 Table:50 Scope:253}`))
120 })
121 })
122
123 Describe("Error type", func() {
124 var example *types.Error
125 BeforeEach(func() {
126 example = &types.Error{
127 Code: 1234,
128 Msg: "some message",
129 Details: "some details",
130 }
131 })
132
133 Describe("Error() method (basic string)", func() {
134 It("returns a formatted string", func() {
135 Expect(example.Error()).To(Equal("some message; some details"))
136 })
137 Context("when details are not present", func() {
138 BeforeEach(func() {
139 example.Details = ""
140 })
141 It("returns only the message", func() {
142 Expect(example.Error()).To(Equal("some message"))
143 })
144 })
145 })
146
147 It("NewError method", func() {
148 err := types.NewError(1234, "some message", "some details")
149 Expect(err).To(Equal(example))
150 })
151 })
152
153 Describe("Result conversion", func() {
154 var result *current.Result
155 BeforeEach(func() {
156 ipv4, err := types.ParseCIDR("1.2.3.30/24")
157 Expect(err).NotTo(HaveOccurred())
158 Expect(ipv4).NotTo(BeNil())
159
160 ipv6, err := types.ParseCIDR("abcd:1234:ffff::cdde/64")
161 Expect(err).NotTo(HaveOccurred())
162 Expect(ipv6).NotTo(BeNil())
163 result = &current.Result{
164 CNIVersion: "1.0.0",
165 Interfaces: []*current.Interface{
166 {
167 Name: "eth0",
168 Mac: "00:11:22:33:44:55",
169 Sandbox: "/proc/3553/ns/net",
170 PciID: "8086:9a01",
171 SocketPath: "/path/to/vhost/fd",
172 },
173 },
174 IPs: []*current.IPConfig{
175 {
176 Interface: current.Int(0),
177 Address: *ipv4,
178 Gateway: net.ParseIP("1.2.3.1"),
179 },
180 {
181 Interface: current.Int(0),
182 Address: *ipv6,
183 Gateway: net.ParseIP("abcd:1234:ffff::1"),
184 },
185 },
186 DNS: types.DNS{
187 Nameservers: []string{"1.2.3.4", "1::cafe"},
188 Domain: "acompany.com",
189 Search: []string{"somedomain.com", "otherdomain.net"},
190 Options: []string{"foo", "bar"},
191 },
192 }
193 })
194
195 It("can create a CNIVersion '' (0.1.0) result", func() {
196 newResult, err := result.GetAsVersion("")
197 Expect(err).NotTo(HaveOccurred())
198 Expect(newResult.Version()).To(Equal("0.1.0"))
199 })
200 })
201 })