@@ -19,23 +19,22 @@ import (
19
19
"go.step.sm/crypto/pemutil"
20
20
)
21
21
22
- // reserveAddress "reserves" a TCP address by opening a listener on a random
23
- // port and immediately closing it. The address can then be assumed to be
22
+ // reservePort "reserves" a TCP port by opening a listener on a random
23
+ // port and immediately closing it. The port can then be assumed to be
24
24
// available for running a server on.
25
- func reserveAddress (t * testing.T ) string {
25
+ func reservePort (t * testing.T ) ( host , port string ) {
26
26
t .Helper ()
27
- l , err := net .Listen ("tcp" , "127.0.0.1:0" )
28
- if err != nil {
29
- if l , err = net .Listen ("tcp6" , "[::1]:0" ); err != nil {
30
- require .NoError (t , err , "failed to listen on a port" )
31
- }
32
- }
27
+ l , err := net .Listen ("tcp" , ":0" )
28
+ require .NoError (t , err )
33
29
34
30
address := l .Addr ().String ()
35
31
err = l .Close ()
36
32
require .NoError (t , err )
37
33
38
- return address
34
+ host , port , err = net .SplitHostPort (address )
35
+ require .NoError (t , err )
36
+
37
+ return
39
38
}
40
39
41
40
func Test_reflectRequestID (t * testing.T ) {
@@ -57,13 +56,13 @@ func Test_reflectRequestID(t *testing.T) {
57
56
58
57
// get a random address to listen on and connect to; currently no nicer way to get one before starting the server
59
58
// TODO(hs): find/implement a nicer way to expose the CA URL, similar to how e.g. httptest.Server exposes it?
60
- address := reserveAddress (t )
59
+ host , port := reservePort (t )
61
60
62
61
cfg := & config.Config {
63
62
Root : []string {rootFilepath },
64
63
IntermediateCert : intermediateCertFilepath ,
65
64
IntermediateKey : intermediateKeyFilepath ,
66
- Address : address , // reuse the address that was just "reserved"
65
+ Address : net . JoinHostPort ( host , port ) , // reuse the address that was just "reserved"
67
66
DNSNames : []string {"127.0.0.1" , "[::1]" , "localhost" },
68
67
AuthorityConfig : & config.AuthConfig {
69
68
AuthorityID : "stepca-test" ,
@@ -76,7 +75,7 @@ func Test_reflectRequestID(t *testing.T) {
76
75
77
76
// instantiate a client for the CA running at the random address
78
77
caClient , err := ca .NewClient (
79
- fmt .Sprintf ("https://%s" , address ),
78
+ fmt .Sprintf ("https://localhost: %s" , port ),
80
79
ca .WithRootFile (rootFilepath ),
81
80
)
82
81
require .NoError (t , err )
@@ -93,8 +92,10 @@ func Test_reflectRequestID(t *testing.T) {
93
92
// require OK health response as the baseline
94
93
ctx := context .Background ()
95
94
healthResponse , err := caClient .HealthWithContext (ctx )
96
- assert .NoError (t , err )
97
- assert .Equal (t , "ok" , healthResponse .Status )
95
+ require .NoError (t , err )
96
+ if assert .NotNil (t , healthResponse ) {
97
+ require .Equal (t , "ok" , healthResponse .Status )
98
+ }
98
99
99
100
// expect an error when retrieving an invalid root
100
101
rootResponse , err := caClient .RootWithContext (ctx , "invalid" )
0 commit comments