github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/jujuc/jujuctesting/relations.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/errors"
    10  	"github.com/juju/testing"
    11  
    12  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    13  )
    14  
    15  // Relations holds the values for the hook context.
    16  type Relations struct {
    17  	Relations map[int]jujuc.ContextRelation
    18  }
    19  
    20  // Reset clears the Relations data.
    21  func (r *Relations) Reset() {
    22  	r.Relations = nil
    23  }
    24  
    25  // SetRelation adds the relation to the set of known relations.
    26  func (r *Relations) SetRelation(id int, relCtx jujuc.ContextRelation) {
    27  	if r.Relations == nil {
    28  		r.Relations = make(map[int]jujuc.ContextRelation)
    29  	}
    30  	r.Relations[id] = relCtx
    31  }
    32  
    33  // SetNewRelation adds the relation to the set of known relations.
    34  func (r *Relations) SetNewRelation(id int, name string, stub *testing.Stub) *Relation {
    35  	if name == "" {
    36  		name = fmt.Sprintf("relation-%d", id)
    37  	}
    38  	rel := &Relation{
    39  		Id:   id,
    40  		Name: name,
    41  	}
    42  	relCtx := &ContextRelation{info: rel}
    43  	relCtx.stub = stub
    44  
    45  	r.SetRelation(id, relCtx)
    46  	return rel
    47  }
    48  
    49  // SetRelated adds the provided unit information to the relation.
    50  func (r *Relations) SetRelated(id int, unit string, settings Settings) {
    51  	relation := r.Relations[id].(*ContextRelation).info
    52  	relation.SetRelated(unit, settings)
    53  }
    54  
    55  // ContextRelations is a test double for jujuc.ContextRelations.
    56  type ContextRelations struct {
    57  	contextBase
    58  	info *Relations
    59  }
    60  
    61  // Relation implements jujuc.ContextRelations.
    62  func (c *ContextRelations) Relation(id int) (jujuc.ContextRelation, error) {
    63  	c.stub.AddCall("Relation", id)
    64  
    65  	r, ok := c.info.Relations[id]
    66  	var err error
    67  	if !ok {
    68  		err = errors.NotFoundf("relation")
    69  	}
    70  	return r, err
    71  }
    72  
    73  // RelationIds implements jujuc.ContextRelations.
    74  func (c *ContextRelations) RelationIds() ([]int, error) {
    75  	c.stub.AddCall("RelationIds")
    76  
    77  	ids := []int{}
    78  	for id := range c.info.Relations {
    79  		ids = append(ids, id)
    80  	}
    81  	return ids, c.stub.NextErr()
    82  }