1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
7
"github.com/juju/errors"
8
"github.com/juju/testing"
9
jc "github.com/juju/testing/checkers"
10
gc "gopkg.in/check.v1"
11
"gopkg.in/juju/charm.v6-unstable/hooks"
13
"github.com/juju/juju/worker/uniter/hook"
14
"github.com/juju/juju/worker/uniter/operation"
17
type FailActionSuite struct {
18
testing.IsolationSuite
21
var _ = gc.Suite(&FailActionSuite{})
23
func (s *FailActionSuite) TestPrepare(c *gc.C) {
24
factory := operation.NewFactory(operation.FactoryParams{})
25
op, err := factory.NewFailAction(someActionId)
26
c.Assert(err, jc.ErrorIsNil)
28
newState, err := op.Prepare(operation.State{})
29
c.Assert(err, jc.ErrorIsNil)
30
c.Assert(newState, jc.DeepEquals, &operation.State{
31
Kind: operation.RunAction,
32
Step: operation.Pending,
33
ActionId: &someActionId,
37
func (s *FailActionSuite) TestExecuteSuccess(c *gc.C) {
38
var stateChangeTests = []struct {
40
before operation.State
43
description: "empty state",
44
after: operation.State{
45
Kind: operation.RunAction,
47
ActionId: &someActionId,
50
description: "preserves appropriate fields",
51
before: overwriteState,
52
after: operation.State{
53
Kind: operation.RunAction,
55
ActionId: &someActionId,
56
Hook: &hook.Info{Kind: hooks.Install},
61
for i, test := range stateChangeTests {
62
c.Logf("test %d: %s", i, test.description)
63
callbacks := &RunActionCallbacks{MockFailAction: &MockFailAction{}}
64
factory := operation.NewFactory(operation.FactoryParams{
67
op, err := factory.NewFailAction(someActionId)
68
c.Assert(err, jc.ErrorIsNil)
69
midState, err := op.Prepare(test.before)
70
c.Assert(midState, gc.NotNil)
71
c.Assert(err, jc.ErrorIsNil)
73
newState, err := op.Execute(*midState)
74
c.Assert(err, jc.ErrorIsNil)
75
c.Assert(newState, jc.DeepEquals, &test.after)
76
c.Assert(*callbacks.MockFailAction.gotMessage, gc.Equals, "action terminated")
77
c.Assert(*callbacks.MockFailAction.gotActionId, gc.Equals, someActionId)
81
func (s *FailActionSuite) TestExecuteFail(c *gc.C) {
82
st := operation.State{
83
Kind: operation.RunAction,
85
ActionId: &someActionId,
87
callbacks := &RunActionCallbacks{MockFailAction: &MockFailAction{err: errors.New("squelch")}}
88
factory := operation.NewFactory(operation.FactoryParams{
91
op, err := factory.NewFailAction(someActionId)
92
c.Assert(err, jc.ErrorIsNil)
93
midState, err := op.Prepare(st)
94
c.Assert(midState, gc.NotNil)
95
c.Assert(err, jc.ErrorIsNil)
97
_, err = op.Execute(*midState)
98
c.Assert(err, gc.ErrorMatches, "squelch")
101
func (s *FailActionSuite) TestCommit(c *gc.C) {
102
var stateChangeTests = []struct {
104
before operation.State
105
after operation.State
107
description: "empty state",
108
after: operation.State{
109
Kind: operation.Continue,
110
Step: operation.Pending,
113
description: "preserves only appropriate fields, no hook",
114
before: operation.State{
115
Kind: operation.Continue,
116
Step: operation.Pending,
118
CharmURL: curl("cs:quantal/wordpress-2"),
119
ActionId: &randomActionId,
121
after: operation.State{
122
Kind: operation.Continue,
123
Step: operation.Pending,
127
description: "preserves only appropriate fields, with hook",
128
before: operation.State{
129
Kind: operation.Continue,
130
Step: operation.Pending,
132
CharmURL: curl("cs:quantal/wordpress-2"),
133
ActionId: &randomActionId,
134
Hook: &hook.Info{Kind: hooks.Install},
136
after: operation.State{
137
Kind: operation.RunHook,
138
Step: operation.Pending,
139
Hook: &hook.Info{Kind: hooks.Install},
144
for i, test := range stateChangeTests {
145
c.Logf("test %d: %s", i, test.description)
146
factory := operation.NewFactory(operation.FactoryParams{})
147
op, err := factory.NewFailAction(someActionId)
148
c.Assert(err, jc.ErrorIsNil)
150
newState, err := op.Commit(test.before)
151
c.Assert(newState, jc.DeepEquals, &test.after)
155
func (s *FailActionSuite) TestNeedsGlobalMachineLock(c *gc.C) {
156
factory := operation.NewFactory(operation.FactoryParams{})
157
op, err := factory.NewFailAction(someActionId)
158
c.Assert(err, jc.ErrorIsNil)
159
c.Assert(op.NeedsGlobalMachineLock(), jc.IsTrue)