github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/jujuc/testing/component.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  	"github.com/juju/errors"
     8  	"github.com/juju/testing"
     9  
    10  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    11  )
    12  
    13  // Components holds the values for the hook context.
    14  type Components struct {
    15  	Components map[string]jujuc.ContextComponent
    16  }
    17  
    18  // SetComponent sets the component on the registry.
    19  func (c *Components) SetComponent(name string, comp jujuc.ContextComponent) {
    20  	if c.Components == nil {
    21  		c.Components = make(map[string]jujuc.ContextComponent)
    22  	}
    23  	c.Components[name] = comp
    24  }
    25  
    26  // SetNewComponent sets the component on the registry.
    27  func (c *Components) SetNewComponent(name string, stub *testing.Stub) *Component {
    28  	info := &Component{
    29  		Name: name,
    30  	}
    31  
    32  	compCtx := NewContextComponent(stub, info)
    33  	c.SetComponent(name, compCtx)
    34  	return info
    35  }
    36  
    37  // ContextComponents is a test double for jujuc.ContextComponents.
    38  type ContextComponents struct {
    39  	contextBase
    40  	info *Components
    41  }
    42  
    43  // ContextComponents implements jujuc.ContextComponents.
    44  func (cc ContextComponents) Component(name string) (jujuc.ContextComponent, error) {
    45  	cc.stub.AddCall("Component", name)
    46  	if err := cc.stub.NextErr(); err != nil {
    47  		return nil, errors.Trace(err)
    48  	}
    49  
    50  	component, found := cc.info.Components[name]
    51  	if !found {
    52  		return nil, errors.NotFoundf("component %q", name)
    53  	}
    54  	return component, nil
    55  }
    56  
    57  // Component holds the values for the hook context.
    58  type Component struct {
    59  	Name string
    60  }
    61  
    62  // ContextComponent is a test double for jujuc.ContextComponent.
    63  type ContextComponent struct {
    64  	contextBase
    65  	info *Component
    66  }
    67  
    68  func NewContextComponent(stub *testing.Stub, info *Component) *ContextComponent {
    69  	compCtx := &ContextComponent{
    70  		info: info,
    71  	}
    72  	compCtx.stub = stub
    73  	return compCtx
    74  }
    75  
    76  // Get implements jujuc.ContextComponent.
    77  func (cc ContextComponent) Get(name string, result interface{}) error {
    78  	cc.stub.AddCall("Get", name, result)
    79  	if err := cc.stub.NextErr(); err != nil {
    80  		return errors.Trace(err)
    81  	}
    82  
    83  	return nil
    84  }
    85  
    86  // Set implements jujuc.ContextComponent.
    87  func (cc ContextComponent) Set(name string, value interface{}) error {
    88  	cc.stub.AddCall("Set", name, value)
    89  	if err := cc.stub.NextErr(); err != nil {
    90  		return errors.Trace(err)
    91  	}
    92  
    93  	return nil
    94  }
    95  
    96  // Flush implements jujuc.ContextComponent.
    97  func (cc ContextComponent) Flush() error {
    98  	cc.stub.AddCall("Flush")
    99  	if err := cc.stub.NextErr(); err != nil {
   100  		return errors.Trace(err)
   101  	}
   102  
   103  	return nil
   104  }