Stat path to binary to handle raw exec driver interpolated binary path

This commit is contained in:
Preetha Appan
2018-10-26 17:24:05 -05:00
parent af3a62e750
commit 4231dc4729
2 changed files with 45 additions and 0 deletions

View File

@@ -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 {

View File

@@ -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)
}