-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_test.go
66 lines (50 loc) · 1.44 KB
/
config_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
package main
import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"testing"
)
func createTempConfig(content string) (tmpfile *os.File, err error) {
tmpfile, err = ioutil.TempFile("", "sitest-test-config")
defer tmpfile.Close()
if err == nil {
_, err = tmpfile.Write([]byte(content))
}
return
}
func TestLoadConfig(t *testing.T) {
assert := assert.New(t)
configFile := `
sites:
"https://site1/":
interval: 15s`
site1 := Site{Config: Config{Interval: 15000000000}}
expectedSites := map[string]*Site{"https://site1/": &site1}
tmpfile, err := createTempConfig(configFile)
defer os.Remove(tmpfile.Name())
assert.Nil(err, "create temp file error")
sitest := new(Sitest)
sitest.ConfigFile = tmpfile.Name()
sitest.LoadConfig()
assert.Equal(sitest.Sites, expectedSites, "assert sites")
}
func TestLoadConfigWithDefault(t *testing.T) {
assert := assert.New(t)
configFile := `default:
interval: 30s
sites:
"https://site2/":
interval: 15s
"https://site3/": {}`
site2 := Site{Config: Config{Interval: 15000000000}}
site3 := Site{Config: Config{Interval: 30000000000}}
expectedSites := map[string]*Site{"https://site2/": &site2, "https://site3/": &site3}
tmpfile, err := createTempConfig(configFile)
defer os.Remove(tmpfile.Name())
assert.Nil(err, "create temp file error")
sitest := new(Sitest)
sitest.ConfigFile = tmpfile.Name()
sitest.LoadConfig()
assert.Equal(sitest.Sites, expectedSites, "assert sites")
}