Skip to content

Commit

Permalink
tools: exit(1) when test/build fail (#31247)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkingrei authored Jan 12, 2022
1 parent 7b0ebc3 commit 1d14d99
Showing 1 changed file with 54 additions and 35 deletions.
89 changes: 54 additions & 35 deletions tools/check/ut.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
_ "go.uber.org/automaxprocs"
)

func usage() {
func usage() bool {
msg := `// run all tests
ut
Expand Down Expand Up @@ -59,6 +59,7 @@ ut build
// build a test package
ut build xxx`
fmt.Println(msg)
return true
}

const modulePath = "github.com/pingcap/tidb"
Expand All @@ -72,19 +73,19 @@ type task struct {
var P int
var workDir string

func cmdList(args ...string) {
func cmdList(args ...string) bool {
pkgs, err := listPackages()
if err != nil {
fmt.Println("list package error", err)
return
return false
}

// list all packages
if len(args) == 0 {
for _, pkg := range pkgs {
fmt.Println(pkg)
}
return
return false
}

// list test case of a single package
Expand All @@ -93,40 +94,41 @@ func cmdList(args ...string) {
pkgs = filter(pkgs, func(s string) bool { return s == pkg })
if len(pkgs) != 1 {
fmt.Println("package not exist", pkg)
return
return false
}

err := buildTestBinary(pkg)
if err != nil {
fmt.Println("build package error", pkg, err)
return
return false
}
exist, err := testBinaryExist(pkg)
if err != nil {
fmt.Println("check test binary existance error", err)
return
return false
}
if !exist {
fmt.Println("no test case in ", pkg)
return
return false
}

res, err := listTestCases(pkg, nil)
if err != nil {
fmt.Println("list test cases for package error", err)
return
return false
}
for _, x := range res {
fmt.Println(x.test)
}
}
return true
}

func cmdBuild(args ...string) {
func cmdBuild(args ...string) bool {
pkgs, err := listPackages()
if err != nil {
fmt.Println("list package error", err)
return
return false
}

// build all packages
Expand All @@ -135,10 +137,10 @@ func cmdBuild(args ...string) {
err := buildTestBinary(pkg)
if err != nil {
fmt.Println("build package error", pkg, err)
return
return false
}
}
return
return true
}

// build test binary of a single package
Expand All @@ -147,17 +149,18 @@ func cmdBuild(args ...string) {
err := buildTestBinary(pkg)
if err != nil {
fmt.Println("build package error", pkg, err)
return
return false
}
}
return true
}

func cmdRun(args ...string) {
func cmdRun(args ...string) bool {
var err error
pkgs, err := listPackages()
if err != nil {
fmt.Println("list packages error", err)
return
return false
}
tasks := make([]task, 0, 5000)
// run all tests
Expand All @@ -167,13 +170,13 @@ func cmdRun(args ...string) {
err := buildTestBinary(pkg)
if err != nil {
fmt.Println("build package error", pkg, err)
return
return false
}

exist, err := testBinaryExist(pkg)
if err != nil {
fmt.Println("check test binary existance error", err)
return
return false
}
if !exist {
fmt.Println("no test case in ", pkg)
Expand All @@ -183,7 +186,7 @@ func cmdRun(args ...string) {
tasks, err = listTestCases(pkg, tasks)
if err != nil {
fmt.Println("list test cases error", err)
return
return false
}
}
}
Expand All @@ -194,22 +197,22 @@ func cmdRun(args ...string) {
err := buildTestBinary(pkg)
if err != nil {
fmt.Println("build package error", pkg, err)
return
return false
}
exist, err := testBinaryExist(pkg)
if err != nil {
fmt.Println("check test binary existance error", err)
return
return false
}

if !exist {
fmt.Println("no test case in ", pkg)
return
return false
}
tasks, err = listTestCases(pkg, tasks)
if err != nil {
fmt.Println("list test cases error", err)
return
return false
}
}

Expand All @@ -219,22 +222,22 @@ func cmdRun(args ...string) {
err := buildTestBinary(pkg)
if err != nil {
fmt.Println("build package error", pkg, err)
return
return false
}
exist, err := testBinaryExist(pkg)
if err != nil {
fmt.Println("check test binary existance error", err)
return
return false
}
if !exist {
fmt.Println("no test case in ", pkg)
return
return false
}

tasks, err = listTestCases(pkg, tasks)
if err != nil {
fmt.Println("list test cases error", err)
return
return false
}
// filter the test case to run
tmp := tasks[:0]
Expand All @@ -249,11 +252,12 @@ func cmdRun(args ...string) {

numactl := numactlExist()
taskCh := make(chan task, 100)
works := make([]numa, P)
var wg sync.WaitGroup
for i := 0; i < P; i++ {
n := numa{fmt.Sprintf("%d", i), numactl}
works[i] = numa{fmt.Sprintf("%d", i), numactl, false}
wg.Add(1)
go n.worker(&wg, taskCh)
go works[i].worker(&wg, taskCh)
}

shuffle(tasks)
Expand All @@ -262,6 +266,12 @@ func cmdRun(args ...string) {
}
close(taskCh)
wg.Wait()
for _, work := range works {
if work.Fail {
return false
}
}
return true
}

func main() {
Expand All @@ -281,15 +291,19 @@ func main() {
}

if len(os.Args) >= 2 {
var isSucceed bool
switch os.Args[1] {
case "list":
cmdList(os.Args[2:]...)
isSucceed = cmdList(os.Args[2:]...)
case "build":
cmdBuild(os.Args[2:]...)
isSucceed = cmdBuild(os.Args[2:]...)
case "run":
cmdRun(os.Args[2:]...)
isSucceed = cmdRun(os.Args[2:]...)
default:
usage()
isSucceed = usage()
}
if !isSucceed {
os.Exit(1)
}
}
}
Expand Down Expand Up @@ -339,6 +353,7 @@ func listPackages() ([]string, error) {
type numa struct {
cpu string
numactl bool
Fail bool
}

func (n *numa) worker(wg *sync.WaitGroup, ch chan task) {
Expand All @@ -349,6 +364,7 @@ func (n *numa) worker(wg *sync.WaitGroup, ch chan task) {
if res.err != nil {
fmt.Println("[FAIL] ", t.pkg, t.test, t.old, time.Since(start), res.err)
io.Copy(os.Stderr, &res.output)
n.Fail = true
}
}
}
Expand Down Expand Up @@ -426,8 +442,11 @@ func buildTestBinary(pkg string) error {
cmd.Dir = path.Join(workDir, pkg)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
return withTrace(err)
if err := cmd.Run(); err != nil {
return withTrace(err)
}
return nil

}

func testBinaryExist(pkg string) (bool, error) {
Expand Down

0 comments on commit 1d14d99

Please sign in to comment.