github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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/errors"
     8  	"github.com/juju/juju/api/base"
     9  	"github.com/juju/juju/network"
    10  	"github.com/juju/utils/clock"
    11  	"gopkg.in/juju/names.v2"
    12  )
    13  
    14  var (
    15  	CertDir               = &certDir
    16  	NewWebsocketDialer    = newWebsocketDialer
    17  	NewWebsocketDialerPtr = &newWebsocketDialer
    18  	WebsocketDialConfig   = &websocketDialConfig
    19  	SlideAddressToFront   = slideAddressToFront
    20  	BestVersion           = bestVersion
    21  	FacadeVersions        = &facadeVersions
    22  	ConnectWebsocket      = connectWebsocket
    23  )
    24  
    25  // RPCConnection defines the methods that are called on the rpc.Conn instance.
    26  type RPCConnection rpcConnection
    27  
    28  // SetServerAddress allows changing the URL to the internal API server
    29  // that AddLocalCharm uses in order to test NotImplementedError.
    30  func SetServerAddress(c *Client, scheme, addr string) {
    31  	c.st.serverScheme = scheme
    32  	c.st.addr = addr
    33  }
    34  
    35  // ServerRoot is exported so that we can test the built URL.
    36  func ServerRoot(c *Client) string {
    37  	return c.st.serverRoot()
    38  }
    39  
    40  // TestingStateParams is the parameters for NewTestingState, so that you can
    41  // only set the bits that you acutally want to test.
    42  type TestingStateParams struct {
    43  	Address        string
    44  	ModelTag       string
    45  	APIHostPorts   [][]network.HostPort
    46  	FacadeVersions map[string][]int
    47  	ServerScheme   string
    48  	ServerRoot     string
    49  	RPCConnection  RPCConnection
    50  	Clock          clock.Clock
    51  }
    52  
    53  // NewTestingState creates an api.State object that can be used for testing. It
    54  // isn't backed onto an actual API server, so actual RPC methods can't be
    55  // called on it. But it can be used for testing general behaviour.
    56  func NewTestingState(params TestingStateParams) Connection {
    57  	var modelTag names.ModelTag
    58  	if params.ModelTag != "" {
    59  		t, err := names.ParseModelTag(params.ModelTag)
    60  		if err != nil {
    61  			panic("invalid model tag")
    62  		}
    63  		modelTag = t
    64  	}
    65  	st := &state{
    66  		client:            params.RPCConnection,
    67  		clock:             params.Clock,
    68  		addr:              params.Address,
    69  		modelTag:          modelTag,
    70  		hostPorts:         params.APIHostPorts,
    71  		facadeVersions:    params.FacadeVersions,
    72  		serverScheme:      params.ServerScheme,
    73  		serverRootAddress: params.ServerRoot,
    74  	}
    75  	return st
    76  }
    77  
    78  // PatchClientFacadeCall changes the internal FacadeCaller to one that lets
    79  // you mock out the FacadeCall method. The function returned by
    80  // PatchClientFacadeCall is a cleanup function that returns the client to its
    81  // original state.
    82  func PatchClientFacadeCall(c *Client, mockCall func(request string, params interface{}, response interface{}) error) func() {
    83  	orig := c.facade
    84  	c.facade = &resultCaller{mockCall}
    85  	return func() {
    86  		c.facade = orig
    87  	}
    88  }
    89  
    90  type resultCaller struct {
    91  	mockCall func(request string, params interface{}, response interface{}) error
    92  }
    93  
    94  func (f *resultCaller) FacadeCall(request string, params, response interface{}) error {
    95  	return f.mockCall(request, params, response)
    96  }
    97  
    98  func (f *resultCaller) Name() string {
    99  	return ""
   100  }
   101  
   102  func (f *resultCaller) BestAPIVersion() int {
   103  	return 0
   104  }
   105  
   106  func (f *resultCaller) RawAPICaller() base.APICaller {
   107  	return nil
   108  }
   109  
   110  // IsMinVersionError returns true if the given error was caused by the charm
   111  // having a minjujuversion higher than the juju environment's version.
   112  func IsMinVersionError(err error) bool {
   113  	_, ok := errors.Cause(err).(minJujuVersionErr)
   114  	return ok
   115  }