Signed-off-by: Claire Wang <claire@tailscale.com>
clairew/test-wrapper-write-file
Claire Wang 2023-07-05 14:27:50 -04:00
parent e2dca1c8b4
commit 67fd260317
2 changed files with 13 additions and 13 deletions

View File

@ -90,7 +90,7 @@ jobs:
- name: build test wrapper - name: build test wrapper
run: ./tool/go build -o /tmp/testwrapper ./cmd/testwrapper run: ./tool/go build -o /tmp/testwrapper ./cmd/testwrapper
- name: test all - name: test all
run: PATH=$PWD/tool:$PATH /tmp/testwrapper -w ./... ${{matrix.buildflags}} run: PATH=$PWD/tool:$PATH /tmp/testwrapper -json test_attempts.json ./... ${{matrix.buildflags}}
env: env:
GOARCH: ${{ matrix.goarch }} GOARCH: ${{ matrix.goarch }}
- name: upload test output - name: upload test output

View File

@ -31,18 +31,18 @@ const maxAttempts = 3
// testAttempt keeps track of the test name, outcome, logs, and if the test is flakey. // 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. // After running the tests, each testAttempt is written to a json file.
type testAttempt struct { type testAttempt struct {
Name testName `json:"name,omitempty,inline"` Name testName `json:",inline"`
Outcome string `json:"outcome,omitempty"` // "pass", "fail", "skip" Outcome string `json:",omitempty"` // "pass", "fail", "skip"
logs bytes.Buffer logs bytes.Buffer
IsMarkedFlaky bool `json:"is_marked_flaky,omitempty"` // set if the test is marked as flaky IsMarkedFlaky bool `json:",omitempty"` // set if the test is marked as flaky
AttemptCount int `json:"attempt_count,omitempty"` AttemptCount int `json:",omitempty"`
PkgFinished bool `json:"pkg_finished,omitempty"` PkgFinished bool `json:",omitempty"`
} }
// testName keeps track of the test name and its package. // testName keeps track of the test name and its package.
type testName struct { type testName struct {
Pkg string `json:"pkg,omitempty"` // "tailscale.com/types/key" Pkg string `json:",omitempty"` // "tailscale.com/types/key"
Name string `json:"name,omitempty"` // "TestFoo" Name string `json:",omitempty"` // "TestFoo"
} }
type packageTests struct { type packageTests struct {
@ -173,12 +173,12 @@ func main() {
// We need to parse the -v flag to figure out whether to print the logs // We need to parse the -v flag to figure out whether to print the logs
// for a test. // for a test.
// The -w flag is to indicate whether we want to write the json test results to a file. // The -json flag is to indicate whether we want to write the json test results to a provided file.
// We run `go test -json` which returns the same information as `go test -v`, // We run `go test -json` which returns the same information as `go test -v`,
// but in a machine-readable format. So this flag is only for testwrapper's // but in a machine-readable format. So this flag is only for testwrapper's
// output. // output.
v := flag.Bool("v", false, "verbose") v := flag.Bool("v", false, "verbose")
w := flag.Bool("w", false, "write") jsonFile := flag.String("json", "", "if set, the file to store all test results in")
flag.Usage = func() { flag.Usage = func() {
fmt.Println("usage: testwrapper [testwrapper-flags] [pattern] [build/test flags & test binary flags]") fmt.Println("usage: testwrapper [testwrapper-flags] [pattern] [build/test flags & test binary flags]")
@ -189,7 +189,7 @@ func main() {
fmt.Println("examples:") fmt.Println("examples:")
fmt.Println("\ttestwrapper -v ./... -count=1") fmt.Println("\ttestwrapper -v ./... -count=1")
fmt.Println("\ttestwrapper ./pkg/foo -run TestBar -count=1") fmt.Println("\ttestwrapper ./pkg/foo -run TestBar -count=1")
fmt.Println("\ttestwrapper -w ./pkg/foo -run TestBar -count=1") fmt.Println("\ttestwrapper -json=test_attempts.json ./pkg/foo -run TestBar -count=1")
fmt.Println() fmt.Println()
fmt.Println("Unlike 'go test', testwrapper requires a package pattern as the first positional argument and only supports a single pattern.") fmt.Println("Unlike 'go test', testwrapper requires a package pattern as the first positional argument and only supports a single pattern.")
} }
@ -236,8 +236,8 @@ func main() {
fmt.Printf("%s\t%s\n", outcome, pkg) fmt.Printf("%s\t%s\n", outcome, pkg)
} }
opts := &options{} opts := &options{}
if *w { if *jsonFile != "" {
f, err := os.Create("test_attempts.json") f, err := os.Create(*jsonFile)
if err != nil { if err != nil {
log.Printf("error creating test attempt json file: %v", err) log.Printf("error creating test attempt json file: %v", err)
} }