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