forked from anatol/pacoloco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefetch_test.go
293 lines (269 loc) · 10.8 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
package main
import (
"database/sql"
"fmt"
"os"
"path"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/require"
)
// 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 TestSetupPrefetch(t *testing.T) {
tmpDir := testSetupHelper(t)
setupPrefetch()
exists, err := fileExists(path.Join(tmpDir, DefaultDBName))
require.NoError(t, err)
require.Truef(t, exists, "setupPrefetch didn't create the db file")
require.NotNil(t, prefetchDB, "Prefetch DB is uninitilized")
conn, err := sql.Open("sqlite3", path.Join(tmpDir, DefaultDBName))
require.NoError(t, err)
require.NotNil(t, conn)
for _, table := range []string{"mirror_dbs", "packages", "mirror_packages"} {
res, err := conn.Query("select * from " + table)
require.NoError(t, err)
for res.Next() {
var pkg Package
err := res.Scan(&pkg.PackageName, &pkg.Version, &pkg.Arch, &pkg.RepoName, &pkg.LastTimeDownloaded, &pkg.LastTimeRepoUpdated)
require.NoError(t, err)
require.Failf(t, "setupPrefetch shouldn't create entries in %v\n", table)
}
}
}
func TestSetupPrefetchTicker(t *testing.T) {
testSetupHelper(t)
setupPrefetch()
ticker := setupPrefetchTicker()
require.NotNil(t, ticker)
ticker.Stop()
}
func TestUpdateDBRequestedFile(t *testing.T) {
tmpDir := testSetupHelper(t)
setupPrefetch()
conn, err := sql.Open("sqlite3", path.Join(tmpDir, DefaultDBName))
require.NoError(t, err)
require.NotNil(t, conn)
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)
require.NoError(t, err)
for res.Next() {
var pkg Package
err := res.Scan(&pkg.PackageName, &pkg.Version, &pkg.Arch, &pkg.RepoName, &pkg.LastTimeDownloaded, &pkg.LastTimeRepoUpdated)
require.NoError(t, err)
require.Failf(t, "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")
require.NoError(t, 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)
require.NoError(t, err)
want := Package{PackageName: "webkit", Version: "2.3.1-1", Arch: "x86_64", RepoName: "foo", LastTimeDownloaded: &now, LastTimeRepoUpdated: &now}
require.True(t, cmp.Equal(got, want, cmpopts.IgnoreFields(Package{}, "LastTimeDownloaded", "LastTimeRepoUpdated")))
dist := want.LastTimeDownloaded.Sub(*got.LastTimeDownloaded)
require.Greater(t, dist, -5*time.Second)
require.Less(t, dist, 5*time.Second)
dist = want.LastTimeRepoUpdated.Sub(*got.LastTimeRepoUpdated)
require.Greater(t, dist, -5*time.Second)
require.Less(t, dist, 5*time.Second)
return
}
require.Fail(t, "updateDBRequestedFile should create entries in packages\n")
}
func TestUpdateDBPrefetchedFile(t *testing.T) {
tmpDir := testSetupHelper(t)
setupPrefetch()
conn, err := sql.Open("sqlite3", path.Join(tmpDir, DefaultDBName))
require.NoError(t, err)
require.NotNil(t, conn)
// 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")
require.NoError(t, os.MkdirAll(path.Join(tmpDir, "pkgs"), 0o755))
require.NoError(t, os.MkdirAll(path.Join(tmpDir, "pkgs", "foo"), 0o755))
_, err = os.Create(oldPkgPath)
require.NoError(t, err)
_, err = os.Create(oldPkgPath + ".sig")
require.NoError(t, err)
// simulate a new prefetched file
newPkgPath := path.Join(tmpDir, "pkgs", "foo", "webkit-2.5.10-4-x86_64.pkg.tar.zst")
_, err = os.Create(newPkgPath)
require.NoError(t, err)
_, err = os.Create(newPkgPath + ".sig")
require.NoError(t, 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'")
require.NoError(t, 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)
require.NoError(t, err)
want := Package{PackageName: "webkit", Version: "2.5.10-4", Arch: "x86_64", RepoName: "foo", LastTimeDownloaded: &now, LastTimeRepoUpdated: &now}
require.True(t, cmp.Equal(got, want, cmpopts.IgnoreFields(Package{}, "LastTimeDownloaded", "LastTimeRepoUpdated")))
// require.Equal(t, want, got)
dist := want.LastTimeDownloaded.Sub(*got.LastTimeDownloaded)
require.Greater(t, dist, -5*time.Second)
require.Less(t, dist, 5*time.Second)
dist = want.LastTimeRepoUpdated.Sub(*got.LastTimeRepoUpdated)
require.Greater(t, dist, -5*time.Second)
require.Less(t, dist, 5*time.Second)
counter++
}
require.Equal(t, 1, counter, "Too many entries")
require.NotEqualf(t, counter, 0, "Too few entries, expected %d, found %d", 1, counter+1)
// now, check if files have been properly handled
exists, err := fileExists(oldPkgPath)
require.NoError(t, err)
require.Falsef(t, exists, "File %v should have been deleted", oldPkgPath)
exists, err = fileExists(oldPkgPath + ".sig")
require.NoError(t, err)
require.Falsef(t, exists, "File %v should have been deleted", oldPkgPath+".sig")
exists, err = fileExists(newPkgPath + ".sig")
require.NoError(t, err)
require.Truef(t, exists, "File %v should exist", newPkgPath+".sig")
exists, err = fileExists(newPkgPath)
require.NoError(t, err)
require.Truef(t, exists, "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")
require.NoError(t, os.MkdirAll(path.Join(tmpDir, "pkgs"), 0o755))
require.NoError(t, os.MkdirAll(path.Join(tmpDir, "pkgs", "foo"), 0o755))
_, err := os.Create(oldPkgPath)
require.NoError(t, err)
_, err = os.Create(oldPkgPath + ".sig")
require.NoError(t, err)
_, err = os.Create(oldPkgPath + ".ssig")
require.NoError(t, err)
pkgToPurge := getPackage("webkit", "x86_64", "foo")
purgePkgIfExists(&pkgToPurge)
// now, check if files have been properly handled
exists, err := fileExists(oldPkgPath)
require.NoError(t, err)
require.Falsef(t, exists, "File %v should have been deleted", oldPkgPath)
exists, err = fileExists(oldPkgPath + ".sig")
require.NoError(t, err)
require.Falsef(t, exists, "File %v should have been deleted", oldPkgPath+".sig")
exists, err = fileExists(oldPkgPath + ".ssig")
require.NoError(t, err)
require.Truef(t, exists, "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")
require.NoError(t, os.MkdirAll(path.Join(tmpDir, "pkgs"), 0o755))
require.NoError(t, os.MkdirAll(path.Join(tmpDir, "pkgs", "foo"), 0o755))
_, err := os.Create(oldPkgPath)
require.NoError(t, err)
_, err = os.Create(oldPkgPath + ".sig")
require.NoError(t, err)
// created some files
cleanPrefetchDB()
// should delete nothing
exists, err := fileExists(oldPkgPath + ".sig")
require.NoError(t, err)
require.Truef(t, exists, "File %v should exist", oldPkgPath+".sig")
exists, err = fileExists(oldPkgPath)
require.NoError(t, err)
require.Truef(t, exists, "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
db := prefetchDB.Save(&pkg)
require.NoError(t, db.Error)
cleanPrefetchDB()
// should delete nothing
latestPkgInDB := getPackage("webkit", "x86_64", "foo")
require.False(t, pkg.PackageName != latestPkgInDB.PackageName || pkg.Arch != latestPkgInDB.Arch || pkg.RepoName != latestPkgInDB.RepoName, "Package shouldn't be altered")
exists, err = fileExists(oldPkgPath + ".sig")
require.NoError(t, err)
require.Truef(t, exists, "File %v should exist", oldPkgPath+".sig")
exists, err = fileExists(oldPkgPath)
require.NoError(t, err)
require.Truef(t, exists, "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
db = prefetchDB.Save(&pkg)
require.NoError(t, db.Error)
cleanPrefetchDB()
latestPkgInDB = getPackage("webkit", "x86_64", "foo")
require.False(t, latestPkgInDB.PackageName != "" && pkg.Arch != "", "Package should have been deleted")
exists, err = fileExists(oldPkgPath + ".sig")
require.NoError(t, err)
require.Falsef(t, exists, "File %v should not exist", oldPkgPath+".sig")
exists, err = fileExists(oldPkgPath)
require.NoError(t, err)
require.Falsef(t, exists, "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)
require.NoError(t, err)
require.Equal(t, got, expectedDuration)
}