Files
spinifex/tests/e2e/harness/wait.go
T
Julian Sommer ee258fd248 refactor: back the e2e Eventually helpers with testify
Delegate the hand-rolled deadline loops to require.Eventually and
require.EventuallyWithT instead of open-coding the polling.
2026-09-01 12:02:39 +10:00

46 lines
1.4 KiB
Go

//go:build e2e
package harness
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Eventually polls cond until it returns true or the timeout expires.
// Replaces every `sleep N; <probe>` loop in the bash scripts.
func Eventually(t *testing.T, cond func() bool, timeout, interval time.Duration, msgAndArgs ...any) {
t.Helper()
require.Eventually(t, cond, timeout, interval, msgAndArgs...)
}
// EventuallyErr is like Eventually but lets cond return an error for the
// failure message. Useful when the probe itself yields diagnostic detail.
func EventuallyErr(t *testing.T, cond func() error, timeout, interval time.Duration) {
t.Helper()
require.EventuallyWithT(t, func(c *assert.CollectT) {
assert.NoError(c, cond())
}, timeout, interval)
}
// RetryWithReset runs fn as "attempt-1"; on failure it calls ResetAllNodes and
// retries as "attempt-2". The retry is a diagnostic aid, not a flake mask.
func RetryWithReset(t *testing.T, c *Cluster, ssh SSH, label string, fn func(*testing.T)) {
t.Helper()
if t.Run("attempt-1", fn) {
return
}
t.Logf("e2e harness: %s failed first attempt, resetting cluster and retrying", label)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
if err := ResetAllNodes(ctx, c, ssh); err != nil {
t.Errorf("e2e harness: reset before retry of %s: %v", label, err)
return
}
t.Run("attempt-2", fn)
}