forked from elastic/package-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package_storage_test.go
183 lines (151 loc) · 7.97 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
// 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"
"net/http"
"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(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(indexer, testCacheTime)},
{"/search?all=true", "/search", "search-all.json", searchHandler(indexer, testCacheTime)},
{"/categories", "/categories", "categories.json", categoriesHandler(indexer, testCacheTime)},
{"/categories?experimental=true", "/categories", "categories-experimental.json", categoriesHandler(indexer, testCacheTime)},
{"/categories?experimental=foo", "/categories", "categories-experimental-error.txt", categoriesHandler(indexer, testCacheTime)},
{"/categories?experimental=true&kibana.version=6.5.2", "/categories", "categories-kibana652.json", categoriesHandler(indexer, testCacheTime)},
{"/categories?prerelease=true", "/categories", "categories-prerelease.json", categoriesHandler(indexer, testCacheTime)},
{"/categories?prerelease=foo", "/categories", "categories-prerelease-error.txt", categoriesHandler(indexer, testCacheTime)},
{"/categories?prerelease=true&kibana.version=6.5.2", "/categories", "categories-prerelease-kibana652.json", categoriesHandler(indexer, testCacheTime)},
{"/categories?include_policy_templates=true", "/categories", "categories-include-policy-templates.json", categoriesHandler(indexer, testCacheTime)},
{"/categories?include_policy_templates=foo", "/categories", "categories-include-policy-templates-error.txt", categoriesHandler(indexer, testCacheTime)},
{"/search?kibana.version=6.5.2", "/search", "search-kibana652.json", searchHandler(indexer, testCacheTime)},
{"/search?kibana.version=7.2.1", "/search", "search-kibana721.json", searchHandler(indexer, testCacheTime)},
{"/search?kibana.version=8.0.0", "/search", "search-kibana800.json", searchHandler(indexer, testCacheTime)},
{"/search?category=web", "/search", "search-category-web.json", searchHandler(indexer, testCacheTime)},
{"/search?category=web&all=true", "/search", "search-category-web-all.json", searchHandler(indexer, testCacheTime)},
{"/search?category=custom", "/search", "search-category-custom.json", searchHandler(indexer, testCacheTime)},
{"/search?experimental=true", "/search", "search-package-experimental.json", searchHandler(indexer, testCacheTime)},
{"/search?experimental=foo", "/search", "search-package-experimental-error.txt", searchHandler(indexer, testCacheTime)},
{"/search?category=datastore&experimental=true", "/search", "search-category-datastore.json", searchHandler(indexer, testCacheTime)},
{"/search?prerelease=true", "/search", "search-package-prerelease.json", searchHandler(indexer, testCacheTime)},
{"/search?prerelease=foo", "/search", "search-package-prerelease-error.txt", searchHandler(indexer, testCacheTime)},
{"/search?category=datastore&prerelease=true", "/search", "search-category-datastore-prerelease.json", searchHandler(indexer, testCacheTime)},
// Removed flags, kept ensure that they don't break requests from old versions.
{"/search?internal=true", "/search", "search-package-internal.json", searchHandler(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(fs.Client(), storage.FakeIndexerOptions)
err := indexer.Init(context.Background())
require.NoError(t, err)
packageIndexHandler := packageIndexHandler(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()
indexer := storage.NewIndexer(fs.Client(), storage.FakeIndexerOptions)
err := indexer.Init(context.Background())
require.NoError(t, err)
artifactsHandler := artifactsHandler(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()
indexer := storage.NewIndexer(fs.Client(), storage.FakeIndexerOptions)
err := indexer.Init(context.Background())
require.NoError(t, err)
signaturesHandler := signaturesHandler(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()
indexer := storage.NewIndexer(fs.Client(), storage.FakeIndexerOptions)
err := indexer.Init(context.Background())
require.NoError(t, err)
staticHandler := staticHandler(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 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)
}