VERSION shouldn't block on stdin
authorYongkun Anfernee Gui <agui@vmware.com>
Thu, 7 Sep 2017 18:40:14 +0000 (11:40 -0700)
committerYongkun Anfernee Gui <agui@vmware.com>
Fri, 8 Sep 2017 05:48:10 +0000 (22:48 -0700)
Right now, I have to do the following
  $ echo | CNI_COMMAND=VERSION bridge
or
  CNI_COMMAND=VERSION bridge
  ^D
to be able to get the result back.

But VERSION doesn't need to read from stdin.

pkg/skel/skel.go
pkg/skel/skel_test.go

index 8644c25..69c58e0 100644 (file)
@@ -17,6 +17,7 @@
 package skel
 
 import (
+       "bytes"
        "fmt"
        "io"
        "io/ioutil"
@@ -123,6 +124,10 @@ func (t *dispatcher) getCmdArgsFromEnv() (string, *CmdArgs, error) {
                return "", nil, fmt.Errorf("required env variables missing")
        }
 
+       if cmd == "VERSION" {
+               t.Stdin = bytes.NewReader(nil)
+       }
+
        stdinData, err := ioutil.ReadAll(t.Stdin)
        if err != nil {
                return "", nil, fmt.Errorf("error reading from stdin: %v", err)
index e85bc2b..2b4d66d 100644 (file)
@@ -247,20 +247,18 @@ var _ = Describe("dispatching to the correct callback", func() {
                        Entry("path", "CNI_PATH", false),
                )
 
-               Context("when the stdin is empty", func() {
-                       BeforeEach(func() {
-                               dispatch.Stdin = strings.NewReader("")
-                       })
+               It("does not read from Stdin", func() {
+                       r := &BadReader{}
+                       dispatch.Stdin = r
 
-                       It("succeeds without error", func() {
-                               err := dispatch.pluginMain(cmdAdd.Func, cmdDel.Func, versionInfo)
+                       err := dispatch.pluginMain(cmdAdd.Func, cmdDel.Func, versionInfo)
 
-                               Expect(err).NotTo(HaveOccurred())
-                               Expect(stdout).To(MatchJSON(`{
-                                       "cniVersion": "0.3.1",
-                                       "supportedVersions": ["9.8.7"]
+                       Expect(err).NotTo(HaveOccurred())
+                       Expect(r.ReadCount).To(Equal(0))
+                       Expect(stdout).To(MatchJSON(`{
+                               "cniVersion": "0.3.1",
+                               "supportedVersions": ["9.8.7"]
                        }`))
-                       })
                })
        })
 
@@ -346,10 +344,12 @@ var _ = Describe("dispatching to the correct callback", func() {
 
 // BadReader is an io.Reader which always errors
 type BadReader struct {
-       Error error
+       Error     error
+       ReadCount int
 }
 
 func (r *BadReader) Read(buffer []byte) (int, error) {
+       r.ReadCount++
        if r.Error != nil {
                return 0, r.Error
        }