forked from anatol/pacoloco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urls_test.go
99 lines (81 loc) · 2.88 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
package main
import (
"os"
"path"
"testing"
"time"
"github.com/stretchr/testify/require"
)
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)
require.NoError(t, err)
f.Write([]byte(mirrorlist))
f.Close()
f, err = os.Open(tmpMirrorfile)
actualURLs, err := parseMirrorlistURLs(f)
require.NoError(t, err)
require.Equal(t, expectedURLs, actualURLs)
}
func TestGetCurrentURLs(t *testing.T) {
temp := t.TempDir()
tmpMirrorfile := path.Join(temp, "tmpMirrorFile")
require.NoError(t, os.WriteFile(tmpMirrorfile, []byte(mirrorlist), 0o644))
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"]
require.Equal(t, expectedURLs, archTest.getUrls())
fileInfo, _ := os.Stat(tmpMirrorfile)
require.Equal(t, fileInfo.ModTime(), archTest.LastModificationTime)
gotCheckTime := archTest.LastMirrorlistCheck
require.LessOrEqual(t, time.Since(gotCheckTime), 3*time.Second)
}
func TestEmptyMirrorlist(t *testing.T) {
temp := t.TempDir()
tmpMirrorfile := path.Join(temp, "tmpMirrorFile")
require.NoError(t, os.WriteFile(tmpMirrorfile, []byte(""), 0o644))
config := parseConfig([]byte(`
cache_dir: ` + temp + `
repos:
archTest:
mirrorlist: ` + tmpMirrorfile + `
`))
archTest := config.Repos["archTest"]
urls, err := archTest.getMirrorlistURLs()
require.Error(t, err)
require.Nil(t, urls)
}