-
Notifications
You must be signed in to change notification settings - Fork 5
/
peergen_test.go
261 lines (230 loc) · 6.98 KB
/
peergen_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
package main
import (
"bufio"
"bytes"
"encoding/json"
"github.com/ipcjk/ixgen/ixtypes"
"github.com/ipcjk/ixgen/peergen"
"html/template"
"io"
"log"
"net"
"strings"
"testing"
)
func TestExtremeNetworksIX(t *testing.T) {
var p = peergen.NewPeerGen("extreme/netiron", "./templates", "./configuration")
var Ix ixtypes.Ix
var buffer bytes.Buffer
var err error
Ix.PeersReady = []ixtypes.ExchangePeer{
{
ASN: "196922",
Active: true,
Ipv4Enabled: true,
Ipv6Enabled: true,
PrefixFilterEnabled: false,
GroupEnabled: true,
Group6Enabled: true,
IsRs: false, IsRsPeer: true,
Ipv4Addr: net.ParseIP("127.0.0.1"),
Ipv6Addr: net.ParseIP("3ffe:ffff::/32"),
InfoPrefixes6: 10,
InfoPrefixes4: 100,
LocalPreference: 90,
IrrAsSet: "AS-196922",
Group: "decix-peer",
Group6: "decix-peer6",
},
{
ASN: "3356",
Active: true,
Ipv4Enabled: true,
Ipv6Enabled: true,
PrefixFilterEnabled: false,
GroupEnabled: true,
Group6Enabled: true,
IsRs: false, IsRsPeer: true,
Ipv4Addr: net.ParseIP("127.3.3.56"),
Ipv6Addr: net.ParseIP("3ffe:ffff:3356::/32"),
InfoPrefixes6: 10,
InfoPrefixes4: 100,
LocalPreference: 90,
IrrAsSet: "AS-3356",
Group: "decix-peer",
Group6: "decix-peer6",
PrefixFilters: ixtypes.PrefixFilters{
Name: "3356peer",
PrefixRules: []ixtypes.PrefixRule{
{Prefix: "178.248.240.4/24", Exact: true},
},
},
PrefixFilters6: ixtypes.PrefixFilters{
Name: "3356peer",
PrefixRules: []ixtypes.PrefixRule{
{Prefix: "2a02:1308::/32", GreaterEqual: 32, LessEqual: 48},
},
},
},
}
writer := bufio.NewWriter(&buffer)
err = p.GenerateIXConfiguration(Ix, writer)
if err != nil {
log.Fatal(err)
}
err = writer.Flush()
if err != nil {
log.Fatal("Cant flush generated configuration into buffer")
}
var countLines, countNeighbor int
var foundSample bool
reader := bufio.NewReader(&buffer)
for {
var line string
line, err = reader.ReadString('\n')
if strings.HasPrefix(line, "neighbor") {
countNeighbor++
}
if strings.HasPrefix(line, "neighbor 127.0.0.1 remote-as 196922") {
foundSample = true
}
countLines++
if err == io.EOF {
break
}
if err != nil {
t.Error("Error reading from template buffer")
}
}
if !foundSample {
t.Error("Did not find bgp neighbor sample command in template buffer")
}
if countLines < 16 {
t.Error("Template too short or broken, not enough output lines for netiron/extreme")
}
if countNeighbor < 8 {
t.Error("Template too short or broken, not enough bgp neighbor commands")
}
filterWriter := bufio.NewWriter(new(bytes.Buffer))
err = p.GeneratePrefixFilter(Ix, filterWriter)
if err != nil {
t.Error(err)
}
err = filterWriter.Flush()
if err != nil {
t.Error("Cant flush generated configuration into buffer")
}
}
func TestExtremeNetironPrefixFilterTemplate(t *testing.T) {
var p = peergen.NewPeerGen("extreme/netiron", "./templates", "./configuration")
var Ix ixtypes.Ix
Ix.PeersReady = []ixtypes.ExchangePeer{
{
ASN: "3356",
Active: true,
Ipv4Enabled: true,
Ipv6Enabled: true,
IrrAsSet: "AS-3356",
PrefixFilterEnabled: true,
PrefixFilters: ixtypes.PrefixFilters{
Name: "3356peer-4",
PrefixRules: []ixtypes.PrefixRule{
{Prefix: "178.248.240.0/21", Exact: true},
{Prefix: "178.248.241.0/24", Exact: true},
},
},
PrefixFilters6: ixtypes.PrefixFilters{
Name: "3356peer-6",
PrefixRules: []ixtypes.PrefixRule{
{Prefix: "2a02:1308::/32", GreaterEqual: 32, LessEqual: 48},
{Prefix: "2a02:1308::/48", Exact: true},
},
},
},
}
buffer := new(bytes.Buffer)
err := p.GeneratePrefixFilter(Ix, buffer)
if err != nil {
t.Error(err)
}
if !strings.ContainsAny(buffer.String(), "2a02:1308::/48") {
t.Error("Cant find my home prefix in ipv6-prefixlist")
}
if !strings.ContainsAny(buffer.String(), "178.248.240.0/24") {
t.Error("Cant find my home prefix in ip prefixlist")
}
}
func TestAllTemplates(t *testing.T) {
templateDir := "./templates/"
supportedTemplate := []string{
"extreme/netiron/router.tt",
"extreme/netiron/prefix.tt",
"extreme/netiron/prefix6.tt",
"extreme/slx/prefix6.tt",
"extreme/slx/router.tt",
"extreme/slx/header.tt",
"extreme/slx/prefix.tt",
"extreme/vdx/header.tt",
"extreme/vdx/router.tt",
"cisco/ios/prefix.tt",
"cisco/ios/prefix6.tt",
"cisco/ios/router.tt",
"juniper/set/header.tt",
"juniper/set/prefix.tt",
"juniper/set/router.tt",
}
for _, v := range supportedTemplate {
_, err := template.New("test").ParseFiles(templateDir + v)
if err != nil {
t.Errorf("broken template: %s, %s", v, err)
} else {
t.Logf("tt %s ok", v)
}
}
}
func TestIXConfigFromJson(t *testing.T) {
var testJSON = `{"additionalconfig":null,"ixname":"DE-CIX Frankfurt","options":{},"peeringgroups":{},"peers_configured":{"DE-CIX Frankfurt":{"196922":[{"active":true,"asn":"196922","group":"","group6":"","groupenabled":true,"group6_enabled":true,"infoprefixes4":0,"infoprefixes6":0,"ipv4addr":"","ipv6addr":"","ipv4enabled":true,"ipv6enabled":true,"irrasset":"","isrs":false,"isrsper":false,"localpreference":0,"prefixfilter":false}]}},"peersready":[{"active":true,"asn":"196922","group":"","group6":"","groupenabled":false,"group6_enabled":false,"infoprefixes4":64,"infoprefixes6":10,"ipv4addr":"80.81.194.25","ipv6addr":"2001:7f8::3:13a:0:1","ipv4enabled":true,"ipv6enabled":true,"irrasset":"AS-HOFMEIR","isrs":false,"isrsper":false,"localpreference":0,"prefixfilter":false}],"routeserverready":null}`
var p = peergen.NewPeerGen("extreme/netiron", "./templates", "./configuration")
var buffer bytes.Buffer
ix := ixtypes.Ix{}
if err := json.Unmarshal([]byte(testJSON), &ix); err != nil {
t.Errorf("error decoding JSON into format, some code has changed? Error %s", err)
}
if ix.IxName != "DE-CIX Frankfurt" {
t.Error("IX-Name has changed, not expected")
}
writer := bufio.NewWriter(&buffer)
p.GenerateIXConfiguration(ix, writer)
err := writer.Flush()
if err != nil {
log.Fatal("Cant flush generated configuration into buffer")
}
var countLines, countNeighbor int
var foundSample bool
reader := bufio.NewReader(&buffer)
for {
line, err := reader.ReadString('\n')
if strings.HasPrefix(line, "neighbor") {
countNeighbor++
}
if strings.HasPrefix(line, "neighbor 80.81.194.25 remote-as 196922") {
foundSample = true
}
countLines++
if err == io.EOF {
break
}
if err != nil {
t.Error("Error reading from template buffer")
}
}
if !foundSample {
t.Error("Did not find any bgp neighbor sample command in template buffer")
}
if countLines < 8 {
t.Error("Template too short or broken, not enough output lines for netiron/extreme")
}
if countNeighbor < 2 {
t.Error("Template too short or broken, not enough bgp neighbor commands")
}
}