-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathconfig_test.go
More file actions
346 lines (318 loc) · 7.4 KB
/
config_test.go
File metadata and controls
346 lines (318 loc) · 7.4 KB
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
package main
import (
"os"
"path"
"testing"
"github.com/stretchr/testify/require"
)
// test that `parseConfig()` can successfully load YAML config
func TestLoadConfig(t *testing.T) {
temp := t.TempDir()
_, err := parseConfig([]byte(`
port: 9129
cache_dir: ` + temp + `
purge_files_after: 2592000 # 3600 * 24 * 30days
download_timeout: 200 # 200 seconds
repos:
archlinux:
urls:
- http://mirror.lty.me/archlinux
- http://mirrors.kernel.org/archlinux
quarry:
url: http://pkgbuild.com/~anatolik/quarry/x86_64
sublime:
url: https://download.sublimetext.com/arch/stable/x86_64
`))
require.NoError(t, err)
}
// test with prefetch set
func TestLoadConfigWithPrefetch(t *testing.T) {
got, err := parseConfig([]byte(`
cache_dir: /tmp
purge_files_after: 2592000 # 3600 * 24 * 30days
prefetch:
cron: 0 0 3 * * * *
ttl_unaccessed_in_days: 5
download_timeout: 200
port: 9139
repos:
archlinux:
url: http://mirrors.kernel.org/archlinux
`))
require.NoError(t, err)
want := &Config{
CacheDir: `/tmp`,
Port: 9139,
Repos: map[string]*Repo{
"archlinux": {
URL: "http://mirrors.kernel.org/archlinux",
},
},
PurgeFilesAfter: 2592000,
DownloadTimeout: 200,
Prefetch: &RefreshPeriod{Cron: "0 0 3 * * * *", TTLUnaccessed: 5, TTLUnupdated: 200},
}
require.Equal(t, want, got)
}
// test that `purgeFilesAfter` is being read correctly
func TestPurgeFilesAfter(t *testing.T) {
got, err := parseConfig([]byte(`
cache_dir: /tmp
purge_files_after: 2592000 # 3600 * 24 * 30days
repos:
archlinux:
url: http://mirrors.kernel.org/archlinux
`))
require.NoError(t, err)
want := &Config{
CacheDir: `/tmp`,
Port: 9129,
Repos: map[string]*Repo{
"archlinux": {
URL: "http://mirrors.kernel.org/archlinux",
},
},
PurgeFilesAfter: 2592000,
DownloadTimeout: 0,
Prefetch: nil,
}
require.Equal(t, want, got)
}
// test that config works without `purgeFilesAfter`
func TestWithoutPurgeFilesAfter(t *testing.T) {
got, err := parseConfig([]byte(`
cache_dir: /tmp
repos:
archlinux:
url: http://mirrors.kernel.org/archlinux
`))
require.NoError(t, err)
want := &Config{
CacheDir: `/tmp`,
Port: 9129,
Repos: map[string]*Repo{
"archlinux": {
URL: "http://mirrors.kernel.org/archlinux",
},
},
PurgeFilesAfter: 0,
DownloadTimeout: 0,
Prefetch: nil,
}
require.Equal(t, want, got)
}
func TestLoadConfigWithMirrorlist(t *testing.T) {
temp := t.TempDir()
tmpfile := path.Join(temp, "tmpMirrorFile")
f, err := os.Create(tmpfile)
require.NoError(t, err)
f.Close()
got, err := parseConfig([]byte(`
cache_dir: ` + temp + `
purge_files_after: 2592000 # 3600 * 24 * 30days
prefetch:
cron: 0 0 3 * * * *
ttl_unaccessed_in_days: 5
download_timeout: 200
port: 9139
repos:
archlinux:
mirrorlist: ` + tmpfile + `
`))
require.NoError(t, err)
want := &Config{
CacheDir: temp,
Port: 9139,
Repos: map[string]*Repo{
"archlinux": {
Mirrorlist: tmpfile,
},
},
PurgeFilesAfter: 2592000,
DownloadTimeout: 200,
Prefetch: &RefreshPeriod{Cron: "0 0 3 * * * *", TTLUnaccessed: 5, TTLUnupdated: 200},
}
require.Equal(t, want, got)
}
func TestLoadConfigWithMirrorlistTimestamps(t *testing.T) {
got, err := parseConfig([]byte(`
cache_dir: /tmp
repos:
archlinux:
url: http://mirrors.kernel.org/archlinux
# these fields *shouldn't* be unmarshalled
lastmirrorlistcheck: 2
lastmodificationtime: 2
`))
require.NoError(t, err)
want := &Config{
CacheDir: "/tmp",
Port: DefaultPort,
Repos: map[string]*Repo{
"archlinux": {
URL: "http://mirrors.kernel.org/archlinux",
},
},
}
require.Equal(t, want, got)
}
// test with Tls enabled
func TestLoadConfigWithTls(t *testing.T) {
got, err := parseConfig([]byte(`
cache_dir: /tmp
download_timeout: 200
port: 9139
tls:
key: config_test.go
cert: config_test.go
repos:
archlinux:
url: http://mirrors.kernel.org/archlinux
`))
require.NoError(t, err)
want := &Config{
CacheDir: `/tmp`,
Port: 9139,
Repos: map[string]*Repo{
"archlinux": {
URL: "http://mirrors.kernel.org/archlinux",
},
},
DownloadTimeout: 200,
Tls: &Tls{
Key: "config_test.go",
Certificate: "config_test.go",
},
}
require.Equal(t, want, got)
}
// Error path tests
func TestParseConfigInvalidYAML(t *testing.T) {
_, err := parseConfig([]byte(`
cache_dir: /tmp
repos:
- this is not valid yaml mapping
archlinux:
`))
require.Error(t, err)
}
func TestParseConfigBothURLAndURLs(t *testing.T) {
_, err := parseConfig([]byte(`
cache_dir: /tmp
repos:
archlinux:
url: http://mirror1.example.com/archlinux
urls:
- http://mirror2.example.com/archlinux
`))
require.Error(t, err)
require.Contains(t, err.Error(), "both url and urls")
}
func TestParseConfigBothURLAndMirrorlist(t *testing.T) {
temp := t.TempDir()
tmpfile := path.Join(temp, "mirrorlist")
require.NoError(t, os.WriteFile(tmpfile, []byte("Server = http://example.com/$repo/os/$arch"), 0o644))
_, err := parseConfig([]byte(`
cache_dir: ` + temp + `
repos:
archlinux:
url: http://mirror1.example.com/archlinux
mirrorlist: ` + tmpfile + `
`))
require.Error(t, err)
require.Contains(t, err.Error(), "both url and mirrorlist")
}
func TestParseConfigNoURLs(t *testing.T) {
_, err := parseConfig([]byte(`
cache_dir: /tmp
repos:
archlinux: {}
`))
require.Error(t, err)
require.Contains(t, err.Error(), "specify url(s) or mirrorlist")
}
func TestParseConfigPurgeFilesAfterTooLow(t *testing.T) {
_, err := parseConfig([]byte(`
cache_dir: /tmp
purge_files_after: 100
repos:
archlinux:
url: http://mirrors.kernel.org/archlinux
`))
require.Error(t, err)
require.Contains(t, err.Error(), "purge_files_after")
}
func TestParseConfigInvalidCron(t *testing.T) {
_, err := parseConfig([]byte(`
cache_dir: /tmp
prefetch:
cron: not-a-valid-cron
repos:
archlinux:
url: http://mirrors.kernel.org/archlinux
`))
require.Error(t, err)
require.Contains(t, err.Error(), "cron")
}
func TestParseConfigNegativeTTLUnaccessed(t *testing.T) {
_, err := parseConfig([]byte(`
cache_dir: /tmp
prefetch:
cron: 0 0 3 * * * *
ttl_unaccessed_in_days: -1
repos:
archlinux:
url: http://mirrors.kernel.org/archlinux
`))
require.Error(t, err)
require.Contains(t, err.Error(), "ttl_unaccessed_in_days")
}
func TestParseConfigNegativeTTLUnupdated(t *testing.T) {
_, err := parseConfig([]byte(`
cache_dir: /tmp
prefetch:
cron: 0 0 3 * * * *
ttl_unupdated_in_days: -5
repos:
archlinux:
url: http://mirrors.kernel.org/archlinux
`))
require.Error(t, err)
require.Contains(t, err.Error(), "ttl_unupdated_in_days")
}
func TestParseConfigInvalidCacheDir(t *testing.T) {
_, err := parseConfig([]byte(`
cache_dir: /nonexistent/path/that/does/not/exist
repos:
archlinux:
url: http://mirrors.kernel.org/archlinux
`))
require.Error(t, err)
require.Contains(t, err.Error(), "does not exist or isn't writable")
}
func TestParseConfigInvalidTLSPaths(t *testing.T) {
_, err := parseConfig([]byte(`
cache_dir: /tmp
tls:
cert: /nonexistent/cert.pem
key: /nonexistent/key.pem
repos:
archlinux:
url: http://mirrors.kernel.org/archlinux
`))
require.Error(t, err)
require.Contains(t, err.Error(), "tls cert file")
}
func TestParseConfigInvalidTLSKey(t *testing.T) {
_, err := parseConfig([]byte(`
cache_dir: /tmp
tls:
cert: config_test.go
key: /nonexistent/key.pem
repos:
archlinux:
url: http://mirrors.kernel.org/archlinux
`))
require.Error(t, err)
require.Contains(t, err.Error(), "tls key file")
}