@@ -382,170 +382,3 @@ func setTestEnv(t *testing.T, env map[string]string) {
382
382
t .Setenv (k , v )
383
383
}
384
384
}
385
-
386
- func TestMergeElasticsearchFromPolicy (t * testing.T ) {
387
- cfg := Elasticsearch {
388
- Protocol : "http" ,
389
- Hosts : []string {"elasticsearch:9200" },
390
- ServiceToken : "token" ,
391
- Timeout : time .Second ,
392
- MaxRetries : 1 ,
393
- MaxConnPerHost : 1 ,
394
- MaxContentLength : 1 ,
395
- }
396
- tests := []struct {
397
- name string
398
- pol Elasticsearch
399
- res Elasticsearch
400
- }{{
401
- name : "default policy" ,
402
- pol : Elasticsearch {
403
- Hosts : []string {"localhost:9200" },
404
- Timeout : DefaultElasticsearchTimeout ,
405
- MaxRetries : DefaultElasticsearchMaxRetries ,
406
- MaxConnPerHost : DefaultElasticsearchMaxConnPerHost ,
407
- MaxContentLength : DefaultElasticsearchMaxContentLength ,
408
- },
409
- res : Elasticsearch {
410
- Protocol : "http" ,
411
- Hosts : []string {"elasticsearch:9200" },
412
- ServiceToken : "token" ,
413
- Timeout : time .Second ,
414
- MaxRetries : 1 ,
415
- MaxConnPerHost : 1 ,
416
- MaxContentLength : 1 ,
417
- },
418
- }, {
419
- name : "hosts differ" ,
420
- pol : Elasticsearch {
421
- Protocol : "https" ,
422
- Hosts : []string {"elasticsearch:9200" , "other:9200" },
423
- Timeout : DefaultElasticsearchTimeout ,
424
- MaxRetries : DefaultElasticsearchMaxRetries ,
425
- MaxConnPerHost : DefaultElasticsearchMaxConnPerHost ,
426
- MaxContentLength : DefaultElasticsearchMaxContentLength ,
427
- },
428
- res : Elasticsearch {
429
- Protocol : "https" ,
430
- Hosts : []string {"elasticsearch:9200" , "other:9200" },
431
- ServiceToken : "token" ,
432
- Timeout : time .Second ,
433
- MaxRetries : 1 ,
434
- MaxConnPerHost : 1 ,
435
- MaxContentLength : 1 ,
436
- },
437
- }, {
438
- name : "all non tls attributes differ" ,
439
- pol : Elasticsearch {
440
- Protocol : "https" ,
441
- Hosts : []string {"elasticsearch:9200" , "other:9200" },
442
- Headers : map [string ]string {"custom" : "value" },
443
- ProxyURL : "http://proxy:8080" ,
444
- ProxyDisable : false ,
445
- ProxyHeaders : map [string ]string {"proxyhead" : "proxyval" },
446
- Timeout : time .Second * 2 ,
447
- MaxRetries : 2 ,
448
- MaxConnPerHost : 3 ,
449
- MaxContentLength : 4 ,
450
- },
451
- res : Elasticsearch {
452
- Protocol : "https" ,
453
- Hosts : []string {"elasticsearch:9200" , "other:9200" },
454
- Headers : map [string ]string {"custom" : "value" },
455
- ProxyURL : "http://proxy:8080" ,
456
- ProxyDisable : false ,
457
- ProxyHeaders : map [string ]string {"proxyhead" : "proxyval" },
458
- ServiceToken : "token" ,
459
- Timeout : 2 * time .Second ,
460
- MaxRetries : 2 ,
461
- MaxConnPerHost : 3 ,
462
- MaxContentLength : 4 ,
463
- },
464
- }}
465
- for _ , tc := range tests {
466
- t .Run (tc .name , func (t * testing.T ) {
467
- res := MergeElasticsearchFromPolicy (cfg , tc .pol )
468
- assert .Equal (t , tc .res .Protocol , res .Protocol )
469
- require .Len (t , res .Hosts , len (tc .res .Hosts ))
470
- for i , host := range tc .res .Hosts {
471
- assert .Equalf (t , host , res .Hosts [i ], "host %d does not match" , i )
472
- }
473
- require .Len (t , res .Headers , len (tc .res .Headers ))
474
- for k , v := range tc .res .Headers {
475
- assert .Equal (t , v , res .Headers [k ])
476
- }
477
- assert .Equal (t , tc .res .ServiceToken , res .ServiceToken )
478
- assert .Equal (t , tc .res .ServiceTokenPath , res .ServiceTokenPath )
479
- assert .Equal (t , tc .res .ProxyURL , res .ProxyURL )
480
- assert .Equal (t , tc .res .ProxyDisable , res .ProxyDisable )
481
- require .Len (t , res .ProxyHeaders , len (tc .res .ProxyHeaders ))
482
- for k , v := range tc .res .ProxyHeaders {
483
- assert .Equal (t , v , res .ProxyHeaders [k ])
484
- }
485
- assert .Nil (t , res .TLS )
486
- assert .Equal (t , tc .res .MaxRetries , res .MaxRetries )
487
- assert .Equal (t , tc .res .MaxConnPerHost , res .MaxConnPerHost )
488
- assert .Equal (t , tc .res .Timeout , res .Timeout )
489
- assert .Equal (t , tc .res .MaxContentLength , res .MaxContentLength )
490
- })
491
- }
492
- }
493
-
494
- func TestMergeElasticsearchTLS (t * testing.T ) {
495
- enabled := true
496
- disabled := false
497
- t .Run ("both nil" , func (t * testing.T ) {
498
- res := mergeElasticsearchTLS (nil , nil )
499
- assert .Nil (t , res )
500
- })
501
- t .Run ("cfg not nil" , func (t * testing.T ) {
502
- res := mergeElasticsearchTLS (& tlscommon.Config {
503
- Enabled : & enabled ,
504
- VerificationMode : tlscommon .VerifyFull ,
505
- }, nil )
506
- require .NotNil (t , res )
507
- assert .True (t , * res .Enabled )
508
- assert .Equal (t , tlscommon .VerifyFull , res .VerificationMode )
509
- })
510
- t .Run ("pol not nil" , func (t * testing.T ) {
511
- res := mergeElasticsearchTLS (nil , & tlscommon.Config {
512
- Enabled : & enabled ,
513
- VerificationMode : tlscommon .VerifyFull ,
514
- })
515
- require .NotNil (t , res )
516
- assert .True (t , * res .Enabled )
517
- assert .Equal (t , tlscommon .VerifyFull , res .VerificationMode )
518
- })
519
- t .Run ("both not nil" , func (t * testing.T ) {
520
- res := mergeElasticsearchTLS (& tlscommon.Config {
521
- Enabled : & disabled ,
522
- VerificationMode : tlscommon .VerifyFull ,
523
- }, & tlscommon.Config {
524
- Enabled : & enabled ,
525
- VerificationMode : tlscommon .VerifyCertificate ,
526
- Versions : []tlscommon.TLSVersion {tlscommon .TLSVersion13 },
527
- CipherSuites : []tlscommon.CipherSuite {tlscommon .CipherSuite (tls .TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA )},
528
- CAs : []string {"/path/to/ca.crt" },
529
- Certificate : tlscommon.CertificateConfig {
530
- Certificate : "/path/to/cert.crt" ,
531
- Key : "/path/to/key.crt" ,
532
- },
533
- CASha256 : []string {"casha256val" },
534
- CATrustedFingerprint : "fingerprint" ,
535
- })
536
- require .NotNil (t , res )
537
- assert .True (t , * res .Enabled )
538
- assert .Equal (t , tlscommon .VerifyCertificate , res .VerificationMode )
539
- require .Len (t , res .Versions , 1 )
540
- assert .Equal (t , tlscommon .TLSVersion13 , res .Versions [0 ])
541
- require .Len (t , res .CipherSuites , 1 )
542
- assert .Equal (t , tlscommon .CipherSuite (tls .TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA ), res .CipherSuites [0 ])
543
- require .Len (t , res .CAs , 1 )
544
- assert .Equal (t , "/path/to/ca.crt" , res .CAs [0 ])
545
- assert .Equal (t , "/path/to/cert.crt" , res .Certificate .Certificate )
546
- assert .Equal (t , "/path/to/key.crt" , res .Certificate .Key )
547
- require .Len (t , res .CASha256 , 1 )
548
- assert .Equal (t , "casha256val" , res .CASha256 [0 ])
549
- assert .Equal (t , "fingerprint" , res .CATrustedFingerprint )
550
- })
551
- }
0 commit comments