github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/client/allocdir/task_dir_linux_test.go (about) 1 package allocdir 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "golang.org/x/sys/unix" 10 ) 11 12 // TestLinuxSpecialDirs ensures mounting /dev and /proc works. 13 func TestLinuxSpecialDirs(t *testing.T) { 14 if unix.Geteuid() != 0 { 15 t.Skip("Must be run as root") 16 } 17 18 allocDir, err := ioutil.TempDir("", "nomadtest-specialdirs") 19 if err != nil { 20 t.Fatalf("unable to create tempdir for test: %v", err) 21 } 22 defer os.RemoveAll(allocDir) 23 24 td := newTaskDir(testLogger(), allocDir, "test") 25 26 // Despite the task dir not existing, unmountSpecialDirs should *not* 27 // return an error 28 if err := td.unmountSpecialDirs(); err != nil { 29 t.Fatalf("error removing nonexistent special dirs: %v", err) 30 } 31 32 // Mounting special dirs in a nonexistent task dir *should* return an 33 // error 34 if err := td.mountSpecialDirs(); err == nil { 35 t.Fatalf("expected mounting in a nonexistent task dir %q to fail", td.Dir) 36 } 37 38 // Create the task dir like TaskDir.Build would 39 if err := os.MkdirAll(td.Dir, 0777); err != nil { 40 t.Fatalf("error creating task dir %q: %v", td.Dir, err) 41 } 42 43 // Mounting special dirs should now work and contain files 44 if err := td.mountSpecialDirs(); err != nil { 45 t.Fatalf("error mounting special dirs in %q: %v", td.Dir, err) 46 } 47 if empty, err := pathEmpty(filepath.Join(td.Dir, "dev")); empty || err != nil { 48 t.Fatalf("expected dev to be populated but found: empty=%v error=%v", empty, err) 49 } 50 if empty, err := pathEmpty(filepath.Join(td.Dir, "proc")); empty || err != nil { 51 t.Fatalf("expected proc to be populated but found: empty=%v error=%v", empty, err) 52 } 53 54 // Remounting again should be fine 55 if err := td.mountSpecialDirs(); err != nil { 56 t.Fatalf("error remounting special dirs in %q: %v", td.Dir, err) 57 } 58 59 // Now unmount 60 if err := td.unmountSpecialDirs(); err != nil { 61 t.Fatalf("error unmounting special dirs in %q: %v", td.Dir, err) 62 } 63 if pathExists(filepath.Join(td.Dir, "dev")) { 64 t.Fatalf("dev was not removed from %q", td.Dir) 65 } 66 if pathExists(filepath.Join(td.Dir, "proc")) { 67 t.Fatalf("proc was not removed from %q", td.Dir) 68 } 69 if err := td.unmountSpecialDirs(); err != nil { 70 t.Fatalf("error re-unmounting special dirs in %q: %v", td.Dir, err) 71 } 72 }