launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/worker/uniter/state_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package uniter_test 5 6 import ( 7 "path/filepath" 8 9 "launchpad.net/errgo/errors" 10 gc "launchpad.net/gocheck" 11 12 "launchpad.net/juju-core/charm" 13 "launchpad.net/juju-core/charm/hooks" 14 "launchpad.net/juju-core/testing/testbase" 15 "launchpad.net/juju-core/utils" 16 "launchpad.net/juju-core/worker/uniter" 17 "launchpad.net/juju-core/worker/uniter/hook" 18 ) 19 20 type StateFileSuite struct { 21 testbase.LoggingSuite 22 } 23 24 var _ = gc.Suite(&StateFileSuite{}) 25 26 var stcurl = charm.MustParseURL("cs:quantal/service-name-123") 27 var relhook = &hook.Info{ 28 Kind: hooks.RelationJoined, 29 RemoteUnit: "some-thing/123", 30 } 31 32 var stateTests = []struct { 33 st uniter.State 34 err string 35 }{ 36 // Invalid op/step. 37 { 38 st: uniter.State{Op: uniter.Op("bloviate")}, 39 err: `unknown operation "bloviate"`, 40 }, { 41 st: uniter.State{ 42 Op: uniter.Continue, 43 OpStep: uniter.OpStep("dudelike"), 44 Hook: &hook.Info{Kind: hooks.ConfigChanged}, 45 }, 46 err: `unknown operation step "dudelike"`, 47 }, 48 // Install operation. 49 { 50 st: uniter.State{ 51 Op: uniter.Install, 52 OpStep: uniter.Pending, 53 Hook: &hook.Info{Kind: hooks.ConfigChanged}, 54 }, 55 err: `unexpected hook info`, 56 }, { 57 st: uniter.State{ 58 Op: uniter.Install, 59 OpStep: uniter.Pending, 60 }, 61 err: `missing charm URL`, 62 }, { 63 st: uniter.State{ 64 Op: uniter.Install, 65 OpStep: uniter.Pending, 66 CharmURL: stcurl, 67 }, 68 }, 69 // RunHook operation. 70 { 71 st: uniter.State{ 72 Op: uniter.RunHook, 73 OpStep: uniter.Pending, 74 Hook: &hook.Info{Kind: hooks.Kind("machine-exploded")}, 75 }, 76 err: `unknown hook kind "machine-exploded"`, 77 }, { 78 st: uniter.State{ 79 Op: uniter.RunHook, 80 OpStep: uniter.Pending, 81 Hook: &hook.Info{Kind: hooks.RelationJoined}, 82 }, 83 err: `"relation-joined" hook requires a remote unit`, 84 }, { 85 st: uniter.State{ 86 Op: uniter.RunHook, 87 OpStep: uniter.Pending, 88 Hook: &hook.Info{Kind: hooks.ConfigChanged}, 89 CharmURL: stcurl, 90 }, 91 err: `unexpected charm URL`, 92 }, { 93 st: uniter.State{ 94 Op: uniter.RunHook, 95 OpStep: uniter.Pending, 96 Hook: &hook.Info{Kind: hooks.ConfigChanged}, 97 }, 98 }, { 99 st: uniter.State{ 100 Op: uniter.RunHook, 101 OpStep: uniter.Pending, 102 Hook: relhook, 103 }, 104 }, 105 // Upgrade operation. 106 { 107 st: uniter.State{ 108 Op: uniter.Upgrade, 109 OpStep: uniter.Pending, 110 }, 111 err: `missing charm URL`, 112 }, { 113 st: uniter.State{ 114 Op: uniter.Upgrade, 115 OpStep: uniter.Pending, 116 CharmURL: stcurl, 117 }, 118 }, { 119 st: uniter.State{ 120 Op: uniter.Upgrade, 121 OpStep: uniter.Pending, 122 Hook: relhook, 123 CharmURL: stcurl, 124 }, 125 }, 126 // Continue operation. 127 { 128 st: uniter.State{ 129 Op: uniter.Continue, 130 OpStep: uniter.Pending, 131 }, 132 err: `missing hook info`, 133 }, { 134 st: uniter.State{ 135 Op: uniter.Continue, 136 OpStep: uniter.Pending, 137 Hook: relhook, 138 CharmURL: stcurl, 139 }, 140 err: `unexpected charm URL`, 141 }, { 142 st: uniter.State{ 143 Op: uniter.Continue, 144 OpStep: uniter.Pending, 145 Hook: relhook, 146 }, 147 }, 148 } 149 150 func (s *StateFileSuite) TestStates(c *gc.C) { 151 for i, t := range stateTests { 152 c.Logf("test %d", i) 153 path := filepath.Join(c.MkDir(), "uniter") 154 file := uniter.NewStateFile(path) 155 _, err := file.Read() 156 c.Assert(errors.Cause(err), gc.Equals, uniter.ErrNoStateFile) 157 write := func() { 158 err := file.Write(t.st.Started, t.st.Op, t.st.OpStep, t.st.Hook, t.st.CharmURL) 159 c.Assert(err, gc.IsNil) 160 } 161 if t.err != "" { 162 c.Assert(write, gc.PanicMatches, "invalid uniter state: "+t.err) 163 err := utils.WriteYaml(path, &t.st) 164 c.Assert(err, gc.IsNil) 165 _, err = file.Read() 166 c.Assert(err, gc.ErrorMatches, "cannot read charm state at .*: invalid uniter state: "+t.err) 167 continue 168 } 169 write() 170 st, err := file.Read() 171 c.Assert(err, gc.IsNil) 172 c.Assert(*st, gc.DeepEquals, t.st) 173 } 174 }