github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/uniter/runner/jujuc/testing/context.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package testing 5 6 import ( 7 "fmt" 8 9 "github.com/juju/testing" 10 ) 11 12 // ContextInfo holds the values for the hook context. 13 type ContextInfo struct { 14 Unit 15 Status 16 Instance 17 NetworkInterface 18 Leadership 19 Metrics 20 Storage 21 Components 22 Relations 23 RelationHook 24 ActionHook 25 } 26 27 // Context returns a Context that wraps the info. 28 func (info *ContextInfo) Context(stub *testing.Stub) *Context { 29 return NewContext(stub, info) 30 } 31 32 // SetAsRelationHook updates the context to work as a relation hook context. 33 func (info *ContextInfo) SetAsRelationHook(id int, remote string) { 34 relation, ok := info.Relations.Relations[id] 35 if !ok { 36 panic(fmt.Sprintf("relation #%d not added yet", id)) 37 } 38 info.HookRelation = relation 39 info.RemoteUnitName = remote 40 } 41 42 // SetAsActionHook updates the context to work as an action hook context. 43 func (info *ContextInfo) SetAsActionHook() { 44 panic("not supported yet") 45 } 46 47 type contextBase struct { 48 stub *testing.Stub 49 } 50 51 // Context is a test double for jujuc.Context. 52 type Context struct { 53 ContextUnit 54 ContextStatus 55 ContextInstance 56 ContextNetworking 57 ContextLeader 58 ContextMetrics 59 ContextStorage 60 ContextComponents 61 ContextRelations 62 ContextRelationHook 63 ContextActionHook 64 } 65 66 // NewContext builds a jujuc.Context test double. 67 func NewContext(stub *testing.Stub, info *ContextInfo) *Context { 68 var ctx Context 69 ctx.ContextUnit.stub = stub 70 ctx.ContextUnit.info = &info.Unit 71 ctx.ContextStatus.stub = stub 72 ctx.ContextStatus.info = &info.Status 73 ctx.ContextInstance.stub = stub 74 ctx.ContextInstance.info = &info.Instance 75 ctx.ContextNetworking.stub = stub 76 ctx.ContextNetworking.info = &info.NetworkInterface 77 ctx.ContextLeader.stub = stub 78 ctx.ContextLeader.info = &info.Leadership 79 ctx.ContextMetrics.stub = stub 80 ctx.ContextMetrics.info = &info.Metrics 81 ctx.ContextStorage.stub = stub 82 ctx.ContextStorage.info = &info.Storage 83 ctx.ContextComponents.stub = stub 84 ctx.ContextComponents.info = &info.Components 85 ctx.ContextRelations.stub = stub 86 ctx.ContextRelations.info = &info.Relations 87 ctx.ContextRelationHook.stub = stub 88 ctx.ContextRelationHook.info = &info.RelationHook 89 ctx.ContextActionHook.stub = stub 90 ctx.ContextActionHook.info = &info.ActionHook 91 return &ctx 92 }