github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/client/allocdir/task_dir_test.go (about) 1 package allocdir 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 9 cstructs "github.com/hashicorp/nomad/client/structs" 10 ) 11 12 // Test that building a chroot will skip nonexistent directories. 13 func TestTaskDir_EmbedNonExistent(t *testing.T) { 14 tmp, err := ioutil.TempDir("", "AllocDir") 15 if err != nil { 16 t.Fatalf("Couldn't create temp dir: %v", err) 17 } 18 defer os.RemoveAll(tmp) 19 20 d := NewAllocDir(testLogger(), tmp) 21 defer d.Destroy() 22 td := d.NewTaskDir(t1.Name) 23 if err := d.Build(); err != nil { 24 t.Fatalf("Build() failed: %v", err) 25 } 26 27 fakeDir := "/foobarbaz" 28 mapping := map[string]string{fakeDir: fakeDir} 29 if err := td.embedDirs(mapping); err != nil { 30 t.Fatalf("embedDirs(%v) should should skip %v since it does not exist", mapping, fakeDir) 31 } 32 } 33 34 // Test that building a chroot copies files from the host into the task dir. 35 func TestTaskDir_EmbedDirs(t *testing.T) { 36 tmp, err := ioutil.TempDir("", "AllocDir") 37 if err != nil { 38 t.Fatalf("Couldn't create temp dir: %v", err) 39 } 40 defer os.RemoveAll(tmp) 41 42 d := NewAllocDir(testLogger(), tmp) 43 defer d.Destroy() 44 td := d.NewTaskDir(t1.Name) 45 if err := d.Build(); err != nil { 46 t.Fatalf("Build() failed: %v", err) 47 } 48 49 // Create a fake host directory, with a file, and a subfolder that contains 50 // a file. 51 host, err := ioutil.TempDir("", "AllocDirHost") 52 if err != nil { 53 t.Fatalf("Couldn't create temp dir: %v", err) 54 } 55 defer os.RemoveAll(host) 56 57 subDirName := "subdir" 58 subDir := filepath.Join(host, subDirName) 59 if err := os.MkdirAll(subDir, 0777); err != nil { 60 t.Fatalf("Failed to make subdir %v: %v", subDir, err) 61 } 62 63 file := "foo" 64 subFile := "bar" 65 if err := ioutil.WriteFile(filepath.Join(host, file), []byte{'a'}, 0777); err != nil { 66 t.Fatalf("Coudn't create file in host dir %v: %v", host, err) 67 } 68 69 if err := ioutil.WriteFile(filepath.Join(subDir, subFile), []byte{'a'}, 0777); err != nil { 70 t.Fatalf("Coudn't create file in host subdir %v: %v", subDir, err) 71 } 72 73 // Create mapping from host dir to task dir. 74 taskDest := "bin/test/" 75 mapping := map[string]string{host: taskDest} 76 if err := td.embedDirs(mapping); err != nil { 77 t.Fatalf("embedDirs(%v) failed: %v", mapping, err) 78 } 79 80 exp := []string{filepath.Join(td.Dir, taskDest, file), filepath.Join(td.Dir, taskDest, subDirName, subFile)} 81 for _, f := range exp { 82 if _, err := os.Stat(f); os.IsNotExist(err) { 83 t.Fatalf("File %v not embeded: %v", f, err) 84 } 85 } 86 } 87 88 // Test that task dirs for image based isolation don't require root. 89 func TestTaskDir_NonRoot_Image(t *testing.T) { 90 if os.Geteuid() == 0 { 91 t.Skip("test should be run as non-root user") 92 } 93 tmp, err := ioutil.TempDir("", "AllocDir") 94 if err != nil { 95 t.Fatalf("Couldn't create temp dir: %v", err) 96 } 97 defer os.RemoveAll(tmp) 98 99 d := NewAllocDir(testLogger(), tmp) 100 defer d.Destroy() 101 td := d.NewTaskDir(t1.Name) 102 if err := d.Build(); err != nil { 103 t.Fatalf("Build() failed: %v", err) 104 } 105 106 if err := td.Build(false, nil, cstructs.FSIsolationImage); err != nil { 107 t.Fatalf("TaskDir.Build failed: %v", err) 108 } 109 } 110 111 // Test that task dirs with no isolation don't require root. 112 func TestTaskDir_NonRoot(t *testing.T) { 113 if os.Geteuid() == 0 { 114 t.Skip("test should be run as non-root user") 115 } 116 tmp, err := ioutil.TempDir("", "AllocDir") 117 if err != nil { 118 t.Fatalf("Couldn't create temp dir: %v", err) 119 } 120 defer os.RemoveAll(tmp) 121 122 d := NewAllocDir(testLogger(), tmp) 123 defer d.Destroy() 124 td := d.NewTaskDir(t1.Name) 125 if err := d.Build(); err != nil { 126 t.Fatalf("Build() failed: %v", err) 127 } 128 129 if err := td.Build(false, nil, cstructs.FSIsolationNone); err != nil { 130 t.Fatalf("TaskDir.Build failed: %v", err) 131 } 132 133 // ${TASK_DIR}/alloc should not exist! 134 if _, err = os.Stat(td.SharedTaskDir); !os.IsNotExist(err) { 135 t.Fatalf("Expected a NotExist error for shared alloc dir in task dir: %q", td.SharedTaskDir) 136 } 137 138 }