-
Notifications
You must be signed in to change notification settings - Fork 14
/
ipvs_test.go
315 lines (268 loc) · 7.45 KB
/
ipvs_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// +build linux
package libipvs
import (
"fmt"
"net"
"os/exec"
"strings"
"syscall"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
schedMethods = []string{
RoundRobin,
LeastConnection,
DestinationHashing,
SourceHashing,
}
schedFlags = Flags{Flags: IP_VS_SVC_F_SCHED1 | IP_VS_SVC_F_SCHED2 | IP_VS_SVC_F_SCHED3, Mask: ^uint32(0)}
schedFlagNames = "(flag-1,flag-2,flag-3)"
shFlagNames = "(sh-fallback,sh-port,flag-3)"
protocols = []string{
"TCP",
"UDP",
"FWM",
}
fwdMethods = []FwdMethod{
IP_VS_CONN_F_MASQ,
IP_VS_CONN_F_TUNNEL,
IP_VS_CONN_F_DROUTE,
}
fwdMethodStrings = []string{
"Masq",
"Tunnel",
"Route",
}
)
func checkDestination(t *testing.T, checkPresent bool, protocol, serviceAddress, realAddress, fwdMethod string) {
var (
realServerStart bool
realServers []string
)
out, err := exec.Command("ipvsadm", "-Ln").CombinedOutput()
require.NoError(t, err)
for _, o := range strings.Split(string(out), "\n") {
cmpStr := serviceAddress
if protocol == "FWM" {
cmpStr = " " + cmpStr
}
if strings.Contains(o, cmpStr) {
realServerStart = true
continue
}
if realServerStart {
if !strings.Contains(o, "->") {
break
}
realServers = append(realServers, o)
}
}
for _, r := range realServers {
if strings.Contains(r, realAddress) {
parts := strings.Fields(r)
assert.Equal(t, fwdMethod, parts[2])
return
}
}
if checkPresent {
t.Fatalf("Did not find the destination %s fwdMethod %s in ipvs output", realAddress, fwdMethod)
}
}
func checkService(t *testing.T, checkPresent bool, protocol, schedMethod, serviceAddress string) {
out, err := exec.Command("ipvsadm", "-Ln").CombinedOutput()
require.NoError(t, err)
for _, o := range strings.Split(string(out), "\n") {
cmpStr := serviceAddress
if protocol == "FWM" {
cmpStr = " " + cmpStr
}
if strings.Contains(o, cmpStr) {
parts := strings.Split(o, " ")
assert.Equal(t, protocol, parts[0])
assert.Equal(t, serviceAddress, parts[2])
assert.Equal(t, schedMethod, parts[3])
if schedMethod == SourceHashing {
assert.Equal(t, shFlagNames, parts[4])
} else {
assert.Equal(t, schedFlagNames, parts[4])
}
if !checkPresent {
t.Fatalf("Did not expect the service %s in ipvs output", serviceAddress)
}
return
}
}
if checkPresent {
t.Fatalf("Did not find the service %s in ipvs output", serviceAddress)
}
}
func clearIpvsState(t *testing.T) {
_, err := exec.Command("ipvsadm", "--clear").CombinedOutput()
require.NoError(t, err)
}
func TestService(t *testing.T) {
defer clearIpvsState(t)
i, err := New()
require.NoError(t, err)
for _, protocol := range protocols {
for _, schedMethod := range schedMethods {
var serviceAddress string
s := Service{
AddressFamily: syscall.AF_INET,
SchedName: schedMethod,
Flags: schedFlags,
}
switch protocol {
case "FWM":
s.FWMark = 1234
serviceAddress = fmt.Sprintf("%d", 1234)
case "TCP":
s.Protocol = syscall.IPPROTO_TCP
s.Port = 80
s.Address = net.ParseIP("1.2.3.4")
s.Netmask = 0xFFFFFFFF
serviceAddress = "1.2.3.4:80"
case "UDP":
s.Protocol = syscall.IPPROTO_UDP
s.Port = 53
s.Address = net.ParseIP("2.3.4.5")
serviceAddress = "2.3.4.5:53"
}
err := i.NewService(&s)
assert.NoError(t, err)
checkService(t, true, protocol, schedMethod, serviceAddress)
svcs, err := i.ListServices()
assert.NoError(t, err)
var listed *Service
assert.NotEmpty(t, svcs, "should list at least one virtual service")
for _, svc := range svcs {
if svc.FWMark > 0 && svc.FWMark == s.FWMark {
listed = svc
break
}
if svc.Address.Equal(s.Address) && svc.Port == s.Port && svc.Protocol == s.Protocol {
listed = svc
break
}
}
assert.NotNil(t, listed, "could not list %v", s)
if listed != nil {
assert.Equal(t, s.AddressFamily, listed.AddressFamily)
assert.Equal(t, s.SchedName, listed.SchedName)
expectedFlags := s.Flags
expectedFlags.Flags |= IP_VS_SVC_F_HASHED
assert.Equal(t, expectedFlags, listed.Flags)
assert.Equal(t, s.Netmask, listed.Netmask)
}
var lastMethod string
for _, updateSchedMethod := range schedMethods {
if updateSchedMethod == schedMethod {
continue
}
s.SchedName = updateSchedMethod
err = i.UpdateService(&s)
assert.NoError(t, err)
checkService(t, true, protocol, updateSchedMethod, serviceAddress)
lastMethod = updateSchedMethod
}
err = i.DelService(&s)
checkService(t, false, protocol, lastMethod, serviceAddress)
}
}
}
func TestDestination(t *testing.T) {
defer clearIpvsState(t)
i, err := New()
require.NoError(t, err)
for _, protocol := range protocols {
var serviceAddress string
s := Service{
AddressFamily: syscall.AF_INET,
SchedName: RoundRobin,
Flags: schedFlags,
}
switch protocol {
case "FWM":
s.FWMark = 1234
serviceAddress = fmt.Sprintf("%d", 1234)
case "TCP":
s.Protocol = syscall.IPPROTO_TCP
s.Port = 80
s.Address = net.ParseIP("1.2.3.4")
s.Netmask = 0xFFFFFFFF
serviceAddress = "1.2.3.4:80"
case "UDP":
s.Protocol = syscall.IPPROTO_UDP
s.Port = 53
s.Address = net.ParseIP("2.3.4.5")
serviceAddress = "2.3.4.5:53"
}
err := i.NewService(&s)
assert.NoError(t, err)
checkService(t, true, protocol, RoundRobin, serviceAddress)
s.SchedName = ""
for j, fwdMethod := range fwdMethods {
d1 := Destination{
AddressFamily: syscall.AF_INET,
Address: net.ParseIP("10.1.1.2"),
Port: 5000,
Weight: 1,
FwdMethod: fwdMethod,
}
realAddress := "10.1.1.2:5000"
err := i.NewDestination(&s, &d1)
assert.NoError(t, err)
checkDestination(t, true, protocol, serviceAddress, realAddress, fwdMethodStrings[j])
d2 := Destination{
AddressFamily: syscall.AF_INET,
Address: net.ParseIP("10.1.1.3"),
Port: 5000,
Weight: 1,
FwdMethod: fwdMethod,
}
realAddress = "10.1.1.3:5000"
err = i.NewDestination(&s, &d2)
assert.NoError(t, err)
checkDestination(t, true, protocol, serviceAddress, realAddress, fwdMethodStrings[j])
d3 := Destination{
AddressFamily: syscall.AF_INET,
Address: net.ParseIP("10.1.1.4"),
Port: 5000,
Weight: 1,
FwdMethod: fwdMethod,
}
realAddress = "10.1.1.4:5000"
err = i.NewDestination(&s, &d3)
assert.NoError(t, err)
checkDestination(t, true, protocol, serviceAddress, realAddress, fwdMethodStrings[j])
for m, updateFwdMethod := range fwdMethods {
if updateFwdMethod == fwdMethod {
continue
}
d1.FwdMethod = updateFwdMethod
realAddress = "10.1.1.2:5000"
err = i.UpdateDestination(&s, &d1)
assert.NoError(t, err)
checkDestination(t, true, protocol, serviceAddress, realAddress, fwdMethodStrings[m])
d2.FwdMethod = updateFwdMethod
realAddress = "10.1.1.3:5000"
err = i.UpdateDestination(&s, &d2)
assert.NoError(t, err)
checkDestination(t, true, protocol, serviceAddress, realAddress, fwdMethodStrings[m])
d3.FwdMethod = updateFwdMethod
realAddress = "10.1.1.4:5000"
err = i.UpdateDestination(&s, &d3)
assert.NoError(t, err)
checkDestination(t, true, protocol, serviceAddress, realAddress, fwdMethodStrings[m])
}
err = i.DelDestination(&s, &d1)
assert.NoError(t, err)
err = i.DelDestination(&s, &d2)
assert.NoError(t, err)
err = i.DelDestination(&s, &d3)
assert.NoError(t, err)
}
}
}