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