2015-09-22 19:14:43 +00:00
|
|
|
package allocdir
|
|
|
|
|
|
|
|
import (
|
2016-09-22 04:28:12 +00:00
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
|
|
|
"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"
|
2016-10-03 21:58:44 +00:00
|
|
|
"strings"
|
2015-09-22 19:14:43 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-10-03 21:58:44 +00:00
|
|
|
tomb "gopkg.in/tomb.v1"
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2015-09-23 04:56:29 +00:00
|
|
|
"github.com/hashicorp/nomad/client/testutil"
|
2015-09-22 19:14:43 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-07-10 21:53:13 +00:00
|
|
|
osMountSharedDirSupport = map[string]bool{
|
2016-07-11 19:19:17 +00:00
|
|
|
"darwin": true,
|
|
|
|
"linux": true,
|
2016-07-10 21:53:13 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:14:43 +00:00
|
|
|
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)
|
|
|
|
|
2016-10-21 20:55:51 +00:00
|
|
|
d := NewAllocDir(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
func TestAllocDir_MountSharedAlloc(t *testing.T) {
|
|
|
|
testutil.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)
|
|
|
|
|
2016-10-21 20:55:51 +00:00
|
|
|
d := NewAllocDir(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)
|
|
|
|
if err := td1.Build(nil, cstructs.FSIsolationNone); err != nil {
|
|
|
|
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)
|
|
|
|
if err := td2.Build(nil, cstructs.FSIsolationNone); err != nil {
|
|
|
|
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 {
|
|
|
|
t.Fatalf("Failed to read shared alloc file from task dir: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
if !bytes.Equal(act, contents) {
|
|
|
|
t.Fatalf("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)
|
|
|
|
|
2016-10-21 20:55:51 +00:00
|
|
|
d := NewAllocDir(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)
|
|
|
|
if err := td1.Build(nil, cstructs.FSIsolationNone); err != nil {
|
|
|
|
t.Fatalf("error build task=%q dir: %v", t1.Name, err)
|
|
|
|
}
|
|
|
|
td2 := d.NewTaskDir(t2.Name)
|
|
|
|
if err := td2.Build(nil, cstructs.FSIsolationNone); err != nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2016-09-22 04:28:12 +00:00
|
|
|
t.Fatalf("couldn't write to task local directory: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
if err := d.Snapshot(&b); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tr := tar.NewReader(&b)
|
|
|
|
var files []string
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(files) != 2 {
|
|
|
|
t.Fatalf("bad files: %#v", files)
|
|
|
|
}
|
|
|
|
}
|
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
|
2016-10-26 22:17:57 +00:00
|
|
|
d1 := NewAllocDir(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()
|
|
|
|
|
2016-10-26 22:17:57 +00:00
|
|
|
d2 := NewAllocDir(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)
|
|
|
|
if err := td1.Build(nil, cstructs.FSIsolationNone); err != nil {
|
|
|
|
t.Fatalf("TaskDir.Build() faild: %v", err)
|
2016-10-03 16:59:57 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
td2 := d2.NewTaskDir(t1.Name)
|
|
|
|
if err := td2.Build(nil, cstructs.FSIsolationNone); err != nil {
|
|
|
|
t.Fatalf("TaskDir.Build() faild: %v", err)
|
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)
|
|
|
|
|
2016-10-21 20:55:51 +00:00
|
|
|
d := NewAllocDir(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
|
|
|
|
tomb := tomb.Tomb{}
|
|
|
|
if _, err := d.BlockUntilExists("../foo", &tomb); err == nil || !strings.Contains(err.Error(), "escapes") {
|
|
|
|
t.Fatalf("BlockUntilExists of escaping path didn't error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeEvents
|
|
|
|
if _, err := d.ChangeEvents("../foo", 0, &tomb); err == nil || !strings.Contains(err.Error(), "escapes") {
|
|
|
|
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)
|
|
|
|
|
|
|
|
d := NewAllocDir(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)
|
|
|
|
if err := td.Build(nil, cstructs.FSIsolationNone); err != nil {
|
|
|
|
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) {
|
|
|
|
dir, err := ioutil.TempDir("/tmp", "tmpdirtest")
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if len(info) != 6 {
|
|
|
|
t.Fatalf("expected: %v, actual: %v", 6, len(info))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllocDir_CreateDir(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("/tmp", "tmpdirtest")
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|