github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/worker/uniter/runner/contextfactory_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package runner_test 5 6 import ( 7 "os" 8 "time" 9 10 "github.com/juju/errors" 11 "github.com/juju/names" 12 "github.com/juju/testing" 13 jc "github.com/juju/testing/checkers" 14 "github.com/juju/utils/fs" 15 gc "gopkg.in/check.v1" 16 "gopkg.in/juju/charm.v5/hooks" 17 18 "github.com/juju/juju/testcharms" 19 "github.com/juju/juju/worker/leadership" 20 "github.com/juju/juju/worker/uniter/hook" 21 "github.com/juju/juju/worker/uniter/runner" 22 ) 23 24 type ContextFactorySuite struct { 25 HookContextSuite 26 paths RealPaths 27 factory runner.ContextFactory 28 membership map[int][]string 29 } 30 31 var _ = gc.Suite(&ContextFactorySuite{}) 32 33 type fakeTracker struct { 34 leadership.Tracker 35 } 36 37 func (fakeTracker) ServiceName() string { 38 return "service-name" 39 } 40 41 func (s *ContextFactorySuite) SetUpTest(c *gc.C) { 42 s.HookContextSuite.SetUpTest(c) 43 s.paths = NewRealPaths(c) 44 s.membership = map[int][]string{} 45 46 contextFactory, err := runner.NewContextFactory( 47 s.uniter, 48 s.unit.Tag().(names.UnitTag), 49 fakeTracker{}, 50 s.getRelationInfos, 51 s.storage, 52 s.paths, 53 ) 54 c.Assert(err, jc.ErrorIsNil) 55 s.factory = contextFactory 56 } 57 58 func (s *ContextFactorySuite) SetCharm(c *gc.C, name string) { 59 err := os.RemoveAll(s.paths.charm) 60 c.Assert(err, jc.ErrorIsNil) 61 err = fs.Copy(testcharms.Repo.CharmDirPath(name), s.paths.charm) 62 c.Assert(err, jc.ErrorIsNil) 63 } 64 65 func (s *ContextFactorySuite) getRelationInfos() map[int]*runner.RelationInfo { 66 info := map[int]*runner.RelationInfo{} 67 for relId, relUnit := range s.apiRelunits { 68 info[relId] = &runner.RelationInfo{ 69 RelationUnit: relUnit, 70 MemberNames: s.membership[relId], 71 } 72 } 73 return info 74 } 75 76 func (s *ContextFactorySuite) testLeadershipContextWiring(c *gc.C, createContext func() runner.Context) { 77 var stub testing.Stub 78 stub.SetErrors(errors.New("bam")) 79 restore := runner.PatchNewLeadershipContext( 80 func(accessor runner.LeadershipSettingsAccessor, tracker leadership.Tracker) runner.LeadershipContext { 81 stub.AddCall("NewLeadershipContext", accessor, tracker) 82 return &StubLeadershipContext{Stub: &stub} 83 }, 84 ) 85 defer restore() 86 87 ctx := createContext() 88 isLeader, err := ctx.IsLeader() 89 c.Check(err, gc.ErrorMatches, "bam") 90 c.Check(isLeader, jc.IsFalse) 91 92 stub.CheckCalls(c, []testing.StubCall{{ 93 FuncName: "NewLeadershipContext", 94 Args: []interface{}{s.uniter.LeadershipSettings, fakeTracker{}}, 95 }, { 96 FuncName: "IsLeader", 97 }}) 98 99 } 100 101 func (s *ContextFactorySuite) TestNewHookRunnerLeadershipContext(c *gc.C) { 102 s.testLeadershipContextWiring(c, func() runner.Context { 103 ctx, err := s.factory.HookContext(hook.Info{Kind: hooks.ConfigChanged}) 104 c.Assert(err, jc.ErrorIsNil) 105 return ctx 106 }) 107 } 108 109 func (s *ContextFactorySuite) TestNewCommandRunnerLeadershipContext(c *gc.C) { 110 s.testLeadershipContextWiring(c, func() runner.Context { 111 ctx, err := s.factory.CommandContext(runner.CommandInfo{RelationId: -1}) 112 c.Assert(err, jc.ErrorIsNil) 113 return ctx 114 }) 115 } 116 117 func (s *ContextFactorySuite) TestNewActionRunnerLeadershipContext(c *gc.C) { 118 s.testLeadershipContextWiring(c, func() runner.Context { 119 s.SetCharm(c, "dummy") 120 action, err := s.State.EnqueueAction(s.unit.Tag(), "snapshot", nil) 121 c.Assert(err, jc.ErrorIsNil) 122 123 actionData := &runner.ActionData{ 124 Name: action.Name(), 125 Tag: names.NewActionTag(action.Id()), 126 Params: action.Parameters(), 127 ResultsMap: map[string]interface{}{}, 128 } 129 130 ctx, err := s.factory.ActionContext(actionData) 131 c.Assert(err, jc.ErrorIsNil) 132 return ctx 133 }) 134 } 135 136 func (s *ContextFactorySuite) TestRelationHookContext(c *gc.C) { 137 hi := hook.Info{ 138 Kind: hooks.RelationBroken, 139 RelationId: 1, 140 } 141 ctx, err := s.factory.HookContext(hi) 142 c.Assert(err, jc.ErrorIsNil) 143 s.AssertCoreContext(c, ctx) 144 s.AssertNotActionContext(c, ctx) 145 s.AssertRelationContext(c, ctx, 1, "") 146 s.AssertNotStorageContext(c, ctx) 147 } 148 149 func (s *ContextFactorySuite) TestMetricsHookContext(c *gc.C) { 150 s.SetCharm(c, "metered") 151 hi := hook.Info{Kind: hooks.CollectMetrics} 152 ctx, err := s.factory.HookContext(hi) 153 c.Assert(err, jc.ErrorIsNil) 154 155 err = ctx.AddMetric("pings", "1", time.Now()) 156 c.Assert(err, jc.ErrorIsNil) 157 158 s.AssertCoreContext(c, ctx) 159 s.AssertNotActionContext(c, ctx) 160 s.AssertNotRelationContext(c, ctx) 161 s.AssertNotStorageContext(c, ctx) 162 } 163 164 func (s *ContextFactorySuite) TestActionContext(c *gc.C) { 165 s.SetCharm(c, "dummy") 166 action, err := s.State.EnqueueAction(s.unit.Tag(), "snapshot", nil) 167 c.Assert(err, jc.ErrorIsNil) 168 169 actionData := &runner.ActionData{ 170 Name: action.Name(), 171 Tag: names.NewActionTag(action.Id()), 172 Params: action.Parameters(), 173 ResultsMap: map[string]interface{}{}, 174 } 175 176 ctx, err := s.factory.ActionContext(actionData) 177 c.Assert(err, jc.ErrorIsNil) 178 179 s.AssertCoreContext(c, ctx) 180 s.AssertActionContext(c, ctx) 181 s.AssertNotRelationContext(c, ctx) 182 s.AssertNotStorageContext(c, ctx) 183 } 184 185 func (s *ContextFactorySuite) TestCommandContext(c *gc.C) { 186 ctx, err := s.factory.CommandContext(runner.CommandInfo{RelationId: -1}) 187 c.Assert(err, jc.ErrorIsNil) 188 189 s.AssertCoreContext(c, ctx) 190 s.AssertNotActionContext(c, ctx) 191 s.AssertNotRelationContext(c, ctx) 192 s.AssertNotStorageContext(c, ctx) 193 } 194 195 type StubLeadershipContext struct { 196 runner.LeadershipContext 197 *testing.Stub 198 } 199 200 func (stub *StubLeadershipContext) IsLeader() (bool, error) { 201 stub.MethodCall(stub, "IsLeader") 202 return false, stub.NextErr() 203 }