github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  	"launchpad.net/gnuflag"
    12  
    13  	"github.com/juju/juju/testing"
    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 := jujuc.NewJujuRebootCommand(nil)
    25  	c.Assert(cmd, gc.DeepEquals, &jujuc.JujuRebootCommand{})
    26  }
    27  
    28  func (s *JujuRebootSuite) TestInfo(c *gc.C) {
    29  	rebootCmd := jujuc.NewJujuRebootCommand(nil)
    30  	cmdInfo := rebootCmd.Info()
    31  
    32  	c.Assert(cmdInfo.Name, gc.Equals, "juju-reboot")
    33  	c.Assert(cmdInfo.Args, gc.Equals, "")
    34  	c.Assert(cmdInfo.Purpose, gc.Equals, "Reboot the host machine")
    35  }
    36  
    37  func (s *JujuRebootSuite) TestSetFlags(c *gc.C) {
    38  	rebootCmd := jujuc.JujuRebootCommand{Now: true}
    39  	fs := &gnuflag.FlagSet{}
    40  
    41  	rebootCmd.SetFlags(fs)
    42  
    43  	flag := fs.Lookup("now")
    44  	c.Assert(flag, gc.NotNil)
    45  }
    46  
    47  func (s *JujuRebootSuite) TestJujuRebootCommand(c *gc.C) {
    48  	var jujuRebootTests = []struct {
    49  		summary  string
    50  		hctx     *Context
    51  		args     []string
    52  		code     int
    53  		priority jujuc.RebootPriority
    54  	}{{
    55  		summary:  "test reboot priority defaulting to RebootAfterHook",
    56  		hctx:     &Context{shouldError: false, rebootPriority: jujuc.RebootSkip},
    57  		args:     []string{},
    58  		code:     0,
    59  		priority: jujuc.RebootAfterHook,
    60  	}, {
    61  		summary:  "test reboot priority being set to RebootNow",
    62  		hctx:     &Context{shouldError: false, rebootPriority: jujuc.RebootSkip},
    63  		args:     []string{"--now"},
    64  		code:     0,
    65  		priority: jujuc.RebootNow,
    66  	}, {
    67  		summary:  "test a failed running of juju-reboot",
    68  		hctx:     &Context{shouldError: true, rebootPriority: jujuc.RebootSkip},
    69  		args:     []string{},
    70  		code:     1,
    71  		priority: jujuc.RebootAfterHook,
    72  	}, {
    73  		summary:  "test a failed running with parameter provided",
    74  		hctx:     &Context{shouldError: true, rebootPriority: jujuc.RebootSkip},
    75  		args:     []string{"--now"},
    76  		code:     1,
    77  		priority: jujuc.RebootNow,
    78  	}, {
    79  		summary:  "test invalid args provided",
    80  		hctx:     &Context{shouldError: false, rebootPriority: jujuc.RebootSkip},
    81  		args:     []string{"--way", "--too", "--many", "--args"},
    82  		code:     2,
    83  		priority: jujuc.RebootSkip,
    84  	}}
    85  
    86  	for i, t := range jujuRebootTests {
    87  		c.Logf("Test %d: %s", i, t.summary)
    88  
    89  		hctx := s.newHookContext(c)
    90  		hctx.shouldError = t.hctx.shouldError
    91  		hctx.rebootPriority = t.hctx.rebootPriority
    92  		com, err := jujuc.NewCommand(hctx, cmdString("juju-reboot"))
    93  		c.Assert(err, jc.ErrorIsNil)
    94  		ctx := testing.Context(c)
    95  		code := cmd.Main(com, ctx, t.args)
    96  		c.Check(code, gc.Equals, t.code)
    97  		c.Check(hctx.rebootPriority, gc.Equals, t.priority)
    98  	}
    99  }
   100  
   101  func (s *JujuRebootSuite) TestRebootInActions(c *gc.C) {
   102  	jujucCtx := &actionGetContext{}
   103  	com, err := jujuc.NewCommand(jujucCtx, cmdString("juju-reboot"))
   104  	c.Assert(err, jc.ErrorIsNil)
   105  	cmdCtx := testing.Context(c)
   106  	code := cmd.Main(com, cmdCtx, nil)
   107  	c.Check(code, gc.Equals, 1)
   108  	c.Assert(testing.Stderr(cmdCtx), gc.Equals, "error: juju-reboot is not supported when running an action.\n")
   109  }