noop plugin: support multiple CNI_ARGS
authorMark St.Godard <markstgodard@gmail.com>
Thu, 10 Nov 2016 21:30:24 +0000 (15:30 -0600)
committerMark St.Godard <markstgodard@gmail.com>
Thu, 10 Nov 2016 22:04:42 +0000 (16:04 -0600)
Updated noop plugin to parse the CNI_ARGS pairs, to allow more than just
the DEBUG arg.

plugins/test/noop/main.go
plugins/test/noop/noop_test.go

index d3c5698..fe67c37 100644 (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())
index 40a3741..49e8616 100644 (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"}`),
                }