~juju-qa/ubuntu/xenial/juju/2.0-rc2

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/apiserver/apiserver_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        }
 
51
}
 
52
 
 
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)
 
58
        return srv
 
59
}
 
60
 
 
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)
 
65
        })
 
66
        return srv
 
67
}
 
68
 
 
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)
 
73
        })
 
74
        return srv
 
75
}
 
76
 
 
77
// APIInfo returns an info struct that has the server's address and ca-cert
 
78
// populated.
 
79
func (s *apiserverBaseSuite) APIInfo(server *apiserver.Server) *api.Info {
 
80
        address := fmt.Sprintf("localhost:%d", server.Addr().Port)
 
81
        return &api.Info{
 
82
                Addrs:  []string{address},
 
83
                CACert: coretesting.CACert,
 
84
        }
 
85
}
 
86
 
 
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)
 
89
        apiInfo.Tag = tag
 
90
        apiInfo.Password = password
 
91
        apiInfo.Nonce = nonce
 
92
        if !controllerOnly {
 
93
                apiInfo.ModelTag = s.State.ModelTag()
 
94
        }
 
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) {
 
99
                conn.Close()
 
100
        })
 
101
        return conn
 
102
}
 
103
 
 
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)
 
109
}
 
110
 
 
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) {
 
116
        if len(jobs) == 0 {
 
117
                jobs = []state.MachineJob{state.JobHostUnits}
 
118
        }
 
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
 
128
}