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