-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipvs_test.go
100 lines (85 loc) · 2.24 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
package libipvs
import (
"testing"
)
func TestFlush(t *testing.T) {
var err error
ipvsHandler, err := NewIPVSHandler()
if err != nil {
t.Fatalf("Failed create IPVSHandler %s", err)
}
err = ipvsHandler.Flush()
if err != nil {
t.Fatalf("Failed Flush %s", err)
}
}
func TestAddAndDeleteService(t *testing.T) {
var err error
vip := "127.1.1.1"
vport := 8888
protocol := "TCP"
scheduler1 := "wlc"
scheduler2 := "wrr"
rip1 := "127.2.1.1"
rip2 := "127.2.1.2"
rport := 8888
weight1 := 100
weight2 := 200
method := "DR"
ipvsHandler, err := NewIPVSHandler()
if err != nil {
t.Fatalf("Failed create IPVSHandler %s", err)
}
// Add VIP
err = ipvsHandler.AddService(vip, vport, protocol, scheduler1)
if err != nil {
t.Fatalf("Failed add service %s", err)
}
isRegisterd, err := ipvsHandler.IsRegisteredService(vip, vport, protocol)
if err != nil {
t.Fatalf("Failed IsRegisteredService %s", err)
}
if !isRegisterd {
t.Fatalf("Failed IsRegisteredService %s", err)
}
// Update VIP
err = ipvsHandler.UpdateService(vip, vport, "TCP", scheduler2)
if err != nil {
t.Fatalf("Failed update service %s", err)
}
service, err := ipvsHandler.GetService(vip, vport, protocol)
if err != nil {
t.Fatalf("Failed get service %s", err)
}
err = ipvsHandler.AddDestination(service, rip1, rport, weight1, method)
err = ipvsHandler.AddDestination(service, rip2, rport, weight2, method)
if err != nil {
t.Fatalf("Failed add destination %s", err)
}
err = ipvsHandler.UpdateDestination(service, rip1, rport, weight2, method)
err = ipvsHandler.UpdateDestination(service, rip2, rport, weight1, method)
if err != nil {
t.Fatalf("Failed update destination %s", err)
}
entries, err := ipvsHandler.GetAllEntry()
if err != nil {
t.Fatalf("Failed get all entries %s", err)
}
for _, entry := range entries {
if entry.Service.Address != vip {
t.Errorf("different address %s", entry.Service.Address)
}
if entry.Service.Protocol != protocol {
t.Errorf("different protocol %s", entry.Service.Address)
}
for _, d := range entry.Destinations {
if d.Method != method {
t.Errorf("different method %s", d.Method)
}
}
}
err = ipvsHandler.DeleteService(vip, vport, protocol)
if err != nil {
t.Fatalf("Failed delete service %s", err)
}
}