github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/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.v5" 13 "gopkg.in/juju/charm.v5/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 }, 42 err: `unknown operation step "dudelike"`, 43 }, 44 // Install operation. 45 { 46 st: operation.State{ 47 Kind: operation.Install, 48 Step: operation.Pending, 49 Hook: &hook.Info{Kind: hooks.ConfigChanged}, 50 }, 51 err: `unexpected hook info with Kind Install`, 52 }, { 53 st: operation.State{ 54 Kind: operation.Install, 55 Step: operation.Pending, 56 }, 57 err: `missing charm URL`, 58 }, { 59 st: operation.State{ 60 Kind: operation.Install, 61 Step: operation.Pending, 62 CharmURL: stcurl, 63 ActionId: &someActionId, 64 }, 65 err: `unexpected action id`, 66 }, { 67 st: operation.State{ 68 Kind: operation.Install, 69 Step: operation.Pending, 70 CharmURL: stcurl, 71 }, 72 }, 73 // RunAction operation. 74 { 75 st: operation.State{ 76 Kind: operation.RunAction, 77 Step: operation.Pending, 78 }, 79 err: `missing action id`, 80 }, { 81 st: operation.State{ 82 Kind: operation.RunAction, 83 Step: operation.Pending, 84 ActionId: &someActionId, 85 CharmURL: stcurl, 86 }, 87 err: `unexpected charm URL`, 88 }, { 89 st: operation.State{ 90 Kind: operation.RunAction, 91 Step: operation.Pending, 92 ActionId: &someActionId, 93 }, 94 }, 95 // RunHook operation. 96 { 97 st: operation.State{ 98 Kind: operation.RunHook, 99 Step: operation.Pending, 100 Hook: &hook.Info{Kind: hooks.Kind("machine-exploded")}, 101 }, 102 err: `unknown hook kind "machine-exploded"`, 103 }, { 104 st: operation.State{ 105 Kind: operation.RunHook, 106 Step: operation.Pending, 107 Hook: &hook.Info{Kind: hooks.RelationJoined}, 108 }, 109 err: `"relation-joined" hook requires a remote unit`, 110 }, { 111 st: operation.State{ 112 Kind: operation.RunHook, 113 Step: operation.Pending, 114 Hook: &hook.Info{Kind: hooks.ConfigChanged}, 115 ActionId: &someActionId, 116 }, 117 err: `unexpected action id`, 118 }, { 119 st: operation.State{ 120 Kind: operation.RunHook, 121 Step: operation.Pending, 122 Hook: &hook.Info{Kind: hooks.ConfigChanged}, 123 CharmURL: stcurl, 124 }, 125 err: `unexpected charm URL`, 126 }, { 127 st: operation.State{ 128 Kind: operation.RunHook, 129 Step: operation.Pending, 130 Hook: &hook.Info{Kind: hooks.ConfigChanged}, 131 }, 132 }, { 133 st: operation.State{ 134 Kind: operation.RunHook, 135 Step: operation.Pending, 136 Hook: relhook, 137 }, 138 }, 139 // Upgrade operation. 140 { 141 st: operation.State{ 142 Kind: operation.Upgrade, 143 Step: operation.Pending, 144 }, 145 err: `missing charm URL`, 146 }, { 147 st: operation.State{ 148 Kind: operation.Upgrade, 149 Step: operation.Pending, 150 CharmURL: stcurl, 151 ActionId: &someActionId, 152 }, 153 err: `unexpected action id`, 154 }, { 155 st: operation.State{ 156 Kind: operation.Upgrade, 157 Step: operation.Pending, 158 CharmURL: stcurl, 159 }, 160 }, { 161 st: operation.State{ 162 Kind: operation.Upgrade, 163 Step: operation.Pending, 164 Hook: relhook, 165 CharmURL: stcurl, 166 }, 167 }, 168 // Continue operation. 169 { 170 st: operation.State{ 171 Kind: operation.Continue, 172 Step: operation.Pending, 173 CharmURL: stcurl, 174 }, 175 err: `unexpected charm URL`, 176 }, { 177 st: operation.State{ 178 Kind: operation.Continue, 179 Step: operation.Pending, 180 ActionId: &someActionId, 181 }, 182 err: `unexpected action id`, 183 }, { 184 st: operation.State{ 185 Kind: operation.Continue, 186 Step: operation.Pending, 187 CollectMetricsTime: 98765432, 188 Leader: true, 189 }, 190 }, 191 } 192 193 func (s *StateFileSuite) TestStates(c *gc.C) { 194 for i, t := range stateTests { 195 c.Logf("test %d", i) 196 path := filepath.Join(c.MkDir(), "uniter") 197 file := operation.NewStateFile(path) 198 _, err := file.Read() 199 c.Assert(err, gc.Equals, operation.ErrNoStateFile) 200 write := func() { 201 err := file.Write(&t.st) 202 c.Assert(err, jc.ErrorIsNil) 203 } 204 if t.err != "" { 205 c.Assert(write, gc.PanicMatches, "invalid operation state: "+t.err) 206 err := utils.WriteYaml(path, &t.st) 207 c.Assert(err, jc.ErrorIsNil) 208 _, err = file.Read() 209 c.Assert(err, gc.ErrorMatches, `cannot read ".*": invalid operation state: `+t.err) 210 continue 211 } 212 write() 213 st, err := file.Read() 214 c.Assert(err, jc.ErrorIsNil) 215 c.Assert(st, jc.DeepEquals, &t.st) 216 } 217 }