Debian Codegraph @

plugins/ipam/dhcp/dhcp2_test.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 // +build ignore
2
3 // Copyright 2015-2018 CNI authors
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 package main
18
19 import (
20 "fmt"
21 "os"
22 "os/exec"
23 "sync"
24 "time"
25
26 . "github.com/onsi/ginkgo/v2"
27 . "github.com/onsi/gomega"
28 "github.com/vishvananda/netlink"
29
30 "github.com/containernetworking/cni/pkg/skel"
31 current "github.com/containernetworking/cni/pkg/types/100"
32 "github.com/containernetworking/plugins/pkg/netlinksafe"
33 "github.com/containernetworking/plugins/pkg/ns"
34 "github.com/containernetworking/plugins/pkg/testutils"
35 )
36
37 var _ = Describe("DHCP Multiple Lease Operations", func() {
38 var originalNS, targetNS ns.NetNS
39 var dhcpServerStopCh chan bool
40 var dhcpServerDone *sync.WaitGroup
41 var clientCmd *exec.Cmd
42 var socketPath string
43 var tmpDir string
44 var err error
45
46 BeforeEach(func() {
47 dhcpServerStopCh, socketPath, originalNS, targetNS, err = dhcpSetupOriginalNS()
48 Expect(err).NotTo(HaveOccurred())
49
50 // Move the container side to the container's NS
51 err = targetNS.Do(func(_ ns.NetNS) error {
52 defer GinkgoRecover()
53
54 link, err := netlinksafe.LinkByName(contVethName0)
55 Expect(err).NotTo(HaveOccurred())
56 err = netlink.LinkSetUp(link)
57 Expect(err).NotTo(HaveOccurred())
58
59 link1, err := netlinksafe.LinkByName(contVethName1)
60 Expect(err).NotTo(HaveOccurred())
61 err = netlink.LinkSetUp(link1)
62 Expect(err).NotTo(HaveOccurred())
63 return nil
64 })
65
66 // Start the DHCP server
67 dhcpServerDone = dhcpServerStart(originalNS, 2, dhcpServerStopCh)
68
69 // Start the DHCP client daemon
70 dhcpPluginPath, err := exec.LookPath("dhcp")
71 Expect(err).NotTo(HaveOccurred())
72 clientCmd = exec.Command(dhcpPluginPath, "daemon", "-socketpath", socketPath, "--timeout", "2s", "--resendtimeout", "8s")
73 err = clientCmd.Start()
74 Expect(err).NotTo(HaveOccurred())
75 Expect(clientCmd.Process).NotTo(BeNil())
76
77 // Wait up to 15 seconds for the client socket
78 Eventually(func() bool {
79 _, err := os.Stat(socketPath)
80 return err == nil
81 }, time.Second*15, time.Second/4).Should(BeTrue())
82 })
83
84 AfterEach(func() {
85 dhcpServerStopCh <- true
86 dhcpServerDone.Wait()
87 clientCmd.Process.Kill()
88 clientCmd.Wait()
89
90 Expect(originalNS.Close()).To(Succeed())
91 Expect(targetNS.Close()).To(Succeed())
92 defer os.RemoveAll(tmpDir)
93 })
94
95 It("configures multiple links with multiple ADD/DEL", func() {
96 conf := fmt.Sprintf(`{
97 "cniVersion": "0.3.1",
98 "name": "mynet",
99 "type": "bridge",
100 "bridge": "%s",
101 "ipam": {
102 "type": "dhcp",
103 "daemonSocketPath": "%s"
104 }
105 }`, hostBridgeName, socketPath)
106
107 args := &skel.CmdArgs{
108 ContainerID: "dummy",
109 Netns: targetNS.Path(),
110 IfName: contVethName0,
111 StdinData: []byte(conf),
112 }
113
114 var addResult *current.Result
115 err := originalNS.Do(func(ns.NetNS) error {
116 defer GinkgoRecover()
117
118 r, _, err := testutils.CmdAddWithArgs(args, func() error {
119 return cmdAdd(args)
120 })
121 Expect(err).NotTo(HaveOccurred())
122
123 addResult, err = current.GetResult(r)
124 Expect(err).NotTo(HaveOccurred())
125 Expect(addResult.IPs).To(HaveLen(1))
126 Expect(addResult.IPs[0].Address.String()).To(Equal("192.168.1.5/24"))
127 return nil
128 })
129 Expect(err).NotTo(HaveOccurred())
130
131 args = &skel.CmdArgs{
132 ContainerID: "dummy",
133 Netns: targetNS.Path(),
134 IfName: contVethName1,
135 StdinData: []byte(conf),
136 }
137
138 err = originalNS.Do(func(ns.NetNS) error {
139 defer GinkgoRecover()
140
141 r, _, err := testutils.CmdAddWithArgs(args, func() error {
142 return cmdAdd(args)
143 })
144 Expect(err).NotTo(HaveOccurred())
145
146 addResult, err = current.GetResult(r)
147 Expect(err).NotTo(HaveOccurred())
148 Expect(addResult.IPs).To(HaveLen(1))
149 Expect(addResult.IPs[0].Address.String()).To(Equal("192.168.1.6/24"))
150 return nil
151 })
152 Expect(err).NotTo(HaveOccurred())
153
154 args = &skel.CmdArgs{
155 ContainerID: "dummy",
156 Netns: targetNS.Path(),
157 IfName: contVethName1,
158 StdinData: []byte(conf),
159 }
160
161 err = originalNS.Do(func(ns.NetNS) error {
162 return testutils.CmdDelWithArgs(args, func() error {
163 return cmdDel(args)
164 })
165 })
166 Expect(err).NotTo(HaveOccurred())
167
168 args = &skel.CmdArgs{
169 ContainerID: "dummy",
170 Netns: targetNS.Path(),
171 IfName: contVethName0,
172 StdinData: []byte(conf),
173 }
174
175 err = originalNS.Do(func(ns.NetNS) error {
176 return testutils.CmdDelWithArgs(args, func() error {
177 return cmdDel(args)
178 })
179 })
180 Expect(err).NotTo(HaveOccurred())
181 })
182 })