forked from anatol/pacoloco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_test.go
409 lines (366 loc) · 12.3 KB
/
integration_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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"path"
"testing"
"time"
)
var (
mirrorURL string
pacolocoURL string
testPacolocoDir string
mirrorDir string
)
// the same as TestPacolocoIntegration, but with prefetching things
func TestPacolocoIntegrationWithPrefetching(t *testing.T) {
var err error
mirrorDir, err = os.MkdirTemp(os.TempDir(), "*-pacoloco-mirror")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(mirrorDir)
// For easier setup we are going to serve several Arch mirror trees by one
// instance of http.FileServer
mirror := httptest.NewServer(http.FileServer(http.Dir(mirrorDir)))
defer mirror.Close()
mirrorURL = mirror.URL
// Now setup pacoloco cache dir
testPacolocoDir, err = os.MkdirTemp(os.TempDir(), "*-pacoloco-repo")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(testPacolocoDir)
notInvokingPrefetchTime := time.Now().Add(-time.Hour) // an hour ago
config = &Config{
CacheDir: testPacolocoDir,
Port: -1,
PurgeFilesAfter: -1,
DownloadTimeout: 999,
Repos: make(map[string]*Repo),
Prefetch: &RefreshPeriod{Cron: "0 0 " + fmt.Sprint(notInvokingPrefetchTime.Hour()) + " ? * 1#1 *"},
}
setupPrefetch()
pacoloco := httptest.NewServer(http.HandlerFunc(pacolocoHandler))
defer pacoloco.Close()
pacolocoURL = pacoloco.URL
t.Run("testInvalidURL", testInvalidURL)
t.Run("testRequestNonExistingDb", testRequestNonExistingDb)
t.Run("testRequestExistingRepo", testRequestExistingRepo)
t.Run("testRequestExistingRepoWithDb", testRequestExistingRepoWithDb)
t.Run("testRequestPackageFile", testRequestPackageFile)
t.Run("testFailover", testFailover)
if _, err := os.Stat(path.Join(testPacolocoDir, DefaultDBName)); os.IsNotExist(err) {
t.Errorf("DB file should be created!")
}
}
func TestPacolocoIntegration(t *testing.T) {
var err error
mirrorDir, err = os.MkdirTemp(os.TempDir(), "*-pacoloco-mirror")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(mirrorDir)
// For easier setup we are going to serve several Arch mirror trees by one
// instance of http.FileServer
mirror := httptest.NewServer(http.FileServer(http.Dir(mirrorDir)))
defer mirror.Close()
mirrorURL = mirror.URL
// Now setup pacoloco cache dir
testPacolocoDir, err = os.MkdirTemp(os.TempDir(), "*-pacoloco-repo")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(testPacolocoDir)
config = &Config{
CacheDir: testPacolocoDir,
Port: -1,
PurgeFilesAfter: -1,
DownloadTimeout: 999,
Repos: make(map[string]*Repo),
Prefetch: nil,
}
pacoloco := httptest.NewServer(http.HandlerFunc(pacolocoHandler))
defer pacoloco.Close()
pacolocoURL = pacoloco.URL
t.Run("testInvalidURL", testInvalidURL)
t.Run("testRequestNonExistingDb", testRequestNonExistingDb)
t.Run("testRequestExistingRepo", testRequestExistingRepo)
t.Run("testRequestExistingRepoWithDb", testRequestExistingRepoWithDb)
t.Run("testRequestPackageFile", testRequestPackageFile)
t.Run("testFailover", testFailover)
if _, err := os.Stat(path.Join(testPacolocoDir, DefaultDBName)); !os.IsNotExist(err) {
t.Errorf("DB file shouldn't be created!")
}
}
func testInvalidURL(t *testing.T) {
req := httptest.NewRequest("GET", "http://example.com/foo", nil)
w := httptest.NewRecorder()
pacolocoHandler(w, req)
resp := w.Result()
if resp.StatusCode != 404 {
t.Error("404 response expected")
}
}
func testRequestNonExistingDb(t *testing.T) {
// Requesting non-existing repo
req := httptest.NewRequest("GET", pacolocoURL+"/repo/test/test.db", nil)
w := httptest.NewRecorder()
pacolocoHandler(w, req)
resp := w.Result()
if resp.StatusCode != 404 {
t.Error("404 response expected")
}
// check that no repo cached
if _, err := os.Stat(path.Join(testPacolocoDir, "pkgs", "test")); !os.IsNotExist(err) {
t.Error("test repo should not cached")
}
}
func testRequestExistingRepo(t *testing.T) {
// Requesting existing repo
config.Repos["repo1"] = &Repo{}
defer delete(config.Repos, "repo1")
req := httptest.NewRequest("GET", pacolocoURL+"/repo/repo1/test.db", nil)
w := httptest.NewRecorder()
pacolocoHandler(w, req)
resp := w.Result()
if resp.StatusCode != 404 {
t.Error("404 response expected")
}
// check that db is not cached
if _, err := os.Stat(path.Join(testPacolocoDir, "pkgs", "repo1", "test.db")); !os.IsNotExist(err) {
t.Error("repo1/test.db should be cached")
}
}
func testRequestExistingRepoWithDb(t *testing.T) {
// Requesting existing repo
repo2 := &Repo{
URL: mirrorURL + "/mirror2",
}
config.Repos["repo2"] = repo2
defer delete(config.Repos, "repo2")
if err := os.Mkdir(path.Join(mirrorDir, "mirror2"), os.ModePerm); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(path.Join(mirrorDir, "mirror2"))
dbAtMirror := path.Join(mirrorDir, "mirror2", "test.db")
dbFileContent := "pacoloco/mirror2.db"
if err := os.WriteFile(dbAtMirror, []byte(dbFileContent), os.ModePerm); err != nil {
t.Fatal(err)
}
// Make the mirror file old enough to distinguish it from the subsequent modifications
dbModTime := time.Now().Add(-time.Hour)
if err := os.Chtimes(dbAtMirror, dbModTime, dbModTime); err != nil {
t.Fatal(err)
}
req := httptest.NewRequest("GET", pacolocoURL+"/repo/repo2/test.db", nil)
w := httptest.NewRecorder()
pacolocoHandler(w, req)
resp := w.Result()
if resp.StatusCode != 200 {
t.Errorf("200 response expected, got %v", resp.StatusCode)
}
content, err := io.ReadAll(w.Body)
if err != nil {
t.Fatal(err)
}
if string(content) != dbFileContent {
t.Errorf("Pacoloco cached incorrect db content: %v", string(content))
}
if resp.ContentLength != int64(len(dbFileContent)) {
t.Errorf("Pacoloco returns incorrect length %v", resp.ContentLength)
}
expectedModTime := dbModTime.UTC().Format(http.TimeFormat)
if w.Header().Get("Last-Modified") != expectedModTime {
t.Errorf("Incorrect Last-Modified received, expected: '%v' got: '%v'",
expectedModTime,
w.Header().Get("Last-Modified"))
}
// check that repo is cached
if _, err = os.Stat(path.Join(testPacolocoDir, "pkgs", "repo2")); os.IsNotExist(err) {
t.Error("repo2 repo should be cached")
}
defer os.RemoveAll(path.Join(testPacolocoDir, "pkgs", "repo2"))
content, err = os.ReadFile(path.Join(testPacolocoDir, "pkgs", "repo2", "test.db"))
if err != nil {
t.Fatal(err)
}
if string(content) != dbFileContent {
t.Errorf("Got incorrect db content: %v", string(content))
}
// Now let's modify the db content, pacoloco should refetch it
dbFileContent = "This is a new content"
if err := os.WriteFile(dbAtMirror, []byte(dbFileContent), os.ModePerm); err != nil {
t.Fatal(err)
}
newDbModTime := time.Now()
if err := os.Chtimes(dbAtMirror, newDbModTime, newDbModTime); err != nil {
t.Fatal(err)
}
req = httptest.NewRequest("GET", pacolocoURL+"/repo/repo2/test.db", nil)
w = httptest.NewRecorder()
pacolocoHandler(w, req)
resp = w.Result()
if resp.StatusCode != 200 {
t.Errorf("200 response expected, got %v", resp.StatusCode)
}
content, err = io.ReadAll(w.Body)
if err != nil {
t.Fatal(err)
}
if string(content) != dbFileContent {
t.Errorf("Pacoloco cached incorrect db content: %v", string(content))
}
// check that repo is cached
if _, err := os.Stat(path.Join(testPacolocoDir, "pkgs", "repo2")); os.IsNotExist(err) {
t.Error("repo2 repo should be cached")
}
content, err = os.ReadFile(path.Join(testPacolocoDir, "pkgs", "repo2", "test.db"))
if err != nil {
t.Fatal(err)
}
if string(content) != dbFileContent {
t.Errorf("Got incorrect db content: %v", string(content))
}
if resp.ContentLength != int64(len(dbFileContent)) {
t.Errorf("Pacoloco returns incorrect length %v", resp.ContentLength)
}
newExpectedModTime := newDbModTime.UTC().Format(http.TimeFormat)
if w.Header().Get("Last-Modified") != newExpectedModTime {
t.Errorf("Incorrect Last-Modified received, expected: '%v' got: '%v'",
newExpectedModTime,
w.Header().Get("Last-Modified"))
}
}
func testRequestPackageFile(t *testing.T) {
// Requesting existing repo
repo3 := &Repo{
URL: mirrorURL + "/mirror3",
}
config.Repos["repo3"] = repo3
defer delete(config.Repos, "repo3")
if err := os.Mkdir(path.Join(mirrorDir, "mirror3"), os.ModePerm); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(path.Join(mirrorDir, "mirror3"))
pkgAtMirror := path.Join(mirrorDir, "mirror3", "test-1-any.pkg.tar.zst")
pkgFileContent := "a package"
if err := os.WriteFile(pkgAtMirror, []byte(pkgFileContent), os.ModePerm); err != nil {
t.Fatal(err)
}
// Make the mirror file old enough to distinguish it from the subsequent modifications
pkgModTime := time.Now().Add(-time.Hour)
if err := os.Chtimes(pkgAtMirror, pkgModTime, pkgModTime); err != nil {
t.Fatal(err)
}
req := httptest.NewRequest("GET", pacolocoURL+"/repo/repo3/test-1-any.pkg.tar.zst", nil)
w := httptest.NewRecorder()
pacolocoHandler(w, req)
resp := w.Result()
defer os.RemoveAll(path.Join(testPacolocoDir, "pkgs", "repo3")) // remove cached content
if resp.StatusCode != 200 {
t.Errorf("200 response expected, got %v", resp.StatusCode)
}
content, err := io.ReadAll(w.Body)
if err != nil {
t.Fatal(err)
}
if string(content) != pkgFileContent {
t.Errorf("Pacoloco cached incorrect pkg content: %v", string(content))
}
if resp.ContentLength != int64(len(pkgFileContent)) {
t.Errorf("Pacoloco returns incorrect length %v", resp.ContentLength)
}
expectedModTime := pkgModTime.UTC().Format(http.TimeFormat)
if w.Header().Get("Last-Modified") != expectedModTime {
t.Errorf("Incorrect Last-Modified received, expected: '%v' got: '%v'",
expectedModTime,
w.Header().Get("Last-Modified"))
}
// check that pkg is cached
content, err = os.ReadFile(path.Join(testPacolocoDir, "pkgs", "repo3", "test-1-any.pkg.tar.zst"))
if err != nil {
t.Fatal(err)
}
if string(content) != pkgFileContent {
t.Errorf("Got incorrect db content: %v", string(content))
}
// Now let's modify the db content, pacoloco should not refetch it
if err := os.WriteFile(pkgAtMirror, []byte("This is a new content"), os.ModePerm); err != nil {
t.Fatal(err)
}
newDbModTime := time.Now()
if err := os.Chtimes(pkgAtMirror, newDbModTime, newDbModTime); err != nil {
t.Fatal(err)
}
req = httptest.NewRequest("GET", pacolocoURL+"/repo/repo3/test-1-any.pkg.tar.zst", nil)
w = httptest.NewRecorder()
pacolocoHandler(w, req)
resp = w.Result()
if resp.StatusCode != 200 {
t.Errorf("200 response expected, got %v", resp.StatusCode)
}
content, err = io.ReadAll(w.Body)
if err != nil {
t.Fatal(err)
}
if string(content) != pkgFileContent {
t.Errorf("Pacoloco cached incorrect db content: %v", string(content))
}
// check that repo is cached
content, err = os.ReadFile(path.Join(testPacolocoDir, "pkgs", "repo3", "test-1-any.pkg.tar.zst"))
if err != nil {
t.Fatal(err)
}
if string(content) != pkgFileContent {
t.Errorf("Got incorrect pkg content: %v", string(content))
}
if resp.ContentLength != int64(len(pkgFileContent)) {
t.Errorf("Pacoloco returns incorrect length %v", resp.ContentLength)
}
if w.Header().Get("Last-Modified") != expectedModTime {
t.Errorf("Incorrect Last-Modified received, expected: '%v' got: '%v'",
expectedModTime,
w.Header().Get("Last-Modified"))
}
}
func testFailover(t *testing.T) {
failover := &Repo{
URLs: []string{
mirrorURL + "/no-mirror",
mirrorURL + "/mirror-failover",
},
}
config.Repos["failover"] = failover
defer delete(config.Repos, "failover")
if err := os.Mkdir(path.Join(mirrorDir, "mirror-failover"), os.ModePerm); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(path.Join(mirrorDir, "mirror-failover"))
pkgAtMirror := path.Join(mirrorDir, "mirror-failover", "test-1-any.pkg.tar.zst")
pkgFileContent := "failover content"
if err := os.WriteFile(pkgAtMirror, []byte(pkgFileContent), os.ModePerm); err != nil {
t.Fatal(err)
}
req := httptest.NewRequest("GET", pacolocoURL+"/repo/failover/test-1-any.pkg.tar.zst", nil)
w := httptest.NewRecorder()
pacolocoHandler(w, req)
resp := w.Result()
defer os.RemoveAll(path.Join(testPacolocoDir, "pkgs", "failover")) // remove cached content
if resp.StatusCode != 200 {
t.Errorf("200 response expected, got %v", resp.StatusCode)
}
content, err := io.ReadAll(w.Body)
if err != nil {
t.Fatal(err)
}
if string(content) != pkgFileContent {
t.Errorf("Pacoloco cached incorrect pkg content: %v", string(content))
}
if resp.ContentLength != int64(len(pkgFileContent)) {
t.Errorf("Pacoloco returns incorrect length %v", resp.ContentLength)
}
}