github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/jujuc/status-set_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc_test
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/cmd/cmdtesting"
     9  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  )
    13  
    14  type statusSetSuite struct {
    15  	ContextSuite
    16  }
    17  
    18  var _ = gc.Suite(&statusSetSuite{})
    19  
    20  func (s *statusSetSuite) SetUpTest(c *gc.C) {
    21  	s.ContextSuite.SetUpTest(c)
    22  }
    23  
    24  var statusSetInitTests = []struct {
    25  	args []string
    26  	err  string
    27  }{
    28  	{[]string{"maintenance"}, ""},
    29  	{[]string{"maintenance", ""}, ""},
    30  	{[]string{"maintenance", "hello"}, ""},
    31  	{[]string{}, `invalid args, require <status> \[message\]`},
    32  	{[]string{"maintenance", "hello", "extra"}, `unrecognized args: \["extra"\]`},
    33  	{[]string{"foo", "hello"}, `invalid status "foo", expected one of \[maintenance blocked waiting active\]`},
    34  }
    35  
    36  func (s *statusSetSuite) TestStatusSetInit(c *gc.C) {
    37  	for i, t := range statusSetInitTests {
    38  		c.Logf("test %d: %#v", i, t.args)
    39  		hctx := s.GetStatusHookContext(c)
    40  		com, err := jujuc.NewCommand(hctx, "status-set")
    41  		c.Assert(err, jc.ErrorIsNil)
    42  		cmdtesting.TestInit(c, com, t.args, t.err)
    43  	}
    44  }
    45  
    46  func (s *statusSetSuite) TestHelp(c *gc.C) {
    47  	hctx := s.GetStatusHookContext(c)
    48  	com, err := jujuc.NewCommand(hctx, "status-set")
    49  	c.Assert(err, jc.ErrorIsNil)
    50  	ctx := cmdtesting.Context(c)
    51  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"--help"})
    52  	c.Assert(code, gc.Equals, 0)
    53  	expectedHelp := "" +
    54  		"Usage: status-set [options] <maintenance | blocked | waiting | active> [message]\n" +
    55  		"\n" +
    56  		"Summary:\n" +
    57  		"set status information\n" +
    58  		"\n" +
    59  		"Options:\n" +
    60  		"--application  (= false)\n" +
    61  		"    set this status for the application to which the unit belongs if the unit is the leader\n" +
    62  		"\n" +
    63  		"Details:\n" +
    64  		"Sets the workload status of the charm. Message is optional.\n" +
    65  		"The \"last updated\" attribute of the status is set, even if the\n" +
    66  		"status and message are the same as what's already set.\n"
    67  
    68  	c.Assert(bufferString(ctx.Stdout), gc.Equals, expectedHelp)
    69  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
    70  }
    71  
    72  func (s *statusSetSuite) TestStatus(c *gc.C) {
    73  	for i, args := range [][]string{
    74  		{"maintenance", "doing some work"},
    75  		{"active", ""},
    76  	} {
    77  		c.Logf("test %d: %#v", i, args)
    78  		hctx := s.GetStatusHookContext(c)
    79  		com, err := jujuc.NewCommand(hctx, "status-set")
    80  		c.Assert(err, jc.ErrorIsNil)
    81  		ctx := cmdtesting.Context(c)
    82  		code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, args)
    83  		c.Assert(code, gc.Equals, 0)
    84  		c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
    85  		c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
    86  		status, err := hctx.UnitStatus()
    87  		c.Assert(err, jc.ErrorIsNil)
    88  		c.Assert(status.Status, gc.Equals, args[0])
    89  		c.Assert(status.Info, gc.Equals, args[1])
    90  	}
    91  }
    92  
    93  func (s *statusSetSuite) TestApplicationStatus(c *gc.C) {
    94  	for i, args := range [][]string{
    95  		{"--application", "maintenance", "doing some work"},
    96  		{"--application", "active", ""},
    97  	} {
    98  		c.Logf("test %d: %#v", i, args)
    99  		hctx := s.GetStatusHookContext(c)
   100  		com, err := jujuc.NewCommand(hctx, "status-set")
   101  		c.Assert(err, jc.ErrorIsNil)
   102  		ctx := cmdtesting.Context(c)
   103  		code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, args)
   104  		c.Assert(code, gc.Equals, 0)
   105  		c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   106  		c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
   107  		status, err := hctx.ApplicationStatus()
   108  		c.Assert(err, jc.ErrorIsNil)
   109  		c.Assert(status.Application.Status, gc.Equals, args[1])
   110  		c.Assert(status.Application.Info, gc.Equals, args[2])
   111  		c.Assert(status.Units, jc.DeepEquals, []jujuc.StatusInfo{})
   112  
   113  	}
   114  }