-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig_test.go
71 lines (64 loc) · 1.81 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
67
68
69
70
71
package main
import (
"bytes"
"strings"
"testing"
)
const testConfigYaml = `
- key:
address: 127.0.0.1
port: 8125
options:
tc_action: block
strategy: src-ip
upstream:
- address: 172.17.0.2
port: 8125
- address: 172.17.0.3
port: 8125
`
func TestConfig(t *testing.T) {
rd := bytes.NewBufferString(testConfigYaml)
cfg, err := newConfigYaml(rd)
if err != nil {
t.Fatal(err)
}
t.Logf("%#v", cfg)
// assert
for _, entry := range *cfg {
// key
if bytes.Compare(entry.Key.Address[:], []byte{0x7f, 0x0, 0x0, 0x1}) != 0 {
t.Fatalf("Key.Address does not match. found: %#v", entry.Key.Address)
}
if bytes.Compare(entry.Key.Port[:], []byte{0x1f, 0xbd}) != 0 {
t.Fatalf("Key.Port does not match. found: %#v", entry.Key.Port)
}
if entry.Key.Slave != 0 {
t.Fatalf("Key.Slave is not 0")
}
if strings.Compare(entry.Key.IP().String(), "127.0.0.1") != 0 {
t.Fatalf("Key.IP() does not return correct address, found: %s", entry.Key.IP().String())
}
// upstream
us1 := entry.Upstream[0]
us2 := entry.Upstream[1]
if strings.Compare(us1.IP().String(), "172.17.0.2") != 0 {
t.Fatalf("us1.IP() does not return correct address, found: %s", us1.IP().String())
}
if strings.Compare(us2.IP().String(), "172.17.0.3") != 0 {
t.Fatalf("us2.IP() does not return correct address, found: %s", us2.IP().String())
}
if bytes.Compare(us1.Port[:], []byte{0x1f, 0xbd}) != 0 {
t.Fatalf("us1.Port does not match. found: %#v", us1.Port)
}
if bytes.Compare(us2.Port[:], []byte{0x1f, 0xbd}) != 0 {
t.Fatalf("us2.Port does not match. found: %#v", us2.Port)
}
if entry.Options.TCAction != 0x2 {
t.Fatalf("options.TCAction is wrong. found: %#v", entry.Options)
}
if entry.Options.Strategy != 0x1 {
t.Fatalf("options.TCAction is wrong. found: %#v", entry.Options)
}
}
}