2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2023-01-18 16:41:58 +00:00
|
|
|
|
|
|
|
// Package flakytest contains test helpers for marking a test as flaky. For
|
|
|
|
// tests run using cmd/testwrapper, a failed flaky test will cause tests to be
|
|
|
|
// re-run a few time until they succeed or exceed our iteration limit.
|
|
|
|
package flakytest
|
|
|
|
|
|
|
|
import (
|
2023-02-15 01:57:02 +00:00
|
|
|
"fmt"
|
2023-01-18 16:41:58 +00:00
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2023-02-15 01:57:02 +00:00
|
|
|
// FlakyTestLogMessage is a sentinel value that is printed to stderr when a
|
|
|
|
// flaky test is marked. This is used by cmd/testwrapper to detect flaky tests
|
|
|
|
// and retry them.
|
|
|
|
const FlakyTestLogMessage = "flakytest: this is a known flaky test"
|
|
|
|
|
|
|
|
// FlakeAttemptEnv is an environment variable that is set by cmd/testwrapper
|
|
|
|
// when a flaky test is retried. It contains the attempt number, starting at 1.
|
|
|
|
const FlakeAttemptEnv = "TS_TESTWRAPPER_ATTEMPT"
|
2023-01-18 16:41:58 +00:00
|
|
|
|
|
|
|
var issueRegexp = regexp.MustCompile(`\Ahttps://github\.com/tailscale/[a-zA-Z0-9_.-]+/issues/\d+\z`)
|
|
|
|
|
|
|
|
// Mark sets the current test as a flaky test, such that if it fails, it will
|
|
|
|
// be retried a few times on failure. issue must be a GitHub issue that tracks
|
|
|
|
// the status of the flaky test being marked, of the format:
|
|
|
|
//
|
|
|
|
// https://github.com/tailscale/myRepo-H3re/issues/12345
|
2023-01-23 18:27:44 +00:00
|
|
|
func Mark(t testing.TB, issue string) {
|
2023-01-18 16:41:58 +00:00
|
|
|
if !issueRegexp.MatchString(issue) {
|
|
|
|
t.Fatalf("bad issue format: %q", issue)
|
|
|
|
}
|
|
|
|
|
2023-02-15 01:57:02 +00:00
|
|
|
fmt.Fprintln(os.Stderr, FlakyTestLogMessage) // sentinel value for testwrapper
|
|
|
|
t.Logf("flakytest: issue tracking this flaky test: %s", issue)
|
2023-01-18 16:41:58 +00:00
|
|
|
}
|