Skip to content

Commit

Permalink
test: retry talosctl time call in the tests
Browse files Browse the repository at this point in the history
As `talosctl time` relies on default time server set in the config, and
our nodes start with `pool.ntp.org`, sometimes request to the timeserver
fails failing the tests.

Retry such errors in the tests to avoid spurious failures.

Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
smira committed Dec 17, 2021
1 parent acf1ac0 commit 17c1474
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
36 changes: 35 additions & 1 deletion internal/integration/base/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strings"

"github.com/stretchr/testify/suite"
"github.com/talos-systems/go-retry/retry"
)

// RunOption configures options for Run.
Expand All @@ -24,6 +25,7 @@ type RunOption func(*runOptions)
type MatchFunc func(output string) error

type runOptions struct {
retryer retry.Retryer
shouldFail bool
stdoutEmpty bool
stderrNotEmpty bool
Expand All @@ -35,6 +37,13 @@ type runOptions struct {
stderrMatchers []MatchFunc
}

// WithRetry retries failing command runs.
func WithRetry(retryer retry.Retryer) RunOption {
return func(opts *runOptions) {
opts.retryer = retryer
}
}

// ShouldFail tells run command should fail (with non-empty stderr).
//
// ShouldFail also sets StdErrNotEmpty.
Expand Down Expand Up @@ -141,6 +150,21 @@ func runAndWait(suite *suite.Suite, cmd *exec.Cmd) (stdoutBuf, stderrBuf *bytes.
return &stdout, &stderr, err
}

// retryRunAndWait retries runAndWait if the command fails to run.
func retryRunAndWait(suite *suite.Suite, cmd *exec.Cmd, retryer retry.Retryer) (stdoutBuf, stderrBuf *bytes.Buffer, err error) {
err = retryer.Retry(func() error {
stdoutBuf, stderrBuf, err = runAndWait(suite, cmd)

if _, ok := err.(*exec.ExitError); ok {
return retry.ExpectedError(err)
}

return err
})

return
}

// run executes command, asserts on its exit status/output, and returns stdout.
//
//nolint:gocyclo,nakedret
Expand All @@ -151,7 +175,17 @@ func run(suite *suite.Suite, cmd *exec.Cmd, options ...RunOption) (stdout string
o(&opts)
}

stdoutBuf, stderrBuf, err := runAndWait(suite, cmd)
var (
stdoutBuf, stderrBuf *bytes.Buffer
err error
)

if opts.retryer != nil {
stdoutBuf, stderrBuf, err = retryRunAndWait(suite, cmd, opts.retryer)
} else {
stdoutBuf, stderrBuf, err = runAndWait(suite, cmd)
}

if err != nil {
// check that command failed, not something else happened
_, ok := err.(*exec.ExitError)
Expand Down
4 changes: 4 additions & 0 deletions internal/integration/cli/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ package cli

import (
"regexp"
"time"

"github.com/talos-systems/go-retry/retry"

"github.com/talos-systems/talos/internal/integration/base"
)
Expand All @@ -28,6 +31,7 @@ func (suite *TimeSuite) TestDefault() {
suite.RunCLI([]string{"time", "--nodes", suite.RandomDiscoveredNode()},
base.StdoutShouldMatch(regexp.MustCompile(`NTP-SERVER`)),
base.StdoutShouldMatch(regexp.MustCompile(`UTC`)),
base.WithRetry(retry.Constant(time.Minute, retry.WithUnits(time.Second))),
)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/miniprocfs/testdata/1920080/exe
2 changes: 1 addition & 1 deletion internal/pkg/miniprocfs/testdata/3731034/exe

0 comments on commit 17c1474

Please sign in to comment.