1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
package main
import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"syscall"
"time"
"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types/current"
"github.com/containernetworking/plugins/pkg/ns"
"github.com/containernetworking/plugins/pkg/testutils"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vishvananda/netlink"
)
func cleanup(d dnsNameFile) error {
_ = d.stop()
return os.RemoveAll(filepath.Dir(d.PidFile))
}
var _ = Describe("dnsname tests", func() {
var originalNS, targetNS ns.NetNS
const IFNAME string = "dummy0"
fullConf := []byte(`{
"cniVersion": "0.4.0",
"name": "test",
"type": "dnsname",
"domainName": "foobar.io",
"prevResult": {
"cniVersion": "0.4.0",
"interfaces": [
{
"name": "dummy0",
"mac": "a6:a7:ca:6b:34:2e"
},
{
"name": "vetha0a83b38",
"mac": "9a:45:bd:b0:2d:dd"
},
{
"name": "eth0",
"mac": "ea:63:0e:63:3e:86",
"sandbox": "/var/run/netns/baude"
}
],
"ips": [
{
"version": "4",
"interface": 2,
"address": "10.88.8.5/24",
"gateway": "10.88.8.1"
}
],
"routes": [
{
"dst": "0.0.0.0/0"
}
]
}
}`)
BeforeEach(func() {
// Create a new NetNS so we don't modify the host
var err error
originalNS, err = testutils.NewNS()
Expect(err).NotTo(HaveOccurred())
err = originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()
err = netlink.LinkAdd(&netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
Name: IFNAME,
},
})
Expect(err).NotTo(HaveOccurred())
_, err = netlink.LinkByName(IFNAME)
Expect(err).NotTo(HaveOccurred())
return nil
})
Expect(err).NotTo(HaveOccurred())
targetNS, err = testutils.NewNS()
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
Expect(originalNS.Close()).To(Succeed())
Expect(targetNS.Close()).To(Succeed())
})
It("dnsname add", func() {
args := &skel.CmdArgs{
ContainerID: "dummy",
Netns: targetNS.Path(),
IfName: IFNAME,
StdinData: fullConf,
}
err := originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()
r, _, err := testutils.CmdAdd(targetNS.Path(), args.ContainerID, IFNAME, fullConf, func() error {
return cmdAdd(args)
})
Expect(err).NotTo(HaveOccurred())
_, err = current.GetResult(r)
Expect(err).NotTo(HaveOccurred())
// Check that all configuration files are created
files, err := ioutil.ReadDir("/run/containers/cni/dnsname/test")
Expect(err).To(BeNil())
expectedFileNames := []string{"addnhosts", "dnsmasq.conf", "lock", "pidfile"}
var resultingFileNames []string
for _, f := range files {
resultingFileNames = append(resultingFileNames, f.Name())
}
Expect(reflect.DeepEqual(expectedFileNames, resultingFileNames)).To(BeTrue())
d, err := newDNSMasqFile("foobar.io", "dummy0", "test")
Expect(err).To(BeNil())
// Check that the dns masq instance is running
pid, err := d.getProcess()
Expect(err).To(BeNil())
// Send it a signal 0; if alive, error will be nil
err = pid.Signal(syscall.Signal(0))
Expect(err).To(BeNil())
// Stop the dnsmasq instance and clean up files in the filesystem
Expect(cleanup(d)).To(BeNil())
return nil
})
Expect(err).NotTo(HaveOccurred())
})
It("dnsname del", func() {
var (
dnsDead bool
counter int
)
args := &skel.CmdArgs{
ContainerID: "dummy",
Netns: targetNS.Path(),
IfName: IFNAME,
StdinData: fullConf,
}
err := originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()
r, _, err := testutils.CmdAdd(targetNS.Path(), args.ContainerID, IFNAME, fullConf, func() error {
return cmdAdd(args)
})
Expect(err).NotTo(HaveOccurred())
_, err = current.GetResult(r)
Expect(err).NotTo(HaveOccurred())
d, err := newDNSMasqFile("foobar.io", "dummy0", "test")
Expect(err).To(BeNil())
pid, err := d.getProcess()
Expect(err).To(BeNil())
err = pid.Signal(syscall.Signal(0))
Expect(err).To(BeNil())
err = testutils.CmdDel(targetNS.Path(), args.ContainerID, IFNAME, func() error {
return cmdDel(args)
})
Expect(err).To(BeNil())
// Ensure the dnsmasq instance has been stopped on del
// It sometimes takes time for the dnsmasq pid to be killed
// check every .5 second for maximum of 10 tries
for {
err = pid.Signal(syscall.Signal(0))
if err != nil {
dnsDead = true
break
}
if counter == 10 {
break
}
counter++
time.Sleep(500 * time.Millisecond)
}
Expect(dnsDead).To(BeTrue())
// Cleanup behind ourselves
Expect(cleanup(d)).To(BeNil())
return nil
})
Expect(err).NotTo(HaveOccurred())
})
})
|