Debian Codegraph @

plugins/ipam/dhcp/dhcp2_test.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Copyright 2015-2018 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 "os"
20 "os/exec"
21 "sync"
22 "time"
23
24 . "github.com/onsi/ginkgo/v2"
25 . "github.com/onsi/gomega"
26 "github.com/vishvananda/netlink"
27
28 "github.com/containernetworking/cni/pkg/skel"
29 current "github.com/containernetworking/cni/pkg/types/100"
30 "github.com/containernetworking/plugins/pkg/netlinksafe"
31 "github.com/containernetworking/plugins/pkg/ns"
32 "github.com/containernetworking/plugins/pkg/testutils"
33 )
34
35 var _ = Describe("DHCP Multiple Lease Operations", func() {
36 var originalNS, targetNS ns.NetNS
37 var dhcpServerStopCh chan bool
38 var dhcpServerDone *sync.WaitGroup
39 var clientCmd *exec.Cmd
40 var socketPath string
41 var tmpDir string
42 var err error
43
44 BeforeEach(func() {
45 dhcpServerStopCh, socketPath, originalNS, targetNS, err = dhcpSetupOriginalNS()
46 Expect(err).NotTo(HaveOccurred())
47
48 // Move the container side to the container's NS
49 err = targetNS.Do(func(_ ns.NetNS) error {
50 defer GinkgoRecover()
51
52 link, err := netlinksafe.LinkByName(contVethName0)
53 Expect(err).NotTo(HaveOccurred())
54 err = netlink.LinkSetUp(link)
55 Expect(err).NotTo(HaveOccurred())
56
57 link1, err := netlinksafe.LinkByName(contVethName1)
58 Expect(err).NotTo(HaveOccurred())
59 err = netlink.LinkSetUp(link1)
60 Expect(err).NotTo(HaveOccurred())
61 return nil
62 })
63
64 // Start the DHCP server
65 dhcpServerDone = dhcpServerStart(originalNS, 2, dhcpServerStopCh)
66
67 // Start the DHCP client daemon
68 dhcpPluginPath, err := exec.LookPath("dhcp")
69 Expect(err).NotTo(HaveOccurred())
70 clientCmd = exec.Command(dhcpPluginPath, "daemon", "-socketpath", socketPath, "--timeout", "2s", "--resendtimeout", "8s")
71 err = clientCmd.Start()
72 Expect(err).NotTo(HaveOccurred())
73 Expect(clientCmd.Process).NotTo(BeNil())
74
75 // Wait up to 15 seconds for the client socket
76 Eventually(func() bool {
77 _, err := os.Stat(socketPath)
78 return err == nil
79 }, time.Second*15, time.Second/4).Should(BeTrue())
80 })
81
82 AfterEach(func() {
83 dhcpServerStopCh <- true
84 dhcpServerDone.Wait()
85 clientCmd.Process.Kill()
86 clientCmd.Wait()
87
88 Expect(originalNS.Close()).To(Succeed())
89 Expect(targetNS.Close()).To(Succeed())
90 defer os.RemoveAll(tmpDir)
91 })
92
93 It("configures multiple links with multiple ADD/DEL", func() {
94 conf := fmt.Sprintf(`{
95 "cniVersion": "0.3.1",
96 "name": "mynet",
97 "type": "bridge",
98 "bridge": "%s",
99 "ipam": {
100 "type": "dhcp",
101 "daemonSocketPath": "%s"
102 }
103 }`, hostBridgeName, socketPath)
104
105 args := &skel.CmdArgs{
106 ContainerID: "dummy",
107 Netns: targetNS.Path(),
108 IfName: contVethName0,
109 StdinData: []byte(conf),
110 }
111
112 var addResult *current.Result
113 err := originalNS.Do(func(ns.NetNS) error {
114 defer GinkgoRecover()
115
116 r, _, err := testutils.CmdAddWithArgs(args, func() error {
117 return cmdAdd(args)
118 })
119 Expect(err).NotTo(HaveOccurred())
120
121 addResult, err = current.GetResult(r)
122 Expect(err).NotTo(HaveOccurred())
123 Expect(addResult.IPs).To(HaveLen(1))
124 Expect(addResult.IPs[0].Address.String()).To(Equal("192.168.1.5/24"))
125 return nil
126 })
127 Expect(err).NotTo(HaveOccurred())
128
129 args = &skel.CmdArgs{
130 ContainerID: "dummy",
131 Netns: targetNS.Path(),
132 IfName: contVethName1,
133 StdinData: []byte(conf),
134 }
135
136 err = originalNS.Do(func(ns.NetNS) error {
137 defer GinkgoRecover()
138
139 r, _, err := testutils.CmdAddWithArgs(args, func() error {
140 return cmdAdd(args)
141 })
142 Expect(err).NotTo(HaveOccurred())
143
144 addResult, err = current.GetResult(r)
145 Expect(err).NotTo(HaveOccurred())
146 Expect(addResult.IPs).To(HaveLen(1))
147 Expect(addResult.IPs[0].Address.String()).To(Equal("192.168.1.6/24"))
148 return nil
149 })
150 Expect(err).NotTo(HaveOccurred())
151
152 args = &skel.CmdArgs{
153 ContainerID: "dummy",
154 Netns: targetNS.Path(),
155 IfName: contVethName1,
156 StdinData: []byte(conf),
157 }
158
159 err = originalNS.Do(func(ns.NetNS) error {
160 return testutils.CmdDelWithArgs(args, func() error {
161 return cmdDel(args)
162 })
163 })
164 Expect(err).NotTo(HaveOccurred())
165
166 args = &skel.CmdArgs{
167 ContainerID: "dummy",
168 Netns: targetNS.Path(),
169 IfName: contVethName0,
170 StdinData: []byte(conf),
171 }
172
173 err = originalNS.Do(func(ns.NetNS) error {
174 return testutils.CmdDelWithArgs(args, func() error {
175 return cmdDel(args)
176 })
177 })
178 Expect(err).NotTo(HaveOccurred())
179 })
180 })