mirror of
https://github.com/tailscale/tailscale.git
synced 2025-06-12 02:38:35 +00:00
cmd/testwrapper/flakytest: add Marked to check if in flakytest (#15119)
Updates tailscale/corp#26637 Signed-off-by: Paul Scott <paul@tailscale.com>
This commit is contained in:
parent
781c1e9624
commit
d1b0e1af06
@ -9,8 +9,12 @@ package flakytest
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"tailscale.com/util/mak"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FlakyTestLogMessage is a sentinel value that is printed to stderr when a
|
// FlakyTestLogMessage is a sentinel value that is printed to stderr when a
|
||||||
@ -25,6 +29,11 @@ const FlakeAttemptEnv = "TS_TESTWRAPPER_ATTEMPT"
|
|||||||
|
|
||||||
var issueRegexp = regexp.MustCompile(`\Ahttps://github\.com/tailscale/[a-zA-Z0-9_.-]+/issues/\d+\z`)
|
var issueRegexp = regexp.MustCompile(`\Ahttps://github\.com/tailscale/[a-zA-Z0-9_.-]+/issues/\d+\z`)
|
||||||
|
|
||||||
|
var (
|
||||||
|
rootFlakesMu sync.Mutex
|
||||||
|
rootFlakes map[string]bool
|
||||||
|
)
|
||||||
|
|
||||||
// Mark sets the current test as a flaky test, such that if it fails, it will
|
// 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
|
// 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:
|
// the status of the flaky test being marked, of the format:
|
||||||
@ -41,4 +50,24 @@ func Mark(t testing.TB, issue string) {
|
|||||||
fmt.Fprintf(os.Stderr, "%s: %s\n", FlakyTestLogMessage, issue)
|
fmt.Fprintf(os.Stderr, "%s: %s\n", FlakyTestLogMessage, issue)
|
||||||
}
|
}
|
||||||
t.Logf("flakytest: issue tracking this flaky test: %s", issue)
|
t.Logf("flakytest: issue tracking this flaky test: %s", issue)
|
||||||
|
|
||||||
|
// Record the root test name as flakey.
|
||||||
|
rootFlakesMu.Lock()
|
||||||
|
defer rootFlakesMu.Unlock()
|
||||||
|
mak.Set(&rootFlakes, t.Name(), true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marked reports whether the current test or one of its parents was marked flaky.
|
||||||
|
func Marked(t testing.TB) bool {
|
||||||
|
n := t.Name()
|
||||||
|
for {
|
||||||
|
if rootFlakes[n] {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
n = path.Dir(n)
|
||||||
|
if n == "." || n == "/" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
@ -41,3 +41,49 @@ func TestFlakeRun(t *testing.T) {
|
|||||||
t.Fatal("First run in testwrapper, failing so that test is retried. This is expected.")
|
t.Fatal("First run in testwrapper, failing so that test is retried. This is expected.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMarked_Root(t *testing.T) {
|
||||||
|
Mark(t, "https://github.com/tailscale/tailscale/issues/0")
|
||||||
|
|
||||||
|
t.Run("child", func(t *testing.T) {
|
||||||
|
t.Run("grandchild", func(t *testing.T) {
|
||||||
|
if got, want := Marked(t), true; got != want {
|
||||||
|
t.Fatalf("Marked(t) = %t, want %t", got, want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if got, want := Marked(t), true; got != want {
|
||||||
|
t.Fatalf("Marked(t) = %t, want %t", got, want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if got, want := Marked(t), true; got != want {
|
||||||
|
t.Fatalf("Marked(t) = %t, want %t", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarked_Subtest(t *testing.T) {
|
||||||
|
t.Run("flaky", func(t *testing.T) {
|
||||||
|
Mark(t, "https://github.com/tailscale/tailscale/issues/0")
|
||||||
|
|
||||||
|
t.Run("child", func(t *testing.T) {
|
||||||
|
t.Run("grandchild", func(t *testing.T) {
|
||||||
|
if got, want := Marked(t), true; got != want {
|
||||||
|
t.Fatalf("Marked(t) = %t, want %t", got, want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if got, want := Marked(t), true; got != want {
|
||||||
|
t.Fatalf("Marked(t) = %t, want %t", got, want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if got, want := Marked(t), true; got != want {
|
||||||
|
t.Fatalf("Marked(t) = %t, want %t", got, want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if got, want := Marked(t), false; got != want {
|
||||||
|
t.Fatalf("Marked(t) = %t, want %t", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user