From ff59b90d41edffec65449d7387862786dd072843 Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Wed, 24 Aug 2022 15:11:41 -0500 Subject: [PATCH] testing: fix flakey check status test This PR fixes a flakey test where we did not wait on the check status to actually become failing (go too fast and you just get a pending check). Instead add a helper for waiting on any check in the alloc to become the state we are looking for. --- command/alloc_status_test.go | 4 ++-- command/testing_test.go | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/command/alloc_status_test.go b/command/alloc_status_test.go index 4bec8a198..35ba53d8d 100644 --- a/command/alloc_status_test.go +++ b/command/alloc_status_test.go @@ -467,8 +467,8 @@ func TestAllocStatusCommand_NSD_Checks(t *testing.T) { // Get an alloc id allocID := getAllocFromJob(t, client, jobID) - // do not wait for alloc running - it will stay pending because the - // health-check will never pass + // wait for the check to be marked failure + waitForCheckStatus(t, client, allocID, "failure") // Run command cmd := &AllocStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}} diff --git a/command/testing_test.go b/command/testing_test.go index c71e61077..e2ec1f6c2 100644 --- a/command/testing_test.go +++ b/command/testing_test.go @@ -164,6 +164,26 @@ func waitForAllocRunning(t *testing.T, client *api.Client, allocID string) { }) } +func waitForCheckStatus(t *testing.T, client *api.Client, allocID, status string) { + testutil.WaitForResult(func() (bool, error) { + results, err := client.Allocations().Checks(allocID, nil) + if err != nil { + return false, err + } + + // pick a check, any check will do + for _, check := range results { + if check.Status == status { + return true, nil + } + } + + return false, fmt.Errorf("no check with status: %s", status) + }, func(err error) { + t.Fatalf("timed out waiting for alloc to be running: %v", err) + }) +} + func getAllocFromJob(t *testing.T, client *api.Client, jobID string) string { var allocID string if allocations, _, err := client.Jobs().Allocations(jobID, false, nil); err == nil {