github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/worker/uniter/operation/state_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package operation_test 5 6 import ( 7 "path/filepath" 8 9 jc "github.com/juju/testing/checkers" 10 "github.com/juju/utils" 11 gc "gopkg.in/check.v1" 12 "gopkg.in/juju/charm.v4" 13 "gopkg.in/juju/charm.v4/hooks" 14 15 "github.com/juju/juju/worker/uniter/hook" 16 "github.com/juju/juju/worker/uniter/operation" 17 ) 18 19 type StateFileSuite struct{} 20 21 var _ = gc.Suite(&StateFileSuite{}) 22 23 var stcurl = charm.MustParseURL("cs:quantal/service-name-123") 24 var relhook = &hook.Info{ 25 Kind: hooks.RelationJoined, 26 RemoteUnit: "some-thing/123", 27 } 28 29 var stateTests = []struct { 30 st operation.State 31 err string 32 }{ 33 // Invalid op/step. 34 { 35 st: operation.State{Kind: operation.Kind("bloviate")}, 36 err: `unknown operation "bloviate"`, 37 }, { 38 st: operation.State{ 39 Kind: operation.Continue, 40 Step: operation.Step("dudelike"), 41 Hook: &hook.Info{Kind: hooks.ConfigChanged}, 42 }, 43 err: `unknown operation step "dudelike"`, 44 }, 45 // Install operation. 46 { 47 st: operation.State{ 48 Kind: operation.Install, 49 Step: operation.Pending, 50 Hook: &hook.Info{Kind: hooks.ConfigChanged}, 51 }, 52 err: `unexpected hook info`, 53 }, { 54 st: operation.State{ 55 Kind: operation.Install, 56 Step: operation.Pending, 57 }, 58 err: `missing charm URL`, 59 }, { 60 st: operation.State{ 61 Kind: operation.Install, 62 Step: operation.Pending, 63 CharmURL: stcurl, 64 ActionId: &someActionId, 65 }, 66 err: `unexpected action id`, 67 }, { 68 st: operation.State{ 69 Kind: operation.Install, 70 Step: operation.Pending, 71 CharmURL: stcurl, 72 }, 73 }, 74 // RunAction operation. 75 { 76 st: operation.State{ 77 Kind: operation.RunAction, 78 Step: operation.Pending, 79 Hook: &hook.Info{Kind: hooks.Install}, 80 }, 81 err: `missing action id`, 82 }, { 83 st: operation.State{ 84 Kind: operation.RunAction, 85 Step: operation.Pending, 86 Hook: &hook.Info{Kind: hooks.Install}, 87 CharmURL: stcurl, 88 }, 89 err: `unexpected charm URL`, 90 }, { 91 st: operation.State{ 92 Kind: operation.RunAction, 93 Step: operation.Pending, 94 Hook: &hook.Info{Kind: hooks.Install}, 95 ActionId: &someActionId, 96 }, 97 }, 98 // RunHook operation. 99 { 100 st: operation.State{ 101 Kind: operation.RunHook, 102 Step: operation.Pending, 103 Hook: &hook.Info{Kind: hooks.Kind("machine-exploded")}, 104 }, 105 err: `unknown hook kind "machine-exploded"`, 106 }, { 107 st: operation.State{ 108 Kind: operation.RunHook, 109 Step: operation.Pending, 110 Hook: &hook.Info{Kind: hooks.RelationJoined}, 111 }, 112 err: `"relation-joined" hook requires a remote unit`, 113 }, { 114 st: operation.State{ 115 Kind: operation.RunHook, 116 Step: operation.Pending, 117 Hook: &hook.Info{Kind: hooks.ConfigChanged}, 118 ActionId: &someActionId, 119 }, 120 err: `unexpected action id`, 121 }, { 122 st: operation.State{ 123 Kind: operation.RunHook, 124 Step: operation.Pending, 125 Hook: &hook.Info{Kind: hooks.ConfigChanged}, 126 CharmURL: stcurl, 127 }, 128 err: `unexpected charm URL`, 129 }, { 130 st: operation.State{ 131 Kind: operation.RunHook, 132 Step: operation.Pending, 133 Hook: &hook.Info{Kind: hooks.ConfigChanged}, 134 }, 135 }, { 136 st: operation.State{ 137 Kind: operation.RunHook, 138 Step: operation.Pending, 139 Hook: relhook, 140 }, 141 }, { 142 st: operation.State{ 143 Kind: operation.RunHook, 144 Step: operation.Pending, 145 Hook: &hook.Info{ 146 Kind: hooks.Action, 147 ActionId: "feedface-dead-4567-beef-123456789012", 148 }, 149 }, 150 }, { 151 st: operation.State{ 152 Kind: operation.RunHook, 153 Step: operation.Pending, 154 Hook: &hook.Info{ 155 Kind: hooks.Action, 156 ActionId: "foo", 157 }, 158 }, 159 err: `action id "foo" cannot be parsed as an action tag`, 160 }, 161 // Upgrade operation. 162 { 163 st: operation.State{ 164 Kind: operation.Upgrade, 165 Step: operation.Pending, 166 }, 167 err: `missing charm URL`, 168 }, { 169 st: operation.State{ 170 Kind: operation.Upgrade, 171 Step: operation.Pending, 172 CharmURL: stcurl, 173 ActionId: &someActionId, 174 }, 175 err: `unexpected action id`, 176 }, { 177 st: operation.State{ 178 Kind: operation.Upgrade, 179 Step: operation.Pending, 180 CharmURL: stcurl, 181 }, 182 }, { 183 st: operation.State{ 184 Kind: operation.Upgrade, 185 Step: operation.Pending, 186 Hook: relhook, 187 CharmURL: stcurl, 188 }, 189 }, 190 // Continue operation. 191 { 192 st: operation.State{ 193 Kind: operation.Continue, 194 Step: operation.Pending, 195 }, 196 err: `missing hook info`, 197 }, { 198 st: operation.State{ 199 Kind: operation.Continue, 200 Step: operation.Pending, 201 Hook: relhook, 202 CharmURL: stcurl, 203 }, 204 err: `unexpected charm URL`, 205 }, { 206 st: operation.State{ 207 Kind: operation.Continue, 208 Step: operation.Pending, 209 Hook: relhook, 210 ActionId: &someActionId, 211 }, 212 err: `unexpected action id`, 213 }, { 214 st: operation.State{ 215 Kind: operation.Continue, 216 Step: operation.Pending, 217 Hook: relhook, 218 CollectMetricsTime: 98765432, 219 }, 220 }, 221 } 222 223 func (s *StateFileSuite) TestStates(c *gc.C) { 224 for i, t := range stateTests { 225 c.Logf("test %d", i) 226 path := filepath.Join(c.MkDir(), "uniter") 227 file := operation.NewStateFile(path) 228 _, err := file.Read() 229 c.Assert(err, gc.Equals, operation.ErrNoStateFile) 230 write := func() { 231 err := file.Write(&t.st) 232 c.Assert(err, jc.ErrorIsNil) 233 } 234 if t.err != "" { 235 c.Assert(write, gc.PanicMatches, "invalid operation state: "+t.err) 236 err := utils.WriteYaml(path, &t.st) 237 c.Assert(err, jc.ErrorIsNil) 238 _, err = file.Read() 239 c.Assert(err, gc.ErrorMatches, `cannot read ".*": invalid operation state: `+t.err) 240 continue 241 } 242 write() 243 st, err := file.Read() 244 c.Assert(err, jc.ErrorIsNil) 245 c.Assert(*st, gc.DeepEquals, t.st) 246 } 247 }