2015-09-22 19:14:43 +00:00
|
|
|
package allocdir
|
|
|
|
|
|
|
|
import (
|
2016-09-22 04:28:12 +00:00
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
2018-01-21 01:19:55 +00:00
|
|
|
"context"
|
2016-09-22 04:28:12 +00:00
|
|
|
"io"
|
2015-09-22 19:14:43 +00:00
|
|
|
"io/ioutil"
|
2016-11-08 18:57:29 +00:00
|
|
|
"log"
|
2015-09-22 19:14:43 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-11-27 19:20:53 +00:00
|
|
|
"runtime"
|
2016-10-03 21:58:44 +00:00
|
|
|
"strings"
|
2017-08-16 17:46:14 +00:00
|
|
|
"syscall"
|
2015-09-22 19:14:43 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2018-06-13 22:33:25 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2015-09-22 19:14:43 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-06-13 22:52:25 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-09-22 19:14:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
t1 = &structs.Task{
|
|
|
|
Name: "web",
|
|
|
|
Driver: "exec",
|
2015-11-15 08:41:56 +00:00
|
|
|
Config: map[string]interface{}{
|
2015-09-22 19:14:43 +00:00
|
|
|
"command": "/bin/date",
|
|
|
|
"args": "+%s",
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
DiskMB: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
t2 = &structs.Task{
|
|
|
|
Name: "web2",
|
|
|
|
Driver: "exec",
|
2015-11-15 08:41:56 +00:00
|
|
|
Config: map[string]interface{}{
|
2015-09-22 19:14:43 +00:00
|
|
|
"command": "/bin/date",
|
|
|
|
"args": "+%s",
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
DiskMB: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
// Test that AllocDir.Build builds just the alloc directory.
|
2015-09-22 19:14:43 +00:00
|
|
|
func TestAllocDir_BuildAlloc(t *testing.T) {
|
|
|
|
tmp, err := ioutil.TempDir("", "AllocDir")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't create temp dir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
d := NewAllocDir(testlog.HCLogger(t), tmp)
|
2016-01-20 20:00:20 +00:00
|
|
|
defer d.Destroy()
|
2016-12-03 01:04:07 +00:00
|
|
|
d.NewTaskDir(t1.Name)
|
|
|
|
d.NewTaskDir(t2.Name)
|
|
|
|
if err := d.Build(); err != nil {
|
|
|
|
t.Fatalf("Build() failed: %v", err)
|
2015-09-22 19:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the AllocDir and each of the task directories exist.
|
|
|
|
if _, err := os.Stat(d.AllocDir); os.IsNotExist(err) {
|
2016-12-03 01:04:07 +00:00
|
|
|
t.Fatalf("Build() didn't create AllocDir %v", d.AllocDir)
|
2015-09-22 19:14:43 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
for _, task := range []*structs.Task{t1, t2} {
|
2015-09-23 04:56:29 +00:00
|
|
|
tDir, ok := d.TaskDirs[task.Name]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Task directory not found for %v", task.Name)
|
2015-09-22 19:14:43 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
if stat, _ := os.Stat(tDir.Dir); stat != nil {
|
|
|
|
t.Fatalf("Build() created TaskDir %v", tDir.Dir)
|
2015-09-22 19:14:43 +00:00
|
|
|
}
|
2016-09-02 19:44:05 +00:00
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
if stat, _ := os.Stat(tDir.SecretsDir); stat != nil {
|
|
|
|
t.Fatalf("Build() created secret dir %v", tDir.Dir)
|
2016-09-02 19:44:05 +00:00
|
|
|
}
|
2015-09-22 19:14:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 19:20:53 +00:00
|
|
|
// HACK: This function is copy/pasted from client.testutil to prevent a test
|
|
|
|
// import cycle, due to testutil transitively importing allocdir. This
|
|
|
|
// should be fixed after DriverManager is implemented.
|
|
|
|
func MountCompatible(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("Windows does not support mount")
|
|
|
|
}
|
|
|
|
|
|
|
|
if syscall.Geteuid() != 0 {
|
|
|
|
t.Skip("Must be root to run test")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
func TestAllocDir_MountSharedAlloc(t *testing.T) {
|
2018-11-27 19:20:53 +00:00
|
|
|
MountCompatible(t)
|
2015-09-22 19:14:43 +00:00
|
|
|
tmp, err := ioutil.TempDir("", "AllocDir")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't create temp dir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
d := NewAllocDir(testlog.HCLogger(t), tmp)
|
2016-01-20 20:00:20 +00:00
|
|
|
defer d.Destroy()
|
2016-12-03 01:04:07 +00:00
|
|
|
if err := d.Build(); err != nil {
|
|
|
|
t.Fatalf("Build() failed: %v", err)
|
2015-09-22 19:14:43 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
// Build 2 task dirs
|
|
|
|
td1 := d.NewTaskDir(t1.Name)
|
2017-03-02 21:21:34 +00:00
|
|
|
if err := td1.Build(false, nil, cstructs.FSIsolationChroot); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
t.Fatalf("error build task=%q dir: %v", t1.Name, err)
|
2015-09-22 19:14:43 +00:00
|
|
|
}
|
2016-12-03 01:04:07 +00:00
|
|
|
td2 := d.NewTaskDir(t2.Name)
|
2017-03-02 21:21:34 +00:00
|
|
|
if err := td2.Build(false, nil, cstructs.FSIsolationChroot); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
t.Fatalf("error build task=%q dir: %v", t2.Name, err)
|
2015-09-23 04:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write a file to the shared dir.
|
2016-12-03 01:04:07 +00:00
|
|
|
contents := []byte("foo")
|
|
|
|
const filename = "bar"
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(d.SharedDir, filename), contents, 0666); err != nil {
|
2015-09-23 04:56:29 +00:00
|
|
|
t.Fatalf("Couldn't write file to shared directory: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
// Check that the file exists in the task directories
|
|
|
|
for _, td := range []*TaskDir{td1, td2} {
|
|
|
|
taskFile := filepath.Join(td.SharedTaskDir, filename)
|
2015-09-23 04:56:29 +00:00
|
|
|
act, err := ioutil.ReadFile(taskFile)
|
|
|
|
if err != nil {
|
2017-01-18 19:28:34 +00:00
|
|
|
t.Errorf("Failed to read shared alloc file from task dir: %v", err)
|
|
|
|
continue
|
2015-09-23 04:56:29 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
if !bytes.Equal(act, contents) {
|
2017-01-18 19:28:34 +00:00
|
|
|
t.Errorf("Incorrect data read from task dir: want %v; got %v", contents, act)
|
2015-09-23 04:56:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-22 04:28:12 +00:00
|
|
|
|
|
|
|
func TestAllocDir_Snapshot(t *testing.T) {
|
|
|
|
tmp, err := ioutil.TempDir("", "AllocDir")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't create temp dir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
d := NewAllocDir(testlog.HCLogger(t), tmp)
|
2016-09-22 04:28:12 +00:00
|
|
|
defer d.Destroy()
|
2016-12-03 01:04:07 +00:00
|
|
|
if err := d.Build(); err != nil {
|
|
|
|
t.Fatalf("Build() failed: %v", err)
|
2016-09-22 04:28:12 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
// Build 2 task dirs
|
|
|
|
td1 := d.NewTaskDir(t1.Name)
|
2017-03-02 21:21:34 +00:00
|
|
|
if err := td1.Build(false, nil, cstructs.FSIsolationImage); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
t.Fatalf("error build task=%q dir: %v", t1.Name, err)
|
|
|
|
}
|
|
|
|
td2 := d.NewTaskDir(t2.Name)
|
2017-03-02 21:21:34 +00:00
|
|
|
if err := td2.Build(false, nil, cstructs.FSIsolationImage); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
t.Fatalf("error build task=%q dir: %v", t2.Name, err)
|
|
|
|
}
|
2016-09-22 04:28:12 +00:00
|
|
|
|
|
|
|
// Write a file to the shared dir.
|
|
|
|
exp := []byte{'f', 'o', 'o'}
|
|
|
|
file := "bar"
|
2016-12-03 01:04:07 +00:00
|
|
|
if err := ioutil.WriteFile(filepath.Join(d.SharedDir, "data", file), exp, 0666); err != nil {
|
2016-09-22 04:28:12 +00:00
|
|
|
t.Fatalf("Couldn't write file to shared directory: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-06-04 06:26:22 +00:00
|
|
|
// Write a symlink to the shared dir
|
|
|
|
link := "qux"
|
|
|
|
if err := os.Symlink("foo", filepath.Join(d.SharedDir, "data", link)); err != nil {
|
|
|
|
t.Fatalf("Couldn't write symlink to shared directory: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-09-22 04:28:12 +00:00
|
|
|
// Write a file to the task local
|
|
|
|
exp = []byte{'b', 'a', 'r'}
|
|
|
|
file1 := "lol"
|
2016-12-03 01:04:07 +00:00
|
|
|
if err := ioutil.WriteFile(filepath.Join(td1.LocalDir, file1), exp, 0666); err != nil {
|
2017-06-04 06:26:22 +00:00
|
|
|
t.Fatalf("couldn't write file to task local directory: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write a symlink to the task local
|
|
|
|
link1 := "baz"
|
|
|
|
if err := os.Symlink("bar", filepath.Join(td1.LocalDir, link1)); err != nil {
|
2018-03-11 17:55:06 +00:00
|
|
|
t.Fatalf("couldn't write symlink to task local directory :%v", err)
|
2016-09-22 04:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
if err := d.Snapshot(&b); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tr := tar.NewReader(&b)
|
|
|
|
var files []string
|
2017-06-04 06:26:22 +00:00
|
|
|
var links []string
|
2016-09-22 04:28:12 +00:00
|
|
|
for {
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if hdr.Typeflag == tar.TypeReg {
|
|
|
|
files = append(files, hdr.FileInfo().Name())
|
2017-06-04 06:26:22 +00:00
|
|
|
} else if hdr.Typeflag == tar.TypeSymlink {
|
|
|
|
links = append(links, hdr.FileInfo().Name())
|
2016-09-22 04:28:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(files) != 2 {
|
|
|
|
t.Fatalf("bad files: %#v", files)
|
|
|
|
}
|
2017-06-04 06:26:22 +00:00
|
|
|
if len(links) != 2 {
|
|
|
|
t.Fatalf("bad links: %#v", links)
|
|
|
|
}
|
2016-09-22 04:28:12 +00:00
|
|
|
}
|
2016-10-03 16:59:57 +00:00
|
|
|
|
|
|
|
func TestAllocDir_Move(t *testing.T) {
|
2016-10-26 22:17:57 +00:00
|
|
|
tmp1, err := ioutil.TempDir("", "AllocDir")
|
2016-10-03 16:59:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't create temp dir: %v", err)
|
|
|
|
}
|
2016-10-26 22:17:57 +00:00
|
|
|
defer os.RemoveAll(tmp1)
|
|
|
|
|
|
|
|
tmp2, err := ioutil.TempDir("", "AllocDir")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't create temp dir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp2)
|
2016-10-03 16:59:57 +00:00
|
|
|
|
|
|
|
// Create two alloc dirs
|
2018-08-29 22:05:03 +00:00
|
|
|
d1 := NewAllocDir(testlog.HCLogger(t), tmp1)
|
2016-12-03 01:04:07 +00:00
|
|
|
if err := d1.Build(); err != nil {
|
|
|
|
t.Fatalf("Build() failed: %v", err)
|
|
|
|
}
|
2016-10-03 16:59:57 +00:00
|
|
|
defer d1.Destroy()
|
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
d2 := NewAllocDir(testlog.HCLogger(t), tmp2)
|
2016-12-03 01:04:07 +00:00
|
|
|
if err := d2.Build(); err != nil {
|
|
|
|
t.Fatalf("Build() failed: %v", err)
|
|
|
|
}
|
2016-10-03 16:59:57 +00:00
|
|
|
defer d2.Destroy()
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
td1 := d1.NewTaskDir(t1.Name)
|
2017-03-02 21:21:34 +00:00
|
|
|
if err := td1.Build(false, nil, cstructs.FSIsolationImage); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
t.Fatalf("TaskDir.Build() faild: %v", err)
|
2016-10-03 16:59:57 +00:00
|
|
|
}
|
|
|
|
|
2017-02-22 01:22:10 +00:00
|
|
|
// Create but don't build second task dir to mimic alloc/task runner
|
|
|
|
// behavior (AllocDir.Move() is called pre-TaskDir.Build).
|
|
|
|
d2.NewTaskDir(t1.Name)
|
2016-10-03 16:59:57 +00:00
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
dataDir := filepath.Join(d1.SharedDir, SharedDataDir)
|
2016-10-03 16:59:57 +00:00
|
|
|
|
|
|
|
// Write a file to the shared dir.
|
2016-12-03 01:04:07 +00:00
|
|
|
exp1 := []byte("foo")
|
|
|
|
file1 := "bar"
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(dataDir, file1), exp1, 0666); err != nil {
|
2016-10-03 16:59:57 +00:00
|
|
|
t.Fatalf("Couldn't write file to shared directory: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write a file to the task local
|
2016-12-03 01:04:07 +00:00
|
|
|
exp2 := []byte("bar")
|
|
|
|
file2 := "lol"
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(td1.LocalDir, file2), exp2, 0666); err != nil {
|
2016-10-03 16:59:57 +00:00
|
|
|
t.Fatalf("couldn't write to task local directory: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the d1 allocdir to d2
|
2016-12-03 01:04:07 +00:00
|
|
|
if err := d2.Move(d1, []*structs.Task{t1}); err != nil {
|
2016-10-03 16:59:57 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the files in d1 are present in d2
|
2016-12-03 01:04:07 +00:00
|
|
|
fi, err := os.Stat(filepath.Join(d2.SharedDir, SharedDataDir, file1))
|
2016-10-03 16:59:57 +00:00
|
|
|
if err != nil || fi == nil {
|
|
|
|
t.Fatalf("data dir was not moved")
|
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
fi, err = os.Stat(filepath.Join(d2.TaskDirs[t1.Name].LocalDir, file2))
|
2016-10-03 16:59:57 +00:00
|
|
|
if err != nil || fi == nil {
|
|
|
|
t.Fatalf("task local dir was not moved")
|
|
|
|
}
|
|
|
|
}
|
2016-10-03 21:58:44 +00:00
|
|
|
|
|
|
|
func TestAllocDir_EscapeChecking(t *testing.T) {
|
|
|
|
tmp, err := ioutil.TempDir("", "AllocDir")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't create temp dir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
d := NewAllocDir(testlog.HCLogger(t), tmp)
|
2016-12-03 01:04:07 +00:00
|
|
|
if err := d.Build(); err != nil {
|
|
|
|
t.Fatalf("Build() failed: %v", err)
|
2016-10-03 21:58:44 +00:00
|
|
|
}
|
2016-12-03 01:04:07 +00:00
|
|
|
defer d.Destroy()
|
2016-10-03 21:58:44 +00:00
|
|
|
|
|
|
|
// Check that issuing calls that escape the alloc dir returns errors
|
|
|
|
// List
|
|
|
|
if _, err := d.List(".."); err == nil || !strings.Contains(err.Error(), "escapes") {
|
|
|
|
t.Fatalf("List of escaping path didn't error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat
|
|
|
|
if _, err := d.Stat("../foo"); err == nil || !strings.Contains(err.Error(), "escapes") {
|
|
|
|
t.Fatalf("Stat of escaping path didn't error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadAt
|
|
|
|
if _, err := d.ReadAt("../foo", 0); err == nil || !strings.Contains(err.Error(), "escapes") {
|
|
|
|
t.Fatalf("ReadAt of escaping path didn't error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlockUntilExists
|
2018-01-21 01:19:55 +00:00
|
|
|
if _, err := d.BlockUntilExists(context.Background(), "../foo"); err == nil || !strings.Contains(err.Error(), "escapes") {
|
2016-10-03 21:58:44 +00:00
|
|
|
t.Fatalf("BlockUntilExists of escaping path didn't error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeEvents
|
2018-01-21 01:19:55 +00:00
|
|
|
if _, err := d.ChangeEvents(context.Background(), "../foo", 0); err == nil || !strings.Contains(err.Error(), "escapes") {
|
2016-10-03 21:58:44 +00:00
|
|
|
t.Fatalf("ChangeEvents of escaping path didn't error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2016-10-24 18:14:05 +00:00
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
// Test that `nomad fs` can't read secrets
|
2016-10-24 18:14:05 +00:00
|
|
|
func TestAllocDir_ReadAt_SecretDir(t *testing.T) {
|
|
|
|
tmp, err := ioutil.TempDir("", "AllocDir")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't create temp dir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2018-08-29 22:05:03 +00:00
|
|
|
d := NewAllocDir(testlog.HCLogger(t), tmp)
|
2016-12-03 01:04:07 +00:00
|
|
|
if err := d.Build(); err != nil {
|
|
|
|
t.Fatalf("Build() failed: %v", err)
|
|
|
|
}
|
2016-10-24 18:14:05 +00:00
|
|
|
defer d.Destroy()
|
2016-12-03 01:04:07 +00:00
|
|
|
|
|
|
|
td := d.NewTaskDir(t1.Name)
|
2017-03-02 21:21:34 +00:00
|
|
|
if err := td.Build(false, nil, cstructs.FSIsolationImage); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
t.Fatalf("TaskDir.Build() failed: %v", err)
|
2016-10-24 18:14:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadAt of secret dir should fail
|
|
|
|
secret := filepath.Join(t1.Name, TaskSecrets, "test_file")
|
|
|
|
if _, err := d.ReadAt(secret, 0); err == nil || !strings.Contains(err.Error(), "secret file prohibited") {
|
|
|
|
t.Fatalf("ReadAt of secret file didn't error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2016-11-08 18:57:29 +00:00
|
|
|
|
|
|
|
func TestAllocDir_SplitPath(t *testing.T) {
|
2017-03-02 21:21:34 +00:00
|
|
|
dir, err := ioutil.TempDir("", "tmpdirtest")
|
2016-11-08 18:57:29 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
dest := filepath.Join(dir, "/foo/bar/baz")
|
|
|
|
if err := os.MkdirAll(dest, os.ModePerm); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
info, err := splitPath(dest)
|
2016-11-08 18:57:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-07-23 02:27:20 +00:00
|
|
|
// Testing that is 6 or more rather than 6 because on osx, the temp dir is
|
|
|
|
// randomized.
|
|
|
|
if len(info) < 6 {
|
|
|
|
t.Fatalf("expected more than: %v, actual: %v", 6, len(info))
|
2016-11-08 18:57:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllocDir_CreateDir(t *testing.T) {
|
2017-08-16 17:46:14 +00:00
|
|
|
if syscall.Geteuid() != 0 {
|
|
|
|
t.Skip("Must be root to run test")
|
|
|
|
}
|
|
|
|
|
2017-03-02 21:21:34 +00:00
|
|
|
dir, err := ioutil.TempDir("", "tmpdirtest")
|
2016-11-08 18:57:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
// create a subdir and a file
|
|
|
|
subdir := filepath.Join(dir, "subdir")
|
|
|
|
if err := os.MkdirAll(subdir, 0760); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
subdirMode, err := os.Stat(subdir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the above hierarchy under another destination
|
|
|
|
dir1, err := ioutil.TempDir("/tmp", "tempdirdest")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
if err := createDir(dir1, subdir); err != nil {
|
2016-11-08 18:57:29 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the subdir had the right perm
|
|
|
|
fi, err := os.Stat(filepath.Join(dir1, dir, "subdir"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if fi.Mode() != subdirMode.Mode() {
|
|
|
|
t.Fatalf("wrong file mode: %v, expected: %v", fi.Mode(), subdirMode.Mode())
|
|
|
|
}
|
|
|
|
}
|
2017-03-02 21:21:34 +00:00
|
|
|
|
2017-07-17 22:07:22 +00:00
|
|
|
// TestAllocDir_Copy asserts that AllocDir.Copy does a deep copy of itself and
|
|
|
|
// all TaskDirs.
|
|
|
|
func TestAllocDir_Copy(t *testing.T) {
|
2018-08-29 22:05:03 +00:00
|
|
|
a := NewAllocDir(testlog.HCLogger(t), "foo")
|
2017-07-17 22:07:22 +00:00
|
|
|
a.NewTaskDir("bar")
|
|
|
|
a.NewTaskDir("baz")
|
|
|
|
|
|
|
|
b := a.Copy()
|
2018-06-13 22:52:25 +00:00
|
|
|
|
|
|
|
// Clear the logger
|
|
|
|
require.Equal(t, a, b)
|
2017-07-17 22:07:22 +00:00
|
|
|
|
|
|
|
// Make sure TaskDirs map is copied
|
|
|
|
a.NewTaskDir("new")
|
|
|
|
if b.TaskDirs["new"] != nil {
|
|
|
|
t.Errorf("TaskDirs map shared between copied")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-02 21:21:34 +00:00
|
|
|
func TestPathFuncs(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "nomadtest-pathfuncs")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating temp dir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
missingDir := filepath.Join(dir, "does-not-exist")
|
|
|
|
|
|
|
|
if !pathExists(dir) {
|
|
|
|
t.Errorf("%q exists", dir)
|
|
|
|
}
|
|
|
|
if pathExists(missingDir) {
|
|
|
|
t.Errorf("%q does not exist", missingDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
if empty, err := pathEmpty(dir); err != nil || !empty {
|
2017-03-03 05:03:05 +00:00
|
|
|
t.Errorf("%q is empty and exists. empty=%v error=%v", dir, empty, err)
|
2017-03-02 21:21:34 +00:00
|
|
|
}
|
|
|
|
if empty, err := pathEmpty(missingDir); err == nil || empty {
|
2017-03-03 05:03:05 +00:00
|
|
|
t.Errorf("%q is missing. empty=%v error=%v", missingDir, empty, err)
|
2017-03-02 21:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
filename := filepath.Join(dir, "just-some-file")
|
|
|
|
f, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create %q: %v", filename, err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
if empty, err := pathEmpty(dir); err != nil || empty {
|
2017-03-03 05:03:05 +00:00
|
|
|
t.Errorf("%q is not empty. empty=%v error=%v", dir, empty, err)
|
2017-03-02 21:21:34 +00:00
|
|
|
}
|
|
|
|
}
|