-
Notifications
You must be signed in to change notification settings - Fork 29
/
subscribe_juniper_junos_test.go
654 lines (578 loc) · 18 KB
/
subscribe_juniper_junos_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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"testing"
"time"
"github.com/golang/protobuf/proto"
na_pb "github.com/nileshsimaria/jtimon/telemetry"
flag "github.com/spf13/pflag"
)
func TestJTISIMSigHup(t *testing.T) {
flag.Parse()
*noppgoroutines = true
*stateHandler = true
*prefixCheck = true
config := "tests/data/juniper-junos/config/jtisim-interfaces-file-list-sig.json"
total := 3 // this file has three workers config
err := GetConfigFiles(configFiles, config)
if err != nil {
t.Errorf("config parsing error: %s", err)
}
// create and start workers group with three workers
workers := NewJWorkers(*configFiles, config, 0)
workers.StartWorkers()
// run them for 4 seconds
time.Sleep(time.Duration(4) * time.Second)
wx := workers.m
testsx := []struct {
config string
totalIn uint64
}{
{
config: "tests/data/juniper-junos/config/jtisim-interfaces-1.json",
totalIn: 40, // after 2nd sighup its new worker
},
{
config: "tests/data/juniper-junos/config/jtisim-interfaces-2.json",
totalIn: 40, // after 2nd sighup its new worker
},
{
config: "tests/data/juniper-junos/config/jtisim-interfaces-3.json",
totalIn: 80, // this worker was running from beginning (it got two sighup though)
},
}
for _, test := range testsx {
if v, ok := wx[test.config]; !ok {
t.Errorf("workers map is missing entry for worker %s", test.config)
} else {
fmt.Println("[1] ----- ", test.config, v.jctx.stats.totalIn)
}
}
// we should have three workers
if len(workers.m) != total {
t.Errorf("workers does not match: want %d got %d", total, len(workers.m))
}
// change fileList
workers.fileList = "tests/data/juniper-junos/config/jtisim-interfaces-file-list-sig-delete.json"
total = 1 // two deleted so we end up with only one worker config
workers.SIGHUPWorkers() // send sighup
// run updated workers for six seconds
time.Sleep(time.Duration(6) * time.Second)
// we should have only one worker
if len(workers.m) != total {
t.Errorf("after sighup, workers does not match: want %d got %d", total, len(workers.m))
}
wy := workers.m
testsy := []struct {
config string
totalIn uint64
}{
{
config: "tests/data/juniper-junos/config/jtisim-interfaces-3.json",
totalIn: 80, // this worker was running from beginning (it got two sighup though)
},
}
for _, test := range testsy {
if v, ok := wy[test.config]; !ok {
t.Errorf("workers map is missing entry for worker %s", test.config)
} else {
fmt.Println("[2] ----- ", test.config, v.jctx.stats.totalIn)
}
}
// change file list again, bring back all three workers again
workers.fileList = "tests/data/juniper-junos/config/jtisim-interfaces-file-list-sig.json"
total = 3 // another sighup, we should have three workers now
workers.SIGHUPWorkers()
// run the stest for 4 seconds. this includes two new workers and one existing worker
time.Sleep(time.Duration(4) * time.Second)
if len(workers.m) != total {
t.Errorf("after sighup, workers does not match: want %d got %d", total, len(workers.m))
}
workers.EndWorkers() // end all workers, this will retain workers map for us to peek data for test
workers.Wait() // this should not block as we have stopped all workers
w := workers.m
tests := []struct {
config string
totalIn uint64
}{
{
config: "tests/data/juniper-junos/config/jtisim-interfaces-1.json",
totalIn: 40, // after 2nd sighup its new worker
},
{
config: "tests/data/juniper-junos/config/jtisim-interfaces-2.json",
totalIn: 40, // after 2nd sighup its new worker
},
{
config: "tests/data/juniper-junos/config/jtisim-interfaces-3.json",
// this worker was running from beginning, it got two SIGHUPs but it's
// config wasnt changed so it should not attempt to re-established the
// connection with JTISIM. Total run time was 4 + 6 + 4 = 14seconds,
// hence it must have received data twice since freq of subscription for
// this worker is 10 seconds.
totalIn: 80,
},
}
for _, test := range tests {
if v, ok := w[test.config]; !ok {
t.Errorf("workers map is missing entry for worker %s", test.config)
} else {
if v.jctx.stats.totalIn != test.totalIn {
t.Errorf("totalIn mismatch for %s, want %d got %d", test.config, test.totalIn, v.jctx.stats.totalIn)
}
}
}
}
// Same as TestJTISIMSigHupChanged, with config change for
// worker-3.
func TestJTISIMSigHupChanged(t *testing.T) {
flag.Parse()
*noppgoroutines = true
*stateHandler = true
*prefixCheck = true
config := "tests/data/juniper-junos/config/jtisim-interfaces-file-list-sig.json"
total := 3 // this file has three workers config
err := GetConfigFiles(configFiles, config)
if err != nil {
t.Errorf("config parsing error: %s", err)
}
// create and start workers group with three workers
workers := NewJWorkers(*configFiles, config, 0)
workers.StartWorkers()
// run them for 4 seconds
time.Sleep(time.Duration(4) * time.Second)
wx := workers.m
testsx := []struct {
config string
totalIn uint64
}{
{
config: "tests/data/juniper-junos/config/jtisim-interfaces-1.json",
totalIn: 40, // after 2nd sighup its new worker
},
{
config: "tests/data/juniper-junos/config/jtisim-interfaces-2.json",
totalIn: 40, // after 2nd sighup its new worker
},
{
config: "tests/data/juniper-junos/config/jtisim-interfaces-3.json",
totalIn: 80, // this worker was running from beginning (it got two sighup though)
},
}
for _, test := range testsx {
if v, ok := wx[test.config]; !ok {
t.Errorf("workers map is missing entry for worker %s", test.config)
} else {
fmt.Println("[1] ----- ", test.config, v.jctx.stats.totalIn)
}
}
// we should have three workers
if len(workers.m) != total {
t.Errorf("workers does not match: want %d got %d", total, len(workers.m))
}
// change fileList
workers.fileList = "tests/data/juniper-junos/config/jtisim-interfaces-file-list-sig-delete.json"
total = 1 // two deleted so we end up with only one worker config
workers.SIGHUPWorkers() // send sighup
// run updated workers for six seconds
time.Sleep(time.Duration(6) * time.Second)
// we should have only one worker
if len(workers.m) != total {
t.Errorf("after sighup, workers does not match: want %d got %d", total, len(workers.m))
}
wy := workers.m
testsy := []struct {
config string
totalIn uint64
}{
{
config: "tests/data/juniper-junos/config/jtisim-interfaces-3.json",
totalIn: 80, // this worker was running from beginning (it got two sighup though)
},
}
for _, test := range testsy {
if v, ok := wy[test.config]; !ok {
t.Errorf("workers map is missing entry for worker %s", test.config)
} else {
fmt.Println("[2] ----- ", test.config, v.jctx.stats.totalIn)
}
}
// change file list again, bring back all three workers again with changed
// config for worker-3
workers.fileList = "tests/data/juniper-junos/config/jtisim-interfaces-file-list-chg.json"
total = 3 // another sighup, we should have three workers now
workers.SIGHUPWorkers()
// run the stest for 4 seconds. this includes two new workers and one existing worker
time.Sleep(time.Duration(4) * time.Second)
if len(workers.m) != total {
t.Errorf("after sighup, workers does not match: want %d got %d", total, len(workers.m))
}
workers.EndWorkers() // end all workers, this will retain workers map for us to peek data for test
workers.Wait() // this should not block as we have stopped all workers
w := workers.m
tests := []struct {
config string
totalIn uint64
}{
{
config: "tests/data/juniper-junos/config/jtisim-interfaces-1.json",
totalIn: 40, // after 2nd sighup its new worker
},
{
config: "tests/data/juniper-junos/config/jtisim-interfaces-2.json",
totalIn: 40, // after 2nd sighup its new worker
},
{
config: "tests/data/juniper-junos/config/jtisim-interfaces-6.json",
// we started this worker as part of config change test in which
// jtisim-interfaces-3.json's config is changed so its being deleted
// and jtisim-interfaces-6.json is introduced. It got chance to run
// only for last 4 seconds so it must have received data only once.
totalIn: 40,
},
}
for _, test := range tests {
if v, ok := w[test.config]; !ok {
t.Errorf("workers map is missing entry for worker %s", test.config)
} else {
if v.jctx.stats.totalIn != test.totalIn {
t.Errorf("totalIn mismatch for %s, want %d got %d", test.config, test.totalIn, v.jctx.stats.totalIn)
}
}
}
}
func TestJTISIMSigInt(t *testing.T) {
flag.Parse()
*noppgoroutines = true
*stateHandler = true
*prefixCheck = true
config := "tests/data/juniper-junos/config/jtisim-interfaces-file-list-sig.json"
total := 3
runTime := 15 // if you change runTime, you will have to change totalIn and totalKV as well
totalIn := uint64(80) // for 12 seconds
totalKV := uint64(3960) // for 12 seconds
err := GetConfigFiles(configFiles, config)
if err != nil {
t.Errorf("config parsing error: %s", err)
}
workers := NewJWorkers(*configFiles, config, 0)
workers.StartWorkers()
if len(workers.m) != total {
t.Errorf("workers does not match: want %d got %d", total, len(workers.m))
}
time.Sleep(time.Duration(runTime) * time.Second)
workers.EndWorkers()
workers.Wait() // this should not block as we have stopped all workers
for _, w := range workers.m {
jctx := w.jctx
if totalIn != jctx.stats.totalIn {
t.Errorf("totalIn failed for config : %s wanted %v got %v", jctx.file, totalIn, jctx.stats.totalIn)
}
if totalKV != jctx.stats.totalKV {
t.Errorf("totalKV failed for config : %s wanted %v got %v", jctx.file, totalKV, jctx.stats.totalKV)
}
}
}
func TestJTISIMRetrySigInt(t *testing.T) {
flag.Parse()
*noppgoroutines = true
*stateHandler = true
*prefixCheck = true
config := "tests/data/juniper-junos/config/jtisim-interfaces-file-list-sig-int.json"
total := 1
runTime := 10
retryTime := 10
err := GetConfigFiles(configFiles, config)
if err != nil {
t.Errorf("config parsing error: %s", err)
}
workers := NewJWorkers(*configFiles, config, 0)
workers.StartWorkers()
if len(workers.m) != total {
t.Errorf("workers does not match: want %d got %d", total, len(workers.m))
}
time.Sleep(time.Duration(runTime) * time.Second)
workers.EndWorkers()
// Wait for the signal to be consumed as the worker may be already waiting
// in retry Timer
time.Sleep(time.Duration(retryTime+5) * time.Second)
for _, w := range workers.m {
jctx := w.jctx
// Check the the interrupt in the control channel is received.
select {
case s := <-jctx.control:
switch s {
case os.Interrupt:
t.Errorf("Interrupt signal is not handled at connection for %s", jctx.file)
}
default:
}
}
}
func TestPrometheus(t *testing.T) {
flag.Parse()
tests := []struct {
name string
config string
total int
maxRun int64
}{
{
name: "influx-1",
config: "tests/data/juniper-junos/config/jtisim-prometheus.json",
maxRun: 6,
total: 1,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
host := "127.0.0.1"
port := 8090
*noppgoroutines = true
*stateHandler = true
*prom = true
exporter = promInit()
defer func() {
*prom = false
exporter = nil
}()
configFiles = &[]string{test.config}
err := GetConfigFiles(configFiles, "")
if err != nil {
t.Errorf("config parsing error: %s", err)
}
workers := NewJWorkers(*configFiles, test.config, test.maxRun)
workers.StartWorkers()
workers.Wait()
if len(workers.m) != test.total {
t.Errorf("workers does not match: want %d got %d", test.total, len(workers.m))
}
if len(workers.m) != 1 {
t.Errorf("cant't run prometheus test for more than one worker")
}
if w, ok := workers.m[test.config]; !ok {
t.Errorf("could not found worker for config %s", test.config)
} else {
jctx := w.jctx
if err := prometheusCollect(host, port, jctx); err != nil {
t.Errorf("%v", err)
} else {
if jctx.testRes, err = os.Open(test.config + ".testres"); err != nil {
t.Errorf("could not open %s", test.config+".testres")
} else {
if err := compareResults(jctx); err != nil {
t.Errorf("%v", err)
}
jctx.testRes.Close()
}
}
}
})
}
}
func TestInflux(t *testing.T) {
flag.Parse()
host := "127.0.0.1"
port := 50052
tests := []struct {
name string
config string
total int
maxRun int64
}{
{
name: "influx-1",
config: "tests/data/juniper-junos/config/jtisim-influx.json",
maxRun: 25,
total: 1,
},
{
name: "influx-alias",
config: "tests/data/juniper-junos/config/jtisim-influx-alias.json",
maxRun: 8,
total: 1,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
*noppgoroutines = true
configFiles = &[]string{test.config}
err := GetConfigFiles(configFiles, "")
if err != nil {
t.Errorf("config parsing error: %s", err)
}
if err := influxStore(host, port, STOREOPEN, test.config+".testres"); err != nil {
t.Errorf("influxStore(open) failed: %v", err)
}
workers := NewJWorkers(*configFiles, test.config, test.maxRun)
workers.StartWorkers()
workers.Wait()
if err := influxStore(host, port, STORECLOSE, test.config+".testres"); err != nil {
t.Errorf("influxStore(close) failed: %v", err)
}
if len(workers.m) != test.total {
t.Errorf("workers does not match: want %d got %d", test.total, len(workers.m))
}
for _, w := range workers.m {
jctx := w.jctx
if jctx.testRes, err = os.Open(test.config + ".testres"); err != nil {
t.Errorf("could not open %s", test.config+".testres")
} else {
if err := compareResults(jctx); err != nil {
t.Errorf("%v", err)
}
jctx.testRes.Close()
}
}
})
}
}
func TestJTISIMMaxRun(t *testing.T) {
tests := []struct {
name string
config string
total int
maxRun int64
totalIn uint64
totalKV uint64
compression string
}{
{
name: "multi-file-list-1",
config: "tests/data/juniper-junos/config/jtisim-interfaces-file-list.json",
total: 2,
maxRun: 25, // if you change maxRun, please change totalIn and totalKV as well
totalIn: 120, // for 25 seconds
totalKV: 5940, // for 25 seconds
compression: "gzip",
},
{
name: "multi-file-list-1",
config: "tests/data/juniper-junos/config/jtisim-interfaces-file-list.json",
total: 2,
maxRun: 25, // if you change maxRun, please change totalIn and totalKV as well
totalIn: 120, // for 25 seconds
totalKV: 5940, // for 25 seconds
compression: "",
},
}
for _, test := range tests {
t.Run(test.name+test.compression, func(t *testing.T) {
flag.Parse()
*noppgoroutines = true
*stateHandler = true
*prefixCheck = true
*compression = test.compression
err := GetConfigFiles(configFiles, test.config)
if err != nil {
t.Errorf("config parsing error: %s", err)
}
workers := NewJWorkers(*configFiles, test.config, test.maxRun)
workers.StartWorkers()
workers.Wait()
if len(workers.m) != test.total {
t.Errorf("workers does not match: want %d got %d", test.total, len(workers.m))
}
for _, w := range workers.m {
jctx := w.jctx
if test.totalIn != jctx.stats.totalIn {
t.Errorf("totalIn failed for config : %s wanted %v got %v", jctx.file, test.totalIn, jctx.stats.totalIn)
}
if test.totalKV != jctx.stats.totalKV {
t.Errorf("totalKV failed for config : %s wanted %v got %v", jctx.file, test.totalKV, jctx.stats.totalKV)
}
switch test.compression {
case "gzip":
if jctx.stats.totalInPayloadWireLength >= jctx.stats.totalInPayloadLength {
t.Errorf("gzip compression failed: totalInPayloadWireLength = %v totalInPayloadLength = %v",
jctx.stats.totalInPayloadWireLength, jctx.stats.totalInPayloadLength)
}
// case "":
// if jctx.stats.totalInPayloadWireLength != jctx.stats.totalInPayloadLength {
// t.Errorf("no compression failed: totalInPayloadWireLength = %v totalInPayloadLength = %v",
// jctx.stats.totalInPayloadWireLength, jctx.stats.totalInPayloadLength)
// }
}
}
})
}
}
func TestVMXTagsPoints(t *testing.T) {
flag.Parse()
*conTestData = true
*noppgoroutines = true
tests := []struct {
name string
config string
jctx *JCtx
}{
{
name: "interfaces",
config: "tests/data/juniper-junos/config/interfaces.json",
jctx: &JCtx{
file: "tests/data/juniper-junos/config/interfaces.json",
},
},
{
name: "interfaces_uint64",
config: "tests/data/juniper-junos/config/uint64.json",
jctx: &JCtx{
file: "tests/data/juniper-junos/config/uint64.json",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
jctx := test.jctx
err := ConfigRead(jctx, true, nil)
if err != nil {
t.Errorf("error %v for test config %s", err, test.config)
}
sizeFileContent, err := ioutil.ReadFile(jctx.file + ".testmeta")
if err != nil {
t.Errorf("error %v for test config %s", err, test.config)
}
data, err := os.Open(jctx.file + ".testbytes")
if err != nil {
t.Errorf("error %v for test config %s", err, test.config)
}
defer data.Close()
testRes, err := os.Create(jctx.file + ".testres")
if err != nil {
t.Errorf("error %v for test config %s", err, test.config)
}
defer testRes.Close()
jctx.testRes = testRes
sizes := strings.Split(string(sizeFileContent), ":")
for _, size := range sizes {
if size != "" {
n, err := strconv.ParseInt(size, 10, 64)
if err != nil {
t.Errorf("error %v for test config %s", err, test.config)
}
d := make([]byte, n)
bytesRead, err := data.Read(d)
if err != nil {
t.Errorf("error %v for test config %s", err, test.config)
}
if int64(bytesRead) != n {
t.Errorf("want %d got %d from testbytes", n, bytesRead)
}
ocData := new(na_pb.OpenConfigData)
err = proto.Unmarshal(d, ocData)
if err != nil {
t.Errorf("error %v for test config %s", err, test.config)
}
addIDB(ocData, jctx, time.Now())
}
}
if err := compareResults(jctx); err != nil {
t.Errorf("%v", err)
}
})
}
}