github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/payload/context/status-set_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package context_test
     5  
     6  import (
     7  	"bytes"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  	"github.com/juju/testing"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/payload"
    16  	"github.com/juju/juju/payload/context"
    17  	coretesting "github.com/juju/juju/testing"
    18  )
    19  
    20  type statusSetSuite struct {
    21  	testing.IsolationSuite
    22  
    23  	stub    *testing.Stub
    24  	compCtx *stubSetStatusContext
    25  	ctx     *cmd.Context
    26  
    27  	cmd *context.StatusSetCmd
    28  }
    29  
    30  var _ = gc.Suite(&statusSetSuite{})
    31  
    32  func (s *statusSetSuite) SetUpTest(c *gc.C) {
    33  	s.IsolationSuite.SetUpTest(c)
    34  
    35  	s.stub = &testing.Stub{}
    36  	s.compCtx = &stubSetStatusContext{stub: s.stub}
    37  	s.ctx = coretesting.Context(c)
    38  
    39  	cmd, err := context.NewStatusSetCmd(s)
    40  	c.Assert(err, jc.ErrorIsNil)
    41  
    42  	s.cmd = cmd
    43  }
    44  
    45  func (s *statusSetSuite) init(c *gc.C, class, id, status string) {
    46  	err := s.cmd.Init([]string{class, id, status})
    47  	c.Assert(err, jc.ErrorIsNil)
    48  }
    49  
    50  func (s *statusSetSuite) Component(name string) (context.Component, error) {
    51  	s.stub.AddCall("Component", name)
    52  	if err := s.stub.NextErr(); err != nil {
    53  		return nil, errors.Trace(err)
    54  	}
    55  
    56  	return s.compCtx, nil
    57  }
    58  
    59  func (s *statusSetSuite) TestHelp(c *gc.C) {
    60  	code := cmd.Main(s.cmd, s.ctx, []string{"--help"})
    61  	c.Assert(code, gc.Equals, 0)
    62  
    63  	c.Check(s.ctx.Stdout.(*bytes.Buffer).String(), gc.Equals, `
    64  Usage: payload-status-set <class> <id> <status>
    65  
    66  Summary:
    67  update the status of a payload
    68  
    69  Details:
    70  "payload-status-set" is used to update the current status of a registered payload.
    71  The <class> and <id> provided must match a payload that has been previously
    72  registered with juju using payload-register. The <status> must be one of the
    73  follow: starting, started, stopping, stopped
    74  `[1:])
    75  }
    76  
    77  func (s *statusSetSuite) TestTooFewArgs(c *gc.C) {
    78  	err := s.cmd.Init([]string{})
    79  	c.Check(err, gc.ErrorMatches, `missing .*`)
    80  
    81  	err = s.cmd.Init([]string{payload.StateRunning})
    82  	c.Check(err, gc.ErrorMatches, `missing .*`)
    83  }
    84  
    85  func (s *statusSetSuite) TestInvalidStatjs(c *gc.C) {
    86  	s.init(c, "docker", "foo", "created")
    87  	err := s.cmd.Run(s.ctx)
    88  
    89  	c.Check(err, gc.ErrorMatches, `status .* not supported; expected .*`)
    90  }
    91  
    92  func (s *statusSetSuite) TestStatusSet(c *gc.C) {
    93  	s.init(c, "docker", "foo", payload.StateStopped)
    94  	err := s.cmd.Run(s.ctx)
    95  
    96  	c.Check(err, jc.ErrorIsNil)
    97  }
    98  
    99  type stubSetStatusContext struct {
   100  	context.Component
   101  	stub *testing.Stub
   102  }
   103  
   104  func (s stubSetStatusContext) SetStatus(class, id, status string) error {
   105  	s.stub.AddCall("SetStatus", class, id, status)
   106  	if err := s.stub.NextErr(); err != nil {
   107  		return errors.Trace(err)
   108  	}
   109  
   110  	return nil
   111  }
   112  
   113  func (s stubSetStatusContext) Flush() error {
   114  	s.stub.AddCall("Flush")
   115  	if err := s.stub.NextErr(); err != nil {
   116  		return errors.Trace(err)
   117  	}
   118  
   119  	return nil
   120  }