github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/resource/context/cmd/get_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package cmd 5 6 import ( 7 "github.com/juju/cmd/cmdtesting" 8 "github.com/juju/errors" 9 "github.com/juju/testing" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 ) 13 14 var _ = gc.Suite(&GetCmdSuite{}) 15 16 type GetCmdSuite struct { 17 testing.IsolationSuite 18 19 stub *testing.Stub 20 hctx *stubHookContext 21 } 22 23 func (s *GetCmdSuite) SetUpTest(c *gc.C) { 24 s.IsolationSuite.SetUpTest(c) 25 26 s.stub = &testing.Stub{} 27 s.hctx = &stubHookContext{stub: s.stub} 28 } 29 30 func (s *GetCmdSuite) TestInitNilArgs(c *gc.C) { 31 getCmd := GetCmd{} 32 33 err := getCmd.Init(nil) 34 35 c.Check(err, gc.NotNil) 36 } 37 38 func (s *GetCmdSuite) TestInitTooFewArgs(c *gc.C) { 39 getCmd := GetCmd{} 40 41 err := getCmd.Init([]string{}) 42 43 c.Check(err, gc.NotNil) 44 } 45 46 func (s *GetCmdSuite) TestInitTooManyArgs(c *gc.C) { 47 getCmd := GetCmd{} 48 49 err := getCmd.Init([]string{"spam", "eggs"}) 50 51 c.Check(err, gc.NotNil) 52 } 53 54 func (s *GetCmdSuite) TestInit(c *gc.C) { 55 getCmd := GetCmd{} 56 57 err := getCmd.Init([]string{"spam"}) 58 c.Assert(err, jc.ErrorIsNil) 59 60 c.Check(getCmd.resourceName, gc.Equals, "spam") 61 } 62 63 func (s *GetCmdSuite) TestRunOkay(c *gc.C) { 64 getCmd := GetCmd{ 65 compContext: s.hctx, 66 resourceName: "spam", 67 } 68 const expected = "/var/lib/juju/agents/unit-foo-1/resources/spam/a-file.tgz" 69 s.hctx.ReturnDownload = expected 70 ctx := cmdtesting.Context(c) 71 72 err := getCmd.Run(ctx) 73 c.Assert(err, jc.ErrorIsNil) 74 75 s.stub.CheckCallNames(c, "Download") 76 s.stub.CheckCall(c, 0, "Download", "spam") 77 c.Check(cmdtesting.Stdout(ctx), gc.Equals, expected) 78 c.Check(cmdtesting.Stderr(ctx), gc.Equals, "") 79 } 80 81 func (s *GetCmdSuite) TestRunDownloadFailure(c *gc.C) { 82 getCmd := GetCmd{ 83 compContext: s.hctx, 84 resourceName: "spam", 85 } 86 failure := errors.New("<failure>") 87 s.stub.SetErrors(failure) 88 ctx := cmdtesting.Context(c) 89 90 err := getCmd.Run(ctx) 91 92 s.stub.CheckCallNames(c, "Download") 93 c.Check(errors.Cause(err), gc.Equals, failure) 94 c.Check(cmdtesting.Stdout(ctx), gc.Equals, "") 95 c.Check(cmdtesting.Stderr(ctx), gc.Equals, "") 96 }