github.com/ryanslade/nomad@v0.2.4-0.20160128061903-fc95782f2089/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]interface{}{ 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]interface{}{ 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 defer d.Destroy() 51 tasks := []*structs.Task{t1, t2} 52 if err := d.Build(tasks); err != nil { 53 t.Fatalf("Build(%v) failed: %v", tasks, err) 54 } 55 56 // Check that the AllocDir and each of the task directories exist. 57 if _, err := os.Stat(d.AllocDir); os.IsNotExist(err) { 58 t.Fatalf("Build(%v) didn't create AllocDir %v", tasks, d.AllocDir) 59 } 60 61 for _, task := range tasks { 62 tDir, ok := d.TaskDirs[task.Name] 63 if !ok { 64 t.Fatalf("Task directory not found for %v", task.Name) 65 } 66 67 if _, err := os.Stat(tDir); os.IsNotExist(err) { 68 t.Fatalf("Build(%v) didn't create TaskDir %v", tasks, tDir) 69 } 70 } 71 } 72 73 func TestAllocDir_EmbedNonExistent(t *testing.T) { 74 tmp, err := ioutil.TempDir("", "AllocDir") 75 if err != nil { 76 t.Fatalf("Couldn't create temp dir: %v", err) 77 } 78 defer os.RemoveAll(tmp) 79 80 d := NewAllocDir(tmp) 81 defer d.Destroy() 82 tasks := []*structs.Task{t1, t2} 83 if err := d.Build(tasks); err != nil { 84 t.Fatalf("Build(%v) failed: %v", tasks, err) 85 } 86 87 fakeDir := "/foobarbaz" 88 task := tasks[0].Name 89 mapping := map[string]string{fakeDir: fakeDir} 90 if err := d.Embed(task, mapping); err != nil { 91 t.Fatalf("Embed(%v, %v) should should skip %v since it does not exist", task, mapping, fakeDir) 92 } 93 } 94 95 func TestAllocDir_EmbedDirs(t *testing.T) { 96 tmp, err := ioutil.TempDir("", "AllocDir") 97 if err != nil { 98 t.Fatalf("Couldn't create temp dir: %v", err) 99 } 100 defer os.RemoveAll(tmp) 101 102 d := NewAllocDir(tmp) 103 defer d.Destroy() 104 tasks := []*structs.Task{t1, t2} 105 if err := d.Build(tasks); err != nil { 106 t.Fatalf("Build(%v) failed: %v", tasks, err) 107 } 108 109 // Create a fake host directory, with a file, and a subfolder that contains 110 // a file. 111 host, err := ioutil.TempDir("", "AllocDirHost") 112 if err != nil { 113 t.Fatalf("Couldn't create temp dir: %v", err) 114 } 115 defer os.RemoveAll(host) 116 117 subDirName := "subdir" 118 subDir := filepath.Join(host, subDirName) 119 if err := os.Mkdir(subDir, 0777); err != nil { 120 t.Fatalf("Failed to make subdir %v: %v", subDir, err) 121 } 122 123 file := "foo" 124 subFile := "bar" 125 if err := ioutil.WriteFile(filepath.Join(host, file), []byte{'a'}, 0777); err != nil { 126 t.Fatalf("Coudn't create file in host dir %v: %v", host, err) 127 } 128 129 if err := ioutil.WriteFile(filepath.Join(subDir, subFile), []byte{'a'}, 0777); err != nil { 130 t.Fatalf("Coudn't create file in host subdir %v: %v", subDir, err) 131 } 132 133 // Create mapping from host dir to task dir. 134 task := tasks[0].Name 135 taskDest := "bin/test/" 136 mapping := map[string]string{host: taskDest} 137 if err := d.Embed(task, mapping); err != nil { 138 t.Fatalf("Embed(%v, %v) failed: %v", task, mapping, err) 139 } 140 141 // Check that the embedding was done properly. 142 taskDir, ok := d.TaskDirs[task] 143 if !ok { 144 t.Fatalf("Task directory not found for %v", task) 145 } 146 147 exp := []string{filepath.Join(taskDir, taskDest, file), filepath.Join(taskDir, taskDest, subDirName, subFile)} 148 for _, e := range exp { 149 if _, err := os.Stat(e); os.IsNotExist(err) { 150 t.Fatalf("File %v not embeded: %v", e, err) 151 } 152 } 153 } 154 155 func TestAllocDir_MountSharedAlloc(t *testing.T) { 156 testutil.MountCompatible(t) 157 tmp, err := ioutil.TempDir("", "AllocDir") 158 if err != nil { 159 t.Fatalf("Couldn't create temp dir: %v", err) 160 } 161 defer os.RemoveAll(tmp) 162 163 d := NewAllocDir(tmp) 164 defer d.Destroy() 165 tasks := []*structs.Task{t1, t2} 166 if err := d.Build(tasks); err != nil { 167 t.Fatalf("Build(%v) failed: %v", tasks, err) 168 } 169 170 // Write a file to the shared dir. 171 exp := []byte{'f', 'o', 'o'} 172 file := "bar" 173 if err := ioutil.WriteFile(filepath.Join(d.SharedDir, file), exp, 0777); err != nil { 174 t.Fatalf("Couldn't write file to shared directory: %v", err) 175 } 176 177 for _, task := range tasks { 178 // Mount and then check that the file exists in the task directory. 179 if err := d.MountSharedDir(task.Name); err != nil { 180 t.Fatalf("MountSharedDir(%v) failed: %v", task.Name, err) 181 } 182 183 taskDir, ok := d.TaskDirs[task.Name] 184 if !ok { 185 t.Fatalf("Task directory not found for %v", task.Name) 186 } 187 188 taskFile := filepath.Join(taskDir, SharedAllocName, file) 189 act, err := ioutil.ReadFile(taskFile) 190 if err != nil { 191 t.Fatalf("Failed to read shared alloc file from task dir: %v", err) 192 } 193 194 if !reflect.DeepEqual(act, exp) { 195 t.Fatalf("Incorrect data read from task dir: want %v; got %v", exp, act) 196 } 197 } 198 }