Skip to content

Commit 535e2a9

Browse files
committed
Fix the e2e request ID test (again)
1 parent b83b8aa commit 535e2a9

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

test/e2e/requestid_test.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,22 @@ import (
1919
"go.step.sm/crypto/pemutil"
2020
)
2121

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
2424
// available for running a server on.
25-
func reserveAddress(t *testing.T) string {
25+
func reservePort(t *testing.T) (host, port string) {
2626
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)
3329

3430
address := l.Addr().String()
3531
err = l.Close()
3632
require.NoError(t, err)
3733

38-
return address
34+
host, port, err = net.SplitHostPort(address)
35+
require.NoError(t, err)
36+
37+
return
3938
}
4039

4140
func Test_reflectRequestID(t *testing.T) {
@@ -57,13 +56,13 @@ func Test_reflectRequestID(t *testing.T) {
5756

5857
// get a random address to listen on and connect to; currently no nicer way to get one before starting the server
5958
// 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)
6160

6261
cfg := &config.Config{
6362
Root: []string{rootFilepath},
6463
IntermediateCert: intermediateCertFilepath,
6564
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"
6766
DNSNames: []string{"127.0.0.1", "[::1]", "localhost"},
6867
AuthorityConfig: &config.AuthConfig{
6968
AuthorityID: "stepca-test",
@@ -76,7 +75,7 @@ func Test_reflectRequestID(t *testing.T) {
7675

7776
// instantiate a client for the CA running at the random address
7877
caClient, err := ca.NewClient(
79-
fmt.Sprintf("https://%s", address),
78+
fmt.Sprintf("https://localhost:%s", port),
8079
ca.WithRootFile(rootFilepath),
8180
)
8281
require.NoError(t, err)
@@ -93,8 +92,10 @@ func Test_reflectRequestID(t *testing.T) {
9392
// require OK health response as the baseline
9493
ctx := context.Background()
9594
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+
}
9899

99100
// expect an error when retrieving an invalid root
100101
rootResponse, err := caClient.RootWithContext(ctx, "invalid")

0 commit comments

Comments
 (0)