github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/jujuc/testing/action.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  )
     9  
    10  // ActionHook holds the values for the hook context.
    11  type ActionHook struct {
    12  	ActionParams map[string]interface{}
    13  }
    14  
    15  // ContextActionHook is a test double for jujuc.ActionHookContext.
    16  type ContextActionHook struct {
    17  	contextBase
    18  	info *ActionHook
    19  }
    20  
    21  // ActionParams implements jujuc.ActionHookContext.
    22  func (c *ContextActionHook) ActionParams() (map[string]interface{}, error) {
    23  	c.stub.AddCall("ActionParams")
    24  	if err := c.stub.NextErr(); err != nil {
    25  		return nil, errors.Trace(err)
    26  	}
    27  
    28  	if c.info.ActionParams == nil {
    29  		return nil, errors.Errorf("not running an action")
    30  	}
    31  	return c.info.ActionParams, nil
    32  }
    33  
    34  // UpdateActionResults implements jujuc.ActionHookContext.
    35  func (c *ContextActionHook) UpdateActionResults(keys []string, value string) error {
    36  	c.stub.AddCall("UpdateActionResults", keys, value)
    37  	if err := c.stub.NextErr(); err != nil {
    38  		return errors.Trace(err)
    39  	}
    40  
    41  	if c.info.ActionParams == nil {
    42  		return errors.Errorf("not running an action")
    43  	}
    44  	return nil
    45  }
    46  
    47  // SetActionMessage implements jujuc.ActionHookContext.
    48  func (c *ContextActionHook) SetActionMessage(message string) error {
    49  	c.stub.AddCall("SetActionMessage", message)
    50  	if err := c.stub.NextErr(); err != nil {
    51  		return errors.Trace(err)
    52  	}
    53  
    54  	if c.info.ActionParams == nil {
    55  		return errors.Errorf("not running an action")
    56  	}
    57  	return nil
    58  }
    59  
    60  // SetActionFailed implements jujuc.ActionHookContext.
    61  func (c *ContextActionHook) SetActionFailed() error {
    62  	c.stub.AddCall("SetActionFailed")
    63  	if err := c.stub.NextErr(); err != nil {
    64  		return errors.Trace(err)
    65  	}
    66  
    67  	if c.info.ActionParams == nil {
    68  		return errors.Errorf("not running an action")
    69  	}
    70  	return nil
    71  }