forked from anatol/pacoloco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefetch_test.go
422 lines (405 loc) · 12.2 KB
/
prefetch_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
410
411
412
413
414
415
416
417
418
419
420
421
422
package main
import (
"database/sql"
"fmt"
"log"
"os"
"path"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
// helper to setup the db
func testSetupHelper(t *testing.T) string {
notInvokingPrefetchTime := time.Now().Add(-time.Hour) // an hour ago
tmpDir := t.TempDir()
c := `
cache_dir: ` + tmpDir + `
purge_files_after: 0 # 3600 * 24 * 30days
prefetch:
cron: 0 0 ` + fmt.Sprint(notInvokingPrefetchTime.Hour()) + ` * * * *
ttl_unaccessed_in_days: 15
ttl_unupdated_in_days: 20
download_timeout: 200
port: 9139
repos:
archlinux:
url: http://mirrors.kernel.org/archlinux
example:
urls:
- http://mirror1.example.org/archlinux
- https://mirror.example.com/mirror/packages/archlinux/
- http://mirror2.example.com/archlinux/test/
`
config = parseConfig([]byte(c))
return tmpDir
}
func testDbConnectionHelper(filepath string) *sql.DB {
db, err := sql.Open("sqlite3", filepath)
if err != nil {
panic(err)
}
if db == nil {
panic("db nil")
}
return db
}
func TestSetupPrefetch(t *testing.T) {
tmpDir := testSetupHelper(t)
setupPrefetch()
exists, err := fileExists(path.Join(tmpDir, DefaultDBName))
if err != nil {
log.Fatal(err)
}
if !exists {
t.Fatal("setupPrefetch didn't create the db file")
}
if prefetchDB == nil {
t.Errorf("Prefetch DB is uninitilized")
}
conn := testDbConnectionHelper(path.Join(tmpDir, DefaultDBName))
for _, table := range []string{"mirror_dbs", "packages", "mirror_packages"} {
res, err := conn.Query("select * from " + table)
if err != nil {
log.Fatal(err)
}
for res.Next() {
var pkg Package
err = res.Scan(&pkg.PackageName, &pkg.Version, &pkg.Arch, &pkg.RepoName, &pkg.LastTimeDownloaded, &pkg.LastTimeRepoUpdated)
fmt.Print(pkg)
if err != nil {
log.Fatal(err)
}
t.Fatalf("setupPrefetch shouldn't create entries in %v\n", table)
}
}
}
func TestSetupPrefetchTicker(t *testing.T) {
testSetupHelper(t)
setupPrefetch()
ticker := setupPrefetchTicker()
if ticker == nil {
t.Errorf("returned ticker shouldn't be nil.")
}
ticker.Stop()
}
func TestUpdateDBRequestedFile(t *testing.T) {
tmpDir := testSetupHelper(t)
setupPrefetch()
conn := testDbConnectionHelper(path.Join(tmpDir, DefaultDBName))
updateDBRequestedFile("nope", "Wrongfile.db")
updateDBRequestedFile("nope", "Wrongfile.zst")
updateDBRequestedFile("nope", "fakeacceptablefile.pkg.tar.zst") // doesn't have the correct format
updateDBRequestedFile("nope", "acl-2.3.1-1-x86_64.pkg.tar.zst.sig") // do not save signatures too in the db
// none of those should be in the db, now i'll check
for _, table := range []string{"mirror_dbs", "packages", "mirror_packages"} {
res, err := conn.Query("select * from " + table)
if err != nil {
log.Fatal(err)
}
for res.Next() {
var pkg Package
err = res.Scan(&pkg.PackageName, &pkg.Version, &pkg.Arch, &pkg.RepoName, &pkg.LastTimeDownloaded, &pkg.LastTimeRepoUpdated)
fmt.Print(pkg)
if err != nil {
log.Fatal(err)
}
t.Fatalf("updateDBRequestedFile shouldn't create entries in %v with bad values\n", table)
}
}
// this one should be added
updateDBRequestedFile("foo", "webkit-2.3.1-1-x86_64.pkg.tar.zst")
res, err := conn.Query("select * from packages")
if err != nil {
log.Fatal(err)
}
if res.Next() {
var got Package
now := time.Now()
err = res.Scan(&got.PackageName, &got.Version, &got.Arch, &got.RepoName, &got.LastTimeDownloaded, &got.LastTimeRepoUpdated)
want := Package{PackageName: "webkit", Version: "2.3.1-1", Arch: "x86_64", RepoName: "foo", LastTimeDownloaded: &now, LastTimeRepoUpdated: &now}
if !cmp.Equal(got, want, cmpopts.IgnoreFields(Package{}, "LastTimeDownloaded", "LastTimeRepoUpdated")) {
t.Errorf("\ngot %v,\nwant %v", got, want)
}
dist := want.LastTimeDownloaded.Sub(*got.LastTimeDownloaded)
if dist < -5*time.Second {
t.Errorf("Unexpected result, got.LastTimeDownloaded is wrong")
}
if dist > 5*time.Second {
t.Errorf("got %d, want %d", dist, 5*time.Second)
}
dist = want.LastTimeRepoUpdated.Sub(*got.LastTimeRepoUpdated)
if dist < -5*time.Second {
t.Errorf("Unexpected result, got.LastTimeRepoUpdated is wrong")
}
if want.LastTimeRepoUpdated.Sub(*got.LastTimeRepoUpdated) > 5*time.Second {
t.Errorf("got %d, want %d", want.LastTimeRepoUpdated.Sub(*got.LastTimeRepoUpdated), 5*time.Second)
}
if err != nil {
log.Fatal(err)
}
return
}
t.Fatalf("updateDBRequestedFile should create entries in packages\n")
}
func TestUpdateDBPrefetchedFile(t *testing.T) {
tmpDir := testSetupHelper(t)
setupPrefetch()
conn := testDbConnectionHelper(path.Join(tmpDir, DefaultDBName))
// add a fake download entry
updateDBRequestedFile("foo", "webkit-2.3.1-1-x86_64.pkg.tar.zst")
oldPkgPath := path.Join(tmpDir, "pkgs", "foo", "webkit-2.3.1-1-x86_64.pkg.tar.zst")
if err := os.MkdirAll(path.Join(tmpDir, "pkgs"), 0o755); err != nil {
log.Fatal(err)
}
if err := os.MkdirAll(path.Join(tmpDir, "pkgs", "foo"), 0o755); err != nil {
log.Fatal(err)
}
if _, err := os.Create(oldPkgPath); err != nil {
log.Fatal(err)
}
if _, err := os.Create(oldPkgPath + ".sig"); err != nil {
log.Fatal(err)
}
// simulate a new prefetched file
newPkgPath := path.Join(tmpDir, "pkgs", "foo", "webkit-2.5.10-4-x86_64.pkg.tar.zst")
if _, err := os.Create(newPkgPath); err != nil {
log.Fatal(err)
}
if _, err := os.Create(newPkgPath + ".sig"); err != nil {
log.Fatal(err)
}
updateDBPrefetchedFile("foo", "webkit-2.5.10-4-x86_64.pkg.tar.zst")
// check if it properly exists
res, err := conn.Query("select * from packages WHERE packages.package_name='webkit' AND packages.arch='x86_64' AND packages.repo_name='foo'")
if err != nil {
log.Fatal(err)
}
counter := 0
for res.Next() {
var got Package
now := time.Now()
err = res.Scan(&got.PackageName, &got.Version, &got.Arch, &got.RepoName, &got.LastTimeDownloaded, &got.LastTimeRepoUpdated)
want := Package{PackageName: "webkit", Version: "2.5.10-4", Arch: "x86_64", RepoName: "foo", LastTimeDownloaded: &now, LastTimeRepoUpdated: &now}
if !cmp.Equal(got, want, cmpopts.IgnoreFields(Package{}, "LastTimeDownloaded", "LastTimeRepoUpdated")) {
t.Errorf("\ngot %v,\nwant %v", got, want)
}
dist := want.LastTimeDownloaded.Sub(*got.LastTimeDownloaded)
if dist < -5*time.Second {
t.Errorf("Unexpected result, got.LastTimeDownloaded is wrong")
}
if dist > 5*time.Second {
t.Errorf("got %d, want %d", dist, 5*time.Second)
}
dist = want.LastTimeRepoUpdated.Sub(*got.LastTimeRepoUpdated)
if dist < -5*time.Second {
t.Errorf("Unexpected result, got.LastTimeRepoUpdated is wrong")
}
if want.LastTimeRepoUpdated.Sub(*got.LastTimeRepoUpdated) > 5*time.Second {
t.Errorf("got %d, want %d", want.LastTimeRepoUpdated.Sub(*got.LastTimeRepoUpdated), 5*time.Second)
}
if err != nil {
log.Fatal(err)
}
if counter >= 1 {
t.Errorf("Too many entries, expected %d, found %d", 1, counter+1)
}
counter++
}
if counter == 0 {
t.Errorf("Too few entries, expected %d, found %d", 1, counter+1)
}
// now, check if files have been properly handled
exists, err := fileExists(oldPkgPath)
if err != nil {
log.Fatal(err)
}
if exists {
t.Errorf("File %v should have been deleted", oldPkgPath)
}
exists, err = fileExists(oldPkgPath + ".sig")
if err != nil {
log.Fatal(err)
}
if exists {
t.Errorf("File %v should have been deleted", oldPkgPath+".sig")
}
exists, err = fileExists(newPkgPath + ".sig")
if err != nil {
log.Fatal(err)
}
if !exists {
t.Errorf("File %v should exist", newPkgPath+".sig")
}
exists, err = fileExists(newPkgPath)
if err != nil {
log.Fatal(err)
}
if !exists {
t.Errorf("File %v should exist", newPkgPath)
}
}
func TestPurgePkgIfExists(t *testing.T) {
tmpDir := testSetupHelper(t)
setupPrefetch()
updateDBRequestedFile("foo", "webkit-2.3.1-1-x86_64.pkg.tar.zst")
oldPkgPath := path.Join(tmpDir, "pkgs", "foo", "webkit-2.3.1-1-x86_64.pkg.tar.zst")
if err := os.MkdirAll(path.Join(tmpDir, "pkgs"), 0o755); err != nil {
log.Fatal(err)
}
if err := os.MkdirAll(path.Join(tmpDir, "pkgs", "foo"), 0o755); err != nil {
log.Fatal(err)
}
if _, err := os.Create(oldPkgPath); err != nil {
log.Fatal(err)
}
if _, err := os.Create(oldPkgPath + ".sig"); err != nil {
log.Fatal(err)
}
if _, err := os.Create(oldPkgPath + ".ssig"); err != nil {
log.Fatal(err)
}
pkgToPurge := getPackage("webkit", "x86_64", "foo")
purgePkgIfExists(&pkgToPurge)
// now, check if files have been properly handled
exists, err := fileExists(oldPkgPath)
if err != nil {
log.Fatal(err)
}
if exists {
t.Errorf("File %v should have been deleted", oldPkgPath)
}
exists, err = fileExists(oldPkgPath + ".sig")
if err != nil {
log.Fatal(err)
}
if exists {
t.Errorf("File %v should have been deleted", oldPkgPath+".sig")
}
exists, err = fileExists(oldPkgPath + ".ssig")
if err != nil {
log.Fatal(err)
}
if !exists {
t.Errorf("File %v should exist", oldPkgPath+".ssig")
}
}
func TestCleanPrefetchDB(t *testing.T) {
tmpDir := testSetupHelper(t)
setupPrefetch()
updateDBRequestedFile("foo", "webkit-2.3.1-1-x86_64.pkg.tar.zst")
oldPkgPath := path.Join(tmpDir, "pkgs", "foo", "webkit-2.3.1-1-x86_64.pkg.tar.zst")
if err := os.MkdirAll(path.Join(tmpDir, "pkgs"), 0o755); err != nil {
log.Fatal(err)
}
if err := os.MkdirAll(path.Join(tmpDir, "pkgs", "foo"), 0o755); err != nil {
log.Fatal(err)
}
if _, err := os.Create(oldPkgPath); err != nil {
log.Fatal(err)
}
if _, err := os.Create(oldPkgPath + ".sig"); err != nil {
log.Fatal(err)
}
// created some files
cleanPrefetchDB()
// should delete nothing
exists, err := fileExists(oldPkgPath + ".sig")
if err != nil {
log.Fatal(err)
}
if !exists {
t.Errorf("File %v should exist", oldPkgPath+".sig")
}
exists, err = fileExists(oldPkgPath)
if err != nil {
log.Fatal(err)
}
if !exists {
t.Errorf("File %v should exist", oldPkgPath)
}
// now i update some of its data
pkg := getPackage("webkit", "x86_64", "foo")
oneMonthAgo := time.Now().AddDate(0, -1, 0) // more or less
// updated one month ago but downloaded now, should not be deleted
pkg.LastTimeRepoUpdated = &oneMonthAgo
if db := prefetchDB.Save(pkg); db.Error != nil {
t.Error(db.Error)
}
cleanPrefetchDB()
// should delete nothing
latestPkgInDB := getPackage("webkit", "x86_64", "foo")
if pkg.PackageName != latestPkgInDB.PackageName || pkg.Arch != latestPkgInDB.Arch || pkg.RepoName != latestPkgInDB.RepoName {
t.Errorf("Package shouldn't be altered, was \n%v, now it is \n%v", pkg, latestPkgInDB)
}
exists, err = fileExists(oldPkgPath + ".sig")
if err != nil {
log.Fatal(err)
}
if !exists {
t.Errorf("File %v should exist", oldPkgPath+".sig")
}
exists, err = fileExists(oldPkgPath)
if err != nil {
log.Fatal(err)
}
if !exists {
t.Errorf("File %v should exist", oldPkgPath)
}
// now it should be deleted, knowing that a package is dead (in this configuration) if older than 20 days
pkg.LastTimeDownloaded = &oneMonthAgo
if db := prefetchDB.Save(pkg); db.Error != nil {
t.Error(db.Error)
}
cleanPrefetchDB()
latestPkgInDB = getPackage("webkit", "x86_64", "foo")
if latestPkgInDB.PackageName != "" && pkg.Arch != "" {
t.Errorf("Package should have been deleted")
}
exists, err = fileExists(oldPkgPath + ".sig")
if err != nil {
log.Fatal(err)
}
if exists {
t.Errorf("File %v should not exist", oldPkgPath+".sig")
}
exists, err = fileExists(oldPkgPath)
if err != nil {
log.Fatal(err)
}
if exists {
t.Errorf("File %v not should exist", oldPkgPath)
}
}
func TestPrefetchAllPkgs(t *testing.T) {
// Fully tested in integration test
testSetupHelper(t)
setupPrefetch()
cleanPrefetchDB()
prefetchAllPkgs()
}
func TestPrefetchPackages(t *testing.T) {
// Fully tested in integration test
testSetupHelper(t)
setupPrefetch()
prefetchPackages()
}
func TestGetCronDuration(t *testing.T) {
now := time.Now()
var expectedTime time.Time
if now.Hour() < 3 {
expectedTime = now
} else {
expectedTime = now.AddDate(0, 0, 1)
}
expectedTime = time.Date(expectedTime.Year(), expectedTime.Month(), expectedTime.Day(), 3, 0, 0, 0, expectedTime.Location())
expectedDuration := expectedTime.Sub(now)
got, err := getCronDuration("0 0 3 * * * *", now)
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if got != expectedDuration {
t.Errorf("getCronDuration() = %v, want %v", got, expectedDuration)
}
}