github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/client/allocdir/alloc_dir_test.go (about) 1 package allocdir 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "reflect" 8 "testing" 9 10 "github.com/hashicorp/nomad/client/testutil" 11 "github.com/hashicorp/nomad/nomad/structs" 12 ) 13 14 var ( 15 t1 = &structs.Task{ 16 Name: "web", 17 Driver: "exec", 18 Config: map[string]string{ 19 "command": "/bin/date", 20 "args": "+%s", 21 }, 22 Resources: &structs.Resources{ 23 DiskMB: 1, 24 }, 25 } 26 27 t2 = &structs.Task{ 28 Name: "web2", 29 Driver: "exec", 30 Config: map[string]string{ 31 "command": "/bin/date", 32 "args": "+%s", 33 }, 34 Resources: &structs.Resources{ 35 DiskMB: 1, 36 }, 37 } 38 ) 39 40 // Test that given a set of tasks, each task gets a directory and that directory 41 // has the shared alloc dir inside of it. 42 func TestAllocDir_BuildAlloc(t *testing.T) { 43 tmp, err := ioutil.TempDir("", "AllocDir") 44 if err != nil { 45 t.Fatalf("Couldn't create temp dir: %v", err) 46 } 47 defer os.RemoveAll(tmp) 48 49 d := NewAllocDir(tmp) 50 tasks := []*structs.Task{t1, t2} 51 if err := d.Build(tasks); err != nil { 52 t.Fatalf("Build(%v) failed: %v", tasks, err) 53 } 54 55 // Check that the AllocDir and each of the task directories exist. 56 if _, err := os.Stat(d.AllocDir); os.IsNotExist(err) { 57 t.Fatalf("Build(%v) didn't create AllocDir %v", tasks, d.AllocDir) 58 } 59 60 for _, task := range tasks { 61 tDir, ok := d.TaskDirs[task.Name] 62 if !ok { 63 t.Fatalf("Task directory not found for %v", task.Name) 64 } 65 66 if _, err := os.Stat(tDir); os.IsNotExist(err) { 67 t.Fatalf("Build(%v) didn't create TaskDir %v", tasks, tDir) 68 } 69 } 70 } 71 72 func TestAllocDir_EmbedNonExistent(t *testing.T) { 73 tmp, err := ioutil.TempDir("", "AllocDir") 74 if err != nil { 75 t.Fatalf("Couldn't create temp dir: %v", err) 76 } 77 defer os.RemoveAll(tmp) 78 79 d := NewAllocDir(tmp) 80 tasks := []*structs.Task{t1, t2} 81 if err := d.Build(tasks); err != nil { 82 t.Fatalf("Build(%v) failed: %v", tasks, err) 83 } 84 85 fakeDir := "/foobarbaz" 86 task := tasks[0].Name 87 mapping := map[string]string{fakeDir: fakeDir} 88 if err := d.Embed(task, mapping); err != nil { 89 t.Fatalf("Embed(%v, %v) should should skip %v since it does not exist", task, mapping, fakeDir) 90 } 91 } 92 93 func TestAllocDir_EmbedDirs(t *testing.T) { 94 tmp, err := ioutil.TempDir("", "AllocDir") 95 if err != nil { 96 t.Fatalf("Couldn't create temp dir: %v", err) 97 } 98 defer os.RemoveAll(tmp) 99 100 d := NewAllocDir(tmp) 101 tasks := []*structs.Task{t1, t2} 102 if err := d.Build(tasks); err != nil { 103 t.Fatalf("Build(%v) failed: %v", tasks, err) 104 } 105 106 // Create a fake host directory, with a file, and a subfolder that contains 107 // a file. 108 host, err := ioutil.TempDir("", "AllocDirHost") 109 if err != nil { 110 t.Fatalf("Couldn't create temp dir: %v", err) 111 } 112 defer os.RemoveAll(host) 113 114 subDirName := "subdir" 115 subDir := filepath.Join(host, subDirName) 116 if err := os.Mkdir(subDir, 0777); err != nil { 117 t.Fatalf("Failed to make subdir %v: %v", subDir, err) 118 } 119 120 file := "foo" 121 subFile := "bar" 122 if err := ioutil.WriteFile(filepath.Join(host, file), []byte{'a'}, 0777); err != nil { 123 t.Fatalf("Coudn't create file in host dir %v: %v", host, err) 124 } 125 126 if err := ioutil.WriteFile(filepath.Join(subDir, subFile), []byte{'a'}, 0777); err != nil { 127 t.Fatalf("Coudn't create file in host subdir %v: %v", subDir, err) 128 } 129 130 // Create mapping from host dir to task dir. 131 task := tasks[0].Name 132 taskDest := "bin/test/" 133 mapping := map[string]string{host: taskDest} 134 if err := d.Embed(task, mapping); err != nil { 135 t.Fatalf("Embed(%v, %v) failed: %v", task, mapping, err) 136 } 137 138 // Check that the embedding was done properly. 139 taskDir, ok := d.TaskDirs[task] 140 if !ok { 141 t.Fatalf("Task directory not found for %v", task) 142 } 143 144 exp := []string{filepath.Join(taskDir, taskDest, file), filepath.Join(taskDir, taskDest, subDirName, subFile)} 145 for _, e := range exp { 146 if _, err := os.Stat(e); os.IsNotExist(err) { 147 t.Fatalf("File %v not embeded: %v", e, err) 148 } 149 } 150 } 151 152 func TestAllocDir_MountSharedAlloc(t *testing.T) { 153 testutil.MountCompatible(t) 154 tmp, err := ioutil.TempDir("", "AllocDir") 155 if err != nil { 156 t.Fatalf("Couldn't create temp dir: %v", err) 157 } 158 defer os.RemoveAll(tmp) 159 160 d := NewAllocDir(tmp) 161 tasks := []*structs.Task{t1, t2} 162 if err := d.Build(tasks); err != nil { 163 t.Fatalf("Build(%v) failed: %v", tasks, err) 164 } 165 166 // Write a file to the shared dir. 167 exp := []byte{'f', 'o', 'o'} 168 file := "bar" 169 if err := ioutil.WriteFile(filepath.Join(d.SharedDir, file), exp, 0777); err != nil { 170 t.Fatalf("Couldn't write file to shared directory: %v", err) 171 } 172 173 for _, task := range tasks { 174 // Mount and then check that the file exists in the task directory. 175 if err := d.MountSharedDir(task.Name); err != nil { 176 t.Fatalf("MountSharedDir(%v) failed: %v", task.Name, err) 177 } 178 179 taskDir, ok := d.TaskDirs[task.Name] 180 if !ok { 181 t.Fatalf("Task directory not found for %v", task.Name) 182 } 183 184 taskFile := filepath.Join(taskDir, SharedAllocName, file) 185 act, err := ioutil.ReadFile(taskFile) 186 if err != nil { 187 t.Fatalf("Failed to read shared alloc file from task dir: %v", err) 188 } 189 190 if !reflect.DeepEqual(act, exp) { 191 t.Fatalf("Incorrect data read from task dir: want %v; got %v", exp, act) 192 } 193 } 194 }