forked from elastic/package-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package_storage_test.go
306 lines (247 loc) · 12.2 KB
/
package_storage_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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"github.com/elastic/package-registry/storage"
)
const storageIndexerGoldenDir = "storage-indexer"
func TestPackageStorage_Endpoints(t *testing.T) {
fs := storage.PrepareFakeServer(t, "./storage/testdata/search-index-all-full.json")
defer fs.Stop()
indexer := storage.NewIndexer(testLogger, fs.Client(), storage.FakeIndexerOptions)
err := indexer.Init(context.Background())
require.NoError(t, err)
tests := []struct {
endpoint string
path string
file string
handler func(w http.ResponseWriter, r *http.Request)
}{
{"/search", "/search", "search.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?all=true", "/search", "search-all.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/categories", "/categories", "categories.json", categoriesHandler(testLogger, indexer, testCacheTime)},
{"/categories?experimental=true", "/categories", "categories-experimental.json", categoriesHandler(testLogger, indexer, testCacheTime)},
{"/categories?experimental=foo", "/categories", "categories-experimental-error.txt", categoriesHandler(testLogger, indexer, testCacheTime)},
{"/categories?experimental=true&kibana.version=6.5.2", "/categories", "categories-kibana652.json", categoriesHandler(testLogger, indexer, testCacheTime)},
{"/categories?prerelease=true", "/categories", "categories-prerelease.json", categoriesHandler(testLogger, indexer, testCacheTime)},
{"/categories?prerelease=foo", "/categories", "categories-prerelease-error.txt", categoriesHandler(testLogger, indexer, testCacheTime)},
{"/categories?prerelease=true&kibana.version=6.5.2", "/categories", "categories-prerelease-kibana652.json", categoriesHandler(testLogger, indexer, testCacheTime)},
{"/categories?include_policy_templates=true", "/categories", "categories-include-policy-templates.json", categoriesHandler(testLogger, indexer, testCacheTime)},
{"/categories?include_policy_templates=foo", "/categories", "categories-include-policy-templates-error.txt", categoriesHandler(testLogger, indexer, testCacheTime)},
{"/search?kibana.version=6.5.2", "/search", "search-kibana652.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?kibana.version=7.2.1", "/search", "search-kibana721.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?kibana.version=8.0.0", "/search", "search-kibana800.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?category=web", "/search", "search-category-web.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?category=web&all=true", "/search", "search-category-web-all.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?category=custom", "/search", "search-category-custom.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?experimental=true", "/search", "search-package-experimental.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?experimental=foo", "/search", "search-package-experimental-error.txt", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?category=datastore&experimental=true", "/search", "search-category-datastore.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?prerelease=true", "/search", "search-package-prerelease.json", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?prerelease=foo", "/search", "search-package-prerelease-error.txt", searchHandler(testLogger, indexer, testCacheTime)},
{"/search?category=datastore&prerelease=true", "/search", "search-category-datastore-prerelease.json", searchHandler(testLogger, indexer, testCacheTime)},
// Removed flags, kept ensure that they don't break requests from old versions.
{"/search?internal=true", "/search", "search-package-internal.json", searchHandler(testLogger, indexer, testCacheTime)},
}
for _, test := range tests {
t.Run(test.endpoint, func(t *testing.T) {
runEndpointWithStorageIndexer(t, test.endpoint, test.path, test.file, test.handler)
})
}
}
func TestPackageStorage_PackageIndex(t *testing.T) {
fs := storage.PrepareFakeServer(t, "./storage/testdata/search-index-all-full.json")
defer fs.Stop()
indexer := storage.NewIndexer(testLogger, fs.Client(), storage.FakeIndexerOptions)
err := indexer.Init(context.Background())
require.NoError(t, err)
packageIndexHandler := packageIndexHandler(testLogger, indexer, testCacheTime)
tests := []struct {
endpoint string
path string
file string
handler func(w http.ResponseWriter, r *http.Request)
}{
{"/package/1password/0.1.1/", packageIndexRouterPath, "1password-0.1.1.json", packageIndexHandler},
{"/package/kubernetes/0.3.0/", packageIndexRouterPath, "kubernetes-0.3.0.json", packageIndexHandler},
{"/package/osquery/1.0.3/", packageIndexRouterPath, "osquery-1.0.3.json", packageIndexHandler},
}
for _, test := range tests {
t.Run(test.endpoint, func(t *testing.T) {
runEndpointWithStorageIndexer(t, test.endpoint, test.path, test.file, test.handler)
})
}
}
func TestPackageStorage_Artifacts(t *testing.T) {
fs := storage.PrepareFakeServer(t, "./storage/testdata/search-index-all-full.json")
defer fs.Stop()
webServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, r.RequestURI)
}))
defer webServer.Close()
testIndexerOptions := storage.FakeIndexerOptions
testIndexerOptions.PackageStorageEndpoint = webServer.URL
indexer := storage.NewIndexer(testLogger, fs.Client(), testIndexerOptions)
err := indexer.Init(context.Background())
require.NoError(t, err)
artifactsHandler := artifactsHandler(testLogger, indexer, testCacheTime)
tests := []struct {
endpoint string
path string
file string
handler func(w http.ResponseWriter, r *http.Request)
}{
{"/epr/1password/1password-0.1.1.zip", artifactsRouterPath, "1password-0.1.1.zip.txt", artifactsHandler},
{"/epr/kubernetes/kubernetes-999.999.999.zip", artifactsRouterPath, "artifact-package-version-not-found.txt", artifactsHandler},
{"/epr/missing/missing-1.0.3.zip", artifactsRouterPath, "artifact-package-not-found.txt", artifactsHandler},
}
for _, test := range tests {
t.Run(test.endpoint, func(t *testing.T) {
runEndpointWithStorageIndexer(t, test.endpoint, test.path, test.file, test.handler)
})
}
}
func TestPackageStorage_Signatures(t *testing.T) {
fs := storage.PrepareFakeServer(t, "./storage/testdata/search-index-all-full.json")
defer fs.Stop()
webServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, r.RequestURI)
}))
defer webServer.Close()
testIndexerOptions := storage.FakeIndexerOptions
testIndexerOptions.PackageStorageEndpoint = webServer.URL
indexer := storage.NewIndexer(testLogger, fs.Client(), testIndexerOptions)
err := indexer.Init(context.Background())
require.NoError(t, err)
signaturesHandler := signaturesHandler(testLogger, indexer, testCacheTime)
tests := []struct {
endpoint string
path string
file string
handler func(w http.ResponseWriter, r *http.Request)
}{
{"/epr/1password/1password-0.1.1.zip.sig", signaturesRouterPath, "1password-0.1.1.zip.sig", signaturesHandler},
{"/epr/checkpoint/checkpoint-0.5.2.zip.sig", signaturesRouterPath, "checkpoint-0.5.2.zip.sig", signaturesHandler},
}
for _, test := range tests {
t.Run(test.endpoint, func(t *testing.T) {
runEndpoint(t, test.endpoint, test.path, test.file, test.handler)
})
}
}
func TestPackageStorage_Statics(t *testing.T) {
fs := storage.PrepareFakeServer(t, "./storage/testdata/search-index-all-full.json")
defer fs.Stop()
webServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, r.RequestURI)
}))
defer webServer.Close()
testIndexerOptions := storage.FakeIndexerOptions
testIndexerOptions.PackageStorageEndpoint = webServer.URL
indexer := storage.NewIndexer(testLogger, fs.Client(), testIndexerOptions)
err := indexer.Init(context.Background())
require.NoError(t, err)
staticHandler := staticHandler(testLogger, indexer, testCacheTime)
tests := []struct {
endpoint string
path string
file string
handler func(w http.ResponseWriter, r *http.Request)
}{
{"/package/1password/0.1.1/img/1password-logo-light-bg.svg", staticRouterPath, "1password-logo-light-bg.svg", staticHandler},
{"/package/cassandra/1.1.0/img/[Logs Cassandra] System Logs.jpg", staticRouterPath, "logs-cassandra-system-logs.jpg", staticHandler},
{"/package/cef/0.1.0/docs/README.md", staticRouterPath, "cef-readme.md", staticHandler},
}
for _, test := range tests {
t.Run(test.endpoint, func(t *testing.T) {
runEndpoint(t, test.endpoint, test.path, test.file, test.handler)
})
}
}
func TestPackageStorage_ResolverHeadersResponse(t *testing.T) {
fs := storage.PrepareFakeServer(t, "./storage/testdata/search-index-all-full.json")
defer fs.Stop()
webServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Foo", "bar")
w.Header().Set("Last-Modified", "time")
w.Header().Set("Content-Type", "image/svg+xml")
fmt.Fprintf(w, "%s\n%s\n%+v\n", r.Method, r.RequestURI, r.Header)
}))
defer webServer.Close()
testIndexerOptions := storage.FakeIndexerOptions
testIndexerOptions.PackageStorageEndpoint = webServer.URL
indexer := storage.NewIndexer(testLogger, fs.Client(), testIndexerOptions)
err := indexer.Init(context.Background())
require.NoError(t, err)
staticHandler := staticHandler(testLogger, indexer, testCacheTime)
tests := []struct {
endpoint string
path string
file string
responseHeaders map[string]string
handler func(w http.ResponseWriter, r *http.Request)
}{
{
endpoint: "/package/1password/0.1.1/img/1password-logo-light-bg.svg",
path: staticRouterPath,
file: "1password-logo-light-bg.svg.response",
responseHeaders: map[string]string{
"Last-Modified": "time",
"Content-Type": "image/svg+xml",
},
handler: staticHandler,
},
}
for _, test := range tests {
t.Run(test.endpoint, func(t *testing.T) {
runEndpointWithStorageIndexerAndHeaders(t, test.endpoint, test.path, test.file, test.responseHeaders, test.handler)
})
}
}
func TestPackageStorage_ResolverErrorResponse(t *testing.T) {
fs := storage.PrepareFakeServer(t, "./storage/testdata/search-index-all-full.json")
defer fs.Stop()
webServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
message := fmt.Sprintf("internal error\n%s\n%s\n%+v\n", r.Method, r.RequestURI, r.Header)
http.Error(w, message, http.StatusInternalServerError)
}))
defer webServer.Close()
testIndexerOptions := storage.FakeIndexerOptions
testIndexerOptions.PackageStorageEndpoint = webServer.URL
indexer := storage.NewIndexer(testLogger, fs.Client(), testIndexerOptions)
err := indexer.Init(context.Background())
require.NoError(t, err)
staticHandler := staticHandler(testLogger, indexer, testCacheTime)
tests := []struct {
endpoint string
path string
file string
handler func(w http.ResponseWriter, r *http.Request)
}{
{
endpoint: "/package/1password/0.1.1/img/1password-logo-light-bg.svg",
path: staticRouterPath,
file: "1password-logo-light-bg.svg.error-response",
handler: staticHandler,
},
}
for _, test := range tests {
t.Run(test.endpoint, func(t *testing.T) {
runEndpointWithStorageIndexer(t, test.endpoint, test.path, test.file, test.handler)
})
}
}
func runEndpointWithStorageIndexer(t *testing.T, endpoint, path, file string, handler func(w http.ResponseWriter, r *http.Request)) {
runEndpoint(t, endpoint, path, filepath.Join(storageIndexerGoldenDir, file), handler)
}
func runEndpointWithStorageIndexerAndHeaders(t *testing.T, endpoint, path, file string, headers map[string]string, handler func(w http.ResponseWriter, r *http.Request)) {
runEndpointWithHeaders(t, endpoint, path, filepath.Join(storageIndexerGoldenDir, file), headers, handler)
}