/
mockstore.go
213 lines (192 loc) · 5.47 KB
/
mockstore.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package testing // import "gopkg.in/juju/charmrepo.v3/testing"
import (
"bytes"
"encoding/json"
"io"
"net"
"net/http"
"os"
"strconv"
"strings"
"github.com/juju/loggo"
"github.com/juju/utils"
gc "gopkg.in/check.v1"
"gopkg.in/juju/charm.v6"
"gopkg.in/juju/charmrepo.v3"
)
var logger = loggo.GetLogger("juju.charm.testing.mockstore")
// MockStore provides a mock charm store implementation useful when testing.
type MockStore struct {
mux *http.ServeMux
listener net.Listener
archiveBytes []byte
// ArchiveSHA256 holds the hex-encoded SHA256 checksum
// of the charm archive served by the mock store.
ArchiveSHA256 string
Downloads []*charm.URL
DownloadsNoStats []*charm.URL
Authorizations []string
Metadata []string
InfoRequestCount int
InfoRequestCountNoStats int
DefaultSeries string
charms map[string]int
}
// NewMockStore creates a mock charm store containing the specified charms.
func NewMockStore(c *gc.C, repo *Repo, charms map[string]int) *MockStore {
s := &MockStore{charms: charms, DefaultSeries: "precise"}
f, err := os.Open(repo.CharmArchivePath(c.MkDir(), "dummy"))
c.Assert(err, gc.IsNil)
defer f.Close()
buf := &bytes.Buffer{}
s.ArchiveSHA256, _, err = utils.ReadSHA256(io.TeeReader(f, buf))
c.Logf("ArchiveSHA256: %v", s.ArchiveSHA256)
c.Assert(err, gc.IsNil)
s.archiveBytes = buf.Bytes()
c.Assert(err, gc.IsNil)
s.mux = http.NewServeMux()
s.mux.HandleFunc("/charm-info", s.serveInfo)
s.mux.HandleFunc("/charm-event", s.serveEvent)
s.mux.HandleFunc("/charm/", s.serveCharm)
lis, err := net.Listen("tcp", "127.0.0.1:0")
c.Assert(err, gc.IsNil)
s.listener = lis
go http.Serve(s.listener, s)
return s
}
// Close closes the mock store's socket.
func (s *MockStore) Close() {
s.listener.Close()
}
// Address returns the URL used to make requests to the mock store.
func (s *MockStore) Address() string {
return "http://" + s.listener.Addr().String()
}
// UpdateStoreRevision sets the revision of the specified charm to rev.
func (s *MockStore) UpdateStoreRevision(ch string, rev int) {
s.charms[ch] = rev
}
// ServeHTTP implements http.ServeHTTP
func (s *MockStore) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.mux.ServeHTTP(w, r)
}
func (s *MockStore) serveInfo(w http.ResponseWriter, r *http.Request) {
if metadata := r.Header.Get("Juju-Metadata"); metadata != "" {
s.Metadata = append(s.Metadata, metadata)
logger.Infof("Juju metadata: " + metadata)
}
r.ParseForm()
if r.Form.Get("stats") == "0" {
s.InfoRequestCountNoStats += 1
} else {
s.InfoRequestCount += 1
}
response := map[string]*charmrepo.InfoResponse{}
for _, url := range r.Form["charms"] {
cr := &charmrepo.InfoResponse{}
response[url] = cr
charmURL, err := charm.ParseURL(url)
if err != nil {
panic(err)
}
if charmURL.Series == "" {
charmURL.Series = s.DefaultSeries
}
switch charmURL.Name {
case "borken":
cr.Errors = append(cr.Errors, "badness")
case "terracotta":
cr.Errors = append(cr.Errors, "cannot get revision")
case "unwise":
cr.Warnings = append(cr.Warnings, "foolishness")
fallthrough
default:
if rev, ok := s.charms[charmURL.WithRevision(-1).String()]; ok {
if charmURL.Revision == -1 {
cr.Revision = rev
} else {
cr.Revision = charmURL.Revision
}
cr.Sha256 = s.ArchiveSHA256
cr.CanonicalURL = charmURL.String()
} else {
cr.Errors = append(cr.Errors, "entry not found")
}
}
}
data, err := json.Marshal(response)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(data)
if err != nil {
panic(err)
}
}
func (s *MockStore) serveEvent(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
response := map[string]*charmrepo.EventResponse{}
for _, url := range r.Form["charms"] {
digest := ""
if i := strings.Index(url, "@"); i >= 0 {
digest = url[i+1:]
url = url[:i]
}
er := &charmrepo.EventResponse{}
response[url] = er
if digest != "" && digest != "the-digest" {
er.Kind = "not-found"
er.Errors = []string{"entry not found"}
continue
}
charmURL := charm.MustParseURL(url)
switch charmURL.Name {
case "borken":
er.Kind = "publish-error"
er.Errors = append(er.Errors, "badness")
case "unwise":
er.Warnings = append(er.Warnings, "foolishness")
fallthrough
default:
if rev, ok := s.charms[charmURL.WithRevision(-1).String()]; ok {
er.Kind = "published"
er.Revision = rev
er.Digest = "the-digest"
} else {
er.Kind = "not-found"
er.Errors = []string{"entry not found"}
}
}
}
data, err := json.Marshal(response)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(data)
if err != nil {
panic(err)
}
}
func (s *MockStore) serveCharm(w http.ResponseWriter, r *http.Request) {
charmURL := charm.MustParseURL("cs:" + r.URL.Path[len("/charm/"):])
r.ParseForm()
if r.Form.Get("stats") == "0" {
s.DownloadsNoStats = append(s.DownloadsNoStats, charmURL)
} else {
s.Downloads = append(s.Downloads, charmURL)
}
if auth := r.Header.Get("Authorization"); auth != "" {
s.Authorizations = append(s.Authorizations, auth)
}
w.Header().Set("Connection", "close")
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Length", strconv.Itoa(len(s.archiveBytes)))
_, err := w.Write(s.archiveBytes)
if err != nil {
panic(err)
}
}