2015-09-01 21:57:21 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
2016-02-18 05:20:22 +00:00
|
|
|
"os"
|
2015-09-25 23:49:26 +00:00
|
|
|
"os/exec"
|
2016-02-18 05:20:22 +00:00
|
|
|
"path/filepath"
|
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
|
|
|
)
|
|
|
|
|
2015-09-25 23:49:26 +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-11-24 20:12:31 +00:00
|
|
|
t.Parallel()
|
2015-11-03 20:47:48 +00:00
|
|
|
ctestutils.JavaCompatible(t)
|
2016-01-06 02:02:11 +00:00
|
|
|
driverCtx, _ := testDriverContexts(&structs.Task{Name: "foo"})
|
|
|
|
d := NewJavaDriver(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)
|
|
|
|
}
|
2015-10-06 23:33:02 +00:00
|
|
|
if apply != javaLocated() {
|
|
|
|
t.Fatalf("Fingerprinter should detect Java when it is installed")
|
2015-09-01 21:57:21 +00:00
|
|
|
}
|
2015-09-02 16:41:25 +00:00
|
|
|
if node.Attributes["driver.java"] != "1" {
|
2015-09-01 21:57:21 +00:00
|
|
|
t.Fatalf("missing driver")
|
|
|
|
}
|
2015-09-02 16:41:25 +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) {
|
2015-11-24 20:12:31 +00:00
|
|
|
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{
|
2015-09-25 23:49:14 +00:00
|
|
|
Name: "demo-app",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2015-11-11 00:59:52 +00:00
|
|
|
"artifact_source": "https://dl.dropboxusercontent.com/u/47675/jar_thing/demoapp.jar",
|
2015-11-18 23:16:42 +00:00
|
|
|
"jvm_options": []string{"-Xmx64m", "-Xms32m"},
|
2015-11-11 00:59:52 +00:00
|
|
|
"checksum": "sha256:58d6e8130308d32e197c5108edd4f56ddf1417408f743097c2e662df0f0b17c8",
|
2015-09-01 21:57:21 +00:00
|
|
|
},
|
2016-02-10 21:54:54 +00:00
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
2015-09-24 07:59:57 +00:00
|
|
|
Resources: basicResources,
|
2015-09-01 21:57:21 +00:00
|
|
|
}
|
2015-09-25 23:49:14 +00:00
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
driverCtx, execCtx := testDriverContexts(task)
|
|
|
|
defer execCtx.AllocDir.Destroy()
|
2015-09-25 23:49:14 +00:00
|
|
|
d := NewJavaDriver(driverCtx)
|
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
handle, err := d.Start(execCtx, task)
|
2015-09-01 21:57:21 +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-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)
|
2016-01-20 20:00:20 +00:00
|
|
|
|
|
|
|
// There is a race condition between the handle waiting and killing. One
|
|
|
|
// will return an error.
|
|
|
|
handle.Kill()
|
2016-02-04 21:22:38 +00:00
|
|
|
handle2.Kill()
|
2015-09-01 21:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJavaDriver_Start_Wait(t *testing.T) {
|
2015-11-24 20:12:31 +00:00
|
|
|
t.Parallel()
|
2015-09-25 23:49:26 +00:00
|
|
|
if !javaLocated() {
|
|
|
|
t.Skip("Java not found; skipping")
|
|
|
|
}
|
|
|
|
|
2015-11-03 20:47:48 +00:00
|
|
|
ctestutils.JavaCompatible(t)
|
2015-09-01 21:57:21 +00:00
|
|
|
task := &structs.Task{
|
2015-09-25 23:49:14 +00:00
|
|
|
Name: "demo-app",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2015-11-05 15:23:45 +00:00
|
|
|
"artifact_source": "https://dl.dropboxusercontent.com/u/47675/jar_thing/demoapp.jar",
|
|
|
|
"checksum": "sha256:58d6e8130308d32e197c5108edd4f56ddf1417408f743097c2e662df0f0b17c8",
|
2015-09-01 21:57:21 +00:00
|
|
|
},
|
2016-02-10 21:54:54 +00:00
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
2015-09-24 07:59:57 +00:00
|
|
|
Resources: basicResources,
|
2015-09-01 21:57:21 +00:00
|
|
|
}
|
2015-09-25 23:49:14 +00:00
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
driverCtx, execCtx := testDriverContexts(task)
|
|
|
|
defer execCtx.AllocDir.Destroy()
|
2015-09-25 23:49:14 +00:00
|
|
|
d := NewJavaDriver(driverCtx)
|
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
handle, err := d.Start(execCtx, task)
|
2015-09-01 21:57:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if handle == nil {
|
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Task should terminate quickly
|
|
|
|
select {
|
2015-11-14 06:07:13 +00:00
|
|
|
case res := <-handle.WaitCh():
|
|
|
|
if !res.Successful() {
|
|
|
|
t.Fatalf("err: %v", res)
|
2015-09-01 21:57:21 +00:00
|
|
|
}
|
2016-01-21 23:24:24 +00:00
|
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()*5) * time.Second):
|
2015-09-01 21:57:21 +00:00
|
|
|
// expect the timeout b/c it's a long lived process
|
|
|
|
break
|
|
|
|
}
|
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
|
2016-02-25 07:07:06 +00:00
|
|
|
stdout := filepath.Join(execCtx.AllocDir.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 = 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) {
|
2015-11-24 20:12:31 +00:00
|
|
|
t.Parallel()
|
2015-09-25 23:49:26 +00:00
|
|
|
if !javaLocated() {
|
|
|
|
t.Skip("Java not found; skipping")
|
|
|
|
}
|
|
|
|
|
2015-11-03 20:47:48 +00:00
|
|
|
ctestutils.JavaCompatible(t)
|
2015-09-01 21:57:21 +00:00
|
|
|
task := &structs.Task{
|
2015-09-25 23:49:14 +00:00
|
|
|
Name: "demo-app",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2015-11-05 15:23:45 +00:00
|
|
|
"artifact_source": "https://dl.dropboxusercontent.com/u/47675/jar_thing/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,
|
|
|
|
},
|
2015-09-24 07:59:57 +00:00
|
|
|
Resources: basicResources,
|
2015-09-01 21:57:21 +00:00
|
|
|
}
|
2015-09-25 23:49:14 +00:00
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
driverCtx, execCtx := testDriverContexts(task)
|
|
|
|
defer execCtx.AllocDir.Destroy()
|
2015-09-25 23:49:14 +00:00
|
|
|
d := NewJavaDriver(driverCtx)
|
|
|
|
|
2016-01-06 02:02:11 +00:00
|
|
|
handle, err := d.Start(execCtx, task)
|
2015-09-01 21:57:21 +00:00
|
|
|
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 {
|
2015-11-14 06:07:13 +00:00
|
|
|
case res := <-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 = handle.Kill(); err != nil {
|
|
|
|
t.Fatalf("Error: %s", err)
|
|
|
|
}
|
2015-09-03 14:49:39 +00:00
|
|
|
}
|
2015-09-01 21:57:21 +00:00
|
|
|
}
|