From 4231dc4729b4d2a758c2ec66af5456692e709c7d Mon Sep 17 00:00:00 2001 From: Preetha Appan Date: Fri, 26 Oct 2018 17:24:05 -0500 Subject: [PATCH] Stat path to binary to handle raw exec driver interpolated binary path --- client/driver/executor/executor.go | 6 ++++ client/driver/executor/executor_test.go | 39 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/client/driver/executor/executor.go b/client/driver/executor/executor.go index 240c6aacf4b9..5075b19d1ad7 100644 --- a/client/driver/executor/executor.go +++ b/client/driver/executor/executor.go @@ -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 { diff --git a/client/driver/executor/executor_test.go b/client/driver/executor/executor_test.go index 490122de18e7..bc2ef623c78f 100644 --- a/client/driver/executor/executor_test.go +++ b/client/driver/executor/executor_test.go @@ -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) + +}