github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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.v6-unstable" 12 "gopkg.in/juju/charm.v6-unstable/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 "switch upgrade to", 74 ) 75 } 76 77 func (s *FactorySuite) TestNewResolvedUpgradeString(c *gc.C) { 78 s.testNewDeployString(c, 79 (operation.Factory).NewResolvedUpgrade, 80 "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_Skip(c *gc.C) { 183 s.testNewHookError(c, (operation.Factory).NewSkipHook) 184 } 185 186 func (s *FactorySuite) TestNewHookString_Run(c *gc.C) { 187 op, err := s.factory.NewRunHook(hook.Info{Kind: hooks.Install}) 188 c.Check(err, jc.ErrorIsNil) 189 c.Check(op.String(), gc.Equals, "run install hook") 190 } 191 192 func (s *FactorySuite) TestNewHookString_Skip(c *gc.C) { 193 op, err := s.factory.NewSkipHook(hook.Info{ 194 Kind: hooks.RelationJoined, 195 RemoteUnit: "foo/22", 196 RelationId: 123, 197 }) 198 c.Check(err, jc.ErrorIsNil) 199 c.Check(op.String(), gc.Equals, "skip run relation-joined (123; foo/22) hook") 200 } 201 202 func (s *FactorySuite) TestNewAcceptLeadershipString(c *gc.C) { 203 op, err := s.factory.NewAcceptLeadership() 204 c.Assert(err, jc.ErrorIsNil) 205 c.Assert(op.String(), gc.Equals, "accept leadership") 206 } 207 208 func (s *FactorySuite) TestNewResignLeadershipString(c *gc.C) { 209 op, err := s.factory.NewResignLeadership() 210 c.Assert(err, jc.ErrorIsNil) 211 c.Assert(op.String(), gc.Equals, "resign leadership") 212 }