github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/worker/uniter/operation/factory_test.go (about) 1 // Copyright 2014-2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package operation_test 5 6 import ( 7 "github.com/juju/testing" 8 jc "github.com/juju/testing/checkers" 9 utilexec "github.com/juju/utils/exec" 10 gc "gopkg.in/check.v1" 11 corecharm "gopkg.in/juju/charm.v5" 12 "gopkg.in/juju/charm.v5/hooks" 13 14 "github.com/juju/juju/worker/uniter/hook" 15 "github.com/juju/juju/worker/uniter/operation" 16 ) 17 18 type FactorySuite struct { 19 testing.IsolationSuite 20 factory operation.Factory 21 } 22 23 var _ = gc.Suite(&FactorySuite{}) 24 25 func (s *FactorySuite) SetUpTest(c *gc.C) { 26 s.IsolationSuite.SetUpTest(c) 27 // Yes, this factory will produce useless ops; this suite is just for 28 // verifying that inadequate args to the factory methods will produce 29 // the expected errors; and that the results of same get a string 30 // representation that does not depend on the factory attributes. 31 s.factory = operation.NewFactory(operation.FactoryParams{}) 32 } 33 34 func (s *FactorySuite) testNewDeployError(c *gc.C, newDeploy newDeploy) { 35 op, err := newDeploy(s.factory, nil) 36 c.Check(op, gc.IsNil) 37 c.Check(err, gc.ErrorMatches, "charm url required") 38 } 39 40 func (s *FactorySuite) TestNewInstallError(c *gc.C) { 41 s.testNewDeployError(c, (operation.Factory).NewInstall) 42 } 43 44 func (s *FactorySuite) TestNewUpgradeError(c *gc.C) { 45 s.testNewDeployError(c, (operation.Factory).NewUpgrade) 46 } 47 48 func (s *FactorySuite) TestNewRevertUpgradeError(c *gc.C) { 49 s.testNewDeployError(c, (operation.Factory).NewRevertUpgrade) 50 } 51 52 func (s *FactorySuite) TestNewResolvedUpgradeError(c *gc.C) { 53 s.testNewDeployError(c, (operation.Factory).NewResolvedUpgrade) 54 } 55 56 func (s *FactorySuite) testNewDeployString(c *gc.C, newDeploy newDeploy, expectPrefix string) { 57 op, err := newDeploy(s.factory, corecharm.MustParseURL("cs:quantal/wordpress-1")) 58 c.Check(err, jc.ErrorIsNil) 59 c.Check(op.String(), gc.Equals, expectPrefix+" cs:quantal/wordpress-1") 60 } 61 62 func (s *FactorySuite) TestNewInstallString(c *gc.C) { 63 s.testNewDeployString(c, (operation.Factory).NewInstall, "install") 64 } 65 66 func (s *FactorySuite) TestNewUpgradeString(c *gc.C) { 67 s.testNewDeployString(c, (operation.Factory).NewUpgrade, "upgrade to") 68 } 69 70 func (s *FactorySuite) TestNewRevertUpgradeString(c *gc.C) { 71 s.testNewDeployString(c, 72 (operation.Factory).NewRevertUpgrade, 73 "clear resolved flag and switch upgrade to", 74 ) 75 } 76 77 func (s *FactorySuite) TestNewResolvedUpgradeString(c *gc.C) { 78 s.testNewDeployString(c, 79 (operation.Factory).NewResolvedUpgrade, 80 "clear resolved flag and continue upgrade to", 81 ) 82 } 83 84 func (s *FactorySuite) TestNewActionError(c *gc.C) { 85 op, err := s.factory.NewAction("lol-something") 86 c.Check(op, gc.IsNil) 87 c.Check(err, gc.ErrorMatches, `invalid action id "lol-something"`) 88 } 89 90 func (s *FactorySuite) TestNewActionString(c *gc.C) { 91 op, err := s.factory.NewAction(someActionId) 92 c.Check(err, jc.ErrorIsNil) 93 c.Check(op.String(), gc.Equals, "run action "+someActionId) 94 } 95 96 func panicSendResponse(*utilexec.ExecResponse, error) { 97 panic("don't call this") 98 } 99 100 func commandArgs(commands string, relationId int, remoteUnit string) operation.CommandArgs { 101 return operation.CommandArgs{ 102 Commands: commands, 103 RelationId: relationId, 104 RemoteUnitName: remoteUnit, 105 } 106 } 107 108 func (s *FactorySuite) TestNewCommandsSendResponseError(c *gc.C) { 109 op, err := s.factory.NewCommands(commandArgs("anything", -1, ""), nil) 110 c.Check(op, gc.IsNil) 111 c.Check(err, gc.ErrorMatches, "response sender required") 112 } 113 114 func (s *FactorySuite) testNewCommandsArgsError( 115 c *gc.C, args operation.CommandArgs, expect string, 116 ) { 117 op, err := s.factory.NewCommands(args, panicSendResponse) 118 c.Check(op, gc.IsNil) 119 c.Check(err, gc.ErrorMatches, expect) 120 } 121 122 func (s *FactorySuite) TestNewCommandsArgsError_NoCommand(c *gc.C) { 123 s.testNewCommandsArgsError(c, 124 commandArgs("", -1, ""), 125 "commands required", 126 ) 127 } 128 129 func (s *FactorySuite) TestNewCommandsArgsError_BadRemoteUnit(c *gc.C) { 130 s.testNewCommandsArgsError(c, 131 commandArgs("any old thing", -1, "unit/1"), 132 "remote unit not valid without relation", 133 ) 134 } 135 136 func (s *FactorySuite) TestNewCommandsArgsError_BadRemoteUnitName(c *gc.C) { 137 s.testNewCommandsArgsError(c, 138 commandArgs("any old thing", 0, "lol"), 139 `invalid remote unit name "lol"`, 140 ) 141 } 142 143 func (s *FactorySuite) testNewCommandsString( 144 c *gc.C, args operation.CommandArgs, expect string, 145 ) { 146 op, err := s.factory.NewCommands(args, panicSendResponse) 147 c.Check(err, jc.ErrorIsNil) 148 c.Check(op.String(), gc.Equals, expect) 149 } 150 151 func (s *FactorySuite) TestNewCommandsString_CommandsOnly(c *gc.C) { 152 s.testNewCommandsString(c, 153 commandArgs("anything", -1, ""), 154 "run commands", 155 ) 156 } 157 158 func (s *FactorySuite) TestNewCommandsString_WithRelation(c *gc.C) { 159 s.testNewCommandsString(c, 160 commandArgs("anything", 0, ""), 161 "run commands (0)", 162 ) 163 } 164 165 func (s *FactorySuite) TestNewCommandsString_WithRelationAndUnit(c *gc.C) { 166 s.testNewCommandsString(c, 167 commandArgs("anything", 3, "unit/123"), 168 "run commands (3; unit/123)", 169 ) 170 } 171 172 func (s *FactorySuite) testNewHookError(c *gc.C, newHook newHook) { 173 op, err := newHook(s.factory, hook.Info{Kind: hooks.Kind("gibberish")}) 174 c.Check(op, gc.IsNil) 175 c.Check(err, gc.ErrorMatches, `unknown hook kind "gibberish"`) 176 } 177 178 func (s *FactorySuite) TestNewHookError_Run(c *gc.C) { 179 s.testNewHookError(c, (operation.Factory).NewRunHook) 180 } 181 182 func (s *FactorySuite) TestNewHookError_Retry(c *gc.C) { 183 s.testNewHookError(c, (operation.Factory).NewRetryHook) 184 } 185 186 func (s *FactorySuite) TestNewHookError_Skip(c *gc.C) { 187 s.testNewHookError(c, (operation.Factory).NewSkipHook) 188 } 189 190 func (s *FactorySuite) TestNewHookString_Run(c *gc.C) { 191 op, err := s.factory.NewRunHook(hook.Info{Kind: hooks.Install}) 192 c.Check(err, jc.ErrorIsNil) 193 c.Check(op.String(), gc.Equals, "run install hook") 194 } 195 196 func (s *FactorySuite) TestNewHookString_Retry(c *gc.C) { 197 op, err := s.factory.NewRetryHook(hook.Info{ 198 Kind: hooks.RelationBroken, 199 RelationId: 123, 200 }) 201 c.Check(err, jc.ErrorIsNil) 202 c.Check(op.String(), gc.Equals, "clear resolved flag and run relation-broken (123) hook") 203 } 204 205 func (s *FactorySuite) TestNewHookString_Skip(c *gc.C) { 206 op, err := s.factory.NewSkipHook(hook.Info{ 207 Kind: hooks.RelationJoined, 208 RemoteUnit: "foo/22", 209 RelationId: 123, 210 }) 211 c.Check(err, jc.ErrorIsNil) 212 c.Check(op.String(), gc.Equals, "clear resolved flag and skip run relation-joined (123; foo/22) hook") 213 } 214 215 func (s *FactorySuite) TestNewUpdateRelationsString(c *gc.C) { 216 op, err := s.factory.NewUpdateRelations([]int{1, 2, 3}) 217 c.Check(err, jc.ErrorIsNil) 218 c.Check(op.String(), gc.Equals, "update relations [1 2 3]") 219 } 220 221 func (s *FactorySuite) TestNewAcceptLeadershipString(c *gc.C) { 222 op, err := s.factory.NewAcceptLeadership() 223 c.Assert(err, jc.ErrorIsNil) 224 c.Assert(op.String(), gc.Equals, "accept leadership") 225 } 226 227 func (s *FactorySuite) TestNewResignLeadershipString(c *gc.C) { 228 op, err := s.factory.NewResignLeadership() 229 c.Assert(err, jc.ErrorIsNil) 230 c.Assert(op.String(), gc.Equals, "resign leadership") 231 }