@@ -398,68 +398,63 @@ func TestServerRunWithSNI(t *testing.T) {
398
398
return strings .Replace (name , "*" , "star" , - 1 )
399
399
}
400
400
401
- NextTest:
402
- for title , test := range tests {
403
- // create server cert
404
- certDir := "testdata/" + specToName (test .Cert )
405
- serverCertBundleFile := filepath .Join (certDir , "cert" )
406
- serverKeyFile := filepath .Join (certDir , "key" )
407
- err := getOrCreateTestCertFiles (serverCertBundleFile , serverKeyFile , test .Cert )
408
- if err != nil {
409
- t .Errorf ("%q - failed to create server cert: %v" , title , err )
410
- continue NextTest
411
- }
412
- ca , err := caCertFromBundle (serverCertBundleFile )
413
- if err != nil {
414
- t .Errorf ("%q - failed to extract ca cert from server cert bundle: %v" , title , err )
415
- continue NextTest
416
- }
417
- caCerts := []* x509.Certificate {ca }
418
-
419
- // create SNI certs
420
- var namedCertKeys []utilflag.NamedCertKey
421
- serverSig , err := certFileSignature (serverCertBundleFile , serverKeyFile )
422
- if err != nil {
423
- t .Errorf ("%q - failed to get server cert signature: %v" , title , err )
424
- continue NextTest
425
- }
426
- signatures := map [string ]int {
427
- serverSig : - 1 ,
428
- }
429
- for j , c := range test .SNICerts {
430
- sniDir := filepath .Join (certDir , specToName (c .TestCertSpec ))
431
- certBundleFile := filepath .Join (sniDir , "cert" )
432
- keyFile := filepath .Join (sniDir , "key" )
433
- err := getOrCreateTestCertFiles (certBundleFile , keyFile , c .TestCertSpec )
401
+ for title := range tests {
402
+ test := tests [title ]
403
+ t .Run (title , func (t * testing.T ) {
404
+ t .Parallel ()
405
+ // create server cert
406
+ certDir := "testdata/" + specToName (test .Cert )
407
+ serverCertBundleFile := filepath .Join (certDir , "cert" )
408
+ serverKeyFile := filepath .Join (certDir , "key" )
409
+ err := getOrCreateTestCertFiles (serverCertBundleFile , serverKeyFile , test .Cert )
434
410
if err != nil {
435
- t .Errorf ("%q - failed to create SNI cert %d: %v" , title , j , err )
436
- continue NextTest
411
+ t .Fatalf ("failed to create server cert: %v" , err )
437
412
}
438
-
439
- namedCertKeys = append (namedCertKeys , utilflag.NamedCertKey {
440
- KeyFile : keyFile ,
441
- CertFile : certBundleFile ,
442
- Names : c .explicitNames ,
443
- })
444
-
445
- ca , err := caCertFromBundle (certBundleFile )
413
+ ca , err := caCertFromBundle (serverCertBundleFile )
446
414
if err != nil {
447
- t .Errorf ("%q - failed to extract ca cert from SNI cert %d: %v" , title , j , err )
448
- continue NextTest
415
+ t .Fatalf ("failed to extract ca cert from server cert bundle: %v" , err )
449
416
}
450
- caCerts = append ( caCerts , ca )
417
+ caCerts := [] * x509. Certificate { ca }
451
418
452
- // store index in namedCertKeys with the signature as the key
453
- sig , err := certFileSignature (certBundleFile , keyFile )
419
+ // create SNI certs
420
+ var namedCertKeys []utilflag.NamedCertKey
421
+ serverSig , err := certFileSignature (serverCertBundleFile , serverKeyFile )
454
422
if err != nil {
455
- t .Errorf ("%q - failed get SNI cert %d signature: %v" , title , j , err )
456
- continue NextTest
423
+ t .Fatalf ("failed to get server cert signature: %v" , err )
457
424
}
458
- signatures [sig ] = j
459
- }
425
+ signatures := map [string ]int {
426
+ serverSig : - 1 ,
427
+ }
428
+ for j , c := range test .SNICerts {
429
+ sniDir := filepath .Join (certDir , specToName (c .TestCertSpec ))
430
+ certBundleFile := filepath .Join (sniDir , "cert" )
431
+ keyFile := filepath .Join (sniDir , "key" )
432
+ err := getOrCreateTestCertFiles (certBundleFile , keyFile , c .TestCertSpec )
433
+ if err != nil {
434
+ t .Fatalf ("failed to create SNI cert %d: %v" , j , err )
435
+ }
460
436
461
- stopCh := make (chan struct {})
462
- func () {
437
+ namedCertKeys = append (namedCertKeys , utilflag.NamedCertKey {
438
+ KeyFile : keyFile ,
439
+ CertFile : certBundleFile ,
440
+ Names : c .explicitNames ,
441
+ })
442
+
443
+ ca , err := caCertFromBundle (certBundleFile )
444
+ if err != nil {
445
+ t .Fatalf ("failed to extract ca cert from SNI cert %d: %v" , j , err )
446
+ }
447
+ caCerts = append (caCerts , ca )
448
+
449
+ // store index in namedCertKeys with the signature as the key
450
+ sig , err := certFileSignature (certBundleFile , keyFile )
451
+ if err != nil {
452
+ t .Fatalf ("failed get SNI cert %d signature: %v" , j , err )
453
+ }
454
+ signatures [sig ] = j
455
+ }
456
+
457
+ stopCh := make (chan struct {})
463
458
defer close (stopCh )
464
459
465
460
// launch server
@@ -483,22 +478,20 @@ NextTest:
483
478
// use a random free port
484
479
ln , err := net .Listen ("tcp" , "127.0.0.1:0" )
485
480
if err != nil {
486
- t .Errorf ("failed to listen on 127.0.0.1:0" )
481
+ t .Fatalf ("failed to listen on 127.0.0.1:0" )
487
482
}
488
483
489
484
secureOptions .Listener = ln
490
485
// get port
491
486
secureOptions .BindPort = ln .Addr ().(* net.TCPAddr ).Port
492
487
config .LoopbackClientConfig = & restclient.Config {}
493
488
if err := secureOptions .ApplyTo (& config ); err != nil {
494
- t .Errorf ("%q - failed applying the SecureServingOptions: %v" , title , err )
495
- return
489
+ t .Fatalf ("failed applying the SecureServingOptions: %v" , err )
496
490
}
497
491
498
492
s , err := config .Complete (nil ).New ("test" , server .NewEmptyDelegate ())
499
493
if err != nil {
500
- t .Errorf ("%q - failed creating the server: %v" , title , err )
501
- return
494
+ t .Fatalf ("failed creating the server: %v" , err )
502
495
}
503
496
504
497
// add poststart hook to know when the server is up.
@@ -530,22 +523,20 @@ NextTest:
530
523
ServerName : test .ServerName , // used for SNI in the client HELLO packet
531
524
})
532
525
if err != nil {
533
- t .Errorf ("%q - failed to connect: %v" , title , err )
534
- return
526
+ t .Fatalf ("failed to connect: %v" , err )
535
527
}
528
+ defer conn .Close ()
536
529
537
530
// check returned server certificate
538
531
sig := x509CertSignature (conn .ConnectionState ().PeerCertificates [0 ])
539
532
gotCertIndex , found := signatures [sig ]
540
533
if ! found {
541
- t .Errorf ("%q - unknown signature returned from server: %s" , title , sig )
534
+ t .Errorf ("unknown signature returned from server: %s" , sig )
542
535
}
543
536
if gotCertIndex != test .ExpectedCertIndex {
544
- t .Errorf ("%q - expected cert index %d, got cert index %d" , title , test .ExpectedCertIndex , gotCertIndex )
537
+ t .Errorf ("expected cert index %d, got cert index %d" , test .ExpectedCertIndex , gotCertIndex )
545
538
}
546
539
547
- conn .Close ()
548
-
549
540
// check that the loopback client can connect
550
541
host := "127.0.0.1"
551
542
if len (test .LoopbackClientBindAddressOverride ) != 0 {
@@ -554,28 +545,25 @@ NextTest:
554
545
s .LoopbackClientConfig .Host = net .JoinHostPort (host , strconv .Itoa (secureOptions .BindPort ))
555
546
if test .ExpectLoopbackClientError {
556
547
if err == nil {
557
- t .Errorf ( "%q - expected error creating loopback client config", title )
548
+ t .Fatalf ( " expected error creating loopback client config" )
558
549
}
559
550
return
560
551
}
561
552
if err != nil {
562
- t .Errorf ("%q - failed creating loopback client config: %v" , title , err )
563
- return
553
+ t .Fatalf ("failed creating loopback client config: %v" , err )
564
554
}
565
555
client , err := discovery .NewDiscoveryClientForConfig (s .LoopbackClientConfig )
566
556
if err != nil {
567
- t .Errorf ("%q - failed to create loopback client: %v" , title , err )
568
- return
557
+ t .Fatalf ("failed to create loopback client: %v" , err )
569
558
}
570
559
got , err := client .ServerVersion ()
571
560
if err != nil {
572
- t .Errorf ("%q - failed to connect with loopback client: %v" , title , err )
573
- return
561
+ t .Fatalf ("failed to connect with loopback client: %v" , err )
574
562
}
575
563
if expected := & v ; ! reflect .DeepEqual (got , expected ) {
576
- t .Errorf ("%q - loopback client didn't get correct version info: expected=%v got=%v" , title , expected , got )
564
+ t .Errorf ("loopback client didn't get correct version info: expected=%v got=%v" , expected , got )
577
565
}
578
- }( )
566
+ })
579
567
}
580
568
}
581
569
0 commit comments