-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
940 lines (883 loc) · 31.3 KB
/
client_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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
//go:build integration
package srljrpc_test
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"testing"
"time"
"github.com/azyablov/srljrpc"
"github.com/azyablov/srljrpc/actions"
"github.com/azyablov/srljrpc/apierr"
"github.com/azyablov/srljrpc/formats"
"github.com/azyablov/srljrpc/yms"
)
const (
intDataFile = "testdata/integration_tests_params.json"
)
type intParams struct {
Host string `json:"host"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
Port int `json:"port,omitempty"`
TLSAttr srljrpc.TLSAttr `json:"tls_attr,omitempty"`
}
type defTarget struct {
DefaultTarget json.RawMessage `json:"default_target"`
}
type certTarget struct {
CertTarget json.RawMessage `json:"cert_target"`
}
type ocTarget struct {
DefaultTarget json.RawMessage `json:"oc_target"`
}
type incorrectCATarget struct {
IncorrectCATarget json.RawMessage `json:"cert_target_incorrect_ca"`
}
func TestNewJSONRPCClient(t *testing.T) {
// Read integration tests parameters
dt := defTarget{}
fh, err := os.Open(intDataFile)
if err != nil {
t.Fatalf("can't open %s: %v", intDataFile, err)
}
defer fh.Close()
bIntParams, err := ioutil.ReadAll(fh)
if err != nil {
t.Fatalf("can't read %s: %v", intDataFile, err)
}
err = json.Unmarshal(bIntParams, &dt)
if err != nil {
t.Fatalf("can't unmarshal %s: %v", intDataFile, err)
}
defIP := intParams{}
json.Unmarshal(dt.DefaultTarget, &defIP)
// Table driven tests to check NewJSONRPCClient() function
var defTestData = []struct {
testName string
host *string
opts []srljrpc.ClientOption
expErr error
}{
{testName: "Creating client with valid creds", host: &defIP.Host, opts: []srljrpc.ClientOption{srljrpc.WithOptCredentials(&defIP.Username, &defIP.Password)}, expErr: nil}, // should succeed
{testName: "Creating client with valid creds and port", host: &defIP.Host, opts: []srljrpc.ClientOption{srljrpc.WithOptCredentials(&defIP.Username, &defIP.Password), srljrpc.WithOptPort(&defIP.Port)}, expErr: nil}, // should succeed
{testName: "Creating client with valid creds, port and TLS skip_verify", host: &defIP.Host, opts: []srljrpc.ClientOption{srljrpc.WithOptCredentials(&defIP.Username, &defIP.Password), srljrpc.WithOptPort(&defIP.Port), srljrpc.WithOptTLS(&defIP.TLSAttr)}, expErr: nil}, // should succeed
}
for _, td := range defTestData {
t.Run(td.testName, func(t *testing.T) {
_, err := srljrpc.NewJSONRPCClient(td.host, td.opts...)
checkClntErrGotVSExp(err, td.expErr, t)
})
}
ct := certTarget{}
err = json.Unmarshal(bIntParams, &ct)
if err != nil {
t.Fatalf("can't unmarshal %s: %v", intDataFile, err)
}
certIP := intParams{}
err = json.Unmarshal(ct.CertTarget, &certIP)
if err != nil {
t.Fatalf("can't unmarshal %s: %v", intDataFile, err)
}
// With all TLS options
var certTestData = []struct {
testName string
host *string
opts []srljrpc.ClientOption
expErr error
}{
{testName: "Creating client with valid TLS inputs and skip_verify=false",
host: &certIP.Host,
opts: []srljrpc.ClientOption{srljrpc.WithOptCredentials(&certIP.Username, &certIP.Password), srljrpc.WithOptPort(&certIP.Port), srljrpc.WithOptTLS(&certIP.TLSAttr)},
expErr: nil}, // should succeed
}
for _, td := range certTestData {
t.Run(td.testName, func(t *testing.T) {
_, err := srljrpc.NewJSONRPCClient(td.host, td.opts...)
checkClntErrGotVSExp(err, td.expErr, t)
})
}
icat := incorrectCATarget{}
err = json.Unmarshal(bIntParams, &icat)
if err != nil {
t.Fatalf("can't unmarshal %s: %v", intDataFile, err)
}
icaIP := intParams{}
err = json.Unmarshal(icat.IncorrectCATarget, &icaIP)
if err != nil {
t.Fatalf("can't unmarshal %s: %v", intDataFile, err)
}
// Incorrect CA certificate
var icaTestData = []struct {
testName string
host *string
opts []srljrpc.ClientOption
expErr error
errMsg string
}{
{testName: "Creating client with valid TLS inputs but ca_cert is incorrect",
host: &icaIP.Host,
opts: []srljrpc.ClientOption{srljrpc.WithOptCredentials(&icaIP.Username, &icaIP.Password), srljrpc.WithOptPort(&icaIP.Port), srljrpc.WithOptTLS(&icaIP.TLSAttr)},
expErr: apierr.ErrClntTargetVerification}, // should fail, CA cert is incorrect
}
for _, td := range icaTestData {
t.Run(td.testName, func(t *testing.T) {
_, err := srljrpc.NewJSONRPCClient(td.host, td.opts...)
checkClntErrGotVSExp(err, td.expErr, t)
})
}
}
func TestGet(t *testing.T) {
// Get default client
c := helperGetDefClient(t)
// Get with default target
var getTestData = []struct {
testName string
paths []string
expErr error
}{
{testName: "Get against RUNNING datastore with default target",
paths: []string{"/system/json-rpc-server", "/network-instance[name=mgmt]"},
expErr: nil,
}, // should succeed
{testName: "Get against RUNNING datastore with default target and invalid path",
paths: []string{"/system/json-rpc-server/invalid"},
expErr: apierr.ErrClntJSONRPCResp,
}, // should fail, invalid path
}
for _, td := range getTestData {
t.Run(td.testName, func(t *testing.T) {
_, err := c.Get(td.paths...)
checkClntErrGotVSExp(err, td.expErr, t)
})
}
}
func TestState(t *testing.T) {
// Get default client
c := helperGetDefClient(t)
// State with default target
var getTestData = []struct {
testName string
paths []string
expErr error
errMsg string
}{
{testName: "Get against STATE datastore with default target",
paths: []string{"/system/lldp/statistics", "/network-instance[name=mgmt]/interface[name=mgmt0.0]/oper-state"},
expErr: nil,
}, // should succeed
{testName: "Get with default target and invalid path",
paths: []string{"/lldp/statistics/invalid"},
expErr: apierr.ErrClntJSONRPCResp,
}, // should fail, invalid path
}
for _, td := range getTestData {
t.Run(td.testName, func(t *testing.T) {
_, err := c.State(td.paths...)
checkClntErrGotVSExp(err, td.expErr, t)
})
}
}
func TestUpdate(t *testing.T) {
// Get default client
c := helperGetDefClient(t)
// SetUpdate with default target
var setTestData = []struct {
testName string
pvs []srljrpc.PV
ct int
expErr error
}{
{testName: "Set Update against CANDIDATE datastore with default target",
pvs: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("test")},
{"/interface[name=mgmt0]/description", srljrpc.CommandValue("MGMT")},
},
ct: 0,
expErr: nil,
}, // should succeed
{testName: "Set Update against CANDIDATE datastore with default target and invalid path",
pvs: []srljrpc.PV{
{"/interface[name=system0]/invalid", srljrpc.CommandValue("test")}},
ct: 0,
expErr: apierr.ErrClntJSONRPCResp,
}, // should fail, invalid path
{testName: "Set Update against CANDIDATE datastore with default target and missed value",
pvs: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("")}},
ct: 0,
expErr: apierr.ErrClntRPCReqCreation,
}, // should fail, missed value
{testName: "Set Update against CANDIDATE datastore with default target and confirm timeout",
pvs: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("test_CT_22")}, // after timeout should be "test", as per test 0.
{"/interface[name=mgmt0]/description", srljrpc.CommandValue("MGMT_CT_22")}, // after timeout should be "MGMT", as per test 0.
},
ct: 5,
expErr: nil,
}, // should succeed with w/ confirm timeout
}
for n, td := range setTestData {
t.Run(td.testName, func(t *testing.T) {
_, err := c.Update(td.ct, td.pvs...)
checkClntErrGotVSExp(err, td.expErr, t)
if n == 3 { // test with confirm timeout
t.Logf("Set Update against CANDIDATE datastore with default target and confirm timeout => Waiting for 1 seconds...")
time.Sleep(1 * time.Second)
ctResp, err := c.Get("/interface[name=system0]/description", "/interface[name=mgmt0]/description")
if err != nil {
t.Fatal(err)
}
// Unmarshal response
var ctRespData []string
err = json.Unmarshal(ctResp.Result, &ctRespData)
if err != nil {
t.Fatal(err)
}
// Check if values are updated
if ctRespData[0] != "test_CT_22" {
t.Errorf("got: %s, while should be: %s", ctRespData[0], "test_CT_22")
}
if ctRespData[1] != "MGMT_CT_22" {
t.Errorf("got: %s, while should be: %s", ctRespData[1], "MGMT_CT_22")
}
// t.Log(ctRespData)
t.Logf("Set Update against CANDIDATE datastore with default target and confirm timeout => Waiting for 5 seconds...")
time.Sleep(5 * time.Second)
ctResp, err = c.Get("/interface[name=system0]/description", "/interface[name=mgmt0]/description")
if err != nil {
t.Fatal(err)
}
// Unmarshal response
err = json.Unmarshal(ctResp.Result, &ctRespData)
if err != nil {
t.Fatal(err)
}
// Check if values are updated
if ctRespData[0] != "test" {
t.Errorf("got: %s, while should be: %s", ctRespData[0], "test")
}
if ctRespData[1] != "MGMT" {
t.Errorf("got: %s, while should be: %s", ctRespData[1], "MGMT")
}
// t.Log(ctRespData)
}
})
}
}
func TestBulkSet(t *testing.T) {
// Get OC client
c := helperGetOCClient(t)
// Test data
var setOCTestData = []struct {
testName string
delete []srljrpc.PV
replace []srljrpc.PV
update []srljrpc.PV
expErr error
}{
{testName: "Set Update against CANDIDATE datastore with OC target",
delete: []srljrpc.PV{
{"/interfaces/interface[name=ethernet-1/2]/config/description", srljrpc.CommandValue("")},
},
replace: []srljrpc.PV{
{"/interfaces/interface[name=ethernet-1/2]/config/name", srljrpc.CommandValue("ethernet-1/2")},
{"/interfaces/interface[name=ethernet-1/2]/config/type", srljrpc.CommandValue("ethernetCsmacd")},
{"/interfaces/interface[name=ethernet-1/2]/config/description", srljrpc.CommandValue("TestBulkSetCandidate_TOUPDATE")},
},
update: []srljrpc.PV{
{"/interfaces/interface[name=ethernet-1/2]/config/description", srljrpc.CommandValue("TestBulkSetCandidate")},
},
expErr: nil}, // should succeed
{testName: "Set Update against CANDIDATE datastore with OC target and invalid path",
delete: []srljrpc.PV{
{"/interfaces/interface[name=ethernet-1/2]/config/description", srljrpc.CommandValue("")},
},
replace: []srljrpc.PV{
{"/interfaces/interface[name=ethernet-1/2]/config/name", srljrpc.CommandValue("ethernet-1/2")},
{"/interfaces/interface[name=ethernet-1/2]/config/invalid", srljrpc.CommandValue("ethernetCsmacd")}, // invalid path
{"/interfaces/interface[name=ethernet-1/2]/config/description", srljrpc.CommandValue("TestBulkSetCandidate_TOUPDATE")},
},
update: []srljrpc.PV{
{"/interfaces/interface[name=ethernet-1/2]/config/description", srljrpc.CommandValue("TestBulkSetCandidate")},
},
expErr: apierr.ErrClntJSONRPCResp, // should fail, invalid path
},
{testName: "Set Update against CANDIDATE datastore with OC target and missed value",
delete: []srljrpc.PV{
{"/interfaces/interface[name=ethernet-1/2]/config/description", srljrpc.CommandValue("")},
},
replace: []srljrpc.PV{
{"/interfaces/interface[name=ethernet-1/2]/config/name", srljrpc.CommandValue("ethernet-1/2")},
{"/interfaces/interface[name=ethernet-1/2]/config/type", srljrpc.CommandValue("")},
{"/interfaces/interface[name=ethernet-1/2]/config/description", srljrpc.CommandValue("TestBulkSetCandidate_TOUPDATE")},
},
update: []srljrpc.PV{
{"/interfaces/interface[name=ethernet-1/2]/config/description", srljrpc.CommandValue("TestBulkSetCandidate")},
},
expErr: apierr.ErrClntRPCReqCreation, // should fail, missed value
},
}
for _, td := range setOCTestData {
t.Run(td.testName, func(t *testing.T) {
_, err := c.BulkSet(td.delete, td.replace, td.update, yms.OC, 0) // ct set to 0 to avoid confirm timeout
checkClntErrGotVSExp(err, td.expErr, t)
})
}
// Get with default client
c = helperGetDefClient(t)
// Test data
var setTestData = []struct {
testName string
delete []srljrpc.PV
replace []srljrpc.PV
update []srljrpc.PV
expErr error
}{
{testName: "Set Update against CANDIDATE datastore with SRL default target",
delete: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("")},
},
replace: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("System")},
{"/interface[name=mgmt0]/description", srljrpc.CommandValue("MGMT")},
},
update: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("System loopback")},
},
expErr: nil}, // should succeed
{testName: "Set Update against CANDIDATE datastore with SRL default target and invalid path",
delete: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("")},
},
replace: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("System")},
{"/interface[name=mgmt0]/invalid", srljrpc.CommandValue("MGMT")},
},
update: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("System loopback")},
},
expErr: apierr.ErrClntJSONRPCResp}, // should fail, invalid path
{testName: "Set Update against CANDIDATE datastore with SRL default target and missed value",
delete: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("")},
},
replace: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("System")},
{"/interface[name=mgmt0]/description", srljrpc.CommandValue("")},
},
update: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("System loopback")},
},
expErr: apierr.ErrClntRPCReqCreation}, // should fail, missed value
}
for _, td := range setTestData {
t.Run(td.testName, func(t *testing.T) {
_, err := c.BulkSet(td.delete, td.replace, td.update, yms.SRL, 0) // ct set to 0 to avoid confirm timeout
checkClntErrGotVSExp(err, td.expErr, t)
})
}
}
func TestBulkSetCallBack(t *testing.T) {
// Get SRL client
c := helperGetDefClient(t)
// Test data
var setTestData = []struct {
testName string
delete []srljrpc.PV
replace []srljrpc.PV
update []srljrpc.PV
expErr error
cbf srljrpc.CallBackConfirm
}{
{testName: "Set Update w/ confirm timeout and CallBackConfirm true against CANDIDATE datastore with SRL default target",
delete: []srljrpc.PV{},
replace: []srljrpc.PV{},
update: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("System loopback")},
},
expErr: nil,
cbf: func(req *srljrpc.Request, resp *srljrpc.Response) (bool, error) {
// For debug purposes only
// b, err := json.Marshal(req)
// if err != nil {
// t.Fatal(err)
// }
// t.Log(string(b))
// b, err = json.Marshal(resp)
// if err != nil {
// t.Fatal(err)
// }
// t.Log(string(b))
return true, nil
}}, // should succeed w/ commit confirmed via tools
{testName: "Set Replace w/ confirm timeout and CallBackConfirm false against CANDIDATE datastore with SRL default target",
delete: []srljrpc.PV{},
replace: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("System loopback")},
},
update: []srljrpc.PV{},
expErr: nil,
cbf: func(req *srljrpc.Request, resp *srljrpc.Response) (bool, error) {
// For debug purposes only
// b, err := json.Marshal(req)
// if err != nil {
// t.Fatal(err)
// }
// t.Log(string(b))
// b, err = json.Marshal(resp)
// if err != nil {
// t.Fatal(err)
// }
// t.Log(string(b))
return false, nil
}}, // should succeed w/ rollback
}
for n, td := range setTestData {
t.Run(td.testName, func(t *testing.T) {
// Set reference values, should not fail
_, err := c.BulkSet([]srljrpc.PV{}, []srljrpc.PV{}, []srljrpc.PV{{"/interface[name=system0]/description", srljrpc.CommandValue("Initial Value")}}, yms.SRL, 0)
if err != nil {
t.Fatal(err)
}
chResp := make(chan *srljrpc.Response)
chErr := make(chan error)
go func() {
resp, err := c.BulkSetCallBack(td.delete, td.replace, td.update, yms.SRL, 5, 3, td.cbf)
chResp <- resp
chErr <- err
}()
switch n {
case 0: // test with confirm timeout and CallBackConfirm returning true.
var ctRespData []string
t.Logf("Set Update w/ confirm timeout and CallBackConfirm true against CANDIDATE datastore with SRL default target => Waiting for 6 seconds...")
time.Sleep(6 * time.Second)
ctResp, err := c.Get("/interface[name=system0]/description")
if err != nil {
t.Fatal(err)
}
// Unmarshal response
err = json.Unmarshal(ctResp.Result, &ctRespData)
if err != nil {
t.Fatal(err)
}
// Check if values are updated
if ctRespData[0] != "System loopback" {
t.Errorf("got: %s, while should be: %s", ctRespData[0], "System loopback")
}
resp := <-chResp
if resp == nil { // should be non-nil, as CallBackConfirm returned true.
t.Errorf("got: %v, while should be: %s", resp, "non-nil")
break
}
b, err := json.Marshal(resp.Error)
if err != nil {
t.Fatal(err)
}
t.Log(string(b))
// t.Log(ctRespData)
case 1: // test with confirm timeout and CallBackConfirm returning false.
var ctRespData []string
t.Logf("Set Replace w/ confirm timeout and CallBackConfirm false against CANDIDATE datastore with SRL default target => Waiting for 6 seconds...")
time.Sleep(6 * time.Second)
ctResp, err := c.Get("/interface[name=system0]/description")
if err != nil {
t.Fatal(err)
}
// Unmarshal response
err = json.Unmarshal(ctResp.Result, &ctRespData)
if err != nil {
t.Fatal(err)
}
// Check if values are rolled back
if ctRespData[0] != "Initial Value" {
t.Errorf("got: %s, while should be: %s", ctRespData[0], "Initial Value")
}
resp := <-chResp
if resp != nil { // should be nil, as CallBackConfirm returned false.
t.Errorf("got: %v, while should be: %s", resp, "nil")
}
}
// Getting error from channel.
err = <-chErr
checkClntErrGotVSExp(err, td.expErr, t)
})
}
}
func TestBulkDiff(t *testing.T) {
// Get OC client
c := helperGetOCClient(t)
// Test data
var setOCTestData = []struct {
testName string
delete []srljrpc.PV
replace []srljrpc.PV
update []srljrpc.PV
expErr error
}{
{testName: "Set Update against CANDIDATE datastore with OC target",
delete: []srljrpc.PV{
{"/interfaces/interface[name=ethernet-1/2]/config/description", srljrpc.CommandValue("")},
},
replace: []srljrpc.PV{
{"/interfaces/interface[name=ethernet-1/2]/config/name", srljrpc.CommandValue("ethernet-1/2")},
{"/interfaces/interface[name=ethernet-1/2]/config/type", srljrpc.CommandValue("ethernetCsmacd")},
{"/interfaces/interface[name=ethernet-1/2]/config/description", srljrpc.CommandValue("TestBulkSetCandidate_TOUPDATE")},
},
update: []srljrpc.PV{
{"/interfaces/interface[name=ethernet-1/2]/config/description", srljrpc.CommandValue("TestBulkSetCandidate")},
},
expErr: nil}, // should succeed
{testName: "Set Update against CANDIDATE datastore with OC target and invalid path",
delete: []srljrpc.PV{
{"/interfaces/interface[name=ethernet-1/2]/config/description", srljrpc.CommandValue("")},
},
replace: []srljrpc.PV{
{"/interfaces/interface[name=ethernet-1/2]/config/name", srljrpc.CommandValue("ethernet-1/2")},
{"/interfaces/interface[name=ethernet-1/2]/config/invalid", srljrpc.CommandValue("ethernetCsmacd")}, // invalid path
{"/interfaces/interface[name=ethernet-1/2]/config/description", srljrpc.CommandValue("TestBulkSetCandidate_TOUPDATE")},
},
update: []srljrpc.PV{
{"/interfaces/interface[name=ethernet-1/2]/config/description", srljrpc.CommandValue("TestBulkSetCandidate")},
},
expErr: apierr.ErrClntJSONRPCResp}, // should fail, invalid path
{testName: "Set Update against CANDIDATE datastore with OC target and missed value",
delete: []srljrpc.PV{
{"/interfaces/interface[name=ethernet-1/2]/config/description", srljrpc.CommandValue("")},
},
replace: []srljrpc.PV{
{"/interfaces/interface[name=ethernet-1/2]/config/name", srljrpc.CommandValue("ethernet-1/2")},
{"/interfaces/interface[name=ethernet-1/2]/config/type", srljrpc.CommandValue("")},
{"/interfaces/interface[name=ethernet-1/2]/config/description", srljrpc.CommandValue("TestBulkSetCandidate_TOUPDATE")},
},
update: []srljrpc.PV{
{"/interfaces/interface[name=ethernet-1/2]/config/description", srljrpc.CommandValue("TestBulkSetCandidate")},
},
expErr: apierr.ErrClntRPCReqCreation}, // should fail, missed value
}
for _, td := range setOCTestData {
t.Run(td.testName, func(t *testing.T) {
_, err := c.BulkDiff(td.delete, td.replace, td.update, yms.OC)
checkClntErrGotVSExp(err, td.expErr, t)
})
}
// Get with default client
c = helperGetDefClient(t)
// Test data
var setTestData = []struct {
testName string
delete []srljrpc.PV
replace []srljrpc.PV
update []srljrpc.PV
expErr error
}{
{testName: "Set Update against CANDIDATE datastore with SRL default target",
delete: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("")},
},
replace: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("System")},
{"/interface[name=mgmt0]/description", srljrpc.CommandValue("MGMT")},
},
update: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("System loopback")},
},
expErr: nil}, // should succeed
{testName: "Set Update against CANDIDATE datastore with SRL default target and invalid path",
delete: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("")},
},
replace: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("System")},
{"/interface[name=mgmt0]/invalid", srljrpc.CommandValue("MGMT")},
},
update: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("System loopback")},
},
expErr: apierr.ErrClntJSONRPCResp}, // should fail, invalid path
{testName: "Set Update against CANDIDATE datastore with SRL default target and missed value",
delete: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("")},
},
replace: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("System")},
{"/interface[name=mgmt0]/description", srljrpc.CommandValue("")},
},
update: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("System loopback")},
},
expErr: apierr.ErrClntRPCReqCreation}, // should fail, missed value
}
for _, td := range setTestData {
t.Run(td.testName, func(t *testing.T) {
_, err := c.BulkDiff(td.delete, td.replace, td.update, yms.SRL)
checkClntErrGotVSExp(err, td.expErr, t)
})
}
}
func TestReplace(t *testing.T) {
// Get default client
c := helperGetDefClient(t)
// SetReplace with default target
var getTestData = []struct {
testName string
pvs []srljrpc.PV
expErr error
errMsg string
}{
{testName: "Set Replace against CANDIDATE datastore with default target",
pvs: []srljrpc.PV{{"/interface[name=system0]/description:test", srljrpc.CommandValue("")}},
expErr: nil,
}, // should succeed
{testName: "Set Replace against CANDIDATE datastore with default target and invalid path",
pvs: []srljrpc.PV{{"/interface[name=system0]/invalid:test", srljrpc.CommandValue("")}},
expErr: apierr.ErrClntJSONRPCResp,
}, // should fail, invalid path
}
for _, td := range getTestData {
t.Run(td.testName, func(t *testing.T) {
_, err := c.Replace(0, td.pvs...)
checkClntErrGotVSExp(err, td.expErr, t)
})
}
}
func TestDelete(t *testing.T) {
// Get default client
c := helperGetDefClient(t)
// Delete with default target
var setTestData = []struct {
testName string
paths []string
expErr error
errMsg string
}{
{testName: "Delete against CANDIDATE datastore with default target",
paths: []string{"/interface[name=system0]/description", "/interface[name=mgmt0]/description"},
expErr: nil,
}, // should succeed
{testName: "Delete against CANDIDATE datastore with default target and invalid path",
paths: []string{"/interface[name=system0]/invalid"},
expErr: apierr.ErrClntJSONRPCResp,
}, // should fail, invalid path
}
for _, td := range setTestData {
t.Run(td.testName, func(t *testing.T) {
_, err := c.Delete(0, td.paths...) // ct set to 0 to avoid confirm timeout
checkClntErrGotVSExp(err, td.expErr, t)
})
}
}
func TestValidate(t *testing.T) {
// Get default client
c := helperGetDefClient(t)
// SetUpdate with default target
var validateTestData = []struct {
testName string
pvs []srljrpc.PV
expErr error
}{
{testName: "Validate against CANDIDATE datastore with default target",
pvs: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("test")},
{"/interface[name=mgmt0]/description", srljrpc.CommandValue("MGMT")},
},
expErr: nil,
}, // should succeed
{testName: "Validate against CANDIDATE datastore with default target and invalid path",
pvs: []srljrpc.PV{
{"/interface[name=system0]/invalid", srljrpc.CommandValue("test")}},
expErr: apierr.ErrClntJSONRPCResp,
}, // should fail, invalid path
{testName: "Validate against CANDIDATE datastore with default target and missed value",
pvs: []srljrpc.PV{
{"/interface[name=system0]/description", srljrpc.CommandValue("")}},
expErr: apierr.ErrClntRPCReqCreation,
}, // should fail, missed value
}
for _, td := range validateTestData {
t.Run(td.testName, func(t *testing.T) {
_, err := c.Validate(actions.UPDATE, td.pvs...)
checkClntErrGotVSExp(err, td.expErr, t)
})
}
}
func TestTools(t *testing.T) {
// Get default client
c := helperGetDefClient(t)
// Test data
var toolsTestData = []struct {
testName string
pvs []srljrpc.PV
expErr error
}{
{testName: "Set against TOOLS w/o value",
pvs: []srljrpc.PV{
{"/interface[name=ethernet-1/1]/ethernet/statistics/clear", srljrpc.CommandValue("")},
},
expErr: nil,
}, // should succeed
{testName: "Set against TOOLS and invalid path",
pvs: []srljrpc.PV{
{"/interface[name=ethernet-1/1]/ethernet/INVALID/clear", srljrpc.CommandValue("")},
},
expErr: apierr.ErrClntJSONRPCResp,
}, // should fail, invalid path
{testName: "Set against TOOLS with double value",
pvs: []srljrpc.PV{
{"/network-instance[name=default]/protocols/bgp/group[group-name=underlay]/soft-clear/peer-as:65020", srljrpc.CommandValue("65020")}},
expErr: apierr.ErrClntRPCReqCreation,
}, // should fail, value specified two times
}
for _, td := range toolsTestData {
t.Run(td.testName, func(t *testing.T) {
_, err := c.Tools(td.pvs...)
checkClntErrGotVSExp(err, td.expErr, t)
})
}
}
func TestCLI(t *testing.T) {
// Get default client
c := helperGetDefClient(t)
// CLI with default target using Do()
shVerTABLE, err := srljrpc.NewCLIRequest([]string{"show version"}, formats.TABLE)
if err != nil {
t.Fatalf("can't create CLI request: %v", err)
}
shRouteTableJSON, err := srljrpc.NewCLIRequest([]string{"show network-instance default route-table"}, formats.JSON)
if err != nil {
t.Fatalf("can't create CLI request: %v", err)
}
// CLI Do() with default target
var cliDoTestData = []struct {
testName string
cliReq *srljrpc.CLIRequest
expErr error
errMsg string
}{
{testName: "CLI show version via Do()",
cliReq: shVerTABLE,
expErr: nil, errMsg: "cli method failed: "},
{testName: "CLI show network-instance default route-table via Do()",
cliReq: shRouteTableJSON,
expErr: nil, errMsg: "cli Do() method failed: "},
}
for _, td := range cliDoTestData {
t.Run(td.testName, func(t *testing.T) {
r, err := c.Do(td.cliReq)
checkClntErrGotVSExp(err, td.expErr, t)
_, err = r.Marshal()
if err != nil {
t.Fatalf("can't marshal response: %v", err)
}
// for debug purposes
// t.Logf("got response: %+v", string(b))
})
var cliTestData = []struct {
testName string
cmds []string
of formats.EnumOutputFormats
expErr error
}{
{testName: "CLI bulk via CLI() in TABLE format",
cmds: []string{"show version", "show network-instance default route-table", "show acl summary"},
of: formats.TABLE,
expErr: nil,
}, // should succeed
{testName: "CLI bulk via CLI() in JSON format",
cmds: []string{"show version", "show network-instance default route-table", "show acl summary"},
of: formats.JSON,
expErr: nil,
}, // should succeed
{testName: "CLI bulk via CLI() with empty commands",
cmds: []string{"show version", "", "show acl summary"},
of: formats.TEXT,
expErr: apierr.ErrClntRPCReqCreation,
}, // should fail, empty command
}
// CLI with default target using CLI()
for _, td := range cliTestData {
t.Run(td.testName, func(t *testing.T) {
r, err := c.CLI(td.cmds, td.of)
checkClntErrGotVSExp(err, td.expErr, t)
_, err = r.Marshal()
if err != nil {
t.Fatalf("can't marshal response: %v", err)
}
// for debug purposes
//t.Logf("got response: %+v", string(b))
})
}
}
}
func helperGetDefClient(t *testing.T) *srljrpc.JSONRPCClient {
// Read integration tests parameters
dt := defTarget{}
fh, err := os.Open(intDataFile)
if err != nil {
t.Fatalf("can't open %s: %v", intDataFile, err)
}
defer fh.Close()
bIntParams, err := ioutil.ReadAll(fh)
if err != nil {
t.Fatalf("can't read %s: %v", intDataFile, err)
}
err = json.Unmarshal(bIntParams, &dt)
if err != nil {
t.Fatalf("can't unmarshal %s: %v", intDataFile, err)
}
defIP := intParams{}
err = json.Unmarshal(dt.DefaultTarget, &defIP)
if err != nil {
t.Fatalf("can't unmarshal %s: %v", intDataFile, err)
}
c, err := srljrpc.NewJSONRPCClient(&defIP.Host, srljrpc.WithOptCredentials(&defIP.Username, &defIP.Password), srljrpc.WithOptPort(&defIP.Port))
if err != nil {
t.Fatalf("can't create client: %v", err)
}
return c
}
func helperGetOCClient(t *testing.T) *srljrpc.JSONRPCClient {
// Read integration tests parameters
dt := ocTarget{}
fh, err := os.Open(intDataFile)
if err != nil {
t.Fatalf("can't open %s: %v", intDataFile, err)
}
defer fh.Close()
bIntParams, err := ioutil.ReadAll(fh)
if err != nil {
t.Fatalf("can't read %s: %v", intDataFile, err)
}
err = json.Unmarshal(bIntParams, &dt)
if err != nil {
t.Fatalf("can't unmarshal %s: %v", intDataFile, err)
}
ocIP := intParams{}
err = json.Unmarshal(dt.DefaultTarget, &ocIP)
if err != nil {
t.Fatalf("can't unmarshal %s: %v", intDataFile, err)
}
c, err := srljrpc.NewJSONRPCClient(&ocIP.Host, srljrpc.WithOptCredentials(&ocIP.Username, &ocIP.Password), srljrpc.WithOptPort(&ocIP.Port))
if err != nil {
uerr := err.(apierr.ClientError).Unwrap().Error()
t.Fatalf("can't create client: %v", err)
t.Logf("underlying error: %v", uerr)
}
return c
}
func checkClntErrGotVSExp(err error, expErr error, t *testing.T) {
switch {
case err == nil && expErr == nil:
case err != nil && expErr != nil:
if !errors.Is(err, expErr) {
t.Errorf("got: [%s], while should be: [%s]", err, expErr)
}
case err == nil && expErr != nil:
t.Errorf("got: [%v], while should be: [%s]", err, expErr)
case err != nil && expErr == nil:
t.Errorf("got: [%s], while should be: [%v]", err, expErr)
default:
t.Errorf("unexpected error - got: [%s], while should be: [%s]", err, expErr)
}
}