open-nomad/client/driver/java_test.go

192 lines
4.4 KiB
Go
Raw Normal View History

2015-09-01 21:57:21 +00:00
package driver
import (
"os/exec"
2015-09-01 21:57:21 +00:00
"testing"
"time"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/nomad/structs"
2015-09-25 23:49:14 +00:00
ctestutils "github.com/hashicorp/nomad/client/testutil"
2015-09-01 21:57:21 +00:00
)
// 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) {
2015-09-25 23:49:14 +00:00
ctestutils.ExecCompatible(t)
d := NewJavaDriver(testDriverContext(""))
2015-09-01 21:57:21 +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 != 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" {
2015-09-01 21:57:21 +00:00
t.Fatalf("missing driver")
}
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
}
2015-09-25 23:49:14 +00:00
/*
TODO: This test is disabled til a follow-up api changes the restore state interface.
The driver/executor interface will be changed from Open to Cleanup, in which
clean-up tears down previous allocs.
2015-09-01 21:57:21 +00:00
func TestJavaDriver_StartOpen_Wait(t *testing.T) {
2015-09-25 23:49:14 +00:00
ctestutils.ExecCompatible(t)
2015-09-01 21:57:21 +00:00
task := &structs.Task{
2015-09-25 23:49:14 +00:00
Name: "demo-app",
2015-09-01 21:57:21 +00:00
Config: map[string]string{
"jar_source": "https://dl.dropboxusercontent.com/u/47675/jar_thing/demoapp.jar",
// "jar_source": "https://s3-us-west-2.amazonaws.com/java-jar-thing/demoapp.jar",
// "args": "-d64",
},
Resources: basicResources,
2015-09-01 21:57:21 +00:00
}
2015-09-25 23:49:14 +00:00
driverCtx := testDriverContext(task.Name)
ctx := testDriverExecContext(task, driverCtx)
defer ctx.AllocDir.Destroy()
d := NewJavaDriver(driverCtx)
2015-09-01 21:57:21 +00:00
handle, err := d.Start(ctx, task)
if err != nil {
t.Fatalf("err: %v", err)
}
if handle == nil {
t.Fatalf("missing handle")
}
// Attempt to open
handle2, err := d.Open(ctx, handle.ID())
if err != nil {
t.Fatalf("err: %v", err)
}
if handle2 == nil {
t.Fatalf("missing handle")
}
time.Sleep(2 * time.Second)
// need to kill long lived process
err = handle.Kill()
if err != nil {
t.Fatalf("Error: %s", err)
}
}
2015-09-25 23:49:14 +00:00
*/
2015-09-01 21:57:21 +00:00
func TestJavaDriver_Start_Wait(t *testing.T) {
if !javaLocated() {
t.Skip("Java not found; skipping")
}
2015-09-25 23:49:14 +00:00
ctestutils.ExecCompatible(t)
2015-09-01 21:57:21 +00:00
task := &structs.Task{
2015-09-25 23:49:14 +00:00
Name: "demo-app",
2015-09-01 21:57:21 +00:00
Config: map[string]string{
"jar_source": "https://dl.dropboxusercontent.com/u/47675/jar_thing/demoapp.jar",
// "jar_source": "https://s3-us-west-2.amazonaws.com/java-jar-thing/demoapp.jar",
// "args": "-d64",
"jvm_options": "-Xmx2048m -Xms256m",
2015-09-01 21:57:21 +00:00
},
Resources: basicResources,
2015-09-01 21:57:21 +00:00
}
2015-09-25 23:49:14 +00:00
driverCtx := testDriverContext(task.Name)
ctx := testDriverExecContext(task, driverCtx)
defer ctx.AllocDir.Destroy()
d := NewJavaDriver(driverCtx)
2015-09-01 21:57:21 +00:00
handle, err := d.Start(ctx, task)
if err != nil {
t.Fatalf("err: %v", err)
}
if handle == nil {
t.Fatalf("missing handle")
}
// Task should terminate quickly
select {
case err := <-handle.WaitCh():
if err != nil {
t.Fatalf("err: %v", err)
}
case <-time.After(2 * time.Second):
// expect the timeout b/c it's a long lived process
break
}
2015-09-03 14:49:39 +00:00
// need to kill long lived process
err = handle.Kill()
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 !javaLocated() {
t.Skip("Java not found; skipping")
}
2015-09-25 23:49:14 +00:00
ctestutils.ExecCompatible(t)
2015-09-01 21:57:21 +00:00
task := &structs.Task{
2015-09-25 23:49:14 +00:00
Name: "demo-app",
2015-09-01 21:57:21 +00:00
Config: map[string]string{
"jar_source": "https://dl.dropboxusercontent.com/u/47675/jar_thing/demoapp.jar",
// "jar_source": "https://s3-us-west-2.amazonaws.com/java-jar-thing/demoapp.jar",
// "args": "-d64",
},
Resources: basicResources,
2015-09-01 21:57:21 +00:00
}
2015-09-25 23:49:14 +00:00
driverCtx := testDriverContext(task.Name)
ctx := testDriverExecContext(task, driverCtx)
defer ctx.AllocDir.Destroy()
d := NewJavaDriver(driverCtx)
2015-09-01 21:57:21 +00:00
handle, err := d.Start(ctx, task)
if err != nil {
t.Fatalf("err: %v", err)
}
if handle == nil {
t.Fatalf("missing handle")
}
go func() {
time.Sleep(100 * time.Millisecond)
err := handle.Kill()
if err != nil {
t.Fatalf("err: %v", err)
}
}()
// Task should terminate quickly
select {
case err := <-handle.WaitCh():
if err == nil {
2015-09-26 00:55:29 +00:00
t.Fatal("should err")
2015-09-01 21:57:21 +00:00
}
case <-time.After(2 * time.Second):
t.Fatalf("timeout")
}
2015-09-03 14:49:39 +00:00
// need to kill long lived process
err = handle.Kill()
if err != nil {
t.Fatalf("Error: %s", err)
}
2015-09-01 21:57:21 +00:00
}