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