Skip to content

Commit

Permalink
Merge pull request #4813 from hashicorp/b-executor_path_fix
Browse files Browse the repository at this point in the history
Stat path to binary to handle raw exec driver interpolated binary path
  • Loading branch information
preetapan committed Oct 27, 2018
2 parents af3a62e + 4231dc4 commit 7404666
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions client/driver/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,12 @@ func (e *UniversalExecutor) Stats() (*cstructs.TaskResourceUsage, error) {
// the following locations, in-order: task/local/, task/, based on host $PATH.
// The return path is absolute.
func lookupBin(taskDir string, bin string) (string, error) {
// Check the binary path first
// This handles the case where the job spec sends a fully interpolated path to the binary
if _, err := os.Stat(bin); err == nil {
return bin, nil
}

// Check in the local directory
local := filepath.Join(taskDir, allocdir.TaskLocal, bin)
if _, err := os.Stat(local); err == nil {
Expand Down
39 changes: 39 additions & 0 deletions client/driver/executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,42 @@ func TestUniversalExecutor_MakeExecutable(t *testing.T) {
t.Fatalf("expected permissions %v; got %v", exp, act)
}
}

func TestUniversalExecutor_LookupPath(t *testing.T) {
t.Parallel()
// Create a temp dir
tmpDir, err := ioutil.TempDir("", "")
require := require.New(t)
require.Nil(err)
defer os.Remove(tmpDir)

// Make a foo subdir
os.MkdirAll(filepath.Join(tmpDir, "foo"), 0700)

// Write a file under foo
filePath := filepath.Join(tmpDir, "foo", "tmp.txt")
err = ioutil.WriteFile(filePath, []byte{1, 2}, os.ModeAppend)
require.Nil(err)

// Lookup with full path to binary
_, err = lookupBin("dummy", filePath)
require.Nil(err)

// Write a file under local subdir
os.MkdirAll(filepath.Join(tmpDir, "local"), 0700)
filePath2 := filepath.Join(tmpDir, "local", "tmp.txt")
ioutil.WriteFile(filePath2, []byte{1, 2}, os.ModeAppend)

// Lookup with file name, should find the one we wrote above
_, err = lookupBin(tmpDir, "tmp.txt")
require.Nil(err)

// Write a file under task dir
filePath3 := filepath.Join(tmpDir, "tmp.txt")
ioutil.WriteFile(filePath3, []byte{1, 2}, os.ModeAppend)

// Lookup with file name, should find the one we wrote above
_, err = lookupBin(tmpDir, "tmp.txt")
require.Nil(err)

}

0 comments on commit 7404666

Please sign in to comment.