From 71447d9008d1c687981d0769f9ac2b302b05cec2 Mon Sep 17 00:00:00 2001 From: Jay Dunkelberger Date: Tue, 30 Aug 2016 17:00:36 -0700 Subject: [PATCH] Update gqt tests to be compatible with netplugin changes --- gqt/create_test.go | 61 ++++++++++++++++------------- gqt/net_test.go | 95 ++++++++++++++++++++++------------------------ 2 files changed, 80 insertions(+), 76 deletions(-) diff --git a/gqt/create_test.go b/gqt/create_test.go index d22c502cf..fadecdcf5 100644 --- a/gqt/create_test.go +++ b/gqt/create_test.go @@ -337,6 +337,7 @@ var _ = Describe("Creating a Container", func() { }) Context("when running with an external network plugin", func() { + var pluginOutput string BeforeEach(func() { tmpDir, err := ioutil.TempDir("", "netplugtest") Expect(err).NotTo(HaveOccurred()) @@ -350,51 +351,57 @@ var _ = Describe("Creating a Container", func() { } }) - It("does not run kawasaki", func() { - container, err := client.Create(garden.ContainerSpec{}) - Expect(err).NotTo(HaveOccurred()) - - out := gbytes.NewBuffer() - process, err := container.Run(garden.ProcessSpec{ - Path: "ip", - Args: []string{ - "-o", - "link", - "show", - }, - }, garden.ProcessIO{ - Stdout: io.MultiWriter(GinkgoWriter, out), + Context("when the plugin returns a properties key", func() { + BeforeEach(func() { + pluginOutput = `{"properties": {"key":"value", "garden.network.container-ip":"10.10.24.3"}}` + args = append(args, "--network-plugin-extra-arg", pluginOutput) }) - Expect(err).NotTo(HaveOccurred()) - exitCode, err := process.Wait() - Expect(err).NotTo(HaveOccurred()) - Expect(exitCode).To(BeZero()) + It("does not run kawasaki", func() { + container, err := client.Create(garden.ContainerSpec{}) + Expect(err).NotTo(HaveOccurred()) + + out := gbytes.NewBuffer() + process, err := container.Run(garden.ProcessSpec{ + Path: "ip", + Args: []string{ + "-o", + "link", + "show", + }, + }, garden.ProcessIO{ + Stdout: io.MultiWriter(GinkgoWriter, out), + }) + Expect(err).NotTo(HaveOccurred()) - // ip link appends a new line on the end so let's trim that first - contents := strings.TrimRight(string(out.Contents()), "\n") + exitCode, err := process.Wait() + Expect(err).NotTo(HaveOccurred()) + Expect(exitCode).To(BeZero()) - // Check that we only have 1 interface, the loopback interface - Expect(strings.Split(contents, "\n")).To(HaveLen(1)) - Expect(contents).To(ContainSubstring("LOOPBACK")) + // ip link appends a new line on the end so let's trim that first + contents := strings.TrimRight(string(out.Contents()), "\n") + + // Check that we only have 1 interface, the loopback interface + Expect(strings.Split(contents, "\n")).To(HaveLen(1)) + Expect(contents).To(ContainSubstring("LOOPBACK")) + }) }) Context("when the external network plugin returns invalid JSON", func() { BeforeEach(func() { - pluginOutput := "invalid-json" + pluginOutput = "invalid-json" args = append(args, "--network-plugin-extra-arg", pluginOutput) - }) It("returns a useful error message", func() { _, err := client.Create(garden.ContainerSpec{}) - Expect(err).To(MatchError(ContainSubstring("network plugin returned invalid JSON"))) + Expect(err).To(MatchError(ContainSubstring("unmarshaling result from external networker: invalid character"))) }) }) Context("when the external network plugin returns JSON without the 'properties' key", func() { BeforeEach(func() { - pluginOutput := `{"not-properties-key":{"foo":"bar"}}` + pluginOutput = `{"not-properties-key":{"foo":"bar"}}` args = append(args, "--network-plugin-extra-arg", pluginOutput) }) diff --git a/gqt/net_test.go b/gqt/net_test.go index 23e7f17c4..64da83f43 100644 --- a/gqt/net_test.go +++ b/gqt/net_test.go @@ -296,6 +296,7 @@ var _ = Describe("Networking", func() { Context("when a network plugin path is provided at startup", func() { var argsFile string var stdinFile string + var pluginReturn string BeforeEach(func() { tmpDir, err := ioutil.TempDir("", "netplugtest") @@ -303,6 +304,7 @@ var _ = Describe("Networking", func() { argsFile = path.Join(tmpDir, "args.log") stdinFile = path.Join(tmpDir, "stdin.log") + args = append(args, "--network-plugin-extra-arg", pluginReturn) args = []string{ "--network-plugin", testNetPluginBin, @@ -311,57 +313,9 @@ var _ = Describe("Networking", func() { } }) - It("executes the network plugin during container creation", func() { - containerHandle := container.Handle() - - Eventually(getContent(argsFile)).Should( - ContainSubstring( - fmt.Sprintf("%s %s --action up --handle %s --network %s", argsFile, stdinFile, containerHandle, containerNetwork), - ), - ) - }) - - It("passes the container pid to plugin's stdin", func() { - Eventually(getContent(stdinFile)).Should( - MatchRegexp(`.*{"PID":[0-9]+}.*`), - ) - }) - - It("executes the network plugin during container destroy", func() { - containerHandle := container.Handle() - - Expect(client.Destroy(containerHandle)).To(Succeed()) - Expect(argsFile).To(BeAnExistingFile()) - - Eventually(getContent(argsFile)).Should( - ContainSubstring( - fmt.Sprintf("%s %s --action down --handle %s", argsFile, stdinFile, containerHandle), - ), - ) - }) - - Context("when the container spec has properties that start with 'network.'", func() { - var expectedJSON string - - BeforeEach(func() { - extraProperties = garden.Properties{ - "network.some-key": "some-value", - "network.some-other-key": "some-other-value", - "some-other-key": "do-not-propagate", - "garden.whatever": "do-not-propagate", - "kawasaki.nope": "do-not-propagate", - } - expectedJSON = `{ "some-key": "some-value", "some-other-key": "some-other-value" }` - }) - - It("propagates those properties as JSON to the network plugin up action", func() { - Eventually(getFlagValue(argsFile, "--properties")).Should(MatchJSON(expectedJSON)) - }) - }) - Context("when the network plugin returns properties", func() { BeforeEach(func() { - pluginReturn := `{"properties":{ + pluginReturn = `{"properties":{ "foo":"bar", "kawasaki.mtu":"1499", "garden.network.container-ip":"10.255.10.10", @@ -370,9 +324,52 @@ var _ = Describe("Networking", func() { args = append(args, "--network-plugin-extra-arg", pluginReturn) extraProperties = garden.Properties{ "some-property-on-the-spec": "some-value", + "network.some-key": "some-value", + "network.some-other-key": "some-other-value", + "some-other-key": "do-not-propagate", + "garden.whatever": "do-not-propagate", + "kawasaki.nope": "do-not-propagate", } }) + Context("when the container spec has properties that start with 'network.'", func() { + var expectedJSON string + + BeforeEach(func() { + expectedJSON = `"some-key":"some-value","some-other-key":"some-other-value"}` + }) + + It("propagates those properties as JSON to the network plugin up action", func() { + Eventually(getContent(stdinFile)).Should(ContainSubstring(expectedJSON)) + }) + }) + + It("executes the network plugin during container destroy", func() { + containerHandle := container.Handle() + + Expect(client.Destroy(containerHandle)).To(Succeed()) + Expect(argsFile).To(BeAnExistingFile()) + + Eventually(getContent(argsFile)).Should(ContainSubstring(fmt.Sprintf("%s %s", argsFile, stdinFile))) + Eventually(getContent(argsFile)).Should(ContainSubstring(fmt.Sprintf("--action down --handle %s", containerHandle))) + }) + + It("passes the container pid to plugin's stdin", func() { + Eventually(getContent(stdinFile)).Should( + MatchRegexp(`.*{"Pid":[0-9]+.*}.*`), + ) + }) + + It("executes the network plugin during container creation", func() { + containerHandle := container.Handle() + + Eventually(getContent(argsFile)).Should( + ContainSubstring( + fmt.Sprintf("%s %s %s --action up --handle %s", argsFile, stdinFile, pluginReturn, containerHandle), + ), + ) + }) + It("persists the returned properties to the container's properties", func() { info, err := container.Info() Expect(err).NotTo(HaveOccurred())