forked from anatol/pacoloco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urls_test.go
109 lines (91 loc) · 2.92 KB
/
urls_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
package main
import (
"os"
"path"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
const mirrorlist = `################################################################################
################# 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/
`
var expectedURLs = []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/`,
}
func TestParseMirrorlist(t *testing.T) {
temp := t.TempDir()
tmpMirrorfile := path.Join(temp, "tmpMirrorFile")
f, err := os.Create(tmpMirrorfile)
if err == nil {
f.Write([]byte(mirrorlist))
f.Close()
f, err = os.Open(tmpMirrorfile)
}
if err != nil {
t.Error(err)
}
actualURLs, err := parseMirrorlistURLs(f)
if err != nil {
t.Error(err)
}
if !cmp.Equal(actualURLs, expectedURLs) {
t.Errorf("Got %v, expected %v", actualURLs, expectedURLs)
}
}
func TestGetCurrentURLs(t *testing.T) {
temp := t.TempDir()
tmpMirrorfile := path.Join(temp, "tmpMirrorFile")
f, err := os.Create(tmpMirrorfile)
if err == nil {
f.Write([]byte(mirrorlist))
f.Close()
f, err = os.Open(tmpMirrorfile)
}
if err != nil {
t.Error(err)
}
config := parseConfig([]byte(`
cache_dir: ` + temp + `
purge_files_after: 2592000 # 3600 * 24 * 30days
download_timeout: 200
port: 9139
repos:
archTest:
mirrorlist: ` + tmpMirrorfile + `
`))
archTest := config.Repos["archTest"]
urls := archTest.getUrls()
if !cmp.Equal(urls, expectedURLs) {
t.Errorf("Got %v, expected %v", urls, expectedURLs)
}
fileInfo, _ := os.Stat(tmpMirrorfile)
expectedModTime := fileInfo.ModTime()
gotModTime := archTest.LastModificationTime
if gotModTime != expectedModTime {
t.Errorf("Got %v mod time, expected %v mod time", gotModTime, expectedModTime)
}
gotCheckTime := archTest.LastMirrorlistCheck
if time.Since(gotCheckTime) > 3*time.Second {
t.Errorf("Got %v check time, expected %v check time", gotCheckTime, time.Now())
}
}