Skip to content

Commit

Permalink
Wait and retry connection to test CA server instead of failing (immed…
Browse files Browse the repository at this point in the history
…iately)
  • Loading branch information
hslatman committed Jan 7, 2025
1 parent 98ddef5 commit a93d51b
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions test/integration/requestid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func reservePort(t *testing.T) (host, port string) {
}

func Test_reflectRequestID(t *testing.T) {
ctx := context.Background()

dir := t.TempDir()
m, err := minica.New(minica.WithName("Step E2E"))
require.NoError(t, err)
Expand Down Expand Up @@ -133,12 +135,29 @@ func Test_reflectRequestID(t *testing.T) {
require.ErrorIs(t, err, http.ErrServerClosed)
}()

// require OK health response as the baseline
ctx := context.Background()
healthResponse, err := caClient.HealthWithContext(ctx)
require.NoError(t, err)
if assert.NotNil(t, healthResponse) {
require.Equal(t, "ok", healthResponse.Status)
// require OK health response from the CA server within
// 10 seconds, retrying every ~100 milliseconds.
connected := false
stopTime := time.Now().Add(10 * time.Second)
for {
healthResponse, err := caClient.HealthWithContext(ctx)
if err == nil {
require.NotNil(t, healthResponse)
require.Equal(t, "ok", healthResponse.Status)
connected = true
}

if connected || time.Now().After(stopTime) {
break
}

time.Sleep(100 * time.Millisecond)
}

// fail the test if CA client fails to connect to the
// CA server at least once within 10 seconds.
if !connected {
require.FailNow(t, fmt.Sprintf("CA client failed to connect to CA server at https://localhost:%s", port))
}

// expect an error when retrieving an invalid root
Expand Down Expand Up @@ -262,8 +281,8 @@ func newAuthorizingServer(t *testing.T, mca *minica.CA) *httptest.Server {

srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if assert.Equal(t, "signRequestID", r.Header.Get("X-Request-Id")) {
json.NewEncoder(w).Encode(struct{ Allow bool }{Allow: true})
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(struct{ Allow bool }{Allow: true})
require.NoError(t, err)
return
}

Expand Down

0 comments on commit a93d51b

Please sign in to comment.