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
This commit is contained in:
Fabricio Voznika
2018-08-16 10:55:45 -07:00
committed by Shentubot
parent eacbe6a678
commit da087e66cc
2 changed files with 45 additions and 59 deletions
+2 -30
View File
@@ -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)
+43 -29
View File
@@ -37,41 +37,55 @@ 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
}
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 exePath == "" {
return fmt.Errorf("path to runsc not found")
}
specutils.ExePath = exePath
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
}
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 len(matches) == 0 {
return "", fmt.Errorf("file %q not found", path)
}
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.
func TestConfig() *boot.Config {
return &boot.Config{