-
Notifications
You must be signed in to change notification settings - Fork 37
/
server_test.go
125 lines (102 loc) · 3.13 KB
/
server_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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charmstore_test
import (
"fmt"
"net/http"
"testing"
"github.com/juju/charmrepo/v6/csclient/params"
jujutesting "github.com/juju/testing"
"github.com/juju/testing/httptesting"
gc "gopkg.in/check.v1"
"gopkg.in/juju/charmstore.v5"
"gopkg.in/juju/charmstore.v5/internal/storetesting"
)
// These tests are copied (almost) verbatim from internal/charmstore/server_test.go
func TestPackage(t *testing.T) {
jujutesting.MgoTestPackage(t, nil)
}
type ServerSuite struct {
jujutesting.IsolatedMgoSuite
config charmstore.ServerParams
}
var _ = gc.Suite(&ServerSuite{})
func (s *ServerSuite) SetUpSuite(c *gc.C) {
s.IsolatedMgoSuite.SetUpSuite(c)
s.config = charmstore.ServerParams{
AuthUsername: "test-user",
AuthPassword: "test-password",
}
}
func (s *ServerSuite) TestNewServerWithNoVersions(c *gc.C) {
h, err := charmstore.NewServer(s.Session.DB("foo"), nil, "", s.config)
c.Assert(err, gc.ErrorMatches, `charm store server must serve at least one version of the API`)
c.Assert(h, gc.IsNil)
}
func (s *ServerSuite) TestNewServerWithUnregisteredVersion(c *gc.C) {
h, err := charmstore.NewServer(s.Session.DB("foo"), nil, "", s.config, "wrong")
c.Assert(err, gc.ErrorMatches, `unknown version "wrong"`)
c.Assert(h, gc.IsNil)
}
type versionResponse struct {
Version string
Path string
}
func (s *ServerSuite) TestVersions(c *gc.C) {
c.Assert(charmstore.Versions(), gc.DeepEquals, []string{"", "docker-registry", "v4", "v5"})
}
func (s *ServerSuite) TestNewServerWithVersions(c *gc.C) {
db := s.Session.DB("foo")
h, err := charmstore.NewServer(db, nil, "", s.config, charmstore.V4)
c.Assert(err, gc.Equals, nil)
defer h.Close()
httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
Handler: h,
URL: "/v4/debug",
ExpectStatus: http.StatusInternalServerError,
ExpectBody: params.Error{
Message: "method not implemented",
},
})
assertDoesNotServeVersion(c, h, "v3")
}
func assertServesVersion(c *gc.C, h http.Handler, vers string) {
httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
Handler: h,
URL: "/" + vers + "/some/path",
ExpectBody: versionResponse{
Version: vers,
Path: "/some/path",
},
})
}
func assertDoesNotServeVersion(c *gc.C, h http.Handler, vers string) {
url := "/" + vers + "/debug"
httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
Handler: h,
URL: url,
ExpectStatus: http.StatusNotFound,
ExpectBody: params.Error{
Message: fmt.Sprintf("no handler for %q", url),
Code: params.ErrNotFound,
},
})
}
type ServerESSuite struct {
storetesting.IsolatedMgoESSuite
config charmstore.ServerParams
}
var _ = gc.Suite(&ServerESSuite{})
func (s *ServerESSuite) SetUpSuite(c *gc.C) {
s.IsolatedMgoESSuite.SetUpSuite(c)
s.config = charmstore.ServerParams{
AuthUsername: "test-user",
AuthPassword: "test-password",
}
}
func (s *ServerESSuite) TestNewServerWithElasticsearch(c *gc.C) {
db := s.Session.DB("foo")
srv, err := charmstore.NewServer(db, s.ES, s.TestIndex, s.config, charmstore.V4)
c.Assert(err, gc.Equals, nil)
srv.Close()
}