mirror of
https://github.com/mulgadc/spinifex.git
synced 2026-10-15 17:11:30 +00:00
ee258fd248
Delegate the hand-rolled deadline loops to require.Eventually and require.EventuallyWithT instead of open-coding the polling.
46 lines
1.4 KiB
Go
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)
|
|
}
|