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