-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathurls_test.go
More file actions
135 lines (111 loc) · 3.92 KB
/
urls_test.go
File metadata and controls
135 lines (111 loc) · 3.92 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
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 TestGetUrlsSingleURL(t *testing.T) {
repo := &Repo{URL: "http://mirror.example.com/archlinux"}
urls := repo.getUrls()
require.Equal(t, []string{"http://mirror.example.com/archlinux"}, urls)
}
func TestGetUrlsMultipleURLs(t *testing.T) {
repo := &Repo{URLs: []string{
"http://mirror1.example.com/archlinux",
"http://mirror2.example.com/archlinux",
}}
urls := repo.getUrls()
require.Equal(t, []string{
"http://mirror1.example.com/archlinux",
"http://mirror2.example.com/archlinux",
}, urls)
}
func TestMirrorlistCaching(t *testing.T) {
temp := t.TempDir()
tmpMirrorfile := path.Join(temp, "tmpMirrorFile")
require.NoError(t, os.WriteFile(tmpMirrorfile, []byte(mirrorlist), 0o644))
repo := &Repo{Mirrorlist: tmpMirrorfile}
// First call should parse the mirrorlist
urls := repo.getUrls()
require.Equal(t, expectedURLs, urls)
// Second immediate call should return cached results (within 5s check interval)
urls2 := repo.getUrls()
require.Equal(t, expectedURLs, urls2)
}
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, err := parseConfig([]byte(`
cache_dir: ` + temp + `
purge_files_after: 2592000 # 3600 * 24 * 30days
download_timeout: 200
port: 9139
repos:
archTest:
mirrorlist: ` + tmpMirrorfile + `
`))
require.NoError(t, err)
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, err := parseConfig([]byte(`
cache_dir: ` + temp + `
repos:
archTest:
mirrorlist: ` + tmpMirrorfile + `
`))
require.NoError(t, err)
archTest := config.Repos["archTest"]
urls, err := archTest.getMirrorlistURLs()
require.Error(t, err)
require.Nil(t, urls)
}