Merge pull request #2344 from hashicorp/b-fix-allocdir-move-test

Fix allocdir Move test and make code more defensive
This commit is contained in:
Alex Dadgar
2017-02-22 18:43:10 -08:00
committed by GitHub
2 changed files with 5 additions and 4 deletions

View File

@@ -161,6 +161,7 @@ func (d *AllocDir) Move(other *AllocDir, tasks []*structs.Task) error {
otherDataDir := filepath.Join(other.SharedDir, SharedDataDir)
dataDir := filepath.Join(d.SharedDir, SharedDataDir)
if fileInfo, err := os.Stat(otherDataDir); fileInfo != nil && err == nil {
os.Remove(dataDir) // remove an empty data dir if it exists
if err := os.Rename(otherDataDir, dataDir); err != nil {
return fmt.Errorf("error moving data dir: %v", err)
}
@@ -179,6 +180,7 @@ func (d *AllocDir) Move(other *AllocDir, tasks []*structs.Task) error {
return fmt.Errorf("error creating task %q dir: %v", task.Name, err)
}
localDir := filepath.Join(newTaskDir, TaskLocal)
os.Remove(localDir) // remove an empty local dir if it exists
if err := os.Rename(otherTaskLocal, localDir); err != nil {
return fmt.Errorf("error moving task %q local dir: %v", task.Name, err)
}

View File

@@ -229,10 +229,9 @@ func TestAllocDir_Move(t *testing.T) {
t.Fatalf("TaskDir.Build() faild: %v", err)
}
td2 := d2.NewTaskDir(t1.Name)
if err := td2.Build(nil, cstructs.FSIsolationImage); err != nil {
t.Fatalf("TaskDir.Build() faild: %v", err)
}
// Create but don't build second task dir to mimic alloc/task runner
// behavior (AllocDir.Move() is called pre-TaskDir.Build).
d2.NewTaskDir(t1.Name)
dataDir := filepath.Join(d1.SharedDir, SharedDataDir)