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