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