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/test/testutil | |
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/test/testutil')
-rw-r--r-- | runsc/test/testutil/testutil.go | 66 |
1 files changed, 40 insertions, 26 deletions
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. |