github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/apiserver/apiserver_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package apiserver_test 5 6 import ( 7 "fmt" 8 "net" 9 10 jc "github.com/juju/testing/checkers" 11 "github.com/juju/utils" 12 "github.com/juju/utils/clock" 13 gc "gopkg.in/check.v1" 14 "gopkg.in/juju/names.v2" 15 16 "github.com/juju/juju/api" 17 "github.com/juju/juju/apiserver" 18 "github.com/juju/juju/apiserver/observer" 19 "github.com/juju/juju/apiserver/observer/fakeobserver" 20 "github.com/juju/juju/state" 21 statetesting "github.com/juju/juju/state/testing" 22 coretesting "github.com/juju/juju/testing" 23 "github.com/juju/juju/worker/workertest" 24 ) 25 26 const ( 27 ownerPassword = "very very secret" 28 ) 29 30 type apiserverBaseSuite struct { 31 statetesting.StateSuite 32 } 33 34 func (s *apiserverBaseSuite) SetUpTest(c *gc.C) { 35 s.StateSuite.SetUpTest(c) 36 u, err := s.State.User(s.Owner) 37 c.Assert(err, jc.ErrorIsNil) 38 err = u.SetPassword(ownerPassword) 39 c.Assert(err, jc.ErrorIsNil) 40 } 41 42 func (s *apiserverBaseSuite) sampleConfig(c *gc.C) apiserver.ServerConfig { 43 return apiserver.ServerConfig{ 44 Clock: clock.WallClock, 45 Cert: coretesting.ServerCert, 46 Key: coretesting.ServerKey, 47 Tag: names.NewMachineTag("0"), 48 LogDir: c.MkDir(), 49 NewObserver: func() observer.Observer { return &fakeobserver.Instance{} }, 50 AutocertURL: "https://0.1.2.3/no-autocert-here", 51 } 52 } 53 54 func (s *apiserverBaseSuite) newServerNoCleanup(c *gc.C, config apiserver.ServerConfig) *apiserver.Server { 55 listener, err := net.Listen("tcp", ":0") 56 c.Assert(err, jc.ErrorIsNil) 57 srv, err := apiserver.NewServer(s.State, listener, config) 58 c.Assert(err, jc.ErrorIsNil) 59 return srv 60 } 61 62 func (s *apiserverBaseSuite) newServer(c *gc.C, config apiserver.ServerConfig) *apiserver.Server { 63 srv := s.newServerNoCleanup(c, config) 64 s.AddCleanup(func(c *gc.C) { 65 workertest.CleanKill(c, srv) 66 }) 67 return srv 68 } 69 70 func (s *apiserverBaseSuite) newServerDirtyKill(c *gc.C, config apiserver.ServerConfig) *apiserver.Server { 71 srv := s.newServerNoCleanup(c, config) 72 s.AddCleanup(func(c *gc.C) { 73 workertest.DirtyKill(c, srv) 74 }) 75 return srv 76 } 77 78 // APIInfo returns an info struct that has the server's address and ca-cert 79 // populated. 80 func (s *apiserverBaseSuite) APIInfo(server *apiserver.Server) *api.Info { 81 address := fmt.Sprintf("localhost:%d", server.Addr().Port) 82 return &api.Info{ 83 Addrs: []string{address}, 84 CACert: coretesting.CACert, 85 } 86 } 87 88 func (s *apiserverBaseSuite) openAPIAs(c *gc.C, srv *apiserver.Server, tag names.Tag, password, nonce string, controllerOnly bool) api.Connection { 89 apiInfo := s.APIInfo(srv) 90 apiInfo.Tag = tag 91 apiInfo.Password = password 92 apiInfo.Nonce = nonce 93 if !controllerOnly { 94 apiInfo.ModelTag = s.State.ModelTag() 95 } 96 conn, err := api.Open(apiInfo, api.DialOpts{}) 97 c.Assert(err, jc.ErrorIsNil) 98 c.Assert(conn, gc.NotNil) 99 s.AddCleanup(func(c *gc.C) { 100 conn.Close() 101 }) 102 return conn 103 } 104 105 // OpenAPIAsNewMachine creates a new client connection logging in as the 106 // controller owner. The returned api.Connection should not be closed by the 107 // caller as a cleanup function has been registered to do that. 108 func (s *apiserverBaseSuite) OpenAPIAsAdmin(c *gc.C, srv *apiserver.Server) api.Connection { 109 return s.openAPIAs(c, srv, s.Owner, ownerPassword, "", false) 110 } 111 112 // OpenAPIAsNewMachine creates a new machine entry that lives in system state, 113 // and then uses that to open the API. The returned api.Connection should not be 114 // closed by the caller as a cleanup function has been registered to do that. 115 // The machine will run the supplied jobs; if none are given, JobHostUnits is assumed. 116 func (s *apiserverBaseSuite) OpenAPIAsNewMachine(c *gc.C, srv *apiserver.Server, jobs ...state.MachineJob) (api.Connection, *state.Machine) { 117 if len(jobs) == 0 { 118 jobs = []state.MachineJob{state.JobHostUnits} 119 } 120 machine, err := s.State.AddMachine("quantal", jobs...) 121 c.Assert(err, jc.ErrorIsNil) 122 password, err := utils.RandomPassword() 123 c.Assert(err, jc.ErrorIsNil) 124 err = machine.SetPassword(password) 125 c.Assert(err, jc.ErrorIsNil) 126 err = machine.SetProvisioned("foo", "fake_nonce", nil) 127 c.Assert(err, jc.ErrorIsNil) 128 return s.openAPIAs(c, srv, machine.Tag(), password, "fake_nonce", false), machine 129 }