Debian Codegraph @

pkg/ip/link_linux_test.go

raw symbols SCIP
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 ip_test
16
17 import (
18 "bytes"
19 "crypto/rand"
20 "fmt"
21 "net"
22
23 . "github.com/onsi/ginkgo/v2"
24 . "github.com/onsi/gomega"
25
26 "github.com/containernetworking/plugins/pkg/ip"
27 "github.com/containernetworking/plugins/pkg/netlinksafe"
28 "github.com/containernetworking/plugins/pkg/ns"
29 "github.com/containernetworking/plugins/pkg/testutils"
30 )
31
32 var _ = Describe("Link", func() {
33 const (
34 ifaceFormatString string = "i%d"
35 mtu int = 1400
36 ip4onehwaddr = "0a:58:01:01:01:01"
37 )
38 var (
39 hostNetNS ns.NetNS
40 containerNetNS ns.NetNS
41 ifaceCounter = 0
42 hostVeth net.Interface
43 containerVeth net.Interface
44 hostVethName string
45 containerVethName string
46
47 originalRandReader = rand.Reader
48 )
49
50 BeforeEach(func() {
51 var err error
52
53 hostNetNS, err = testutils.NewNS()
54 Expect(err).NotTo(HaveOccurred())
55
56 containerNetNS, err = testutils.NewNS()
57 Expect(err).NotTo(HaveOccurred())
58
59 fakeBytes := make([]byte, 20)
60 // to be reset in AfterEach block
61 rand.Reader = bytes.NewReader(fakeBytes)
62
63 _ = containerNetNS.Do(func(ns.NetNS) error {
64 defer GinkgoRecover()
65
66 hostVeth, containerVeth, err = ip.SetupVeth(fmt.Sprintf(ifaceFormatString, ifaceCounter), mtu, "", hostNetNS)
67 if err != nil {
68 return err
69 }
70 Expect(err).NotTo(HaveOccurred())
71
72 hostVethName = hostVeth.Name
73 containerVethName = containerVeth.Name
74
75 return nil
76 })
77 })
78
79 AfterEach(func() {
80 Expect(containerNetNS.Close()).To(Succeed())
81 Expect(hostNetNS.Close()).To(Succeed())
82 ifaceCounter++
83 rand.Reader = originalRandReader
84 })
85
86 Describe("GetVethPeerIfindex", func() {
87 It("returns the link and peer index of the named interface", func() {
88 By("looking up the container veth index using the host veth name")
89 _ = hostNetNS.Do(func(ns.NetNS) error {
90 defer GinkgoRecover()
91
92 gotHostLink, gotContainerIndex, err := ip.GetVethPeerIfindex(hostVethName)
93 Expect(err).NotTo(HaveOccurred())
94
95 By("checking we got back the host link")
96 attrs := gotHostLink.Attrs()
97 Expect(attrs.Index).To(Equal(hostVeth.Index))
98 Expect(attrs.Name).To(Equal(hostVeth.Name))
99
100 By("checking we got back the container veth index")
101 Expect(gotContainerIndex).To(Equal(containerVeth.Index))
102
103 return nil
104 })
105
106 By("looking up the host veth index using the container veth name")
107 _ = containerNetNS.Do(func(ns.NetNS) error {
108 defer GinkgoRecover()
109
110 gotContainerLink, gotHostIndex, err := ip.GetVethPeerIfindex(containerVethName)
111 Expect(err).NotTo(HaveOccurred())
112
113 By("checking we got back the container link")
114 attrs := gotContainerLink.Attrs()
115 Expect(attrs.Index).To(Equal(containerVeth.Index))
116 Expect(attrs.Name).To(Equal(containerVeth.Name))
117
118 By("checking we got back the host veth index")
119 Expect(gotHostIndex).To(Equal(hostVeth.Index))
120
121 return nil
122 })
123 })
124 })
125
126 It("SetupVeth must put the veth endpoints into the separate namespaces", func() {
127 _ = containerNetNS.Do(func(ns.NetNS) error {
128 defer GinkgoRecover()
129
130 containerVethFromName, err := netlinksafe.LinkByName(containerVethName)
131 Expect(err).NotTo(HaveOccurred())
132 Expect(containerVethFromName.Attrs().Index).To(Equal(containerVeth.Index))
133
134 return nil
135 })
136
137 _ = hostNetNS.Do(func(ns.NetNS) error {
138 defer GinkgoRecover()
139
140 hostVethFromName, err := netlinksafe.LinkByName(hostVethName)
141 Expect(err).NotTo(HaveOccurred())
142 Expect(hostVethFromName.Attrs().Index).To(Equal(hostVeth.Index))
143
144 return nil
145 })
146 })
147
148 Context("when container already has an interface with the same name", func() {
149 It("returns useful error", func() {
150 _ = containerNetNS.Do(func(ns.NetNS) error {
151 defer GinkgoRecover()
152 testHostVethName := "t" + hostVethName
153 _, _, err := ip.SetupVethWithName(containerVethName, testHostVethName, mtu, "", hostNetNS)
154 Expect(err.Error()).To(Equal(fmt.Sprintf("container veth name (%q) peer provided (%q) already exists", containerVethName, testHostVethName)))
155
156 return nil
157 })
158 })
159 })
160
161 Context("deleting an non-existent device", func() {
162 It("returns known error", func() {
163 _ = containerNetNS.Do(func(ns.NetNS) error {
164 defer GinkgoRecover()
165
166 // This string should match the expected error codes in the cmdDel functions of some of the plugins
167 _, err := ip.DelLinkByNameAddr("THIS_DONT_EXIST")
168 Expect(err).To(Equal(ip.ErrLinkNotFound))
169
170 return nil
171 })
172 })
173 })
174
175 Context("when there is no name available for the host-side", func() {
176 BeforeEach(func() {
177 // adding different interface to container ns
178 containerVethName += "0"
179 })
180 It("returns useful error", func() {
181 _ = containerNetNS.Do(func(ns.NetNS) error {
182 defer GinkgoRecover()
183 _, _, err := ip.SetupVethWithName(containerVethName, hostVethName, mtu, "", hostNetNS)
184 Expect(err.Error()).To(Equal(fmt.Sprintf("container veth name (%q) peer provided (%q) already exists", containerVethName, hostVethName)))
185 return nil
186 })
187 })
188 })
189
190 Context("when there is no name conflict for the host or container interfaces", func() {
191 BeforeEach(func() {
192 // adding different interface to container and host ns
193 containerVethName += "0"
194 rand.Reader = originalRandReader
195 })
196 It("successfully creates the second veth pair", func() {
197 _ = containerNetNS.Do(func(ns.NetNS) error {
198 defer GinkgoRecover()
199
200 hostVeth, _, err := ip.SetupVeth(containerVethName, mtu, "", hostNetNS)
201 Expect(err).NotTo(HaveOccurred())
202 hostVethName = hostVeth.Name
203 return nil
204 })
205
206 // verify veths are in different namespaces
207 _ = containerNetNS.Do(func(ns.NetNS) error {
208 defer GinkgoRecover()
209
210 _, err := netlinksafe.LinkByName(containerVethName)
211 Expect(err).NotTo(HaveOccurred())
212
213 return nil
214 })
215
216 _ = hostNetNS.Do(func(ns.NetNS) error {
217 defer GinkgoRecover()
218
219 _, err := netlinksafe.LinkByName(hostVethName)
220 Expect(err).NotTo(HaveOccurred())
221
222 return nil
223 })
224 })
225
226 It("successfully creates a veth pair with an explicit mac", func() {
227 const mac = "02:00:00:00:01:23"
228 _ = containerNetNS.Do(func(ns.NetNS) error {
229 defer GinkgoRecover()
230
231 hostVeth, _, err := ip.SetupVeth(containerVethName, mtu, mac, hostNetNS)
232 Expect(err).NotTo(HaveOccurred())
233 hostVethName = hostVeth.Name
234
235 link, err := netlinksafe.LinkByName(containerVethName)
236 Expect(err).NotTo(HaveOccurred())
237 Expect(link.Attrs().HardwareAddr.String()).To(Equal(mac))
238
239 return nil
240 })
241
242 _ = hostNetNS.Do(func(ns.NetNS) error {
243 defer GinkgoRecover()
244
245 link, err := netlinksafe.LinkByName(hostVethName)
246 Expect(err).NotTo(HaveOccurred())
247 Expect(link.Attrs().HardwareAddr.String()).NotTo(Equal(mac))
248
249 return nil
250 })
251 })
252 })
253
254 It("DelLinkByName must delete the veth endpoints", func() {
255 _ = containerNetNS.Do(func(ns.NetNS) error {
256 defer GinkgoRecover()
257
258 // this will delete the host endpoint too
259 err := ip.DelLinkByName(containerVethName)
260 Expect(err).NotTo(HaveOccurred())
261
262 _, err = netlinksafe.LinkByName(containerVethName)
263 Expect(err).To(HaveOccurred())
264
265 return nil
266 })
267
268 _ = hostNetNS.Do(func(ns.NetNS) error {
269 defer GinkgoRecover()
270
271 _, err := netlinksafe.LinkByName(hostVethName)
272 Expect(err).To(HaveOccurred())
273
274 return nil
275 })
276 })
277
278 It("DelLinkByNameAddr should return no IPs when no IPs are configured", func() {
279 _ = containerNetNS.Do(func(ns.NetNS) error {
280 defer GinkgoRecover()
281
282 // this will delete the host endpoint too
283 addr, err := ip.DelLinkByNameAddr(containerVethName)
284 Expect(err).NotTo(HaveOccurred())
285 Expect(addr).To(BeEmpty())
286 return nil
287 })
288 })
289 })