open-nomad/client/driver/java_test.go

427 lines
9.6 KiB
Go
Raw Normal View History

2015-09-01 21:57:21 +00:00
package driver
import (
2016-02-18 05:20:22 +00:00
"os"
"os/exec"
2016-02-18 05:20:22 +00:00
"path/filepath"
"runtime"
"strings"
2016-10-10 18:46:27 +00:00
"syscall"
2015-09-01 21:57:21 +00:00
"testing"
"time"
"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-25 23:49:14 +00:00
ctestutils "github.com/hashicorp/nomad/client/testutil"
2015-09-01 21:57:21 +00:00
)
var (
osJavaDriverSupport = map[string]bool{
"linux": true,
}
)
// javaLocated checks whether java is installed so we can run java stuff.
func javaLocated() bool {
_, err := exec.Command("java", "-version").CombinedOutput()
return err == nil
}
// The fingerprinter test should always pass, even if Java is not installed.
2015-09-01 21:57:21 +00:00
func TestJavaDriver_Fingerprint(t *testing.T) {
2017-07-23 03:38:03 +00:00
if !testutil.IsTravis() {
t.Parallel()
}
ctestutils.JavaCompatible(t)
2016-08-23 21:51:09 +00:00
task := &structs.Task{
Name: "foo",
Driver: "java",
2016-08-23 21:51:09 +00:00
Resources: structs.DefaultResources(),
}
ctx := testDriverContexts(t, task)
defer ctx.AllocDir.Destroy()
d := NewJavaDriver(ctx.DriverCtx)
2015-09-01 21:57:21 +00:00
node := &structs.Node{
2016-02-18 19:34:17 +00:00
Attributes: map[string]string{
"unique.cgroup.mountpoint": "/sys/fs/cgroups",
},
2015-09-01 21:57:21 +00:00
}
apply, err := d.Fingerprint(&config.Config{}, node)
if err != nil {
t.Fatalf("err: %v", err)
}
if apply != javaLocated() {
t.Fatalf("Fingerprinter should detect Java when it is installed")
2015-09-01 21:57:21 +00:00
}
if node.Attributes["driver.java"] != "1" {
if v, ok := osJavaDriverSupport[runtime.GOOS]; v && ok {
t.Fatalf("missing java driver")
} else {
t.Skipf("missing java driver, no OS support")
}
2015-09-01 21:57:21 +00:00
}
for _, key := range []string{"driver.java.version", "driver.java.runtime", "driver.java.vm"} {
if node.Attributes[key] == "" {
t.Fatalf("missing driver key (%s)", key)
}
}
2015-09-01 21:57:21 +00:00
}
func TestJavaDriver_StartOpen_Wait(t *testing.T) {
if !testutil.IsTravis() {
t.Parallel()
}
2015-11-11 00:59:52 +00:00
if !javaLocated() {
t.Skip("Java not found; skipping")
}
ctestutils.JavaCompatible(t)
2015-09-01 21:57:21 +00:00
task := &structs.Task{
Name: "demo-app",
Driver: "java",
Config: map[string]interface{}{
2016-03-15 20:28:25 +00:00
"jar_path": "demoapp.jar",
"jvm_options": []string{"-Xmx64m", "-Xms32m"},
2015-09-01 21:57:21 +00:00
},
2016-02-10 21:54:54 +00:00
LogConfig: &structs.LogConfig{
MaxFiles: 10,
MaxFileSizeMB: 10,
},
Resources: basicResources,
2015-09-01 21:57:21 +00:00
}
2015-09-25 23:49:14 +00:00
ctx := testDriverContexts(t, task)
defer ctx.AllocDir.Destroy()
d := NewJavaDriver(ctx.DriverCtx)
2015-09-25 23:49:14 +00:00
2016-03-15 20:28:25 +00:00
// Copy the test jar into the task's directory
dst := ctx.ExecCtx.TaskDir.Dir
2016-03-15 20:28:25 +00:00
copyFile("./test-resources/java/demoapp.jar", filepath.Join(dst, "demoapp.jar"), t)
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
t.Fatalf("prestart err: %v", err)
}
resp, err := d.Start(ctx.ExecCtx, task)
2015-09-01 21:57:21 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
// Attempt to open
handle2, err := d.Open(ctx.ExecCtx, resp.Handle.ID())
2015-09-01 21:57:21 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if handle2 == nil {
t.Fatalf("missing handle")
}
time.Sleep(2 * time.Second)
// There is a race condition between the handle waiting and killing. One
// will return an error.
resp.Handle.Kill()
handle2.Kill()
2015-09-01 21:57:21 +00:00
}
func TestJavaDriver_Start_Wait(t *testing.T) {
if !testutil.IsTravis() {
t.Parallel()
}
if !javaLocated() {
t.Skip("Java not found; skipping")
}
ctestutils.JavaCompatible(t)
2015-09-01 21:57:21 +00:00
task := &structs.Task{
Name: "demo-app",
Driver: "java",
Config: map[string]interface{}{
2016-03-15 20:28:25 +00:00
"jar_path": "demoapp.jar",
"args": []string{"1"},
2015-09-01 21:57:21 +00:00
},
2016-02-10 21:54:54 +00:00
LogConfig: &structs.LogConfig{
MaxFiles: 10,
MaxFileSizeMB: 10,
},
Resources: basicResources,
2015-09-01 21:57:21 +00:00
}
2015-09-25 23:49:14 +00:00
ctx := testDriverContexts(t, task)
defer ctx.AllocDir.Destroy()
d := NewJavaDriver(ctx.DriverCtx)
2015-09-25 23:49:14 +00:00
2016-03-15 20:28:25 +00:00
// Copy the test jar into the task's directory
dst := ctx.ExecCtx.TaskDir.Dir
2016-03-15 20:28:25 +00:00
copyFile("./test-resources/java/demoapp.jar", filepath.Join(dst, "demoapp.jar"), t)
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
t.Fatalf("prestart err: %v", err)
}
resp, err := d.Start(ctx.ExecCtx, task)
2015-09-01 21:57:21 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
// Task should terminate after 1 seconds
2015-09-01 21:57:21 +00:00
select {
case res := <-resp.Handle.WaitCh():
if !res.Successful() {
t.Fatal("err: %v", res.String())
2015-09-01 21:57:21 +00:00
}
case <-time.After(5 * time.Second):
t.Fatalf("timeout")
2015-09-01 21:57:21 +00:00
}
2015-09-03 14:49:39 +00:00
2016-02-18 05:20:22 +00:00
// Get the stdout of the process and assrt that it's not empty
stdout := filepath.Join(ctx.ExecCtx.TaskDir.LogDir, "demo-app.stdout.0")
2016-02-18 05:20:22 +00:00
fInfo, err := os.Stat(stdout)
if err != nil {
t.Fatalf("failed to get stdout of process: %v", err)
}
if fInfo.Size() == 0 {
t.Fatalf("stdout of process is empty")
}
2015-09-03 14:49:39 +00:00
// need to kill long lived process
err = resp.Handle.Kill()
2015-09-03 14:49:39 +00:00
if err != nil {
t.Fatalf("Error: %s", err)
}
2015-09-01 21:57:21 +00:00
}
func TestJavaDriver_Start_Kill_Wait(t *testing.T) {
if !testutil.IsTravis() {
t.Parallel()
}
if !javaLocated() {
t.Skip("Java not found; skipping")
}
ctestutils.JavaCompatible(t)
2015-09-01 21:57:21 +00:00
task := &structs.Task{
Name: "demo-app",
Driver: "java",
Config: map[string]interface{}{
2016-03-15 20:28:25 +00:00
"jar_path": "demoapp.jar",
2015-09-01 21:57:21 +00:00
},
2016-02-10 21:54:54 +00:00
LogConfig: &structs.LogConfig{
MaxFiles: 10,
MaxFileSizeMB: 10,
},
Resources: basicResources,
2015-09-01 21:57:21 +00:00
}
2015-09-25 23:49:14 +00:00
ctx := testDriverContexts(t, task)
defer ctx.AllocDir.Destroy()
d := NewJavaDriver(ctx.DriverCtx)
2015-09-25 23:49:14 +00:00
2016-03-15 20:28:25 +00:00
// Copy the test jar into the task's directory
dst := ctx.ExecCtx.TaskDir.Dir
2016-03-15 20:28:25 +00:00
copyFile("./test-resources/java/demoapp.jar", filepath.Join(dst, "demoapp.jar"), t)
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
t.Fatalf("prestart err: %v", err)
}
resp, err := d.Start(ctx.ExecCtx, task)
2015-09-01 21:57:21 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
go func() {
time.Sleep(100 * time.Millisecond)
err := resp.Handle.Kill()
2015-09-01 21:57:21 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
}()
// Task should terminate quickly
select {
case res := <-resp.Handle.WaitCh():
2016-10-10 18:46:27 +00:00
if res.Successful() {
t.Fatal("should err")
}
case <-time.After(time.Duration(testutil.TestMultiplier()*10) * time.Second):
t.Fatalf("timeout")
// Need to kill long lived process
if err = resp.Handle.Kill(); err != nil {
2016-10-10 18:46:27 +00:00
t.Fatalf("Error: %s", err)
}
}
}
func TestJavaDriver_Signal(t *testing.T) {
if !testutil.IsTravis() {
t.Parallel()
}
2016-10-10 18:46:27 +00:00
if !javaLocated() {
t.Skip("Java not found; skipping")
}
ctestutils.JavaCompatible(t)
task := &structs.Task{
Name: "demo-app",
Driver: "java",
2016-10-10 18:46:27 +00:00
Config: map[string]interface{}{
"jar_path": "demoapp.jar",
},
LogConfig: &structs.LogConfig{
MaxFiles: 10,
MaxFileSizeMB: 10,
},
Resources: basicResources,
}
ctx := testDriverContexts(t, task)
defer ctx.AllocDir.Destroy()
d := NewJavaDriver(ctx.DriverCtx)
2016-10-10 18:46:27 +00:00
// Copy the test jar into the task's directory
dst := ctx.ExecCtx.TaskDir.Dir
2016-10-10 18:46:27 +00:00
copyFile("./test-resources/java/demoapp.jar", filepath.Join(dst, "demoapp.jar"), t)
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
t.Fatalf("prestart err: %v", err)
}
resp, err := d.Start(ctx.ExecCtx, task)
2016-10-10 18:46:27 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
go func() {
time.Sleep(100 * time.Millisecond)
err := resp.Handle.Signal(syscall.SIGHUP)
2016-10-10 18:46:27 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
}()
// Task should terminate quickly
select {
case res := <-resp.Handle.WaitCh():
if res.Successful() {
2015-09-26 00:55:29 +00:00
t.Fatal("should err")
2015-09-01 21:57:21 +00:00
}
2016-01-21 23:24:24 +00:00
case <-time.After(time.Duration(testutil.TestMultiplier()*10) * time.Second):
2015-09-01 21:57:21 +00:00
t.Fatalf("timeout")
2015-09-03 14:49:39 +00:00
2016-02-17 18:04:19 +00:00
// Need to kill long lived process
if err = resp.Handle.Kill(); err != nil {
2016-02-17 18:04:19 +00:00
t.Fatalf("Error: %s", err)
}
2015-09-03 14:49:39 +00:00
}
2015-09-01 21:57:21 +00:00
}
func TestJavaDriver_User(t *testing.T) {
if !testutil.IsTravis() {
t.Parallel()
}
if !javaLocated() {
t.Skip("Java not found; skipping")
}
2017-07-23 02:27:20 +00:00
if runtime.GOOS != "linux" {
t.Skip("Linux only test")
}
ctestutils.JavaCompatible(t)
task := &structs.Task{
Name: "demo-app",
Driver: "java",
User: "alice",
Config: map[string]interface{}{
"jar_path": "demoapp.jar",
},
LogConfig: &structs.LogConfig{
MaxFiles: 10,
MaxFileSizeMB: 10,
},
Resources: basicResources,
}
ctx := testDriverContexts(t, task)
defer ctx.AllocDir.Destroy()
d := NewJavaDriver(ctx.DriverCtx)
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
t.Fatalf("prestart err: %v", err)
}
resp, err := d.Start(ctx.ExecCtx, task)
if err == nil {
resp.Handle.Kill()
t.Fatalf("Should've failed")
}
msg := "user alice"
if !strings.Contains(err.Error(), msg) {
t.Fatalf("Expecting '%v' in '%v'", msg, err)
}
}
func TestJavaDriver_Start_Wait_Class(t *testing.T) {
if !testutil.IsTravis() {
t.Parallel()
}
if !javaLocated() {
t.Skip("Java not found; skipping")
}
ctestutils.JavaCompatible(t)
task := &structs.Task{
Name: "demo-app",
Driver: "java",
Config: map[string]interface{}{
"class_path": "${NOMAD_TASK_DIR}",
"class": "Hello",
"args": []string{"1"},
},
LogConfig: &structs.LogConfig{
MaxFiles: 10,
MaxFileSizeMB: 10,
},
Resources: basicResources,
}
ctx := testDriverContexts(t, task)
2017-01-20 22:12:30 +00:00
defer ctx.AllocDir.Destroy()
d := NewJavaDriver(ctx.DriverCtx)
// Copy the test jar into the task's directory
dst := ctx.ExecCtx.TaskDir.LocalDir
copyFile("./test-resources/java/Hello.class", filepath.Join(dst, "Hello.class"), t)
2017-01-20 22:12:30 +00:00
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
t.Fatalf("prestart err: %v", err)
}
resp, err := d.Start(ctx.ExecCtx, task)
if err != nil {
t.Fatalf("err: %v", err)
}
// Task should terminate after 1 seconds
select {
case res := <-resp.Handle.WaitCh():
if !res.Successful() {
t.Fatal("err: %v", res.String())
}
case <-time.After(5 * time.Second):
t.Fatalf("timeout")
}
// Get the stdout of the process and assrt that it's not empty
stdout := filepath.Join(ctx.ExecCtx.TaskDir.LogDir, "demo-app.stdout.0")
fInfo, err := os.Stat(stdout)
if err != nil {
t.Fatalf("failed to get stdout of process: %v", err)
}
if fInfo.Size() == 0 {
t.Fatalf("stdout of process is empty")
}
// need to kill long lived process
if err := resp.Handle.Kill(); err != nil {
t.Fatalf("Error: %s", err)
}
}