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 | |
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
-rw-r--r-- | test/runtimes/common/BUILD | 10 | ||||
-rw-r--r-- | test/runtimes/common/common.go | 85 | ||||
-rw-r--r-- | test/runtimes/go/BUILD | 1 | ||||
-rw-r--r-- | test/runtimes/go/proctor-go.go | 76 | ||||
-rw-r--r-- | test/runtimes/java/BUILD | 1 | ||||
-rw-r--r-- | test/runtimes/java/proctor-java.go | 53 | ||||
-rw-r--r-- | test/runtimes/nodejs/BUILD | 1 | ||||
-rw-r--r-- | test/runtimes/nodejs/proctor-nodejs.go | 53 | ||||
-rw-r--r-- | test/runtimes/php/BUILD | 1 | ||||
-rw-r--r-- | test/runtimes/php/proctor-php.go | 53 | ||||
-rw-r--r-- | test/runtimes/python/BUILD | 1 | ||||
-rw-r--r-- | test/runtimes/python/proctor-python.go | 53 |
12 files changed, 161 insertions, 227 deletions
diff --git a/test/runtimes/common/BUILD b/test/runtimes/common/BUILD new file mode 100644 index 000000000..7147e841a --- /dev/null +++ b/test/runtimes/common/BUILD @@ -0,0 +1,10 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +package(licenses = ["notice"]) + +go_library( + name = "common", + srcs = ["common.go"], + importpath = "gvisor.dev/gvisor/test/runtimes/common", + visibility = ["//:sandbox"], +) diff --git a/test/runtimes/common/common.go b/test/runtimes/common/common.go new file mode 100644 index 000000000..3f289d459 --- /dev/null +++ b/test/runtimes/common/common.go @@ -0,0 +1,85 @@ +// Copyright 2019 The gVisor Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package common executes functions for proctor binaries. +package common + +import ( + "flag" + "fmt" + "os" +) + +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") +) + +// TestRunner is an interface to be implemented in each proctor binary. +type TestRunner interface { + // ListTests returns a string slice of tests available to run. + ListTests() ([]string, error) + + // RunTest runs a single test. + RunTest(test string) error +} + +// LaunchFunc parses flags passed by a proctor binary and calls the requested behavior. +func LaunchFunc(tr TestRunner) error { + flag.Parse() + + if *list && *test != "" { + flag.PrintDefaults() + return fmt.Errorf("cannot specify 'list' and 'test' flags simultaneously") + } + if *list { + tests, err := tr.ListTests() + if err != nil { + return fmt.Errorf("failed to list tests: %v", err) + } + for _, test := range tests { + fmt.Println(test) + } + return nil + } + if *version { + fmt.Println(os.Getenv("LANG_NAME"), "version:", os.Getenv("LANG_VER"), "is installed.") + return nil + } + if *test != "" { + if err := tr.RunTest(*test); err != nil { + return fmt.Errorf("test %q failed to run: %v", *test, err) + } + return nil + } + + if err := runAllTests(tr); err != nil { + return fmt.Errorf("error running all tests: %v", err) + } + return nil +} + +func runAllTests(tr TestRunner) error { + tests, err := tr.ListTests() + if err != nil { + return fmt.Errorf("failed to list tests: %v", err) + } + for _, test := range tests { + if err := tr.RunTest(test); err != nil { + return fmt.Errorf("test %q failed to run: %v", test, err) + } + } + return nil +} diff --git a/test/runtimes/go/BUILD b/test/runtimes/go/BUILD index c34f49ea6..ce971ee9d 100644 --- a/test/runtimes/go/BUILD +++ b/test/runtimes/go/BUILD @@ -5,4 +5,5 @@ package(licenses = ["notice"]) go_binary( name = "proctor-go", srcs = ["proctor-go.go"], + deps = ["//test/runtimes/common"], ) 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 } diff --git a/test/runtimes/java/BUILD b/test/runtimes/java/BUILD index 7e2808ece..8c39d39ec 100644 --- a/test/runtimes/java/BUILD +++ b/test/runtimes/java/BUILD @@ -5,4 +5,5 @@ package(licenses = ["notice"]) go_binary( name = "proctor-java", srcs = ["proctor-java.go"], + deps = ["//test/runtimes/common"], ) diff --git a/test/runtimes/java/proctor-java.go b/test/runtimes/java/proctor-java.go index 0177f421d..c1e611b4b 100644 --- a/test/runtimes/java/proctor-java.go +++ b/test/runtimes/java/proctor-java.go @@ -16,7 +16,6 @@ package main import ( - "flag" "fmt" "log" "os" @@ -24,47 +23,26 @@ 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") jtreg = filepath.Join(dir, "jtreg/bin/jtreg") exclDirs = regexp.MustCompile(`(^(sun\/security)|(java\/util\/stream)|(java\/time)| )`) ) -func main() { - flag.Parse() +type javaRunner 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("Java version: ", os.Getenv("LANG_VER"), " is installed.") - return - } - if *test != "" { - runTest(*test) - return +func main() { + if err := common.LaunchFunc(javaRunner{}); err != nil { + log.Fatalf("Failed to start: %v", err) } - runAllTests() } -func listTests() ([]string, error) { +func (j javaRunner) ListTests() ([]string, error) { args := []string{ "-dir:test/jdk", "-ignore:quiet", @@ -90,21 +68,12 @@ func listTests() ([]string, error) { return testSlice, nil } -func runTest(test string) { +func (j javaRunner) RunTest(test string) error { args := []string{"-dir:test/jdk/", test} cmd := exec.Command(jtreg, args...) cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr if err := cmd.Run(); err != nil { - log.Fatalf("Failed to run: %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("failed to run: %v", err) } + return nil } diff --git a/test/runtimes/nodejs/BUILD b/test/runtimes/nodejs/BUILD index 0fe5ff83e..0594c250b 100644 --- a/test/runtimes/nodejs/BUILD +++ b/test/runtimes/nodejs/BUILD @@ -5,4 +5,5 @@ package(licenses = ["notice"]) go_binary( name = "proctor-nodejs", srcs = ["proctor-nodejs.go"], + deps = ["//test/runtimes/common"], ) diff --git a/test/runtimes/nodejs/proctor-nodejs.go b/test/runtimes/nodejs/proctor-nodejs.go index 8ddfb67fe..468025682 100644 --- a/test/runtimes/nodejs/proctor-nodejs.go +++ b/test/runtimes/nodejs/proctor-nodejs.go @@ -16,53 +16,31 @@ package main import ( - "flag" "fmt" "log" "os" "os/exec" "path/filepath" "regexp" + + "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") testRegEx = regexp.MustCompile(`^test-.+\.js$`) ) -func main() { - flag.Parse() +type nodejsRunner 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("Node.js version: ", os.Getenv("LANG_VER"), " is installed.") - return - } - if *test != "" { - runTest(*test) - return +func main() { + if err := common.LaunchFunc(nodejsRunner{}); err != nil { + log.Fatalf("Failed to start: %v", err) } - runAllTests() } -func listTests() ([]string, error) { +func (n nodejsRunner) ListTests() ([]string, error) { var testSlice []string root := filepath.Join(dir, "test") @@ -88,21 +66,12 @@ func listTests() ([]string, error) { return testSlice, nil } -func runTest(test string) { +func (n nodejsRunner) RunTest(test string) error { args := []string{filepath.Join(dir, "tools", "test.py"), test} cmd := exec.Command("/usr/bin/python", args...) cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr if err := cmd.Run(); err != nil { - log.Fatalf("Failed to run: %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("failed to run: %v", err) } + return nil } diff --git a/test/runtimes/php/BUILD b/test/runtimes/php/BUILD index 22aef7ba4..31799b77a 100644 --- a/test/runtimes/php/BUILD +++ b/test/runtimes/php/BUILD @@ -5,4 +5,5 @@ package(licenses = ["notice"]) go_binary( name = "proctor-php", srcs = ["proctor-php.go"], + deps = ["//test/runtimes/common"], ) diff --git a/test/runtimes/php/proctor-php.go b/test/runtimes/php/proctor-php.go index 9dfb33b04..3ee4cf056 100644 --- a/test/runtimes/php/proctor-php.go +++ b/test/runtimes/php/proctor-php.go @@ -16,53 +16,31 @@ package main import ( - "flag" "fmt" "log" "os" "os/exec" "path/filepath" "regexp" + + "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") testRegEx = regexp.MustCompile(`^.+\.phpt$`) ) -func main() { - flag.Parse() +type phpRunner 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("PHP version: ", os.Getenv("LANG_VER"), " is installed.") - return - } - if *test != "" { - runTest(*test) - return +func main() { + if err := common.LaunchFunc(phpRunner{}); err != nil { + log.Fatalf("Failed to start: %v", err) } - runAllTests() } -func listTests() ([]string, error) { +func (p phpRunner) ListTests() ([]string, error) { var testSlice []string err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { @@ -87,21 +65,12 @@ func listTests() ([]string, error) { return testSlice, nil } -func runTest(test string) { +func (p phpRunner) RunTest(test string) error { args := []string{"test", "TESTS=" + test} cmd := exec.Command("make", args...) cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr if err := cmd.Run(); err != nil { - log.Fatalf("Failed to run: %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("failed to run: %v", err) } + return nil } diff --git a/test/runtimes/python/BUILD b/test/runtimes/python/BUILD index 501f77d63..37fd6a0f2 100644 --- a/test/runtimes/python/BUILD +++ b/test/runtimes/python/BUILD @@ -5,4 +5,5 @@ package(licenses = ["notice"]) go_binary( name = "proctor-python", srcs = ["proctor-python.go"], + deps = ["//test/runtimes/common"], ) diff --git a/test/runtimes/python/proctor-python.go b/test/runtimes/python/proctor-python.go index 73c8deb49..3f83f3d70 100644 --- a/test/runtimes/python/proctor-python.go +++ b/test/runtimes/python/proctor-python.go @@ -16,53 +16,31 @@ package main import ( - "flag" "fmt" "log" "os" "os/exec" "path/filepath" "regexp" + + "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") testRegEx = regexp.MustCompile(`^test_.+\.py$`) ) -func main() { - flag.Parse() +type pythonRunner 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("Python version: ", os.Getenv("LANG_VER"), " is installed.") - return - } - if *test != "" { - runTest(*test) - return +func main() { + if err := common.LaunchFunc(pythonRunner{}); err != nil { + log.Fatalf("Failed to start: %v", err) } - runAllTests() } -func listTests() ([]string, error) { +func (p pythonRunner) ListTests() ([]string, error) { var testSlice []string root := filepath.Join(dir, "Lib/test") @@ -88,21 +66,12 @@ func listTests() ([]string, error) { return testSlice, nil } -func runTest(test string) { +func (p pythonRunner) RunTest(test string) error { args := []string{"-m", "test", test} cmd := exec.Command(filepath.Join(dir, "python"), args...) cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr if err := cmd.Run(); err != nil { - log.Fatalf("Failed to run: %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("failed to run: %v", err) } + return nil } |