diff options
author | Brett Landau <brettlandau@google.com> | 2019-07-31 11:34:34 -0700 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2019-07-31 11:35:47 -0700 |
commit | edcc60b931232d5bea4254af31965da126f07a68 (patch) | |
tree | eda49ea8c8435a74fb0de2e1d3ad0f69f7a230a1 /test/runtimes/go/proctor-go.go | |
parent | 12c4eb294a43f4a84a789871730869d164ef52c9 (diff) |
Refactor proctor binaries to implement testRunner interface.
Shared code among proctor-*.go files has been refactored
into common/common.go. The common package is imported in
each proctor binary and a struct is created to implement
the testRunner interface defined in common.go. This allows
for the proctor binaries to be updated without having to
copy/paste the same code across all files. There are no
usage or functionality changes.
PiperOrigin-RevId: 260967080
Diffstat (limited to 'test/runtimes/go/proctor-go.go')
-rw-r--r-- | test/runtimes/go/proctor-go.go | 76 |
1 files changed, 17 insertions, 59 deletions
diff --git a/test/runtimes/go/proctor-go.go b/test/runtimes/go/proctor-go.go index c5387e21d..eae8b5e55 100644 --- a/test/runtimes/go/proctor-go.go +++ b/test/runtimes/go/proctor-go.go @@ -21,7 +21,6 @@ package main import ( - "flag" "fmt" "log" "os" @@ -29,14 +28,13 @@ import ( "path/filepath" "regexp" "strings" + + "gvisor.dev/gvisor/test/runtimes/common" ) var ( - list = flag.Bool("list", false, "list all available tests") - test = flag.String("test", "", "run a single test from the list of available tests") - version = flag.Bool("v", false, "print out the version of node that is installed") - dir = os.Getenv("LANG_DIR") + goBin = filepath.Join(dir, "bin/go") testDir = filepath.Join(dir, "test") testRegEx = regexp.MustCompile(`^.+\.go$`) @@ -45,35 +43,16 @@ var ( exclDirs = regexp.MustCompile(`^.+\/(bench|stress)\/.+$|^.+\.dir.+$`) ) -func main() { - flag.Parse() +type goRunner struct { +} - if *list && *test != "" { - flag.PrintDefaults() - os.Exit(1) - } - if *list { - tests, err := listTests() - if err != nil { - log.Fatalf("Failed to list tests: %v", err) - } - for _, test := range tests { - fmt.Println(test) - } - return - } - if *version { - fmt.Println("Go version: ", os.Getenv("LANG_VER"), " is installed.") - return - } - if *test != "" { - runTest(*test) - return +func main() { + if err := common.LaunchFunc(goRunner{}); err != nil { + log.Fatalf("Failed to start: %v", err) } - runAllTests() } -func listTests() ([]string, error) { +func (g goRunner) ListTests() ([]string, error) { // Go tool dist test tests. args := []string{"tool", "dist", "test", "-list"} cmd := exec.Command(filepath.Join(dir, "bin/go"), args...) @@ -112,50 +91,29 @@ func listTests() ([]string, error) { return testSlice, nil } -func runTest(test string) { - toolArgs := []string{ - "tool", - "dist", - "test", - } - diskArgs := []string{ - "run", - "run.go", - "-v", - } +func (g goRunner) RunTest(test string) error { // Check if test exists on disk by searching for file of the same name. // This will determine whether or not it is a Go test on disk. if _, err := os.Stat(test); err == nil { relPath, err := filepath.Rel(testDir, test) if err != nil { - log.Fatalf("Failed to get rel path: %v", err) + return fmt.Errorf("failed to get rel path: %v", err) } - diskArgs = append(diskArgs, "--", relPath) - cmd := exec.Command(filepath.Join(dir, "bin/go"), diskArgs...) + cmd := exec.Command(goBin, "run", "run.go", "-v", "--", relPath) cmd.Dir = testDir cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr if err := cmd.Run(); err != nil { - log.Fatalf("Failed to run: %v", err) + return fmt.Errorf("failed to run test: %v", err) } } else if os.IsNotExist(err) { // File was not found, try running as Go tool test. - toolArgs = append(toolArgs, "-run", test) - cmd := exec.Command(filepath.Join(dir, "bin/go"), toolArgs...) + cmd := exec.Command(goBin, "tool", "dist", "test", "-run", test) cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr if err := cmd.Run(); err != nil { - log.Fatalf("Failed to run: %v", err) + return fmt.Errorf("failed to run test: %v", err) } } else { - log.Fatalf("Error searching for test: %v", err) - } -} - -func runAllTests() { - tests, err := listTests() - if err != nil { - log.Fatalf("Failed to list tests: %v", err) - } - for _, test := range tests { - runTest(test) + return fmt.Errorf("error searching for test: %v", err) } + return nil } |