-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlb_norace_test.go
167 lines (142 loc) · 6.72 KB
/
lb_norace_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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//go:build !race
// +build !race
package main
import (
"fmt"
"net"
"os"
"testing"
"time"
)
// can't test with -race flag due to defaultDnsResolver change
func TestLbChanges(t *testing.T) {
config := `lb:
- engine: testEngine # load balancer engine. only 'nftables' supported for now
targets:
- name: TestInitDnsCheck # target name
protocol: tcp # transport protocol. only tcp supported for now
port: 8082 # target port
upstream_group: # upstream_group to be used for target
name: ug # upstream_group name
distribution: round-robin # ug traffic distribution mode. only round-robin supported for now
upstreams:
- name: lt8082 # upstream name
host: lobby-test.ipbuff.com # upstream host. IP Address or domain name
port: 8082 # upstream port
dns: # include in case you want to use specific DNS to resolve the fqdn host address. If host is IPv4 or IPv6 this setting will not have any effect. In case this mapping is not present the OS resolvers will be used
servers: # dns address list. Queries will be done sequentially in case of failure
- 1.1.1.1 # cloudflare IPv4 DNS
- 2606:4700::1111 # cloudflare IPv6 DNS
ttl: 1 # custom ttl can be specified to overwrite the DNS response TTL
health_check: # don't include the health_check mapping or leave it empty to disable health_check. upstreams will be considered always as active when health_checks are not enabled
protocol: tcp # health-heck protocol. only tcp supported for now
port: 8082 # health_check port
start_available: true # set 'true' if upstream should be considered as available at start. set 'false' otherwise
probe:
check_interval: 1 # seconds. Max value: 65536
timeout: 2 # seconds. Max value: 256
success_count: 3 # amount of successful health checks to become active
`
testConfigPath := "/tmp/lobby_test_conf.yaml"
lobbySettings.configFilePath = testConfigPath
os.WriteFile(testConfigPath, []byte(config), 0400)
// mock dns resolvers
var mockResolverSucceed resolver = func(f string, dnsa []string) (net.IP, uint32, error) {
return net.ParseIP("1.1.1.1"), 600, nil
}
var mockResolverFail resolver = func(f string, dnsa []string) (net.IP, uint32, error) {
return net.ParseIP("1.1.1.1"), 600, fmt.Errorf("Some DNS error")
}
wt := 6
// start by testing dns check fail on boot
defaultDnsResolver = mockResolverFail
lbs, err := lbInit()
if err != nil {
t.Errorf("lbInit returned an unexpected error: '%v'", err)
}
for _, l := range lbs {
if err := l.start(); err != nil {
t.Errorf("lb start returned an unexpected error: '%v'", err)
}
}
os.Remove(testConfigPath)
t.Logf("waiting %ds for LB to boot", wt)
time.Sleep(time.Duration(wt) * time.Second)
// test success
defaultDnsResolver = mockResolverSucceed
t.Logf("waiting %ds for DNS check and upstream update to complete", wt)
time.Sleep(time.Duration(wt) * time.Second)
defaultDnsResolver = miekgResolver
t.Logf("waiting %ds for DNS check and upstream update to complete", wt)
time.Sleep(time.Duration(wt) * time.Second)
// test failure
defaultDnsResolver = mockResolverFail
t.Logf("waiting %ds for DNS check and upstream update to complete", wt)
time.Sleep(time.Duration(wt) * time.Second)
defaultDnsResolver = miekgResolver
lbs[0].stop()
}
func Test0TTL(t *testing.T) {
config := `lb:
- engine: testEngine # load balancer engine. only 'nftables' supported for now
targets:
- name: TestInitDnsCheck # target name
protocol: tcp # transport protocol. only tcp supported for now
port: 8082 # target port
upstream_group: # upstream_group to be used for target
name: ug # upstream_group name
distribution: round-robin # ug traffic distribution mode. only round-robin supported for now
upstreams:
- name: lt8082 # upstream name
host: lobby-test.ipbuff.com # upstream host. IP Address or domain name
port: 8082 # upstream port
dns: # include in case you want to use specific DNS to resolve the fqdn host address. If host is IPv4 or IPv6 this setting will not have any effect. In case this mapping is not present the OS resolvers will be used
servers: # dns address list. Queries will be done sequentially in case of failure
- 1.1.1.1 # cloudflare IPv4 DNS
- 2606:4700::1111 # cloudflare IPv6 DNS
`
testConfigPath := "/tmp/lobby_test_conf.yaml"
lobbySettings.configFilePath = testConfigPath
os.WriteFile(testConfigPath, []byte(config), 0400)
// mock dns resolvers
var mockResolverSucceed resolver = func(f string, dnsa []string) (net.IP, uint32, error) {
return net.ParseIP("1.1.1.1"), 1, nil
}
var mockResolver0TTL resolver = func(f string, dnsa []string) (net.IP, uint32, error) {
return net.ParseIP("1.1.1.1"), 0, nil
}
wt := 3
defaultDnsResolver = mockResolverSucceed
lbs, err := lbInit()
if err != nil {
t.Errorf("lbInit returned an unexpected error: '%v'", err)
}
for _, l := range lbs {
if err := l.start(); err != nil {
t.Errorf("lb start returned an unexpected error: '%v'", err)
}
}
t.Logf("waiting %ds for LB to boot", wt)
time.Sleep(time.Duration(wt) * time.Second)
// test returned 0 TTL
defaultDnsResolver = mockResolver0TTL
t.Logf("waiting %ds for DNS check and upstream update to complete", wt)
time.Sleep(time.Duration(wt) * time.Second)
lbs[0].stop()
// Now test with 0 TTL at start
defaultDnsResolver = mockResolver0TTL
lbs, err = lbInit()
if err != nil {
t.Errorf("lbInit returned an unexpected error: '%v'", err)
}
for _, l := range lbs {
if err := l.start(); err != nil {
t.Errorf("lb start returned an unexpected error: '%v'", err)
}
}
os.Remove(testConfigPath)
t.Logf("waiting %ds for LB to boot", wt)
time.Sleep(time.Duration(wt) * time.Second)
lbs[0].stop()
defaultDnsResolver = miekgResolver
}