github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/api/export_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package api
     5  
     6  import (
     7  	"github.com/juju/juju/api/base"
     8  	"github.com/juju/juju/network"
     9  )
    10  
    11  var (
    12  	CertDir               = &certDir
    13  	NewWebsocketDialer    = newWebsocketDialer
    14  	NewWebsocketDialerPtr = &newWebsocketDialer
    15  	WebsocketDialConfig   = &websocketDialConfig
    16  	SlideAddressToFront   = slideAddressToFront
    17  	BestVersion           = bestVersion
    18  	FacadeVersions        = &facadeVersions
    19  	NewHTTPClient         = &newHTTPClient
    20  )
    21  
    22  // SetServerAddress allows changing the URL to the internal API server
    23  // that AddLocalCharm uses in order to test NotImplementedError.
    24  func SetServerAddress(c *Client, scheme, addr string) {
    25  	c.st.serverScheme = scheme
    26  	c.st.addr = addr
    27  }
    28  
    29  // ServerRoot is exported so that we can test the built URL.
    30  func ServerRoot(c *Client) string {
    31  	return c.st.serverRoot()
    32  }
    33  
    34  // PatchEnvironTag patches the value of the environment tag.
    35  // It returns a function that reverts the change.
    36  func PatchEnvironTag(st *State, envTag string) func() {
    37  	originalTag := st.environTag
    38  	st.environTag = envTag
    39  	return func() {
    40  		st.environTag = originalTag
    41  	}
    42  }
    43  
    44  // TestingStateParams is the parameters for NewTestingState, so that you can
    45  // only set the bits that you acutally want to test.
    46  type TestingStateParams struct {
    47  	Address        string
    48  	EnvironTag     string
    49  	APIHostPorts   [][]network.HostPort
    50  	FacadeVersions map[string][]int
    51  	ServerScheme   string
    52  	ServerRoot     string
    53  }
    54  
    55  // NewTestingState creates an api.State object that can be used for testing. It
    56  // isn't backed onto an actual API server, so actual RPC methods can't be
    57  // called on it. But it can be used for testing general behavior.
    58  func NewTestingState(params TestingStateParams) *State {
    59  	st := &State{
    60  		addr:              params.Address,
    61  		environTag:        params.EnvironTag,
    62  		hostPorts:         params.APIHostPorts,
    63  		facadeVersions:    params.FacadeVersions,
    64  		serverScheme:      params.ServerScheme,
    65  		serverRootAddress: params.ServerRoot,
    66  	}
    67  	return st
    68  }
    69  
    70  // PatchClientFacadeCall changes the internal FacadeCaller to one that lets
    71  // you mock out the FacadeCall method. The function returned by
    72  // PatchClientFacadeCall is a cleanup function that returns the client to its
    73  // original state.
    74  func PatchClientFacadeCall(c *Client, mockCall func(request string, params interface{}, response interface{}) error) func() {
    75  	orig := c.facade
    76  	c.facade = &resultCaller{mockCall}
    77  	return func() {
    78  		c.facade = orig
    79  	}
    80  }
    81  
    82  type resultCaller struct {
    83  	mockCall func(request string, params interface{}, response interface{}) error
    84  }
    85  
    86  func (f *resultCaller) FacadeCall(request string, params, response interface{}) error {
    87  	return f.mockCall(request, params, response)
    88  }
    89  
    90  func (f *resultCaller) Name() string {
    91  	return ""
    92  }
    93  
    94  func (f *resultCaller) BestAPIVersion() int {
    95  	return 0
    96  }
    97  
    98  func (f *resultCaller) RawAPICaller() base.APICaller {
    99  	return nil
   100  }