-
Notifications
You must be signed in to change notification settings - Fork 0
/
keepalived_config_test.go
150 lines (135 loc) · 2.83 KB
/
keepalived_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
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
package main
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEmpty(t *testing.T) {
reader := strings.NewReader("")
cfg, err := parseKeepalivedConfig(reader)
if assert.NoError(t, err) {
assert.Equal(t, cfg, &keepalivedConfig{
vrrpInstances: make(map[string]*keepalivedConfigVrrpInstance),
})
}
}
func TestSimple(t *testing.T) {
reader := strings.NewReader(`
vrrp_instance foo {
virtual_ipaddress {
192.0.2.200/32
192.0.2.201/24
}
}
vrrp_instance empty {
}
vrrp_instance ipfailover_VIP_2 {
interface eth1
state MASTER
virtual_router_id 2
priority 142
preempt_delay 300
authentication {
auth_type PASS
auth_pass ipfailover
}
track_script {
chk_ipfailover
}
notify "/utils/notify"
virtual_ipaddress {
192.0.2.101 dev eth1
192.0.2.102
}
}
vrrp_instance last {
virtual_ipaddress {
2001:db8::ff00
2001:db8::/64
192.0.2.100/1
}
}
`)
cfg, err := parseKeepalivedConfig(reader)
if assert.NoError(t, err) {
expected := map[string]*keepalivedConfigVrrpInstance{
"foo": &keepalivedConfigVrrpInstance{
Name: "foo",
Addresses: []netAddress{
mustParseNetAddress("192.0.2.200/32"),
mustParseNetAddress("192.0.2.201/24"),
},
},
"empty": &keepalivedConfigVrrpInstance{
Name: "empty",
},
"ipfailover_VIP_2": &keepalivedConfigVrrpInstance{
Name: "ipfailover_VIP_2",
Addresses: []netAddress{
mustParseNetAddress("192.0.2.101/32"),
mustParseNetAddress("192.0.2.102"),
},
},
"last": &keepalivedConfigVrrpInstance{
Name: "last",
Addresses: []netAddress{
mustParseNetAddress("2001:db8::ff00"),
mustParseNetAddress("2001:db8::/64"),
mustParseNetAddress("192.0.2.100/1"),
},
},
}
assert.Equal(t, cfg.vrrpInstances, expected)
}
}
func TestFaultyIPAddress(t *testing.T) {
reader := strings.NewReader(`
vrrp_instance bar {
}
vrrp_instance foo {
virtual_ipaddress {
0.invalid.ip.address
}
}
`)
cfg, err := parseKeepalivedConfig(reader)
assert.Nil(t, cfg)
if assert.Error(t, err) {
assert.Equal(t, err.Error(),
`Line 6: Parsing IP address "0.invalid.ip.address" failed`)
}
}
/*
func TestUnterminatedVRRPInstance(t *testing.T) {
reader := strings.NewReader(`
vrrp_instance bar {
virtual_ipaddress {
192.0.2.100
}
}
vrrp_instance foo {
`)
cfg, err := parseKeepalivedConfig(reader)
assert.Nil(t, cfg)
if assert.Error(t, err) {
assert.Equal(t, err.Error(),
`End-of-file: Unterminated VRRP instance "foo"`)
}
}
*/
func TestDuplicatedVRRPInstanceName(t *testing.T) {
reader := strings.NewReader(`
vrrp_instance hello {
}
vrrp_instance bar {
}
vrrp_instance hello {
}
`)
cfg, err := parseKeepalivedConfig(reader)
assert.Nil(t, cfg)
if assert.Error(t, err) {
assert.Equal(t, err.Error(),
`Line 6: Duplicate VRRP instance name "hello"`)
}
}