-
Notifications
You must be signed in to change notification settings - Fork 0
/
debuglog_db_test.go
115 lines (92 loc) · 3.51 KB
/
debuglog_db_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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiserver_test
import (
"net/http"
"net/url"
"github.com/gorilla/websocket"
jujuhttp "github.com/juju/http/v2"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
apitesting "github.com/juju/juju/apiserver/testing"
"github.com/juju/juju/apiserver/websocket/websockettest"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/testing/factory"
)
type debugLogDBSuite struct {
apiserverBaseSuite
}
var _ = gc.Suite(&debugLogDBSuite{})
// See debuglog_db_internal_test.go for DB specific unit tests and the
// featuretests package for an end-to-end integration test.
func (s *debugLogDBSuite) TestBadParams(c *gc.C) {
conn := s.dialWebsocket(c, url.Values{"maxLines": {"foo"}})
defer conn.Close()
websockettest.AssertJSONError(c, conn, `maxLines value "foo" is not a valid unsigned number`)
websockettest.AssertWebsocketClosed(c, conn)
}
func (s *debugLogDBSuite) TestWithHTTP(c *gc.C) {
uri := s.logURL("http", nil).String()
apitesting.SendHTTPRequest(c, apitesting.HTTPRequestParams{
Method: "GET",
URL: uri,
ExpectStatus: http.StatusBadRequest,
})
}
func (s *debugLogDBSuite) TestNoAuth(c *gc.C) {
conn, _, err := s.dialWebsocketInternal(c, nil, nil)
c.Assert(err, jc.ErrorIsNil)
websockettest.AssertJSONError(c, conn, "authentication failed: no credentials provided")
websockettest.AssertWebsocketClosed(c, conn)
}
func (s *debugLogDBSuite) TestUnitLoginsRejected(c *gc.C) {
u, password := s.Factory.MakeUnitReturningPassword(c, nil)
header := jujuhttp.BasicAuthHeader(u.Tag().String(), password)
conn, _, err := s.dialWebsocketInternal(c, nil, header)
c.Assert(err, jc.ErrorIsNil)
websockettest.AssertJSONError(c, conn, "authorization failed: tag kind unit not valid")
websockettest.AssertWebsocketClosed(c, conn)
}
var noResultsPlease = url.Values{"maxLines": {"0"}, "noTail": {"true"}}
func (s *debugLogDBSuite) TestUserLoginsAccepted(c *gc.C) {
u := s.Factory.MakeUser(c, &factory.UserParams{
Name: "oryx",
Password: "gardener",
})
header := jujuhttp.BasicAuthHeader(u.Tag().String(), "gardener")
conn, _, err := s.dialWebsocketInternal(c, noResultsPlease, header)
c.Assert(err, jc.ErrorIsNil)
c.Assert(conn, gc.NotNil)
defer conn.Close()
result := websockettest.ReadJSONErrorLine(c, conn)
c.Assert(result.Error, gc.IsNil)
}
func (s *debugLogDBSuite) TestMachineLoginsAccepted(c *gc.C) {
m, password := s.Factory.MakeMachineReturningPassword(c, &factory.MachineParams{
Nonce: "foo-nonce",
})
header := jujuhttp.BasicAuthHeader(m.Tag().String(), password)
header.Add(params.MachineNonceHeader, "foo-nonce")
conn, _, err := s.dialWebsocketInternal(c, noResultsPlease, header)
c.Assert(err, jc.ErrorIsNil)
defer conn.Close()
result := websockettest.ReadJSONErrorLine(c, conn)
c.Assert(result.Error, gc.IsNil)
}
func (s *debugLogDBSuite) logURL(scheme string, queryParams url.Values) *url.URL {
url := s.URL("/log", queryParams)
url.Scheme = scheme
return url
}
func (s *debugLogDBSuite) dialWebsocket(c *gc.C, queryParams url.Values) *websocket.Conn {
header := jujuhttp.BasicAuthHeader(s.Owner.String(), ownerPassword)
conn, _, err := s.dialWebsocketInternal(c, queryParams, header)
c.Assert(err, jc.ErrorIsNil)
return conn
}
func (s *debugLogDBSuite) dialWebsocketInternal(
c *gc.C, queryParams url.Values, header http.Header,
) (*websocket.Conn, *http.Response, error) {
server := s.logURL("wss", queryParams).String()
return dialWebsocketFromURL(c, server, header)
}