Skip to content

Commit

Permalink
Update gqt tests to be compatible with netplugin changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay Dunkelberger committed Aug 31, 2016
1 parent 3e3c3d1 commit 71447d9
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 76 deletions.
61 changes: 34 additions & 27 deletions gqt/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)

})
Expand Down
95 changes: 46 additions & 49 deletions gqt/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,15 @@ 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")
Expect(err).NotTo(HaveOccurred())

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,
Expand All @@ -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",
Expand All @@ -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())
Expand Down

0 comments on commit 71447d9

Please sign in to comment.