1
// Copyright 2014 Canonical Ltd.
2
// Copyright 2014 Cloudbase Solutions SRL
3
// Licensed under the AGPLv3, see LICENCE file for details.
9
jc "github.com/juju/testing/checkers"
10
gc "gopkg.in/check.v1"
11
"launchpad.net/gnuflag"
13
"github.com/juju/juju/testing"
14
"github.com/juju/juju/worker/uniter/runner/jujuc"
17
type JujuRebootSuite struct {
21
var _ = gc.Suite(&JujuRebootSuite{})
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{})
29
func (s *JujuRebootSuite) TestInfo(c *gc.C) {
30
rebootCmd, err := jujuc.NewJujuRebootCommand(nil)
31
c.Assert(err, jc.ErrorIsNil)
32
cmdInfo := rebootCmd.Info()
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")
39
func (s *JujuRebootSuite) TestSetFlags(c *gc.C) {
40
rebootCmd := jujuc.JujuRebootCommand{Now: true}
41
fs := &gnuflag.FlagSet{}
43
rebootCmd.SetFlags(fs)
45
flag := fs.Lookup("now")
46
c.Assert(flag, gc.NotNil)
49
func (s *JujuRebootSuite) TestJujuRebootCommand(c *gc.C) {
50
var jujuRebootTests = []struct {
55
priority jujuc.RebootPriority
57
summary: "test reboot priority defaulting to RebootAfterHook",
58
hctx: &Context{shouldError: false, rebootPriority: jujuc.RebootSkip},
61
priority: jujuc.RebootAfterHook,
63
summary: "test reboot priority being set to RebootNow",
64
hctx: &Context{shouldError: false, rebootPriority: jujuc.RebootSkip},
65
args: []string{"--now"},
67
priority: jujuc.RebootNow,
69
summary: "test a failed running of juju-reboot",
70
hctx: &Context{shouldError: true, rebootPriority: jujuc.RebootSkip},
73
priority: jujuc.RebootAfterHook,
75
summary: "test a failed running with parameter provided",
76
hctx: &Context{shouldError: true, rebootPriority: jujuc.RebootSkip},
77
args: []string{"--now"},
79
priority: jujuc.RebootNow,
81
summary: "test invalid args provided",
82
hctx: &Context{shouldError: false, rebootPriority: jujuc.RebootSkip},
83
args: []string{"--way", "--too", "--many", "--args"},
85
priority: jujuc.RebootSkip,
88
for i, t := range jujuRebootTests {
89
c.Logf("Test %d: %s", i, t.summary)
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 := testing.Context(c)
97
code := cmd.Main(com, ctx, t.args)
98
c.Check(code, gc.Equals, t.code)
99
c.Check(hctx.rebootPriority, gc.Equals, t.priority)
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 := testing.Context(c)
108
code := cmd.Main(com, cmdCtx, nil)
109
c.Check(code, gc.Equals, 1)
110
c.Assert(testing.Stderr(cmdCtx), gc.Equals, "error: juju-reboot is not supported when running an action.\n")