github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/uniter/runner/jujuc/jujuctesting/context.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuctesting
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/loggo"
    10  	"github.com/juju/testing"
    11  )
    12  
    13  // ContextInfo holds the values for the hook context.
    14  type ContextInfo struct {
    15  	Unit
    16  	UnitCharmState
    17  	Status
    18  	Instance
    19  	NetworkInterface
    20  	Leadership
    21  	Metrics
    22  	Storage
    23  	Relations
    24  	RelationHook
    25  	ActionHook
    26  	Version
    27  	WorkloadHook
    28  }
    29  
    30  // Context returns a Context that wraps the info.
    31  func (info *ContextInfo) Context(stub *testing.Stub) *Context {
    32  	return NewContext(stub, info)
    33  }
    34  
    35  // SetAsRelationHook updates the context to work as a relation hook context.
    36  func (info *ContextInfo) SetAsRelationHook(id int, remote string) {
    37  	relation, ok := info.Relations.Relations[id]
    38  	if !ok {
    39  		panic(fmt.Sprintf("relation #%d not added yet", id))
    40  	}
    41  	info.HookRelation = relation
    42  	info.RemoteUnitName = remote
    43  }
    44  
    45  // SetRemoteApplicationName defines the remote application
    46  func (info *ContextInfo) SetRemoteApplicationName(remote string) {
    47  	info.RemoteApplicationName = remote
    48  }
    49  
    50  // SetAsActionHook updates the context to work as an action hook context.
    51  func (info *ContextInfo) SetAsActionHook() {
    52  	panic("not supported yet")
    53  }
    54  
    55  type contextBase struct {
    56  	stub *testing.Stub
    57  }
    58  
    59  // Context is a test double for jujuc.Context.
    60  type Context struct {
    61  	ContextUnit
    62  	ContextUnitCharmState
    63  	ContextStatus
    64  	ContextInstance
    65  	ContextNetworking
    66  	ContextLeader
    67  	ContextMetrics
    68  	ContextStorage
    69  	ContextResources
    70  	ContextPayloads
    71  	ContextRelations
    72  	ContextRelationHook
    73  	ContextActionHook
    74  	ContextVersion
    75  	ContextWorkloadHook
    76  	ContextSecrets
    77  }
    78  
    79  // NewContext builds a jujuc.Context test double.
    80  func NewContext(stub *testing.Stub, info *ContextInfo) *Context {
    81  	var ctx Context
    82  	ctx.ContextUnit.stub = stub
    83  	ctx.ContextUnit.info = &info.Unit
    84  	ctx.ContextStatus.stub = stub
    85  	ctx.ContextStatus.info = &info.Status
    86  	ctx.ContextInstance.stub = stub
    87  	ctx.ContextInstance.info = &info.Instance
    88  	ctx.ContextNetworking.stub = stub
    89  	ctx.ContextNetworking.info = &info.NetworkInterface
    90  	ctx.ContextLeader.stub = stub
    91  	ctx.ContextLeader.info = &info.Leadership
    92  	ctx.ContextMetrics.stub = stub
    93  	ctx.ContextMetrics.info = &info.Metrics
    94  	ctx.ContextStorage.stub = stub
    95  	ctx.ContextStorage.info = &info.Storage
    96  	ctx.ContextResources.stub = stub
    97  	ctx.ContextPayloads.stub = stub
    98  	ctx.ContextRelations.stub = stub
    99  	ctx.ContextRelations.info = &info.Relations
   100  	ctx.ContextRelationHook.stub = stub
   101  	ctx.ContextRelationHook.info = &info.RelationHook
   102  	ctx.ContextActionHook.stub = stub
   103  	ctx.ContextActionHook.info = &info.ActionHook
   104  	ctx.ContextVersion.stub = stub
   105  	ctx.ContextVersion.info = &info.Version
   106  	ctx.ContextUnitCharmState.stub = stub
   107  	ctx.ContextUnitCharmState.info = &info.UnitCharmState
   108  	ctx.ContextWorkloadHook.stub = stub
   109  	ctx.ContextWorkloadHook.info = &info.WorkloadHook
   110  	ctx.ContextSecrets.stub = stub
   111  	return &ctx
   112  }
   113  
   114  func (c *Context) GetLogger(module string) loggo.Logger {
   115  	return loggo.GetLogger(module)
   116  }