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