2017-10-10 06:01:55 +00:00
|
|
|
// +build linux
|
|
|
|
|
2015-09-29 22:33:25 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
2017-02-01 00:43:57 +00:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2015-10-07 22:24:16 +00:00
|
|
|
"fmt"
|
2015-10-09 19:14:56 +00:00
|
|
|
"io/ioutil"
|
2016-07-11 21:46:45 +00:00
|
|
|
"os"
|
2017-11-16 22:26:35 +00:00
|
|
|
"os/exec"
|
2015-10-09 19:14:56 +00:00
|
|
|
"path/filepath"
|
2016-03-02 19:28:50 +00:00
|
|
|
"reflect"
|
2016-03-24 07:47:23 +00:00
|
|
|
"strings"
|
2016-10-07 23:49:00 +00:00
|
|
|
"syscall"
|
2015-10-07 22:24:16 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2015-09-29 22:33:25 +00:00
|
|
|
|
2015-10-07 22:24:16 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2018-01-24 14:09:53 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2015-10-07 22:24:16 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2016-01-21 23:24:24 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2017-12-05 15:12:40 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-09-29 22:33:25 +00:00
|
|
|
|
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) {
|
2017-07-21 19:06:39 +00:00
|
|
|
t.Parallel()
|
2016-07-11 21:46:45 +00:00
|
|
|
if os.Getenv("NOMAD_TEST_RKT") == "" {
|
2017-05-04 23:21:40 +00:00
|
|
|
t.Skip("NOMAD_TEST_RKT unset, skipping")
|
2016-07-11 21:46:45 +00:00
|
|
|
}
|
2016-07-11 21:10:09 +00:00
|
|
|
|
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
|
|
|
// The fingerprinter test should always pass, even if rkt is not installed.
|
|
|
|
func TestRktDriver_Fingerprint(t *testing.T) {
|
2017-07-21 19:06:39 +00:00
|
|
|
t.Parallel()
|
2016-07-11 21:46:45 +00:00
|
|
|
if os.Getenv("NOMAD_TEST_RKT") == "" {
|
|
|
|
t.Skip("skipping rkt tests")
|
|
|
|
}
|
2016-07-11 21:10:09 +00:00
|
|
|
|
2015-10-07 22:24:16 +00:00
|
|
|
ctestutils.RktCompatible(t)
|
2016-12-03 01:04:07 +00:00
|
|
|
ctx := testDriverContexts(t, &structs.Task{Name: "foo", Driver: "rkt"})
|
|
|
|
d := NewRktDriver(ctx.DriverCtx)
|
2015-10-07 22:24:16 +00:00
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
|
|
|
|
request := &cstructs.FingerprintRequest{Config: &config.Config{}, Node: node}
|
|
|
|
response := &cstructs.FingerprintResponse{
|
|
|
|
Attributes: make(map[string]string, 0),
|
|
|
|
Links: make(map[string]string, 0),
|
|
|
|
Resources: &structs.Resources{},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := d.Fingerprint(request, response)
|
2015-10-07 22:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
|
|
|
|
if response.Attributes["driver.rkt"] != "1" {
|
2015-10-07 22:24:16 +00:00
|
|
|
t.Fatalf("Missing Rkt driver")
|
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
if response.Attributes["driver.rkt.version"] == "" {
|
2015-10-07 22:24:16 +00:00
|
|
|
t.Fatalf("Missing Rkt driver version")
|
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
if response.Attributes["driver.rkt.appc.version"] == "" {
|
2015-10-07 22:24:16 +00:00
|
|
|
t.Fatalf("Missing appc version for the Rkt driver")
|
|
|
|
}
|
2015-09-29 22:33:25 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 05:28:24 +00:00
|
|
|
func TestRktDriver_Start_DNS(t *testing.T) {
|
2017-07-23 04:01:22 +00:00
|
|
|
if !testutil.IsTravis() {
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
|
|
|
}
|
2016-07-11 21:46:45 +00:00
|
|
|
if os.Getenv("NOMAD_TEST_RKT") == "" {
|
|
|
|
t.Skip("skipping rkt tests")
|
|
|
|
}
|
2016-07-11 21:10:09 +00:00
|
|
|
|
2015-10-07 22:24:16 +00:00
|
|
|
ctestutils.RktCompatible(t)
|
|
|
|
// TODO: use test server to load from a fixture
|
|
|
|
task := &structs.Task{
|
2016-12-03 01:04:07 +00:00
|
|
|
Name: "etcd",
|
|
|
|
Driver: "rkt",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2016-03-10 22:56:43 +00:00
|
|
|
"trust_prefix": "coreos.com/etcd",
|
|
|
|
"image": "coreos.com/etcd:v2.0.4",
|
|
|
|
"command": "/etcd",
|
|
|
|
"dns_servers": []string{"8.8.8.8", "8.8.4.4"},
|
|
|
|
"dns_search_domains": []string{"example.com", "example.org", "example.net"},
|
2015-10-07 22:24:16 +00:00
|
|
|
},
|
2016-02-19 00:46:54 +00:00
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
2015-12-21 06:06:45 +00:00
|
|
|
Resources: &structs.Resources{
|
2016-02-06 08:23:56 +00:00
|
|
|
MemoryMB: 128,
|
|
|
|
CPU: 100,
|
2015-12-21 06:06:45 +00:00
|
|
|
},
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
ctx := testDriverContexts(t, task)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewRktDriver(ctx.DriverCtx)
|
2016-02-10 23:55:24 +00:00
|
|
|
|
2017-01-10 21:24:45 +00:00
|
|
|
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
|
2016-11-30 00:39:36 +00:00
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
2015-10-07 22:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
defer resp.Handle.Kill()
|
2015-10-07 22:24:16 +00:00
|
|
|
|
|
|
|
// Attempt to open
|
2017-06-13 21:02:11 +00:00
|
|
|
handle2, err := d.Open(ctx.ExecCtx, resp.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")
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
handle2.Kill()
|
2015-09-29 22:33:25 +00:00
|
|
|
}
|
2015-09-29 22:55:23 +00:00
|
|
|
|
|
|
|
func TestRktDriver_Start_Wait(t *testing.T) {
|
2017-07-23 04:01:22 +00:00
|
|
|
if !testutil.IsTravis() {
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
|
|
|
}
|
2016-07-11 21:46:45 +00:00
|
|
|
if os.Getenv("NOMAD_TEST_RKT") == "" {
|
|
|
|
t.Skip("skipping rkt tests")
|
|
|
|
}
|
2016-07-11 21:10:09 +00:00
|
|
|
|
2015-10-07 22:24:16 +00:00
|
|
|
ctestutils.RktCompatible(t)
|
|
|
|
task := &structs.Task{
|
2016-12-03 01:04:07 +00:00
|
|
|
Name: "etcd",
|
|
|
|
Driver: "rkt",
|
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
|
|
|
},
|
2016-02-19 00:46:54 +00:00
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
2015-12-21 06:09:11 +00:00
|
|
|
Resources: &structs.Resources{
|
2016-02-06 08:23:56 +00:00
|
|
|
MemoryMB: 128,
|
|
|
|
CPU: 100,
|
2015-12-21 06:09:11 +00:00
|
|
|
},
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
ctx := testDriverContexts(t, task)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewRktDriver(ctx.DriverCtx)
|
2015-10-07 22:24:16 +00:00
|
|
|
|
2017-01-10 21:24:45 +00:00
|
|
|
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
|
2016-11-30 00:39:36 +00:00
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
2015-10-07 22:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-11-16 22:26:35 +00:00
|
|
|
handle := resp.Handle.(*rktHandle)
|
|
|
|
defer handle.Kill()
|
2015-10-07 22:24:16 +00:00
|
|
|
|
|
|
|
// Update should be a no-op
|
2017-11-16 22:26:35 +00:00
|
|
|
if err := handle.Update(task); err != nil {
|
2015-10-07 22:24:16 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-10-07 23:49:00 +00:00
|
|
|
// Signal should be an error
|
2017-11-16 22:26:35 +00:00
|
|
|
if err := resp.Handle.Signal(syscall.SIGTERM); err == nil {
|
2016-10-07 23:49:00 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-07 22:24:16 +00:00
|
|
|
select {
|
2017-06-13 21:02:11 +00:00
|
|
|
case res := <-resp.Handle.WaitCh():
|
2015-11-14 06:07:13 +00:00
|
|
|
if !res.Successful() {
|
|
|
|
t.Fatalf("err: %v", res)
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
2016-10-13 23:18:18 +00:00
|
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()*15) * time.Second):
|
2015-10-07 22:24:16 +00:00
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
2017-11-16 22:26:35 +00:00
|
|
|
|
|
|
|
// Make sure pod was removed #3561
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd := exec.Command(rktCmd, "status", handle.uuid)
|
|
|
|
cmd.Stdout = ioutil.Discard
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
if err := cmd.Run(); err == nil {
|
|
|
|
t.Fatalf("expected error running 'rkt status %s' on removed container", handle.uuid)
|
|
|
|
}
|
|
|
|
if out := stderr.String(); !strings.Contains(out, "no matches found") {
|
|
|
|
t.Fatalf("expected 'no matches found' but received: %s", out)
|
|
|
|
}
|
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) {
|
2017-07-23 04:01:22 +00:00
|
|
|
if !testutil.IsTravis() {
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
|
|
|
}
|
2016-07-11 21:46:45 +00:00
|
|
|
if os.Getenv("NOMAD_TEST_RKT") == "" {
|
|
|
|
t.Skip("skipping rkt tests")
|
|
|
|
}
|
2016-07-11 21:10:09 +00:00
|
|
|
|
2015-10-12 21:37:56 +00:00
|
|
|
ctestutils.RktCompatible(t)
|
|
|
|
task := &structs.Task{
|
2016-12-03 01:04:07 +00:00
|
|
|
Name: "etcd",
|
|
|
|
Driver: "rkt",
|
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
|
|
|
},
|
2016-02-19 00:46:54 +00:00
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
2015-12-21 06:09:11 +00:00
|
|
|
Resources: &structs.Resources{
|
2016-02-06 08:23:56 +00:00
|
|
|
MemoryMB: 128,
|
|
|
|
CPU: 100,
|
2015-12-21 06:09:11 +00:00
|
|
|
},
|
2015-10-12 21:37:56 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
ctx := testDriverContexts(t, task)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewRktDriver(ctx.DriverCtx)
|
2015-10-12 21:37:56 +00:00
|
|
|
|
2017-01-10 21:24:45 +00:00
|
|
|
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
|
2016-11-30 00:39:36 +00:00
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
2015-10-12 21:37:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
defer resp.Handle.Kill()
|
2015-10-12 21:37:56 +00:00
|
|
|
|
|
|
|
// Update should be a no-op
|
2017-06-13 21:02:11 +00:00
|
|
|
err = resp.Handle.Update(task)
|
2015-10-12 21:37:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2017-06-13 21:02:11 +00:00
|
|
|
case res := <-resp.Handle.WaitCh():
|
2015-11-14 06:07:13 +00:00
|
|
|
if !res.Successful() {
|
|
|
|
t.Fatalf("err: %v", res)
|
2015-10-12 21:37:56 +00:00
|
|
|
}
|
2016-10-13 23:18:18 +00:00
|
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()*15) * time.Second):
|
2015-10-12 21:37:56 +00:00
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 19:28:50 +00:00
|
|
|
func TestRktDriver_Start_Wait_AllocDir(t *testing.T) {
|
2017-07-23 04:01:22 +00:00
|
|
|
if !testutil.IsTravis() {
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
|
|
|
}
|
2016-07-11 21:46:45 +00:00
|
|
|
if os.Getenv("NOMAD_TEST_RKT") == "" {
|
|
|
|
t.Skip("skipping rkt tests")
|
|
|
|
}
|
2016-07-11 21:10:09 +00:00
|
|
|
|
2015-10-09 19:14:56 +00:00
|
|
|
ctestutils.RktCompatible(t)
|
2016-03-02 19:28:50 +00:00
|
|
|
|
|
|
|
exp := []byte{'w', 'i', 'n'}
|
|
|
|
file := "output.txt"
|
2016-10-13 23:38:31 +00:00
|
|
|
tmpvol, err := ioutil.TempDir("", "nomadtest_rktdriver_volumes")
|
2016-10-13 23:34:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating temporary dir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpvol)
|
|
|
|
hostpath := filepath.Join(tmpvol, file)
|
2016-03-02 19:28:50 +00:00
|
|
|
|
2015-10-09 19:14:56 +00:00
|
|
|
task := &structs.Task{
|
2017-03-07 22:34:08 +00:00
|
|
|
Name: "rkttest_alpine",
|
2016-12-03 01:04:07 +00:00
|
|
|
Driver: "rkt",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2016-03-05 00:17:14 +00:00
|
|
|
"image": "docker://alpine",
|
|
|
|
"command": "/bin/sh",
|
|
|
|
"args": []string{
|
|
|
|
"-c",
|
2016-10-13 23:34:47 +00:00
|
|
|
fmt.Sprintf(`echo -n %s > foo/%s`, string(exp), file),
|
2016-03-02 19:28:50 +00:00
|
|
|
},
|
2016-11-30 00:39:36 +00:00
|
|
|
"net": []string{"none"},
|
2016-10-13 23:34:47 +00:00
|
|
|
"volumes": []string{fmt.Sprintf("%s:/foo", tmpvol)},
|
2015-10-09 19:14:56 +00:00
|
|
|
},
|
2016-02-19 00:46:54 +00:00
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
2015-12-21 06:09:11 +00:00
|
|
|
Resources: &structs.Resources{
|
2016-02-06 08:23:56 +00:00
|
|
|
MemoryMB: 128,
|
|
|
|
CPU: 100,
|
2015-12-21 06:09:11 +00:00
|
|
|
},
|
2015-10-09 19:14:56 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
ctx := testDriverContexts(t, task)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewRktDriver(ctx.DriverCtx)
|
2015-10-09 19:14:56 +00:00
|
|
|
|
2017-01-10 21:24:45 +00:00
|
|
|
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
|
2016-11-30 00:39:36 +00:00
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
2015-10-09 19:14:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
defer resp.Handle.Kill()
|
2015-10-09 19:14:56 +00:00
|
|
|
|
|
|
|
select {
|
2017-06-13 21:02:11 +00:00
|
|
|
case res := <-resp.Handle.WaitCh():
|
2015-11-14 06:07:13 +00:00
|
|
|
if !res.Successful() {
|
|
|
|
t.Fatalf("err: %v", res)
|
2015-10-09 19:14:56 +00:00
|
|
|
}
|
2016-10-13 23:18:18 +00:00
|
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()*15) * time.Second):
|
2015-10-09 19:14:56 +00:00
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
|
2016-03-02 19:28:50 +00:00
|
|
|
// Check that data was written to the shared alloc directory.
|
2016-10-13 23:34:47 +00:00
|
|
|
act, err := ioutil.ReadFile(hostpath)
|
2016-03-05 00:17:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't read expected output: %v", err)
|
|
|
|
}
|
2015-10-09 19:14:56 +00:00
|
|
|
|
2016-03-05 00:17:14 +00:00
|
|
|
if !reflect.DeepEqual(act, exp) {
|
|
|
|
t.Fatalf("Command output is %v; expected %v", act, exp)
|
|
|
|
}
|
2015-10-09 19:14:56 +00:00
|
|
|
}
|
2016-03-24 07:47:23 +00:00
|
|
|
|
|
|
|
func TestRktDriverUser(t *testing.T) {
|
2017-12-05 15:12:40 +00:00
|
|
|
assert := assert.New(t)
|
2017-07-23 04:01:22 +00:00
|
|
|
if !testutil.IsTravis() {
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
|
|
|
}
|
2016-07-11 21:46:45 +00:00
|
|
|
if os.Getenv("NOMAD_TEST_RKT") == "" {
|
|
|
|
t.Skip("skipping rkt tests")
|
|
|
|
}
|
2016-07-11 21:10:09 +00:00
|
|
|
|
2016-03-24 07:47:23 +00:00
|
|
|
ctestutils.RktCompatible(t)
|
|
|
|
task := &structs.Task{
|
2016-12-03 01:04:07 +00:00
|
|
|
Name: "etcd",
|
|
|
|
Driver: "rkt",
|
|
|
|
User: "alice",
|
2016-03-24 07:47:23 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"trust_prefix": "coreos.com/etcd",
|
|
|
|
"image": "coreos.com/etcd:v2.0.4",
|
|
|
|
"command": "/etcd",
|
|
|
|
"args": []string{"--version"},
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 128,
|
|
|
|
CPU: 100,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
ctx := testDriverContexts(t, task)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewRktDriver(ctx.DriverCtx)
|
2016-03-24 07:47:23 +00:00
|
|
|
|
2017-12-05 15:12:40 +00:00
|
|
|
_, err := d.Prestart(ctx.ExecCtx, task)
|
|
|
|
assert.Nil(err)
|
2017-06-13 21:02:11 +00:00
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
2017-12-05 15:12:40 +00:00
|
|
|
assert.Nil(err)
|
|
|
|
defer resp.Handle.Kill()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case res := <-resp.Handle.WaitCh():
|
|
|
|
assert.False(res.Successful())
|
|
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()*15) * time.Second):
|
|
|
|
t.Fatalf("timeout")
|
2016-03-24 07:47:23 +00:00
|
|
|
}
|
2017-12-05 15:12:40 +00:00
|
|
|
|
2016-03-24 07:47:23 +00:00
|
|
|
}
|
2016-08-04 01:18:58 +00:00
|
|
|
|
|
|
|
func TestRktTrustPrefix(t *testing.T) {
|
2017-07-23 04:01:22 +00:00
|
|
|
if !testutil.IsTravis() {
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
|
|
|
}
|
2016-08-04 01:18:58 +00:00
|
|
|
if os.Getenv("NOMAD_TEST_RKT") == "" {
|
|
|
|
t.Skip("skipping rkt tests")
|
|
|
|
}
|
|
|
|
ctestutils.RktCompatible(t)
|
|
|
|
task := &structs.Task{
|
2016-12-03 01:04:07 +00:00
|
|
|
Name: "etcd",
|
|
|
|
Driver: "rkt",
|
2016-08-04 01:18:58 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"trust_prefix": "example.com/invalid",
|
|
|
|
"image": "coreos.com/etcd:v2.0.4",
|
|
|
|
"command": "/etcd",
|
|
|
|
"args": []string{"--version"},
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 128,
|
|
|
|
CPU: 100,
|
|
|
|
},
|
|
|
|
}
|
2016-12-03 01:04:07 +00:00
|
|
|
ctx := testDriverContexts(t, task)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewRktDriver(ctx.DriverCtx)
|
2016-08-04 01:18:58 +00:00
|
|
|
|
2017-01-10 21:24:45 +00:00
|
|
|
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
|
2016-11-30 00:39:36 +00:00
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
2016-08-04 01:18:58 +00:00
|
|
|
if err == nil {
|
2017-06-13 21:02:11 +00:00
|
|
|
resp.Handle.Kill()
|
2016-08-04 01:18:58 +00:00
|
|
|
t.Fatalf("Should've failed")
|
|
|
|
}
|
|
|
|
msg := "Error running rkt trust"
|
|
|
|
if !strings.Contains(err.Error(), msg) {
|
|
|
|
t.Fatalf("Expecting '%v' in '%v'", msg, err)
|
|
|
|
}
|
|
|
|
}
|
2016-08-04 08:15:18 +00:00
|
|
|
|
|
|
|
func TestRktTaskValidate(t *testing.T) {
|
2017-07-21 19:06:39 +00:00
|
|
|
t.Parallel()
|
2016-08-04 08:15:18 +00:00
|
|
|
ctestutils.RktCompatible(t)
|
|
|
|
task := &structs.Task{
|
2016-12-03 01:04:07 +00:00
|
|
|
Name: "etcd",
|
|
|
|
Driver: "rkt",
|
2016-08-04 08:15:18 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"trust_prefix": "coreos.com/etcd",
|
|
|
|
"image": "coreos.com/etcd:v2.0.4",
|
|
|
|
"command": "/etcd",
|
|
|
|
"args": []string{"--version"},
|
|
|
|
"dns_servers": []string{"8.8.8.8", "8.8.4.4"},
|
|
|
|
"dns_search_domains": []string{"example.com", "example.org", "example.net"},
|
|
|
|
},
|
2016-09-02 19:44:05 +00:00
|
|
|
Resources: basicResources,
|
2016-08-04 08:15:18 +00:00
|
|
|
}
|
2016-12-03 01:04:07 +00:00
|
|
|
ctx := testDriverContexts(t, task)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewRktDriver(ctx.DriverCtx)
|
2016-08-04 08:15:18 +00:00
|
|
|
|
|
|
|
if err := d.Validate(task.Config); err != nil {
|
|
|
|
t.Fatalf("Validation error in TaskConfig : '%v'", err)
|
|
|
|
}
|
|
|
|
}
|
2016-08-18 15:07:11 +00:00
|
|
|
|
|
|
|
// TODO: Port Mapping test should be ran with proper ACI image and test the port access.
|
|
|
|
func TestRktDriver_PortsMapping(t *testing.T) {
|
2017-07-23 04:01:22 +00:00
|
|
|
if !testutil.IsTravis() {
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
|
|
|
}
|
2016-08-18 15:07:11 +00:00
|
|
|
if os.Getenv("NOMAD_TEST_RKT") == "" {
|
|
|
|
t.Skip("skipping rkt tests")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctestutils.RktCompatible(t)
|
|
|
|
task := &structs.Task{
|
2016-12-03 01:04:07 +00:00
|
|
|
Name: "etcd",
|
|
|
|
Driver: "rkt",
|
2016-08-18 15:07:11 +00:00
|
|
|
Config: map[string]interface{}{
|
2016-10-25 23:27:35 +00:00
|
|
|
"image": "docker://redis:latest",
|
2016-08-18 15:07:11 +00:00
|
|
|
"port_map": []map[string]string{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2016-10-25 23:27:35 +00:00
|
|
|
"main": "6379-tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"debug": "true",
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
Networks: []*structs.NetworkResource{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2016-10-25 23:27:35 +00:00
|
|
|
IP: "127.0.0.1",
|
2017-02-28 00:00:19 +00:00
|
|
|
ReservedPorts: []structs.Port{{Label: "main", Value: 8080}},
|
2016-08-18 15:07:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
ctx := testDriverContexts(t, task)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewRktDriver(ctx.DriverCtx)
|
2016-08-18 15:07:11 +00:00
|
|
|
|
2017-01-10 21:24:45 +00:00
|
|
|
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
|
2016-11-30 00:39:36 +00:00
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
2016-08-18 15:07:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-09-26 07:10:50 +00:00
|
|
|
if resp.Network == nil {
|
|
|
|
t.Fatalf("Expected driver to set a DriverNetwork, but it did not!")
|
|
|
|
}
|
2016-08-18 15:07:11 +00:00
|
|
|
|
2016-11-16 00:27:07 +00:00
|
|
|
failCh := make(chan error, 1)
|
2016-11-15 23:49:05 +00:00
|
|
|
go func() {
|
|
|
|
time.Sleep(1 * time.Second)
|
2017-06-13 21:02:11 +00:00
|
|
|
if err := resp.Handle.Kill(); err != nil {
|
2016-11-16 00:27:07 +00:00
|
|
|
failCh <- err
|
2016-10-25 23:27:35 +00:00
|
|
|
}
|
2016-11-15 23:49:05 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2016-11-16 00:27:07 +00:00
|
|
|
case err := <-failCh:
|
|
|
|
t.Fatalf("failed to kill handle: %v", err)
|
2017-06-13 21:02:11 +00:00
|
|
|
case <-resp.Handle.WaitCh():
|
2016-10-25 23:27:35 +00:00
|
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()*15) * time.Second):
|
|
|
|
t.Fatalf("timeout")
|
2016-08-18 15:07:11 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-01 00:43:57 +00:00
|
|
|
|
2017-12-02 01:20:43 +00:00
|
|
|
// TestRktDriver_PortsMapping_Host asserts that port_map isn't required when
|
|
|
|
// host networking is used.
|
|
|
|
func TestRktDriver_PortsMapping_Host(t *testing.T) {
|
|
|
|
if !testutil.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
if os.Getenv("NOMAD_TEST_RKT") == "" {
|
|
|
|
t.Skip("skipping rkt tests")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctestutils.RktCompatible(t)
|
|
|
|
task := &structs.Task{
|
|
|
|
Name: "etcd",
|
|
|
|
Driver: "rkt",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image": "docker://redis:latest",
|
|
|
|
"net": []string{"host"},
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 256,
|
|
|
|
CPU: 512,
|
|
|
|
Networks: []*structs.NetworkResource{
|
|
|
|
{
|
|
|
|
IP: "127.0.0.1",
|
|
|
|
ReservedPorts: []structs.Port{{Label: "main", Value: 8080}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := testDriverContexts(t, task)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewRktDriver(ctx.DriverCtx)
|
|
|
|
|
|
|
|
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
|
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if resp.Network != nil {
|
|
|
|
t.Fatalf("No network should be returned with --net=host but found: %#v", resp.Network)
|
|
|
|
}
|
|
|
|
|
|
|
|
failCh := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
if err := resp.Handle.Kill(); err != nil {
|
|
|
|
failCh <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-failCh:
|
|
|
|
t.Fatalf("failed to kill handle: %v", err)
|
|
|
|
case <-resp.Handle.WaitCh():
|
|
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()*15) * time.Second):
|
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 00:43:57 +00:00
|
|
|
func TestRktDriver_HandlerExec(t *testing.T) {
|
2017-07-23 04:01:22 +00:00
|
|
|
if !testutil.IsTravis() {
|
2017-07-23 02:04:36 +00:00
|
|
|
t.Parallel()
|
|
|
|
}
|
2017-02-01 00:43:57 +00:00
|
|
|
if os.Getenv("NOMAD_TEST_RKT") == "" {
|
|
|
|
t.Skip("skipping rkt tests")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctestutils.RktCompatible(t)
|
|
|
|
task := &structs.Task{
|
|
|
|
Name: "etcd",
|
|
|
|
Driver: "rkt",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"trust_prefix": "coreos.com/etcd",
|
|
|
|
"image": "coreos.com/etcd:v2.0.4",
|
|
|
|
"command": "/etcd",
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
MemoryMB: 128,
|
|
|
|
CPU: 100,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := testDriverContexts(t, task)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewRktDriver(ctx.DriverCtx)
|
|
|
|
|
|
|
|
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
|
|
|
|
t.Fatalf("error in prestart: %v", err)
|
|
|
|
}
|
2017-06-13 21:02:11 +00:00
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
2017-02-01 00:43:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give the pod a second to start
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
// Exec a command that should work
|
2017-06-13 21:02:11 +00:00
|
|
|
out, code, err := resp.Handle.Exec(context.TODO(), "/etcd", []string{"--version"})
|
2017-02-01 00:43:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error exec'ing etcd --version: %v", err)
|
|
|
|
}
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("expected `etcd --version` to succeed but exit code was: %d\n%s", code, string(out))
|
|
|
|
}
|
|
|
|
if expected := []byte("etcd version "); !bytes.HasPrefix(out, expected) {
|
|
|
|
t.Fatalf("expected output to start with %q but found:\n%q", expected, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exec a command that should fail
|
2017-06-13 21:02:11 +00:00
|
|
|
out, code, err = resp.Handle.Exec(context.TODO(), "/etcd", []string{"--kaljdshf"})
|
2017-02-01 00:43:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error exec'ing bad command: %v", err)
|
|
|
|
}
|
|
|
|
if code == 0 {
|
|
|
|
t.Fatalf("expected `stat` to fail but exit code was: %d", code)
|
|
|
|
}
|
|
|
|
if expected := "flag provided but not defined"; !bytes.Contains(out, []byte(expected)) {
|
|
|
|
t.Fatalf("expected output to contain %q but found: %q", expected, out)
|
|
|
|
}
|
|
|
|
|
2017-06-13 21:02:11 +00:00
|
|
|
if err := resp.Handle.Kill(); err != nil {
|
2017-02-01 00:43:57 +00:00
|
|
|
t.Fatalf("error killing handle: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2017-11-16 23:45:14 +00:00
|
|
|
|
|
|
|
func TestRktDriver_Remove_Error(t *testing.T) {
|
|
|
|
if !testutil.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
if os.Getenv("NOMAD_TEST_RKT") == "" {
|
|
|
|
t.Skip("skipping rkt tests")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctestutils.RktCompatible(t)
|
|
|
|
|
|
|
|
// Removing a non-existent pod should return an error
|
|
|
|
if err := rktRemove("00000000-0000-0000-0000-000000000000"); err == nil {
|
|
|
|
t.Fatalf("expected an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rktRemove("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"); err == nil {
|
|
|
|
t.Fatalf("expected an error")
|
|
|
|
}
|
|
|
|
}
|