forked from anatol/pacoloco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirrorlist_test.go
104 lines (96 loc) · 3.05 KB
/
mirrorlist_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
package main
import (
"io/ioutil"
"os"
"path"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestUpdateRepoMirrorlist(t *testing.T) {
var temp = t.TempDir()
var tmpMirrorfile = path.Join(temp, "tmpMirrorFile")
f, err := os.Create(tmpMirrorfile)
if err != nil {
t.Error(err)
}
f.Write([]byte(`################################################################################
################# Arch Linux mirrorlist generated by Reflector #################
################################################################################
# With: reflector --country Germany --verbose --sort rate --save /etc/pacman.d/mirrorlist
# When: 2020-01-10 11:51:21 GMT+7
# From: https://archlinux.org/mirrors/status/json/
# Retrieved: 2020-01-10 11:51:21 GMT+7
# Last Check: 2020-01-10 11:51:21 GMT+7
Server = http://mirror.sample.ca/archlinux/$repo/os/$arch
Server = https://a.ab.net/archlinux/$arch/os/$repo
Server = http://example.tld/repos/$repo/os/$arch#something # Comment
Server =https://whatever.mirror.server.com/$repo/os/$arch
Server= http://localhost/mirror/packages/Blackarch/$repo/os/$arch
Server = https://tooManyTests.com/archlinux/$whatever/os/$arch #Comment
Server = http://another.test.co.uk/archlinux/$repo/o\$s/$arch
Server = http://ThisOneShouldNotBeIncluded.test.co.uk/archlinux/
`))
f.Close()
fileInfo, err := os.Stat(tmpMirrorfile)
if err != nil {
t.Error(err)
}
got := parseConfig([]byte(`
cache_dir: ` + temp + `
purge_files_after: 2592000 # 3600 * 24 * 30days
download_timeout: 200
port: 9139
repos:
archTest:
mirrorlist: ` + tmpMirrorfile + `
`))
config = got
updateMirrorlists()
gotModTime, ok := lastModificationTime[config.Repos["archTest"].Mirrorlist]
if !ok {
t.Errorf("Modification time not recorded")
}
expectedModTime := fileInfo.ModTime()
if gotModTime != expectedModTime {
t.Errorf("Got %v mod time, expected %v mod time", gotModTime, expectedModTime)
}
gotCheckTime, ok := lastMirrorlistCheck[config.Repos["archTest"].Mirrorlist]
if !ok {
t.Errorf("Check time not recorded")
}
if time.Since(gotCheckTime) > 3*time.Second {
t.Errorf("Got %v check time, expected %v check time", gotCheckTime, time.Now())
}
want := &Config{
CacheDir: temp,
Port: 9139,
Repos: map[string]Repo{
"archTest": Repo{
Mirrorlist: tmpMirrorfile,
URLs: []string{`http://mirror.sample.ca/archlinux/`,
`https://a.ab.net/archlinux/`,
`http://example.tld/repos/`,
`https://whatever.mirror.server.com/`,
`http://localhost/mirror/packages/Blackarch/`,
`https://tooManyTests.com/archlinux/`,
`http://another.test.co.uk/archlinux/`,
},
},
},
PurgeFilesAfter: 2592000,
DownloadTimeout: 200,
}
if !cmp.Equal(*got, *want) {
t.Errorf("got %v, want %v", *got, *want)
}
// now, check that it won't update again the file, because it has just updated it
if err := ioutil.WriteFile(tmpMirrorfile, []byte("Nope"), os.ModePerm); err != nil {
t.Fatal(err)
}
updateMirrorlists()
got = config
if !cmp.Equal(*got, *want) {
t.Errorf("got %v, want %v", *got, *want)
}
}