2015-08-30 03:07:24 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2016-01-21 22:09:04 +00:00
|
|
|
"fmt"
|
2015-08-30 03:07:24 +00:00
|
|
|
"log"
|
2016-03-15 21:34:25 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2015-08-30 03:07:24 +00:00
|
|
|
"os"
|
2015-09-25 23:49:14 +00:00
|
|
|
"path/filepath"
|
2015-08-30 03:07:24 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
2015-08-30 03:07:24 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver"
|
2016-05-27 18:25:36 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/driver/structs"
|
2015-08-30 03:07:24 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2015-09-23 00:10:03 +00:00
|
|
|
|
2015-09-23 01:48:42 +00:00
|
|
|
ctestutil "github.com/hashicorp/nomad/client/testutil"
|
2015-08-30 03:07:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func testLogger() *log.Logger {
|
2016-03-22 20:49:52 +00:00
|
|
|
return prefixedTestLogger("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func prefixedTestLogger(prefix string) *log.Logger {
|
|
|
|
return log.New(os.Stderr, prefix, log.LstdFlags)
|
2015-08-30 03:07:24 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 19:09:29 +00:00
|
|
|
type MockTaskStateUpdater struct {
|
|
|
|
state string
|
|
|
|
events []*structs.TaskEvent
|
|
|
|
}
|
2015-08-30 03:07:24 +00:00
|
|
|
|
2016-02-02 19:09:29 +00:00
|
|
|
func (m *MockTaskStateUpdater) Update(name, state string, event *structs.TaskEvent) {
|
|
|
|
m.state = state
|
|
|
|
m.events = append(m.events, event)
|
|
|
|
}
|
2015-08-30 03:07:24 +00:00
|
|
|
|
2015-11-14 06:07:13 +00:00
|
|
|
func testTaskRunner(restarts bool) (*MockTaskStateUpdater, *TaskRunner) {
|
2016-03-15 21:34:25 +00:00
|
|
|
return testTaskRunnerFromAlloc(restarts, mock.Alloc())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a mock task runner using the first task in the first task group of
|
|
|
|
// the passed allocation.
|
|
|
|
func testTaskRunnerFromAlloc(restarts bool, alloc *structs.Allocation) (*MockTaskStateUpdater, *TaskRunner) {
|
2015-08-30 03:07:24 +00:00
|
|
|
logger := testLogger()
|
|
|
|
conf := DefaultConfig()
|
2015-09-24 21:29:53 +00:00
|
|
|
conf.StateDir = os.TempDir()
|
|
|
|
conf.AllocDir = os.TempDir()
|
2015-08-30 03:07:24 +00:00
|
|
|
upd := &MockTaskStateUpdater{}
|
|
|
|
task := alloc.Job.TaskGroups[0].Tasks[0]
|
2015-09-24 23:00:36 +00:00
|
|
|
// Initialize the port listing. This should be done by the offer process but
|
|
|
|
// we have a mock so that doesn't happen.
|
2015-11-15 06:16:32 +00:00
|
|
|
task.Resources.Networks[0].ReservedPorts = []structs.Port{{"", 80}}
|
2015-09-24 23:00:36 +00:00
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
allocDir := allocdir.NewAllocDir(filepath.Join(conf.AllocDir, alloc.ID))
|
|
|
|
allocDir.Build([]*structs.Task{task})
|
|
|
|
|
2015-11-06 02:31:39 +00:00
|
|
|
ctx := driver.NewExecContext(allocDir, alloc.ID)
|
2016-03-23 19:19:19 +00:00
|
|
|
tr := NewTaskRunner(logger, conf, upd.Update, ctx, alloc, task)
|
2015-11-14 06:07:13 +00:00
|
|
|
if !restarts {
|
2016-02-04 00:16:48 +00:00
|
|
|
tr.restartTracker = noRestartsTracker()
|
2015-11-14 06:07:13 +00:00
|
|
|
}
|
2015-08-30 03:07:24 +00:00
|
|
|
return upd, tr
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskRunner_SimpleRun(t *testing.T) {
|
2015-09-23 01:48:42 +00:00
|
|
|
ctestutil.ExecCompatible(t)
|
2016-02-02 19:09:29 +00:00
|
|
|
upd, tr := testTaskRunner(false)
|
2016-03-22 20:49:52 +00:00
|
|
|
tr.MarkReceived()
|
2015-08-30 03:07:24 +00:00
|
|
|
go tr.Run()
|
|
|
|
defer tr.Destroy()
|
2015-09-24 23:59:09 +00:00
|
|
|
defer tr.ctx.AllocDir.Destroy()
|
2015-08-30 03:07:24 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-tr.WaitCh():
|
2016-01-21 23:24:24 +00:00
|
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()*15) * time.Second):
|
2015-08-30 03:07:24 +00:00
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
|
2016-02-19 22:49:43 +00:00
|
|
|
if len(upd.events) != 3 {
|
|
|
|
t.Fatalf("should have 3 updates: %#v", upd.events)
|
2015-08-30 03:07:24 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 19:09:29 +00:00
|
|
|
if upd.state != structs.TaskStateDead {
|
|
|
|
t.Fatalf("TaskState %v; want %v", upd.state, structs.TaskStateDead)
|
2015-08-30 03:07:24 +00:00
|
|
|
}
|
2015-11-14 06:07:13 +00:00
|
|
|
|
2016-02-19 22:49:43 +00:00
|
|
|
if upd.events[0].Type != structs.TaskReceived {
|
|
|
|
t.Fatalf("First Event was %v; want %v", upd.events[0].Type, structs.TaskReceived)
|
2015-08-30 03:07:24 +00:00
|
|
|
}
|
2015-11-14 06:07:13 +00:00
|
|
|
|
2016-02-19 22:49:43 +00:00
|
|
|
if upd.events[1].Type != structs.TaskStarted {
|
|
|
|
t.Fatalf("Second Event was %v; want %v", upd.events[1].Type, structs.TaskStarted)
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.events[2].Type != structs.TaskTerminated {
|
|
|
|
t.Fatalf("Third Event was %v; want %v", upd.events[2].Type, structs.TaskTerminated)
|
2015-08-30 03:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskRunner_Destroy(t *testing.T) {
|
2015-09-23 01:48:42 +00:00
|
|
|
ctestutil.ExecCompatible(t)
|
2016-02-02 19:09:29 +00:00
|
|
|
upd, tr := testTaskRunner(true)
|
2016-03-22 20:49:52 +00:00
|
|
|
tr.MarkReceived()
|
2015-09-24 23:59:09 +00:00
|
|
|
defer tr.ctx.AllocDir.Destroy()
|
2015-08-30 03:07:24 +00:00
|
|
|
|
|
|
|
// Change command to ensure we run for a bit
|
|
|
|
tr.task.Config["command"] = "/bin/sleep"
|
2016-02-29 00:56:05 +00:00
|
|
|
tr.task.Config["args"] = []string{"1000"}
|
2015-08-30 03:07:24 +00:00
|
|
|
go tr.Run()
|
|
|
|
|
2016-02-29 00:56:05 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
if l := len(upd.events); l != 2 {
|
|
|
|
return false, fmt.Errorf("Expect two events; got %v", l)
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.events[0].Type != structs.TaskReceived {
|
|
|
|
return false, fmt.Errorf("First Event was %v; want %v", upd.events[0].Type, structs.TaskReceived)
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.events[1].Type != structs.TaskStarted {
|
|
|
|
return false, fmt.Errorf("Second Event was %v; want %v", upd.events[1].Type, structs.TaskStarted)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
|
2016-05-29 02:38:09 +00:00
|
|
|
// Make sure we are collecting afew stats
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
stats := tr.StatsReporter().ResourceUsage()
|
|
|
|
if len(stats) == 0 {
|
|
|
|
t.Fatalf("expected task runner to have some stats")
|
|
|
|
}
|
|
|
|
|
2015-08-30 03:07:24 +00:00
|
|
|
// Begin the tear down
|
2016-02-29 00:56:05 +00:00
|
|
|
tr.Destroy()
|
2015-08-30 03:07:24 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-tr.WaitCh():
|
2016-01-21 20:55:35 +00:00
|
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()*15) * time.Second):
|
2015-08-30 03:07:24 +00:00
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
|
2016-02-19 22:49:43 +00:00
|
|
|
if len(upd.events) != 3 {
|
|
|
|
t.Fatalf("should have 3 updates: %#v", upd.events)
|
2015-08-30 03:07:24 +00:00
|
|
|
}
|
2015-11-14 06:07:13 +00:00
|
|
|
|
2016-02-02 19:09:29 +00:00
|
|
|
if upd.state != structs.TaskStateDead {
|
|
|
|
t.Fatalf("TaskState %v; want %v", upd.state, structs.TaskStateDead)
|
2015-08-30 03:07:24 +00:00
|
|
|
}
|
2015-11-14 06:07:13 +00:00
|
|
|
|
2016-02-19 22:49:43 +00:00
|
|
|
if upd.events[2].Type != structs.TaskKilled {
|
|
|
|
t.Fatalf("Third Event was %v; want %v", upd.events[2].Type, structs.TaskKilled)
|
2015-08-30 03:07:24 +00:00
|
|
|
}
|
2015-11-14 06:07:13 +00:00
|
|
|
|
2015-08-30 03:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskRunner_Update(t *testing.T) {
|
2015-09-23 01:48:42 +00:00
|
|
|
ctestutil.ExecCompatible(t)
|
2015-11-14 06:07:13 +00:00
|
|
|
_, tr := testTaskRunner(false)
|
2015-08-30 03:07:24 +00:00
|
|
|
|
|
|
|
// Change command to ensure we run for a bit
|
|
|
|
tr.task.Config["command"] = "/bin/sleep"
|
2016-02-04 03:58:39 +00:00
|
|
|
tr.task.Config["args"] = []string{"100"}
|
2015-08-30 03:07:24 +00:00
|
|
|
go tr.Run()
|
|
|
|
defer tr.Destroy()
|
2015-09-24 23:59:09 +00:00
|
|
|
defer tr.ctx.AllocDir.Destroy()
|
2015-08-30 03:07:24 +00:00
|
|
|
|
|
|
|
// Update the task definition
|
2016-02-04 03:43:44 +00:00
|
|
|
updateAlloc := tr.alloc.Copy()
|
|
|
|
|
|
|
|
// Update the restart policy
|
|
|
|
newTG := updateAlloc.Job.TaskGroups[0]
|
|
|
|
newMode := "foo"
|
|
|
|
newTG.RestartPolicy.Mode = newMode
|
|
|
|
|
|
|
|
newTask := updateAlloc.Job.TaskGroups[0].Tasks[0]
|
2015-08-30 03:07:24 +00:00
|
|
|
newTask.Driver = "foobar"
|
2016-02-04 03:43:44 +00:00
|
|
|
|
|
|
|
// Update the kill timeout
|
2016-02-04 03:58:39 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
if tr.handle == nil {
|
|
|
|
return false, fmt.Errorf("task not started")
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
|
2016-02-04 03:43:44 +00:00
|
|
|
oldHandle := tr.handle.ID()
|
|
|
|
newTask.KillTimeout = time.Hour
|
|
|
|
|
|
|
|
tr.Update(updateAlloc)
|
2015-08-30 03:07:24 +00:00
|
|
|
|
|
|
|
// Wait for update to take place
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
2016-02-04 03:43:44 +00:00
|
|
|
if tr.task != newTask {
|
|
|
|
return false, fmt.Errorf("task not updated")
|
|
|
|
}
|
|
|
|
if tr.restartTracker.policy.Mode != newMode {
|
|
|
|
return false, fmt.Errorf("restart policy not updated")
|
|
|
|
}
|
|
|
|
if tr.handle.ID() == oldHandle {
|
|
|
|
return false, fmt.Errorf("handle not updated")
|
|
|
|
}
|
|
|
|
return true, nil
|
2015-08-30 03:07:24 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskRunner_SaveRestoreState(t *testing.T) {
|
2015-11-11 00:53:59 +00:00
|
|
|
ctestutil.ExecCompatible(t)
|
2015-11-14 06:07:13 +00:00
|
|
|
upd, tr := testTaskRunner(false)
|
2015-08-30 03:07:24 +00:00
|
|
|
|
|
|
|
// Change command to ensure we run for a bit
|
|
|
|
tr.task.Config["command"] = "/bin/sleep"
|
2015-11-18 23:16:42 +00:00
|
|
|
tr.task.Config["args"] = []string{"10"}
|
2015-08-30 03:07:24 +00:00
|
|
|
go tr.Run()
|
|
|
|
defer tr.Destroy()
|
|
|
|
|
|
|
|
// Snapshot state
|
2016-01-20 20:00:20 +00:00
|
|
|
time.Sleep(2 * time.Second)
|
2015-11-11 00:49:38 +00:00
|
|
|
if err := tr.SaveState(); err != nil {
|
2015-08-30 03:07:24 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new task runner
|
|
|
|
tr2 := NewTaskRunner(tr.logger, tr.config, upd.Update,
|
2016-03-23 19:19:19 +00:00
|
|
|
tr.ctx, tr.alloc, &structs.Task{Name: tr.task.Name})
|
2015-11-11 00:49:38 +00:00
|
|
|
if err := tr2.RestoreState(); err != nil {
|
2015-08-30 03:07:24 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
go tr2.Run()
|
2015-11-11 00:49:38 +00:00
|
|
|
defer tr2.Destroy()
|
2015-08-30 03:07:24 +00:00
|
|
|
|
|
|
|
// Destroy and wait
|
2016-01-21 22:09:04 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
return tr2.handle != nil, fmt.Errorf("RestoreState() didn't open handle")
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
2015-08-30 03:07:24 +00:00
|
|
|
}
|
2016-03-15 21:34:25 +00:00
|
|
|
|
|
|
|
func TestTaskRunner_Download_List(t *testing.T) {
|
|
|
|
ctestutil.ExecCompatible(t)
|
|
|
|
|
|
|
|
ts := httptest.NewServer(http.FileServer(http.Dir(filepath.Dir("."))))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
// Create an allocation that has a task with a list of artifacts.
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
task := alloc.Job.TaskGroups[0].Tasks[0]
|
|
|
|
f1 := "task_runner_test.go"
|
|
|
|
f2 := "task_runner.go"
|
|
|
|
artifact1 := structs.TaskArtifact{
|
|
|
|
GetterSource: fmt.Sprintf("%s/%s", ts.URL, f1),
|
|
|
|
}
|
|
|
|
artifact2 := structs.TaskArtifact{
|
|
|
|
GetterSource: fmt.Sprintf("%s/%s", ts.URL, f2),
|
|
|
|
}
|
|
|
|
task.Artifacts = []*structs.TaskArtifact{&artifact1, &artifact2}
|
|
|
|
|
|
|
|
upd, tr := testTaskRunnerFromAlloc(false, alloc)
|
2016-03-22 20:49:52 +00:00
|
|
|
tr.MarkReceived()
|
2016-03-15 21:34:25 +00:00
|
|
|
go tr.Run()
|
|
|
|
defer tr.Destroy()
|
|
|
|
defer tr.ctx.AllocDir.Destroy()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-tr.WaitCh():
|
|
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()*15) * time.Second):
|
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(upd.events) != 4 {
|
|
|
|
t.Fatalf("should have 4 updates: %#v", upd.events)
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.state != structs.TaskStateDead {
|
|
|
|
t.Fatalf("TaskState %v; want %v", upd.state, structs.TaskStateDead)
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.events[0].Type != structs.TaskReceived {
|
|
|
|
t.Fatalf("First Event was %v; want %v", upd.events[0].Type, structs.TaskReceived)
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.events[1].Type != structs.TaskDownloadingArtifacts {
|
|
|
|
t.Fatalf("Second Event was %v; want %v", upd.events[1].Type, structs.TaskDownloadingArtifacts)
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.events[2].Type != structs.TaskStarted {
|
|
|
|
t.Fatalf("Third Event was %v; want %v", upd.events[2].Type, structs.TaskStarted)
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.events[3].Type != structs.TaskTerminated {
|
|
|
|
t.Fatalf("Fourth Event was %v; want %v", upd.events[3].Type, structs.TaskTerminated)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that both files exist.
|
|
|
|
taskDir := tr.ctx.AllocDir.TaskDirs[task.Name]
|
|
|
|
if _, err := os.Stat(filepath.Join(taskDir, f1)); err != nil {
|
|
|
|
t.Fatalf("%v not downloaded", f1)
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(filepath.Join(taskDir, f2)); err != nil {
|
|
|
|
t.Fatalf("%v not downloaded", f2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskRunner_Download_Retries(t *testing.T) {
|
|
|
|
ctestutil.ExecCompatible(t)
|
|
|
|
|
|
|
|
// Create an allocation that has a task with bad artifacts.
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
task := alloc.Job.TaskGroups[0].Tasks[0]
|
|
|
|
artifact := structs.TaskArtifact{
|
|
|
|
GetterSource: "http://127.1.1.111:12315/foo/bar/baz",
|
|
|
|
}
|
|
|
|
task.Artifacts = []*structs.TaskArtifact{&artifact}
|
|
|
|
|
|
|
|
// Make the restart policy try one update
|
|
|
|
alloc.Job.TaskGroups[0].RestartPolicy = &structs.RestartPolicy{
|
|
|
|
Attempts: 1,
|
|
|
|
Interval: 10 * time.Minute,
|
|
|
|
Delay: 1 * time.Second,
|
|
|
|
Mode: structs.RestartPolicyModeFail,
|
|
|
|
}
|
|
|
|
|
|
|
|
upd, tr := testTaskRunnerFromAlloc(true, alloc)
|
2016-03-22 20:49:52 +00:00
|
|
|
tr.MarkReceived()
|
2016-03-15 21:34:25 +00:00
|
|
|
go tr.Run()
|
|
|
|
defer tr.Destroy()
|
|
|
|
defer tr.ctx.AllocDir.Destroy()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-tr.WaitCh():
|
|
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()*15) * time.Second):
|
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(upd.events) != 7 {
|
|
|
|
t.Fatalf("should have 7 updates: %#v", upd.events)
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.state != structs.TaskStateDead {
|
|
|
|
t.Fatalf("TaskState %v; want %v", upd.state, structs.TaskStateDead)
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.events[0].Type != structs.TaskReceived {
|
|
|
|
t.Fatalf("First Event was %v; want %v", upd.events[0].Type, structs.TaskReceived)
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.events[1].Type != structs.TaskDownloadingArtifacts {
|
|
|
|
t.Fatalf("Second Event was %v; want %v", upd.events[1].Type, structs.TaskDownloadingArtifacts)
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.events[2].Type != structs.TaskArtifactDownloadFailed {
|
|
|
|
t.Fatalf("Third Event was %v; want %v", upd.events[2].Type, structs.TaskArtifactDownloadFailed)
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.events[3].Type != structs.TaskRestarting {
|
|
|
|
t.Fatalf("Fourth Event was %v; want %v", upd.events[3].Type, structs.TaskRestarting)
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.events[4].Type != structs.TaskDownloadingArtifacts {
|
|
|
|
t.Fatalf("Fifth Event was %v; want %v", upd.events[4].Type, structs.TaskDownloadingArtifacts)
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.events[5].Type != structs.TaskArtifactDownloadFailed {
|
|
|
|
t.Fatalf("Sixth Event was %v; want %v", upd.events[5].Type, structs.TaskArtifactDownloadFailed)
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.events[6].Type != structs.TaskNotRestarting {
|
|
|
|
t.Fatalf("Seventh Event was %v; want %v", upd.events[6].Type, structs.TaskNotRestarting)
|
|
|
|
}
|
|
|
|
}
|
2016-03-24 17:55:14 +00:00
|
|
|
|
|
|
|
func TestTaskRunner_Validate_UserEnforcement(t *testing.T) {
|
|
|
|
_, tr := testTaskRunner(false)
|
|
|
|
|
|
|
|
// Try to run as root with exec.
|
|
|
|
tr.task.Driver = "exec"
|
|
|
|
tr.task.User = "root"
|
|
|
|
if err := tr.validateTask(); err == nil {
|
|
|
|
t.Fatalf("expected error running as root with exec")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to run a non-blacklisted user with exec.
|
|
|
|
tr.task.Driver = "exec"
|
|
|
|
tr.task.User = "foobar"
|
|
|
|
if err := tr.validateTask(); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to run as root with docker.
|
|
|
|
tr.task.Driver = "docker"
|
|
|
|
tr.task.User = "root"
|
|
|
|
if err := tr.validateTask(); err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-05-27 18:25:36 +00:00
|
|
|
|
|
|
|
func TestTaskRunnerResouseUsageTS(t *testing.T) {
|
|
|
|
_, tr := testTaskRunner(false)
|
|
|
|
|
|
|
|
t1, _ := time.Parse(time.RFC822, "02 Jan 06 15:03 MST")
|
|
|
|
t2, _ := time.Parse(time.RFC822, "02 Jan 06 15:05 MST")
|
|
|
|
t3, _ := time.Parse(time.RFC822, "02 Jan 06 15:06 MST")
|
|
|
|
t4, _ := time.Parse(time.RFC822, "02 Jan 06 15:07 MST")
|
|
|
|
t5, _ := time.Parse(time.RFC822, "02 Jan 06 15:08 MST")
|
|
|
|
|
2016-05-29 02:38:09 +00:00
|
|
|
ru1 := cstructs.TaskResourceUsage{Timestamp: t1.UnixNano()}
|
|
|
|
ru2 := cstructs.TaskResourceUsage{Timestamp: t2.UnixNano()}
|
|
|
|
ru3 := cstructs.TaskResourceUsage{Timestamp: t3.UnixNano()}
|
|
|
|
ru4 := cstructs.TaskResourceUsage{Timestamp: t4.UnixNano()}
|
|
|
|
ru5 := cstructs.TaskResourceUsage{Timestamp: t5.UnixNano()}
|
2016-05-27 18:25:36 +00:00
|
|
|
|
|
|
|
tr.resourceUsage.Enqueue(&ru1)
|
|
|
|
tr.resourceUsage.Enqueue(&ru2)
|
|
|
|
tr.resourceUsage.Enqueue(&ru3)
|
|
|
|
tr.resourceUsage.Enqueue(&ru4)
|
|
|
|
tr.resourceUsage.Enqueue(&ru5)
|
|
|
|
|
2016-05-29 02:38:09 +00:00
|
|
|
values := tr.ResourceUsageTS(t3.Add(-1 * time.Second).UnixNano())
|
2016-05-27 18:25:36 +00:00
|
|
|
if len(values) != 3 {
|
|
|
|
t.Fatalf("expected values: 3, actual: %v", len(values))
|
|
|
|
}
|
|
|
|
|
2016-05-29 02:38:09 +00:00
|
|
|
values = tr.ResourceUsageTS(t3.UnixNano())
|
2016-05-27 18:25:36 +00:00
|
|
|
if len(values) != 3 {
|
|
|
|
t.Fatalf("expected values: 3, actual: %v", len(values))
|
|
|
|
}
|
|
|
|
|
|
|
|
begenning, _ := time.Parse(time.RFC822, "01 Jan 1970 00:00 UTC")
|
2016-05-29 02:38:09 +00:00
|
|
|
values = tr.ResourceUsageTS(begenning.UnixNano())
|
2016-05-27 18:25:36 +00:00
|
|
|
if len(values) != 5 {
|
|
|
|
t.Fatalf("expected values: 5, actual: %v", len(values))
|
|
|
|
}
|
|
|
|
|
2016-05-29 02:38:09 +00:00
|
|
|
values = tr.ResourceUsageTS(t5.Add(1 * time.Second).UnixNano())
|
2016-05-27 18:25:36 +00:00
|
|
|
if len(values) != 0 {
|
|
|
|
t.Fatalf("expected values: 3, actual: %v", len(values))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|