diff options
author | Fabricio Voznika <fvoznika@google.com> | 2018-08-16 10:54:21 -0700 |
---|---|---|
committer | Shentubot <shentubot@google.com> | 2018-08-16 10:55:45 -0700 |
commit | da087e66cc0eb1616437e5b729576801671d3696 (patch) | |
tree | c4075198b446502abd4f81c7c1f3b7a0b3677bdf /runsc | |
parent | eacbe6a678ec08751543868ef19f9197c167fe60 (diff) |
Combine functions to search for file under one common function
Bazel adds the build type in front of directories making it hard to
refer to binaries in code.
PiperOrigin-RevId: 209010854
Change-Id: I6c9da1ac3bbe79766868a3b14222dd42d03b4ec5
Diffstat (limited to 'runsc')
-rw-r--r-- | runsc/container/container_test.go | 32 | ||||
-rw-r--r-- | runsc/test/testutil/testutil.go | 66 |
2 files changed, 42 insertions, 56 deletions
diff --git a/runsc/container/container_test.go b/runsc/container/container_test.go index 33c53e189..10b10d100 100644 --- a/runsc/container/container_test.go +++ b/runsc/container/container_test.go @@ -206,27 +206,6 @@ func run(spec *specs.Spec, conf *boot.Config) error { return nil } -// findUDSApp finds the uds_test_app binary to be used in the UnixDomainSocket test. -func findUDSApp() (string, error) { - // TODO: Use bazel FindBinary function. - - // uds_test_app is in a directory like: - // './linux_amd64_pure_stripped/uds_test_app.go'. - // - // Since I don't want to construct 'linux_amd64_pure_stripped' based on the - // build type, do a quick search for: './*/uds_test_app' - // Note: This glob will only succeed when file is one directory deep. - matches, err := filepath.Glob("./*/uds_test_app") - if err != nil { - return "", fmt.Errorf("error globbing: %v", err) - } - if i := len(matches); i != 1 { - return "", fmt.Errorf("error identifying uds_test_app from matches: got %d matches", i) - } - - return matches[0], nil -} - type configOption int const ( @@ -760,16 +739,9 @@ func TestUnixDomainSockets(t *testing.T) { // Get file path for corresponding output file in sandbox. outputFileSandbox := filepath.Join(goferRoot, output) - // Need to get working directory, even though not intuitive. - wd, _ := os.Getwd() - localPath, err := findUDSApp() + app, err := testutil.FindFile("runsc/container/uds_test_app") if err != nil { - t.Fatalf("error finding localPath: %v", err) - } - app := filepath.Join(wd, localPath) - - if _, err = os.Stat(app); err != nil { - t.Fatalf("error finding the uds_test_app: %v", err) + t.Fatal("error finding uds_test_app:", err) } socketPath := filepath.Join(dir, socket) diff --git a/runsc/test/testutil/testutil.go b/runsc/test/testutil/testutil.go index d2b39b58c..2553e7453 100644 --- a/runsc/test/testutil/testutil.go +++ b/runsc/test/testutil/testutil.go @@ -37,39 +37,53 @@ var RaceEnabled = false // ConfigureExePath configures the executable for runsc in the test environment. func ConfigureExePath() error { - - // runsc is in a directory like: 'runsc/linux_amd64_pure_stripped/runsc'. - // Since I don't want to construct 'linux_amd64_pure_stripped' based on the - // build type, do a quick search for: 'runsc/*/runsc' - exePath := "" - lv1 := "./runsc" - lv1fis, err := ioutil.ReadDir(lv1) + path, err := FindFile("runsc/runsc") if err != nil { return err } - for _, fi := range lv1fis { - if !fi.IsDir() { - continue - } - lv2fis, err := ioutil.ReadDir(filepath.Join(lv1, fi.Name())) - if err != nil { - return err + specutils.ExePath = path + return nil +} + +// FindFile searchs for a file inside the test run environment. It returns the +// full path to the file. It fails if none or more than one file is found. +func FindFile(path string) (string, error) { + wd, err := os.Getwd() + if err != nil { + return "", err + } + + // The test root is demarcated by a path element called "__main__". Search for + // it backwards from the in the working directory. + root := wd + for { + dir, name := filepath.Split(root) + if name == "__main__" { + break } - for _, candidate := range lv2fis { - if !candidate.IsDir() && candidate.Name() == "runsc" { - exePath, err = filepath.Abs(filepath.Join(lv1, fi.Name(), candidate.Name())) - if err != nil { - return err - } - break - } + if len(dir) == 0 { + return "", fmt.Errorf("directory __main__ not found in %q", wd) } + // Remove ending slash to loop around. + root = dir[:len(dir)-1] + } + + // bazel adds the build type to the directory structure. Since I don't want + // to guess what build type it's, just place '*' to match anything. + // + // The pattern goes like: /test-path/__main__/directories/*/file. + pattern := filepath.Join(root, filepath.Dir(path), "*", filepath.Base(path)) + matches, err := filepath.Glob(pattern) + if err != nil { + return "", fmt.Errorf("error globbing %q: %v", pattern, err) } - if exePath == "" { - return fmt.Errorf("path to runsc not found") + if len(matches) == 0 { + return "", fmt.Errorf("file %q not found", path) } - specutils.ExePath = exePath - return nil + if len(matches) != 1 { + return "", fmt.Errorf("more than one match found for %q: %s", path, matches) + } + return matches[0], nil } // TestConfig return the default configuration to use in tests. |