~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/gopkg.in/juju/charmstore.v5-unstable/internal/charmstore/server_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2014 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package charmstore // import "gopkg.in/juju/charmstore.v5-unstable/internal/charmstore"
 
5
 
 
6
import (
 
7
        "net/http"
 
8
 
 
9
        "github.com/juju/testing/httptesting"
 
10
        gc "gopkg.in/check.v1"
 
11
 
 
12
        "gopkg.in/juju/charmstore.v5-unstable/internal/router"
 
13
        "gopkg.in/juju/charmstore.v5-unstable/internal/storetesting"
 
14
)
 
15
 
 
16
var serverParams = ServerParams{
 
17
        AuthUsername:   "test-user",
 
18
        AuthPassword:   "test-password",
 
19
        IdentityAPIURL: "http://0.1.2.3",
 
20
}
 
21
 
 
22
type ServerSuite struct {
 
23
        storetesting.IsolatedMgoESSuite
 
24
}
 
25
 
 
26
var _ = gc.Suite(&ServerSuite{})
 
27
 
 
28
func (s *ServerSuite) TestNewServerWithNoVersions(c *gc.C) {
 
29
        h, err := NewServer(s.Session.DB("foo"), nil, serverParams, nil)
 
30
        c.Assert(err, gc.ErrorMatches, `charm store server must serve at least one version of the API`)
 
31
        c.Assert(h, gc.IsNil)
 
32
}
 
33
 
 
34
type versionResponse struct {
 
35
        Version string
 
36
        Path    string
 
37
}
 
38
 
 
39
func (s *ServerSuite) TestNewServerWithVersions(c *gc.C) {
 
40
        db := s.Session.DB("foo")
 
41
        serveVersion := func(vers string) NewAPIHandlerFunc {
 
42
                return func(p *Pool, config ServerParams, _ string) HTTPCloseHandler {
 
43
                        return nopCloseHandler{
 
44
                                router.HandleJSON(func(_ http.Header, req *http.Request) (interface{}, error) {
 
45
                                        return versionResponse{
 
46
                                                Version: vers,
 
47
                                                Path:    req.URL.Path,
 
48
                                        }, nil
 
49
                                }),
 
50
                        }
 
51
                }
 
52
        }
 
53
 
 
54
        h, err := NewServer(db, nil, serverParams, map[string]NewAPIHandlerFunc{
 
55
                "version1": serveVersion("version1"),
 
56
        })
 
57
        c.Assert(err, gc.IsNil)
 
58
        defer h.Close()
 
59
        assertServesVersion(c, h, "version1")
 
60
        assertDoesNotServeVersion(c, h, "version2")
 
61
        assertDoesNotServeVersion(c, h, "version3")
 
62
 
 
63
        h, err = NewServer(db, nil, serverParams, map[string]NewAPIHandlerFunc{
 
64
                "version1": serveVersion("version1"),
 
65
                "version2": serveVersion("version2"),
 
66
        })
 
67
        c.Assert(err, gc.IsNil)
 
68
        defer h.Close()
 
69
        assertServesVersion(c, h, "version1")
 
70
        assertServesVersion(c, h, "version2")
 
71
        assertDoesNotServeVersion(c, h, "version3")
 
72
 
 
73
        h, err = NewServer(db, nil, serverParams, map[string]NewAPIHandlerFunc{
 
74
                "version1": serveVersion("version1"),
 
75
                "version2": serveVersion("version2"),
 
76
                "version3": serveVersion("version3"),
 
77
        })
 
78
        c.Assert(err, gc.IsNil)
 
79
        defer h.Close()
 
80
        assertServesVersion(c, h, "version1")
 
81
        assertServesVersion(c, h, "version2")
 
82
        assertServesVersion(c, h, "version3")
 
83
 
 
84
        h, err = NewServer(db, nil, serverParams, map[string]NewAPIHandlerFunc{
 
85
                "version1": serveVersion("version1"),
 
86
                "":         serveVersion(""),
 
87
        })
 
88
        c.Assert(err, gc.IsNil)
 
89
        defer h.Close()
 
90
        assertServesVersion(c, h, "")
 
91
        assertServesVersion(c, h, "version1")
 
92
}
 
93
 
 
94
func (s *ServerSuite) TestNewServerWithConfig(c *gc.C) {
 
95
 
 
96
        params := ServerParams{
 
97
                AuthUsername:   "test-user",
 
98
                AuthPassword:   "test-password",
 
99
                IdentityAPIURL: "http://0.1.2.3/",
 
100
        }
 
101
        serveConfig := func(p *Pool, config ServerParams, _ string) HTTPCloseHandler {
 
102
                return nopCloseHandler{
 
103
                        router.HandleJSON(func(_ http.Header, req *http.Request) (interface{}, error) {
 
104
                                return config, nil
 
105
                        }),
 
106
                }
 
107
        }
 
108
        h, err := NewServer(s.Session.DB("foo"), nil, params, map[string]NewAPIHandlerFunc{
 
109
                "version1": serveConfig,
 
110
        })
 
111
        c.Assert(err, gc.IsNil)
 
112
        defer h.Close()
 
113
 
 
114
        // The IdentityLocation field is filled out from the IdentityAPIURL
 
115
        // and the final slash is trimmed.
 
116
 
 
117
        httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
 
118
                Handler: h,
 
119
                URL:     "/version1/some/path",
 
120
                ExpectBody: ServerParams{
 
121
                        AuthUsername:     "test-user",
 
122
                        AuthPassword:     "test-password",
 
123
                        IdentityAPIURL:   "http://0.1.2.3",
 
124
                        IdentityLocation: "http://0.1.2.3",
 
125
                },
 
126
        })
 
127
}
 
128
 
 
129
func (s *ServerSuite) TestNewServerWithElasticSearch(c *gc.C) {
 
130
        params := ServerParams{
 
131
                AuthUsername:   "test-user",
 
132
                AuthPassword:   "test-password",
 
133
                IdentityAPIURL: "http://0.1.2.3",
 
134
        }
 
135
        serveConfig := func(p *Pool, config ServerParams, _ string) HTTPCloseHandler {
 
136
                return nopCloseHandler{
 
137
                        router.HandleJSON(func(_ http.Header, req *http.Request) (interface{}, error) {
 
138
                                return config, nil
 
139
                        }),
 
140
                }
 
141
        }
 
142
        h, err := NewServer(s.Session.DB("foo"), &SearchIndex{s.ES, s.TestIndex}, params,
 
143
                map[string]NewAPIHandlerFunc{
 
144
                        "version1": serveConfig,
 
145
                })
 
146
        c.Assert(err, gc.IsNil)
 
147
        defer h.Close()
 
148
        httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
 
149
                Handler: h,
 
150
                URL:     "/version1/some/path",
 
151
                ExpectBody: ServerParams{
 
152
                        AuthUsername:     "test-user",
 
153
                        AuthPassword:     "test-password",
 
154
                        IdentityAPIURL:   "http://0.1.2.3",
 
155
                        IdentityLocation: "http://0.1.2.3",
 
156
                },
 
157
        })
 
158
}
 
159
 
 
160
func assertServesVersion(c *gc.C, h http.Handler, vers string) {
 
161
        path := vers
 
162
        if path != "" {
 
163
                path = "/" + path
 
164
        }
 
165
        httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
 
166
                Handler: h,
 
167
                URL:     path + "/some/path",
 
168
                ExpectBody: versionResponse{
 
169
                        Version: vers,
 
170
                        Path:    "/some/path",
 
171
                },
 
172
        })
 
173
}
 
174
 
 
175
func assertDoesNotServeVersion(c *gc.C, h http.Handler, vers string) {
 
176
        rec := httptesting.DoRequest(c, httptesting.DoRequestParams{
 
177
                Handler: h,
 
178
                URL:     "/" + vers + "/some/path",
 
179
        })
 
180
        c.Assert(rec.Code, gc.Equals, http.StatusNotFound)
 
181
}
 
182
 
 
183
type nopCloseHandler struct {
 
184
        http.Handler
 
185
}
 
186
 
 
187
func (nopCloseHandler) Close() {
 
188
}