flannel: updated flannel test to use pkg/testutils
reworked the flannel test to use testutils CmdAddWithResult and CmdDelWithResult
This commit is contained in:
parent
e6113c6517
commit
15de81eac6
@ -14,13 +14,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
"github.com/onsi/gomega/gexec"
|
|
||||||
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -29,38 +24,3 @@ func TestFlannel(t *testing.T) {
|
|||||||
RegisterFailHandler(Fail)
|
RegisterFailHandler(Fail)
|
||||||
RunSpecs(t, "Flannel Suite")
|
RunSpecs(t, "Flannel Suite")
|
||||||
}
|
}
|
||||||
|
|
||||||
const flannelPackage = "github.com/containernetworking/cni/plugins/meta/flannel"
|
|
||||||
const noopPackage = "github.com/containernetworking/cni/plugins/test/noop"
|
|
||||||
|
|
||||||
var paths testPaths
|
|
||||||
|
|
||||||
type testPaths struct {
|
|
||||||
PathToPlugin string
|
|
||||||
CNIPath string
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ = SynchronizedBeforeSuite(func() []byte {
|
|
||||||
noopBin, err := gexec.Build(noopPackage)
|
|
||||||
Expect(err).NotTo(HaveOccurred())
|
|
||||||
noopDir, _ := filepath.Split(noopBin)
|
|
||||||
|
|
||||||
pathToPlugin, err := gexec.Build(flannelPackage)
|
|
||||||
Expect(err).NotTo(HaveOccurred())
|
|
||||||
flannelDir, _ := filepath.Split(pathToPlugin)
|
|
||||||
|
|
||||||
paths := testPaths{
|
|
||||||
PathToPlugin: pathToPlugin,
|
|
||||||
CNIPath: fmt.Sprintf("%s:%s", flannelDir, noopDir),
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := json.Marshal(paths)
|
|
||||||
Expect(err).NotTo(HaveOccurred())
|
|
||||||
return data
|
|
||||||
}, func(data []byte) {
|
|
||||||
Expect(json.Unmarshal(data, &paths)).To(Succeed())
|
|
||||||
})
|
|
||||||
|
|
||||||
var _ = SynchronizedAfterSuite(func() {}, func() {
|
|
||||||
gexec.CleanupBuildArtifacts()
|
|
||||||
})
|
|
||||||
|
@ -17,44 +17,40 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
|
"github.com/containernetworking/cni/pkg/ns"
|
||||||
"github.com/containernetworking/cni/pkg/skel"
|
"github.com/containernetworking/cni/pkg/skel"
|
||||||
|
"github.com/containernetworking/cni/pkg/testutils"
|
||||||
|
|
||||||
noop_debug "github.com/containernetworking/cni/plugins/test/noop/debug"
|
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
"github.com/onsi/gomega/gexec"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ = Describe("Flannel", func() {
|
var _ = Describe("Flannel", func() {
|
||||||
var (
|
var (
|
||||||
cmd *exec.Cmd
|
originalNS ns.NetNS
|
||||||
debugFileName string
|
|
||||||
input string
|
input string
|
||||||
debug *noop_debug.Debug
|
|
||||||
expectedCmdArgs skel.CmdArgs
|
|
||||||
subnetFile string
|
subnetFile string
|
||||||
stateDir string
|
stateDir string
|
||||||
)
|
)
|
||||||
|
|
||||||
const delegateInput = `
|
BeforeEach(func() {
|
||||||
{
|
var err error
|
||||||
"type": "noop",
|
originalNS, err = ns.NewNS()
|
||||||
"some": "other data"
|
Expect(err).NotTo(HaveOccurred())
|
||||||
}
|
})
|
||||||
`
|
|
||||||
|
AfterEach(func() {
|
||||||
|
Expect(originalNS.Close()).To(Succeed())
|
||||||
|
})
|
||||||
|
|
||||||
const inputTemplate = `
|
const inputTemplate = `
|
||||||
{
|
{
|
||||||
"name": "cni-flannel",
|
"name": "cni-flannel",
|
||||||
"type": "flannel",
|
"type": "flannel",
|
||||||
"subnetFile": "%s",
|
"subnetFile": "%s",
|
||||||
"stateDir": "%s",
|
"stateDir": "%s"
|
||||||
"delegate": ` +
|
}`
|
||||||
delegateInput +
|
|
||||||
`}`
|
|
||||||
|
|
||||||
const flannelSubnetEnv = `
|
const flannelSubnetEnv = `
|
||||||
FLANNEL_NETWORK=10.1.0.0/16
|
FLANNEL_NETWORK=10.1.0.0/16
|
||||||
@ -71,32 +67,8 @@ FLANNEL_IPMASQ=true
|
|||||||
return file.Name()
|
return file.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
var cniCommand = func(command, input string) *exec.Cmd {
|
|
||||||
toReturn := exec.Command(paths.PathToPlugin)
|
|
||||||
toReturn.Env = []string{
|
|
||||||
"CNI_COMMAND=" + command,
|
|
||||||
"CNI_CONTAINERID=some-container-id",
|
|
||||||
"CNI_NETNS=/some/netns/path",
|
|
||||||
"CNI_IFNAME=some-eth0",
|
|
||||||
"CNI_PATH=" + paths.CNIPath,
|
|
||||||
"CNI_ARGS=DEBUG=" + debugFileName,
|
|
||||||
}
|
|
||||||
toReturn.Stdin = strings.NewReader(input)
|
|
||||||
return toReturn
|
|
||||||
}
|
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
debugFile, err := ioutil.TempFile("", "cni_debug")
|
var err error
|
||||||
Expect(err).NotTo(HaveOccurred())
|
|
||||||
Expect(debugFile.Close()).To(Succeed())
|
|
||||||
debugFileName = debugFile.Name()
|
|
||||||
|
|
||||||
debug = &noop_debug.Debug{
|
|
||||||
ReportResult: `{ "ip4": { "ip": "1.2.3.4/32" } }`,
|
|
||||||
ReportVersionSupport: []string{"0.1.0", "0.2.0", "0.3.0"},
|
|
||||||
}
|
|
||||||
Expect(debug.WriteDebug(debugFileName)).To(Succeed())
|
|
||||||
|
|
||||||
// flannel subnet.env
|
// flannel subnet.env
|
||||||
subnetFile = writeSubnetEnv(flannelSubnetEnv)
|
subnetFile = writeSubnetEnv(flannelSubnetEnv)
|
||||||
|
|
||||||
@ -107,31 +79,33 @@ FLANNEL_IPMASQ=true
|
|||||||
})
|
})
|
||||||
|
|
||||||
AfterEach(func() {
|
AfterEach(func() {
|
||||||
os.Remove(debugFileName)
|
|
||||||
os.Remove(subnetFile)
|
os.Remove(subnetFile)
|
||||||
os.Remove(stateDir)
|
os.Remove(stateDir)
|
||||||
})
|
})
|
||||||
|
|
||||||
Describe("CNI lifecycle", func() {
|
Describe("CNI lifecycle", func() {
|
||||||
|
It("uses stateDir for storing network configuration", func() {
|
||||||
|
const IFNAME = "eth0"
|
||||||
|
|
||||||
BeforeEach(func() {
|
targetNs, err := ns.NewNS()
|
||||||
expectedCmdArgs = skel.CmdArgs{
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
defer targetNs.Close()
|
||||||
|
|
||||||
|
args := &skel.CmdArgs{
|
||||||
ContainerID: "some-container-id",
|
ContainerID: "some-container-id",
|
||||||
Netns: "/some/netns/path",
|
Netns: targetNs.Path(),
|
||||||
IfName: "some-eth0",
|
IfName: IFNAME,
|
||||||
Args: "DEBUG=" + debugFileName,
|
|
||||||
Path: "/some/bin/path",
|
|
||||||
StdinData: []byte(input),
|
StdinData: []byte(input),
|
||||||
}
|
}
|
||||||
cmd = cniCommand("ADD", input)
|
|
||||||
})
|
|
||||||
|
|
||||||
It("uses stateDir for storing network configuration", func() {
|
err = originalNS.Do(func(ns.NetNS) error {
|
||||||
|
defer GinkgoRecover()
|
||||||
|
|
||||||
By("calling ADD")
|
By("calling ADD")
|
||||||
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
|
_, err := testutils.CmdAddWithResult(targetNs.Path(), IFNAME, func() error {
|
||||||
|
return cmdAdd(args)
|
||||||
|
})
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Eventually(session).Should(gexec.Exit(0))
|
|
||||||
Expect(session.Out.Contents()).To(MatchJSON(`{ "ip4": { "ip": "1.2.3.4/32" }, "dns":{} }`))
|
|
||||||
|
|
||||||
By("check that plugin writes to net config to stateDir")
|
By("check that plugin writes to net config to stateDir")
|
||||||
path := fmt.Sprintf("%s/%s", stateDir, "some-container-id")
|
path := fmt.Sprintf("%s/%s", stateDir, "some-container-id")
|
||||||
@ -141,7 +115,7 @@ FLANNEL_IPMASQ=true
|
|||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
expected := `{
|
expected := `{
|
||||||
"name" : "cni-flannel",
|
"name" : "cni-flannel",
|
||||||
"type" : "noop",
|
"type" : "bridge",
|
||||||
"ipam" : {
|
"ipam" : {
|
||||||
"type" : "host-local",
|
"type" : "host-local",
|
||||||
"subnet" : "10.1.17.0/24",
|
"subnet" : "10.1.17.0/24",
|
||||||
@ -153,19 +127,22 @@ FLANNEL_IPMASQ=true
|
|||||||
},
|
},
|
||||||
"mtu" : 1472,
|
"mtu" : 1472,
|
||||||
"ipMasq" : false,
|
"ipMasq" : false,
|
||||||
"some" : "other data"
|
"isGateway": true
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
Expect(netConfBytes).Should(MatchJSON(expected))
|
Expect(netConfBytes).Should(MatchJSON(expected))
|
||||||
|
|
||||||
By("calling DEL")
|
By("calling DEL")
|
||||||
cmd = cniCommand("DEL", input)
|
err = testutils.CmdDelWithResult(targetNs.Path(), IFNAME, func() error {
|
||||||
session, err = gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
|
return cmdDel(args)
|
||||||
|
})
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Eventually(session).Should(gexec.Exit(0))
|
|
||||||
|
|
||||||
By("check that plugin removes net config from state dir")
|
By("check that plugin removes net config from state dir")
|
||||||
Expect(path).ShouldNot(BeAnExistingFile())
|
Expect(path).ShouldNot(BeAnExistingFile())
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -185,10 +162,8 @@ FLANNEL_IPMASQ=true
|
|||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
input = `{
|
input = `{
|
||||||
"name": "cni-flannel",
|
"name": "cni-flannel",
|
||||||
"type": "flannel",
|
"type": "flannel"
|
||||||
"delegate": ` +
|
}`
|
||||||
delegateInput +
|
|
||||||
`}`
|
|
||||||
})
|
})
|
||||||
|
|
||||||
It("loads flannel network config with defaults", func() {
|
It("loads flannel network config with defaults", func() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user