From f5302a2aa935064e67ee8926f433ed001d39e4a0 Mon Sep 17 00:00:00 2001 From: Yongkun Anfernee Gui Date: Thu, 7 Sep 2017 11:40:14 -0700 Subject: [PATCH] VERSION shouldn't block on stdin 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 | 5 +++++ pkg/skel/skel_test.go | 24 ++++++++++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/pkg/skel/skel.go b/pkg/skel/skel.go index 8644c25..69c58e0 100644 --- a/pkg/skel/skel.go +++ b/pkg/skel/skel.go @@ -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) diff --git a/pkg/skel/skel_test.go b/pkg/skel/skel_test.go index e85bc2b..2b4d66d 100644 --- a/pkg/skel/skel_test.go +++ b/pkg/skel/skel_test.go @@ -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 } -- 2.44.0