noop plugin: support multiple CNI_ARGS

Updated noop plugin to parse the CNI_ARGS pairs, to allow more than just
the DEBUG arg.
This commit is contained in:
Mark St.Godard 2016-11-10 15:30:24 -06:00
parent 7b2006eed8
commit 48d3b46eb2
2 changed files with 39 additions and 5 deletions

View File

@ -32,13 +32,34 @@ import (
"github.com/containernetworking/cni/plugins/test/noop/debug"
)
// parse extra args i.e. FOO=BAR;ABC=123
func parseExtraArgs(args string) (map[string]string, error) {
m := make(map[string]string)
items := strings.Split(args, ";")
for _, item := range items {
kv := strings.Split(item, "=")
if len(kv) != 2 {
return nil, fmt.Errorf("CNI_ARGS invalid key/value pair: %s\n", kv)
}
m[kv[0]] = kv[1]
}
return m, nil
}
func debugBehavior(args *skel.CmdArgs, command string) error {
if !strings.HasPrefix(args.Args, "DEBUG=") {
extraArgs, err := parseExtraArgs(args.Args)
if err != nil {
return err
}
debugFilePath, ok := extraArgs["DEBUG"]
if !ok {
fmt.Printf(`{}`)
os.Stderr.WriteString("CNI_ARGS empty, no debug behavior\n")
return nil
}
debugFilePath := strings.TrimPrefix(args.Args, "DEBUG=")
debug, err := debug.ReadDebug(debugFilePath)
if err != nil {
return err
@ -69,7 +90,17 @@ func debugGetSupportedVersions() []string {
if cniArgs == "" {
return vers
}
debugFilePath := strings.TrimPrefix(cniArgs, "DEBUG=")
extraArgs, err := parseExtraArgs(cniArgs)
if err != nil {
panic("test setup error: invalid CNI_ARGS format")
}
debugFilePath, ok := extraArgs["DEBUG"]
if !ok {
panic("test setup error: missing DEBUG in CNI_ARGS")
}
debug, err := debug.ReadDebug(debugFilePath)
if err != nil {
panic("test setup error: unable to read debug file: " + err.Error())

View File

@ -15,6 +15,7 @@
package main_test
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
@ -52,10 +53,12 @@ var _ = Describe("No-op plugin", func() {
Expect(debug.WriteDebug(debugFileName)).To(Succeed())
cmd = exec.Command(pathToPlugin)
args := fmt.Sprintf("DEBUG=%s;FOO=BAR", debugFileName)
cmd.Env = []string{
"CNI_COMMAND=ADD",
"CNI_CONTAINERID=some-container-id",
"CNI_ARGS=DEBUG=" + debugFileName,
"CNI_ARGS=" + args,
"CNI_NETNS=/some/netns/path",
"CNI_IFNAME=some-eth0",
"CNI_PATH=/some/bin/path",
@ -65,7 +68,7 @@ var _ = Describe("No-op plugin", func() {
ContainerID: "some-container-id",
Netns: "/some/netns/path",
IfName: "some-eth0",
Args: "DEBUG=" + debugFileName,
Args: args,
Path: "/some/bin/path",
StdinData: []byte(`{"some":"stdin-json", "cniVersion": "0.2.0"}`),
}