1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
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"
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"
27
ownerPassword = "very very secret"
30
type apiserverBaseSuite struct {
31
statetesting.StateSuite
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)
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"),
49
NewObserver: func() observer.Observer { return &fakeobserver.Instance{} },
53
func (s *apiserverBaseSuite) newServerNoCleanup(c *gc.C, config apiserver.ServerConfig) *apiserver.Server {
54
listener, err := net.Listen("tcp", ":0")
55
c.Assert(err, jc.ErrorIsNil)
56
srv, err := apiserver.NewServer(s.State, listener, config)
57
c.Assert(err, jc.ErrorIsNil)
61
func (s *apiserverBaseSuite) newServer(c *gc.C, config apiserver.ServerConfig) *apiserver.Server {
62
srv := s.newServerNoCleanup(c, config)
63
s.AddCleanup(func(c *gc.C) {
64
workertest.CleanKill(c, srv)
69
func (s *apiserverBaseSuite) newServerDirtyKill(c *gc.C, config apiserver.ServerConfig) *apiserver.Server {
70
srv := s.newServerNoCleanup(c, config)
71
s.AddCleanup(func(c *gc.C) {
72
workertest.DirtyKill(c, srv)
77
// APIInfo returns an info struct that has the server's address and ca-cert
79
func (s *apiserverBaseSuite) APIInfo(server *apiserver.Server) *api.Info {
80
address := fmt.Sprintf("localhost:%d", server.Addr().Port)
82
Addrs: []string{address},
83
CACert: coretesting.CACert,
87
func (s *apiserverBaseSuite) openAPIAs(c *gc.C, srv *apiserver.Server, tag names.Tag, password, nonce string, controllerOnly bool) api.Connection {
88
apiInfo := s.APIInfo(srv)
90
apiInfo.Password = password
93
apiInfo.ModelTag = s.State.ModelTag()
95
conn, err := api.Open(apiInfo, api.DialOpts{})
96
c.Assert(err, jc.ErrorIsNil)
97
c.Assert(conn, gc.NotNil)
98
s.AddCleanup(func(c *gc.C) {
104
// OpenAPIAsNewMachine creates a new client connection logging in as the
105
// controller owner. The returned api.Connection should not be closed by the
106
// caller as a cleanup function has been registered to do that.
107
func (s *apiserverBaseSuite) OpenAPIAsAdmin(c *gc.C, srv *apiserver.Server) api.Connection {
108
return s.openAPIAs(c, srv, s.Owner, ownerPassword, "", false)
111
// OpenAPIAsNewMachine creates a new machine entry that lives in system state,
112
// and then uses that to open the API. The returned api.Connection should not be
113
// closed by the caller as a cleanup function has been registered to do that.
114
// The machine will run the supplied jobs; if none are given, JobHostUnits is assumed.
115
func (s *apiserverBaseSuite) OpenAPIAsNewMachine(c *gc.C, srv *apiserver.Server, jobs ...state.MachineJob) (api.Connection, *state.Machine) {
117
jobs = []state.MachineJob{state.JobHostUnits}
119
machine, err := s.State.AddMachine("quantal", jobs...)
120
c.Assert(err, jc.ErrorIsNil)
121
password, err := utils.RandomPassword()
122
c.Assert(err, jc.ErrorIsNil)
123
err = machine.SetPassword(password)
124
c.Assert(err, jc.ErrorIsNil)
125
err = machine.SetProvisioned("foo", "fake_nonce", nil)
126
c.Assert(err, jc.ErrorIsNil)
127
return s.openAPIAs(c, srv, machine.Tag(), password, "fake_nonce", false), machine