2015-09-29 22:33:25 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
2015-10-07 22:24:16 +00:00
|
|
|
"fmt"
|
2015-10-09 19:14:56 +00:00
|
|
|
"io/ioutil"
|
2015-10-07 22:24:16 +00:00
|
|
|
"os"
|
2015-10-09 19:14:56 +00:00
|
|
|
"path/filepath"
|
2015-10-07 22:24:16 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2015-09-29 22:33:25 +00:00
|
|
|
|
2015-10-09 19:14:56 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
2015-10-07 22:24:16 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2016-01-21 23:24:24 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2015-09-29 22:33:25 +00:00
|
|
|
|
2015-11-14 06:07:13 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/driver/structs"
|
2015-10-07 22:24:16 +00:00
|
|
|
ctestutils "github.com/hashicorp/nomad/client/testutil"
|
2015-09-29 22:33:25 +00:00
|
|
|
)
|
|
|
|
|
2015-10-21 01:08:46 +00:00
|
|
|
func TestRktVersionRegex(t *testing.T) {
|
2015-10-23 23:23:43 +00:00
|
|
|
input_rkt := "rkt version 0.8.1"
|
|
|
|
input_appc := "appc version 1.2.0"
|
|
|
|
expected_rkt := "0.8.1"
|
|
|
|
expected_appc := "1.2.0"
|
|
|
|
rktMatches := reRktVersion.FindStringSubmatch(input_rkt)
|
|
|
|
appcMatches := reAppcVersion.FindStringSubmatch(input_appc)
|
|
|
|
if rktMatches[1] != expected_rkt {
|
|
|
|
fmt.Printf("Test failed; got %q; want %q\n", rktMatches[1], expected_rkt)
|
|
|
|
}
|
|
|
|
if appcMatches[1] != expected_appc {
|
|
|
|
fmt.Printf("Test failed; got %q; want %q\n", appcMatches[1], expected_appc)
|
|
|
|
}
|
2015-10-21 01:08:46 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 22:33:25 +00:00
|
|
|
func TestRktDriver_Handle(t *testing.T) {
|
2015-10-07 22:24:16 +00:00
|
|
|
h := &rktHandle{
|
2015-12-23 00:10:30 +00:00
|
|
|
proc: &os.Process{Pid: 123},
|
|
|
|
image: "foo",
|
|
|
|
killTimeout: 5 * time.Nanosecond,
|
|
|
|
doneCh: make(chan struct{}),
|
|
|
|
waitCh: make(chan *cstructs.WaitResult, 1),
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
actual := h.ID()
|
2015-12-23 00:10:30 +00:00
|
|
|
expected := `Rkt:{"Pid":123,"Image":"foo","KillTimeout":5}`
|
2015-10-07 22:24:16 +00:00
|
|
|
if actual != expected {
|
|
|
|
t.Errorf("Expected `%s`, found `%s`", expected, actual)
|
|
|
|
}
|
2015-09-29 22:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The fingerprinter test should always pass, even if rkt is not installed.
|
|
|
|
func TestRktDriver_Fingerprint(t *testing.T) {
|
2015-10-07 22:24:16 +00:00
|
|
|
ctestutils.RktCompatible(t)
|
2016-01-06 02:02:11 +00:00
|
|
|
driverCtx, _ := testDriverContexts(&structs.Task{Name: "foo"})
|
|
|
|
d := NewRktDriver(driverCtx)
|
2015-10-07 22:24:16 +00:00
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
|
|
|
apply, err := d.Fingerprint(&config.Config{}, node)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !apply {
|
|
|
|
t.Fatalf("should apply")
|
|
|
|
}
|
2015-10-12 20:15:37 +00:00
|
|
|
if node.Attributes["driver.rkt"] != "1" {
|
2015-10-07 22:24:16 +00:00
|
|
|
t.Fatalf("Missing Rkt driver")
|
|
|
|
}
|
|
|
|
if node.Attributes["driver.rkt.version"] == "" {
|
|
|
|
t.Fatalf("Missing Rkt driver version")
|
|
|
|
}
|
|
|
|
if node.Attributes["driver.rkt.appc.version"] == "" {
|
|
|
|
t.Fatalf("Missing appc version for the Rkt driver")
|
|
|
|
}
|
2015-09-29 22:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRktDriver_Start(t *testing.T) {
|
2015-10-07 22:24:16 +00:00
|
|
|
ctestutils.RktCompatible(t)
|
|
|
|
// TODO: use test server to load from a fixture
|
|
|
|
task := &structs.Task{
|
|
|
|
Name: "etcd",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2015-10-07 22:24:16 +00:00
|
|
|
"trust_prefix": "coreos.com/etcd",
|
2015-10-12 21:37:56 +00:00
|
|
|
"image": "coreos.com/etcd:v2.0.4",
|
|
|
|
"command": "/etcd",
|
2015-10-07 22:24:16 +00:00
|
|
|
},
|
2015-12-21 06:06:45 +00:00
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
},
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
driverCtx, execCtx := testDriverContexts(task)
|
2015-10-07 22:24:16 +00:00
|
|
|
d := NewRktDriver(driverCtx)
|
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
handle, err := d.Start(execCtx, task)
|
2015-10-07 22:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if handle == nil {
|
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to open
|
2016-01-06 02:02:11 +00:00
|
|
|
handle2, err := d.Open(execCtx, handle.ID())
|
2015-10-07 22:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if handle2 == nil {
|
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
2015-09-29 22:33:25 +00:00
|
|
|
}
|
2015-09-29 22:55:23 +00:00
|
|
|
|
|
|
|
func TestRktDriver_Start_Wait(t *testing.T) {
|
2015-10-07 22:24:16 +00:00
|
|
|
ctestutils.RktCompatible(t)
|
|
|
|
task := &structs.Task{
|
|
|
|
Name: "etcd",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2015-10-07 22:24:16 +00:00
|
|
|
"trust_prefix": "coreos.com/etcd",
|
2015-10-12 21:37:56 +00:00
|
|
|
"image": "coreos.com/etcd:v2.0.4",
|
|
|
|
"command": "/etcd",
|
2015-11-18 23:16:42 +00:00
|
|
|
"args": []string{"--version"},
|
2015-10-07 22:24:16 +00:00
|
|
|
},
|
2015-12-21 06:09:11 +00:00
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
},
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
driverCtx, execCtx := testDriverContexts(task)
|
|
|
|
defer execCtx.AllocDir.Destroy()
|
2015-10-07 22:24:16 +00:00
|
|
|
d := NewRktDriver(driverCtx)
|
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
handle, err := d.Start(execCtx, task)
|
2015-10-07 22:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if handle == nil {
|
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
|
|
|
defer handle.Kill()
|
|
|
|
|
|
|
|
// Update should be a no-op
|
|
|
|
err = handle.Update(task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2015-11-14 06:07:13 +00:00
|
|
|
case res := <-handle.WaitCh():
|
|
|
|
if !res.Successful() {
|
|
|
|
t.Fatalf("err: %v", res)
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
2016-01-21 23:24:24 +00:00
|
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()*5) * time.Second):
|
2015-10-07 22:24:16 +00:00
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
2015-09-29 22:55:23 +00:00
|
|
|
}
|
2015-10-09 19:14:56 +00:00
|
|
|
|
2015-10-12 21:37:56 +00:00
|
|
|
func TestRktDriver_Start_Wait_Skip_Trust(t *testing.T) {
|
|
|
|
ctestutils.RktCompatible(t)
|
|
|
|
task := &structs.Task{
|
|
|
|
Name: "etcd",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2015-10-12 21:37:56 +00:00
|
|
|
"image": "coreos.com/etcd:v2.0.4",
|
|
|
|
"command": "/etcd",
|
2015-11-18 23:16:42 +00:00
|
|
|
"args": []string{"--version"},
|
2015-10-12 21:37:56 +00:00
|
|
|
},
|
2015-12-21 06:09:11 +00:00
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
},
|
2015-10-12 21:37:56 +00:00
|
|
|
}
|
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
driverCtx, execCtx := testDriverContexts(task)
|
|
|
|
defer execCtx.AllocDir.Destroy()
|
2015-10-12 21:37:56 +00:00
|
|
|
d := NewRktDriver(driverCtx)
|
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
handle, err := d.Start(execCtx, task)
|
2015-10-12 21:37:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if handle == nil {
|
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
|
|
|
defer handle.Kill()
|
|
|
|
|
|
|
|
// Update should be a no-op
|
|
|
|
err = handle.Update(task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2015-11-14 06:07:13 +00:00
|
|
|
case res := <-handle.WaitCh():
|
|
|
|
if !res.Successful() {
|
|
|
|
t.Fatalf("err: %v", res)
|
2015-10-12 21:37:56 +00:00
|
|
|
}
|
2016-01-21 23:24:24 +00:00
|
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()*5) * time.Second):
|
2015-10-12 21:37:56 +00:00
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-09 19:14:56 +00:00
|
|
|
func TestRktDriver_Start_Wait_Logs(t *testing.T) {
|
|
|
|
ctestutils.RktCompatible(t)
|
|
|
|
task := &structs.Task{
|
|
|
|
Name: "etcd",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2015-10-09 19:14:56 +00:00
|
|
|
"trust_prefix": "coreos.com/etcd",
|
2015-10-12 21:37:56 +00:00
|
|
|
"image": "coreos.com/etcd:v2.0.4",
|
|
|
|
"command": "/etcd",
|
2015-11-18 23:16:42 +00:00
|
|
|
"args": []string{"--version"},
|
2015-10-09 19:14:56 +00:00
|
|
|
},
|
2015-12-21 06:09:11 +00:00
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
},
|
2015-10-09 19:14:56 +00:00
|
|
|
}
|
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
driverCtx, execCtx := testDriverContexts(task)
|
|
|
|
defer execCtx.AllocDir.Destroy()
|
2015-10-09 19:14:56 +00:00
|
|
|
d := NewRktDriver(driverCtx)
|
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
handle, err := d.Start(execCtx, task)
|
2015-10-09 19:14:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if handle == nil {
|
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
|
|
|
defer handle.Kill()
|
|
|
|
|
|
|
|
select {
|
2015-11-14 06:07:13 +00:00
|
|
|
case res := <-handle.WaitCh():
|
|
|
|
if !res.Successful() {
|
|
|
|
t.Fatalf("err: %v", res)
|
2015-10-09 19:14:56 +00:00
|
|
|
}
|
2016-01-21 23:24:24 +00:00
|
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()*5) * time.Second):
|
2015-10-09 19:14:56 +00:00
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
taskDir, ok := execCtx.AllocDir.TaskDirs[task.Name]
|
2015-10-09 19:14:56 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Could not find task directory for task: %v", task)
|
|
|
|
}
|
|
|
|
stdout := filepath.Join(taskDir, allocdir.TaskLocal, fmt.Sprintf("%v.stdout", task.Name))
|
|
|
|
data, err := ioutil.ReadFile(stdout)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to read tasks stdout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
t.Fatal("Task's stdout is empty")
|
|
|
|
}
|
|
|
|
}
|