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

     1  // Copyright 2014 Canonical Ltd.
     2  // Copyright 2014 Cloudbase Solutions SRL
     3  // Licensed under the AGPLv3, see LICENCE file for details.
     4  
     5  package jujuc_test
     6  
     7  import (
     8  	"github.com/juju/cmd"
     9  	"github.com/juju/cmd/cmdtesting"
    10  	"github.com/juju/gnuflag"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    15  )
    16  
    17  type JujuRebootSuite struct {
    18  	ContextSuite
    19  }
    20  
    21  var _ = gc.Suite(&JujuRebootSuite{})
    22  
    23  func (s *JujuRebootSuite) TestNewJujuRebootCommand(c *gc.C) {
    24  	cmd, err := jujuc.NewJujuRebootCommand(nil)
    25  	c.Assert(err, jc.ErrorIsNil)
    26  	c.Assert(cmd, gc.DeepEquals, &jujuc.JujuRebootCommand{})
    27  }
    28  
    29  func (s *JujuRebootSuite) TestInfo(c *gc.C) {
    30  	rebootCmd, err := jujuc.NewJujuRebootCommand(nil)
    31  	c.Assert(err, jc.ErrorIsNil)
    32  	cmdInfo := rebootCmd.Info()
    33  
    34  	c.Assert(cmdInfo.Name, gc.Equals, "juju-reboot")
    35  	c.Assert(cmdInfo.Args, gc.Equals, "")
    36  	c.Assert(cmdInfo.Purpose, gc.Equals, "Reboot the host machine")
    37  }
    38  
    39  func (s *JujuRebootSuite) TestSetFlags(c *gc.C) {
    40  	rebootCmd := jujuc.JujuRebootCommand{Now: true}
    41  	fs := &gnuflag.FlagSet{}
    42  
    43  	rebootCmd.SetFlags(fs)
    44  
    45  	flag := fs.Lookup("now")
    46  	c.Assert(flag, gc.NotNil)
    47  }
    48  
    49  func (s *JujuRebootSuite) TestJujuRebootCommand(c *gc.C) {
    50  	var jujuRebootTests = []struct {
    51  		summary  string
    52  		hctx     *Context
    53  		args     []string
    54  		code     int
    55  		priority jujuc.RebootPriority
    56  	}{{
    57  		summary:  "test reboot priority defaulting to RebootAfterHook",
    58  		hctx:     &Context{shouldError: false, rebootPriority: jujuc.RebootSkip},
    59  		args:     []string{},
    60  		code:     0,
    61  		priority: jujuc.RebootAfterHook,
    62  	}, {
    63  		summary:  "test reboot priority being set to RebootNow",
    64  		hctx:     &Context{shouldError: false, rebootPriority: jujuc.RebootSkip},
    65  		args:     []string{"--now"},
    66  		code:     0,
    67  		priority: jujuc.RebootNow,
    68  	}, {
    69  		summary:  "test a failed running of juju-reboot",
    70  		hctx:     &Context{shouldError: true, rebootPriority: jujuc.RebootSkip},
    71  		args:     []string{},
    72  		code:     1,
    73  		priority: jujuc.RebootAfterHook,
    74  	}, {
    75  		summary:  "test a failed running with parameter provided",
    76  		hctx:     &Context{shouldError: true, rebootPriority: jujuc.RebootSkip},
    77  		args:     []string{"--now"},
    78  		code:     1,
    79  		priority: jujuc.RebootNow,
    80  	}, {
    81  		summary:  "test invalid args provided",
    82  		hctx:     &Context{shouldError: false, rebootPriority: jujuc.RebootSkip},
    83  		args:     []string{"--way", "--too", "--many", "--args"},
    84  		code:     2,
    85  		priority: jujuc.RebootSkip,
    86  	}}
    87  
    88  	for i, t := range jujuRebootTests {
    89  		c.Logf("Test %d: %s", i, t.summary)
    90  
    91  		hctx := s.newHookContext(c)
    92  		hctx.shouldError = t.hctx.shouldError
    93  		hctx.rebootPriority = t.hctx.rebootPriority
    94  		com, err := jujuc.NewCommand(hctx, cmdString("juju-reboot"))
    95  		c.Assert(err, jc.ErrorIsNil)
    96  		ctx := cmdtesting.Context(c)
    97  		code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, t.args)
    98  		c.Check(code, gc.Equals, t.code)
    99  		c.Check(hctx.rebootPriority, gc.Equals, t.priority)
   100  	}
   101  }
   102  
   103  func (s *JujuRebootSuite) TestRebootInActions(c *gc.C) {
   104  	jujucCtx := &actionGetContext{}
   105  	com, err := jujuc.NewCommand(jujucCtx, cmdString("juju-reboot"))
   106  	c.Assert(err, jc.ErrorIsNil)
   107  	cmdCtx := cmdtesting.Context(c)
   108  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), cmdCtx, nil)
   109  	c.Check(code, gc.Equals, 1)
   110  	c.Assert(cmdtesting.Stderr(cmdCtx), gc.Equals, "ERROR juju-reboot is not supported when running an action.\n")
   111  }