github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/uniter/runner/jujuc/jujuctesting/relationhook.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  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    10  )
    11  
    12  // RelationHook holds the values for the hook context.
    13  type RelationHook struct {
    14  	HookRelation          jujuc.ContextRelation
    15  	RemoteUnitName        string
    16  	RemoteApplicationName string
    17  }
    18  
    19  // Reset clears the RelationHook's data.
    20  func (rh *RelationHook) Reset() {
    21  	rh.HookRelation = nil
    22  	rh.RemoteUnitName = ""
    23  	rh.RemoteApplicationName = ""
    24  }
    25  
    26  // ContextRelationHook is a test double for jujuc.RelationHookContext.
    27  type ContextRelationHook struct {
    28  	contextBase
    29  	info *RelationHook
    30  }
    31  
    32  // HookRelation implements jujuc.RelationHookContext.
    33  func (c *ContextRelationHook) HookRelation() (jujuc.ContextRelation, error) {
    34  	c.stub.AddCall("HookRelation")
    35  	var err error
    36  	if c.info.HookRelation == nil {
    37  		err = errors.NotFoundf("hook relation")
    38  	}
    39  
    40  	return c.info.HookRelation, err
    41  }
    42  
    43  // RemoteUnitName implements jujuc.RelationHookContext.
    44  func (c *ContextRelationHook) RemoteUnitName() (string, error) {
    45  	c.stub.AddCall("RemoteUnitName")
    46  	_ = c.stub.NextErr()
    47  	var err error
    48  	if c.info.RemoteUnitName == "" {
    49  		err = errors.NotFoundf("remote unit")
    50  	}
    51  
    52  	return c.info.RemoteUnitName, err
    53  }
    54  
    55  // RemoteApplicationName implements jujuc.RelationHookContext.
    56  func (c *ContextRelationHook) RemoteApplicationName() (string, error) {
    57  	c.stub.AddCall("RemoteApplicationName")
    58  	_ = c.stub.NextErr()
    59  	var err error
    60  	if c.info.RemoteApplicationName == "" {
    61  		err = errors.NotFoundf("saas application")
    62  	}
    63  
    64  	return c.info.RemoteApplicationName, err
    65  }