From 0bde93060c809a9a7295f1c11916b91db78a5c5c Mon Sep 17 00:00:00 2001 From: Claire Wang Date: Fri, 30 Jun 2023 16:48:12 -0400 Subject: [PATCH] cmd/testwrapper: Use testwrapper output in github workflows Fixes #8493 Signed-off-by: Claire Wang --- .github/workflows/test.yml | 7 +++++++ cmd/testwrapper/testwrapper.go | 25 ++++++++++++++++--------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8e6f77a0f..504f0d9d8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -93,6 +93,13 @@ jobs: run: PATH=$PWD/tool:$PATH /tmp/testwrapper ./... ${{matrix.buildflags}} env: GOARCH: ${{ matrix.goarch }} + - name: upload test output + uses: actions/upload-artifact@v3 + with: + name: ${{ github.job }}-${{ runner.os }}-${{ matrix.goarch }}-test-attempts.json + path: test_attempts.json + - name: remove upload test output + run: rm test_attempts.json - name: bench all run: PATH=$PWD/tool:$PATH /tmp/testwrapper ./... ${{matrix.buildflags}} -bench=. -benchtime=1x -run=^$ env: diff --git a/cmd/testwrapper/testwrapper.go b/cmd/testwrapper/testwrapper.go index de472fffd..9b1425d55 100644 --- a/cmd/testwrapper/testwrapper.go +++ b/cmd/testwrapper/testwrapper.go @@ -28,18 +28,21 @@ import ( const maxAttempts = 3 +// testAttempt keeps track of the test name, outcome, logs, and if the test is flakey. +// After running the tests, each testAttempt is written to a json file. type testAttempt struct { - name testName - outcome string // "pass", "fail", "skip" + name testName `json:"testName,omitempty"` + outcome string `json:"outcome,omitempty"` // "pass", "fail", "skip" logs bytes.Buffer isMarkedFlaky bool // set if the test is marked as flaky pkgFinished bool } +// testName keeps track of the test name and its package. type testName struct { - pkg string // "tailscale.com/types/key" - name string // "TestFoo" + Pkg string `json:"pkg,omitempty"` // "tailscale.com/types/key" + Name string `json:"name,omitempty"` // "TestFoo" } type packageTests struct { @@ -120,11 +123,11 @@ func runTests(ctx context.Context, attempt int, pt *packageTests, otherArgs []st continue } name := testName{ - pkg: goOutput.Package, - name: goOutput.Test, + Pkg: goOutput.Package, + Name: goOutput.Test, } if test, _, isSubtest := strings.Cut(goOutput.Test, "/"); isSubtest { - name.name = test + name.Name = test if goOutput.Action == "output" { resultMap[name].logs.WriteString(goOutput.Output) } @@ -135,19 +138,23 @@ func runTests(ctx context.Context, attempt int, pt *packageTests, otherArgs []st // ignore case "run": resultMap[name] = &testAttempt{ - name: name, + Name: name, } case "skip", "pass", "fail": resultMap[name].outcome = goOutput.Action ch <- resultMap[name] case "output": if strings.TrimSpace(goOutput.Output) == flakytest.FlakyTestLogMessage { - resultMap[name].isMarkedFlaky = true + resultMap[name].IsMarkedFlaky = true } else { resultMap[name].logs.WriteString(goOutput.Output) } } } + for _, result := range resultMap { + testAttemptJson, _ := json.Marshal(result) + f.Write(testAttemptJson) + } <-done }