Skip to content

Commit

Permalink
Merge pull request #14301 from hashicorp/b-fix-check-status-test-racey
Browse files Browse the repository at this point in the history
testing: fix flakey check status test
  • Loading branch information
shoenig committed Aug 25, 2022
2 parents 546bdb8 + 3ae6db6 commit 7cd0c35
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions command/alloc_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
20 changes: 20 additions & 0 deletions command/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7cd0c35

Please sign in to comment.