github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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  	Version
    26  }
    27  
    28  // Context returns a Context that wraps the info.
    29  func (info *ContextInfo) Context(stub *testing.Stub) *Context {
    30  	return NewContext(stub, info)
    31  }
    32  
    33  // SetAsRelationHook updates the context to work as a relation hook context.
    34  func (info *ContextInfo) SetAsRelationHook(id int, remote string) {
    35  	relation, ok := info.Relations.Relations[id]
    36  	if !ok {
    37  		panic(fmt.Sprintf("relation #%d not added yet", id))
    38  	}
    39  	info.HookRelation = relation
    40  	info.RemoteUnitName = remote
    41  }
    42  
    43  // SetAsActionHook updates the context to work as an action hook context.
    44  func (info *ContextInfo) SetAsActionHook() {
    45  	panic("not supported yet")
    46  }
    47  
    48  type contextBase struct {
    49  	stub *testing.Stub
    50  }
    51  
    52  // Context is a test double for jujuc.Context.
    53  type Context struct {
    54  	ContextUnit
    55  	ContextStatus
    56  	ContextInstance
    57  	ContextNetworking
    58  	ContextLeader
    59  	ContextMetrics
    60  	ContextStorage
    61  	ContextComponents
    62  	ContextRelations
    63  	ContextRelationHook
    64  	ContextActionHook
    65  	ContextVersion
    66  }
    67  
    68  // NewContext builds a jujuc.Context test double.
    69  func NewContext(stub *testing.Stub, info *ContextInfo) *Context {
    70  	var ctx Context
    71  	ctx.ContextUnit.stub = stub
    72  	ctx.ContextUnit.info = &info.Unit
    73  	ctx.ContextStatus.stub = stub
    74  	ctx.ContextStatus.info = &info.Status
    75  	ctx.ContextInstance.stub = stub
    76  	ctx.ContextInstance.info = &info.Instance
    77  	ctx.ContextNetworking.stub = stub
    78  	ctx.ContextNetworking.info = &info.NetworkInterface
    79  	ctx.ContextLeader.stub = stub
    80  	ctx.ContextLeader.info = &info.Leadership
    81  	ctx.ContextMetrics.stub = stub
    82  	ctx.ContextMetrics.info = &info.Metrics
    83  	ctx.ContextStorage.stub = stub
    84  	ctx.ContextStorage.info = &info.Storage
    85  	ctx.ContextComponents.stub = stub
    86  	ctx.ContextComponents.info = &info.Components
    87  	ctx.ContextRelations.stub = stub
    88  	ctx.ContextRelations.info = &info.Relations
    89  	ctx.ContextRelationHook.stub = stub
    90  	ctx.ContextRelationHook.info = &info.RelationHook
    91  	ctx.ContextActionHook.stub = stub
    92  	ctx.ContextActionHook.info = &info.ActionHook
    93  	ctx.ContextVersion.stub = stub
    94  	ctx.ContextVersion.info = &info.Version
    95  	return &ctx
    96  }