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