github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/api/uniter/export_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package uniter
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"gopkg.in/juju/names.v2"
    10  
    11  	"github.com/juju/juju/api/base/testing"
    12  	"github.com/juju/juju/apiserver/params"
    13  )
    14  
    15  var (
    16  	NewSettings = newSettings
    17  )
    18  
    19  // PatchUnitResponse changes the internal FacadeCaller to one that lets you return
    20  // canned results. The responseFunc will get the 'response' interface object,
    21  // and can set attributes of it to fix the response to the caller.
    22  // It can also return an error to have the FacadeCall return an error. The expected
    23  // request is specified using the expectedRequest parameter. If the request name does
    24  // not match, the function panics.
    25  // The function returned by PatchResponses is a cleanup function that returns
    26  // the client to its original state.
    27  func PatchUnitResponse(p testing.Patcher, u *Unit, expectedRequest string, responseFunc func(interface{}) error) {
    28  	testing.PatchFacadeCall(p, &u.st.facade, func(request string, params, response interface{}) error {
    29  		if request != expectedRequest {
    30  			panic(fmt.Errorf("unexpected request %q received - expecting %q", request, expectedRequest))
    31  		}
    32  		return responseFunc(response)
    33  	})
    34  }
    35  
    36  // PatchUnitFacadeCall changes the internal FacadeCaller to one that calls the provided request handler function.
    37  func PatchUnitFacadeCall(p testing.Patcher, u *Unit, respFunc func(request string, params, response interface{}) error) {
    38  	testing.PatchFacadeCall(p, &u.st.facade, respFunc)
    39  }
    40  
    41  // CreateUnit creates uniter.Unit for tests.
    42  func CreateUnit(st *State, tag names.UnitTag) *Unit {
    43  	return &Unit{st, tag, params.Alive}
    44  }