github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/export_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package apiserver 5 6 import ( 7 "fmt" 8 "net" 9 "time" 10 11 "github.com/juju/names" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 "gopkg.in/macaroon.v1" 15 16 "github.com/juju/juju/apiserver/authentication" 17 "github.com/juju/juju/apiserver/common" 18 "github.com/juju/juju/apiserver/params" 19 "github.com/juju/juju/rpc" 20 "github.com/juju/juju/state" 21 ) 22 23 var ( 24 NewPingTimeout = newPingTimeout 25 MaxClientPingInterval = &maxClientPingInterval 26 MongoPingInterval = &mongoPingInterval 27 NewBackups = &newBackups 28 AllowedMethodsDuringUpgrades = allowedMethodsDuringUpgrades 29 BZMimeType = bzMimeType 30 JSMimeType = jsMimeType 31 SpritePath = spritePath 32 ) 33 34 func ServerMacaroon(srv *Server) (*macaroon.Macaroon, error) { 35 auth, err := srv.authCtxt.macaroonAuth() 36 if err != nil { 37 return nil, err 38 } 39 return auth.(*authentication.ExternalMacaroonAuthenticator).Macaroon, nil 40 } 41 42 func ServerBakeryService(srv *Server) (authentication.BakeryService, error) { 43 auth, err := srv.authCtxt.macaroonAuth() 44 if err != nil { 45 return nil, err 46 } 47 return auth.(*authentication.ExternalMacaroonAuthenticator).Service, nil 48 } 49 50 // ServerAuthenticatorForTag calls the authenticatorForTag method 51 // of the server's authContext. 52 func ServerAuthenticatorForTag(srv *Server, tag names.Tag) (authentication.EntityAuthenticator, error) { 53 return srv.authCtxt.authenticatorForTag(tag) 54 } 55 56 func ApiHandlerWithEntity(entity state.Entity) *apiHandler { 57 return &apiHandler{entity: entity} 58 } 59 60 const LoginRateLimit = loginRateLimit 61 62 // DelayLogins changes how the Login code works so that logins won't proceed 63 // until they get a message on the returned channel. 64 // After calling this function, the caller is responsible for sending messages 65 // on the nextChan in order for Logins to succeed. The original behavior can be 66 // restored by calling the cleanup function. 67 func DelayLogins() (nextChan chan struct{}, cleanup func()) { 68 nextChan = make(chan struct{}, 10) 69 cleanup = func() { 70 doCheckCreds = checkCreds 71 } 72 delayedCheckCreds := func(st *state.State, c params.LoginRequest, lookForModelUser bool, authenticator authentication.EntityAuthenticator) (state.Entity, *time.Time, error) { 73 <-nextChan 74 return checkCreds(st, c, lookForModelUser, authenticator) 75 } 76 doCheckCreds = delayedCheckCreds 77 return 78 } 79 80 func NewErrRoot(err error) *errRoot { 81 return &errRoot{err} 82 } 83 84 // TestingApiRoot gives you an ApiRoot as a rpc.Methodfinder that is 85 // *barely* connected to anything. Just enough to let you probe some 86 // of the interfaces, but not enough to actually do any RPC calls. 87 func TestingApiRoot(st *state.State) rpc.MethodFinder { 88 return newApiRoot(st, common.NewResources(), nil) 89 } 90 91 // TestingApiHandler gives you an ApiHandler that isn't connected to 92 // anything real. It's enough to let test some basic functionality though. 93 func TestingApiHandler(c *gc.C, srvSt, st *state.State) (*apiHandler, *common.Resources) { 94 authCtxt, err := newAuthContext(srvSt) 95 c.Assert(err, jc.ErrorIsNil) 96 srv := &Server{ 97 authCtxt: authCtxt, 98 state: srvSt, 99 tag: names.NewMachineTag("0"), 100 } 101 h, err := newApiHandler(srv, st, nil, nil, st.ModelUUID()) 102 c.Assert(err, jc.ErrorIsNil) 103 return h, h.getResources() 104 } 105 106 // TestingUpgradingRoot returns a limited srvRoot 107 // in an upgrade scenario. 108 func TestingUpgradingRoot(st *state.State) rpc.MethodFinder { 109 r := TestingApiRoot(st) 110 return newUpgradingRoot(r) 111 } 112 113 // TestingRestrictedApiHandler returns a restricted srvRoot as if accessed 114 // from the root of the API path with a recent (verison > 1) login. 115 func TestingRestrictedApiHandler(st *state.State) rpc.MethodFinder { 116 r := TestingApiRoot(st) 117 return newRestrictedRoot(r) 118 } 119 120 type preFacadeAdminApi struct{} 121 122 func newPreFacadeAdminApi(srv *Server, root *apiHandler, reqNotifier *requestNotifier) interface{} { 123 return &preFacadeAdminApi{} 124 } 125 126 func (r *preFacadeAdminApi) Admin(id string) (*preFacadeAdminApi, error) { 127 return r, nil 128 } 129 130 var PreFacadeModelTag = names.NewModelTag("383c49f3-526d-4f9e-b50a-1e6fa4e9b3d9") 131 132 func (r *preFacadeAdminApi) Login(c params.Creds) (params.LoginResult, error) { 133 return params.LoginResult{ 134 ModelTag: PreFacadeModelTag.String(), 135 }, nil 136 } 137 138 type failAdminApi struct{} 139 140 func newFailAdminApi(srv *Server, root *apiHandler, reqNotifier *requestNotifier) interface{} { 141 return &failAdminApi{} 142 } 143 144 func (r *failAdminApi) Admin(id string) (*failAdminApi, error) { 145 return r, nil 146 } 147 148 func (r *failAdminApi) Login(c params.Creds) (params.LoginResult, error) { 149 return params.LoginResult{}, fmt.Errorf("fail") 150 } 151 152 // SetPreFacadeAdminApi is used to create a test scenario where the API server 153 // does not know about API facade versioning. In this case, the client should 154 // login to the v1 facade, which sends backwards-compatible login fields. 155 // The v0 facade will fail on a pre-defined error. 156 func SetPreFacadeAdminApi(srv *Server) { 157 srv.adminApiFactories = map[int]adminApiFactory{ 158 0: newFailAdminApi, 159 1: newPreFacadeAdminApi, 160 } 161 } 162 163 func SetAdminApiVersions(srv *Server, versions ...int) { 164 factories := make(map[int]adminApiFactory) 165 for _, n := range versions { 166 switch n { 167 case 3: 168 factories[n] = newAdminApiV3 169 default: 170 panic(fmt.Errorf("unknown admin API version %d", n)) 171 } 172 } 173 srv.adminApiFactories = factories 174 } 175 176 // TestingRestoreInProgressRoot returns a limited restoreInProgressRoot 177 // containing a srvRoot as returned by TestingSrvRoot. 178 func TestingRestoreInProgressRoot(st *state.State) *restoreInProgressRoot { 179 r := TestingApiRoot(st) 180 return newRestoreInProgressRoot(r) 181 } 182 183 // TestingAboutToRestoreRoot returns a limited aboutToRestoreRoot 184 // containing a srvRoot as returned by TestingSrvRoot. 185 func TestingAboutToRestoreRoot(st *state.State) *aboutToRestoreRoot { 186 r := TestingApiRoot(st) 187 return newAboutToRestoreRoot(r) 188 } 189 190 // Addr returns the address that the server is listening on. 191 func (srv *Server) Addr() *net.TCPAddr { 192 return srv.lis.Addr().(*net.TCPAddr) // cannot fail 193 } 194 195 // PatchGetMigrationBackend overrides the getMigrationBackend function 196 // to support testing. 197 func PatchGetMigrationBackend(p Patcher, st migrationBackend) { 198 p.PatchValue(&getMigrationBackend, func(*state.State) migrationBackend { 199 return st 200 }) 201 } 202 203 // PatchGetControllerCACert overrides the getControllerCACert function 204 // to support testing. 205 func PatchGetControllerCACert(p Patcher, caCert string) { 206 p.PatchValue(&getControllerCACert, func(migrationBackend) (string, error) { 207 return caCert, nil 208 }) 209 } 210 211 // Patcher defines an interface that matches the PatchValue method on 212 // CleanupSuite 213 type Patcher interface { 214 PatchValue(ptr, value interface{}) 215 }