2018-10-31 16:19:07 +00:00
|
|
|
package java
|
|
|
|
|
|
|
|
import (
|
2018-12-05 00:35:20 +00:00
|
|
|
"fmt"
|
2018-10-31 16:19:07 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2018-11-27 19:03:58 +00:00
|
|
|
dtestutil "github.com/hashicorp/nomad/plugins/drivers/testutils"
|
|
|
|
|
2018-10-31 16:19:07 +00:00
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/hcl2/hcl"
|
|
|
|
ctestutil "github.com/hashicorp/nomad/client/testutil"
|
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
|
|
|
"github.com/hashicorp/nomad/plugins/shared"
|
|
|
|
"github.com/hashicorp/nomad/plugins/shared/hclspec"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func javaCompatible(t *testing.T) {
|
|
|
|
ctestutil.JavaCompatible(t)
|
|
|
|
|
|
|
|
_, _, _, err := javaVersionInfo()
|
|
|
|
if err != nil {
|
|
|
|
t.Skipf("java not found; skipping: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 03:13:27 +00:00
|
|
|
func TestJavaDriver_Fingerprint(t *testing.T) {
|
2018-10-31 16:19:07 +00:00
|
|
|
javaCompatible(t)
|
|
|
|
if !testutil.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
|
|
|
|
d := NewDriver(testlog.HCLogger(t))
|
2018-11-27 19:03:58 +00:00
|
|
|
harness := dtestutil.NewDriverHarness(t, d)
|
2018-10-31 16:19:07 +00:00
|
|
|
|
|
|
|
fpCh, err := harness.Fingerprint(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case fp := <-fpCh:
|
|
|
|
require.Equal(t, drivers.HealthStateHealthy, fp.Health)
|
2018-11-21 00:30:39 +00:00
|
|
|
detected, _ := fp.Attributes["driver.java"].GetBool()
|
|
|
|
require.True(t, detected)
|
2018-10-31 16:19:07 +00:00
|
|
|
case <-time.After(time.Duration(testutil.TestMultiplier()*5) * time.Second):
|
|
|
|
require.Fail(t, "timeout receiving fingerprint")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 03:13:27 +00:00
|
|
|
func TestJavaDriver_Jar_Start_Wait(t *testing.T) {
|
2018-10-31 16:19:07 +00:00
|
|
|
javaCompatible(t)
|
|
|
|
if !testutil.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
|
|
|
|
require := require.New(t)
|
|
|
|
d := NewDriver(testlog.HCLogger(t))
|
2018-11-27 19:03:58 +00:00
|
|
|
harness := dtestutil.NewDriverHarness(t, d)
|
2018-10-31 16:19:07 +00:00
|
|
|
|
|
|
|
task := basicTask(t, "demo-app", map[string]interface{}{
|
|
|
|
"jar_path": "demoapp.jar",
|
|
|
|
"args": []string{"1"},
|
|
|
|
"jvm_options": []string{"-Xmx64m", "-Xms32m"},
|
|
|
|
})
|
|
|
|
|
|
|
|
cleanup := harness.MkAllocDir(task, true)
|
|
|
|
defer cleanup()
|
|
|
|
|
2018-11-08 02:00:34 +00:00
|
|
|
copyFile("./test-resources/demoapp.jar", filepath.Join(task.TaskDir().Dir, "demoapp.jar"), t)
|
2018-10-31 16:19:07 +00:00
|
|
|
|
|
|
|
handle, _, err := harness.StartTask(task)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
ch, err := harness.WaitTask(context.Background(), handle.Config.ID)
|
|
|
|
require.NoError(err)
|
|
|
|
result := <-ch
|
|
|
|
require.Nil(result.Err)
|
|
|
|
|
|
|
|
require.Zero(result.ExitCode)
|
|
|
|
|
|
|
|
// Get the stdout of the process and assert that it's not empty
|
|
|
|
stdout, err := ioutil.ReadFile(filepath.Join(task.TaskDir().LogDir, "demo-app.stdout.0"))
|
|
|
|
require.NoError(err)
|
|
|
|
require.Contains(string(stdout), "Hello")
|
|
|
|
|
|
|
|
require.NoError(harness.DestroyTask(task.ID, true))
|
|
|
|
}
|
|
|
|
|
2018-11-08 03:13:27 +00:00
|
|
|
func TestJavaDriver_Jar_Stop_Wait(t *testing.T) {
|
2018-10-31 16:19:07 +00:00
|
|
|
javaCompatible(t)
|
|
|
|
if !testutil.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
|
|
|
|
require := require.New(t)
|
|
|
|
d := NewDriver(testlog.HCLogger(t))
|
2018-11-27 19:03:58 +00:00
|
|
|
harness := dtestutil.NewDriverHarness(t, d)
|
2018-10-31 16:19:07 +00:00
|
|
|
|
|
|
|
task := basicTask(t, "demo-app", map[string]interface{}{
|
|
|
|
"jar_path": "demoapp.jar",
|
2018-12-05 00:35:20 +00:00
|
|
|
"args": []string{"600"},
|
2018-10-31 16:19:07 +00:00
|
|
|
"jvm_options": []string{"-Xmx64m", "-Xms32m"},
|
|
|
|
})
|
|
|
|
|
|
|
|
cleanup := harness.MkAllocDir(task, true)
|
|
|
|
defer cleanup()
|
|
|
|
|
2018-11-08 02:00:34 +00:00
|
|
|
copyFile("./test-resources/demoapp.jar", filepath.Join(task.TaskDir().Dir, "demoapp.jar"), t)
|
2018-10-31 16:19:07 +00:00
|
|
|
|
|
|
|
handle, _, err := harness.StartTask(task)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
ch, err := harness.WaitTask(context.Background(), handle.Config.ID)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
require.NoError(harness.WaitUntilStarted(task.ID, 1*time.Second))
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
2018-12-05 00:35:20 +00:00
|
|
|
harness.StopTask(task.ID, 2*time.Second, "SIGINT")
|
2018-10-31 16:19:07 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2018-12-05 00:35:20 +00:00
|
|
|
case result := <-ch:
|
|
|
|
require.False(result.Successful())
|
|
|
|
case <-time.After(10 * time.Second):
|
2018-10-31 16:19:07 +00:00
|
|
|
require.Fail("timeout waiting for task to shutdown")
|
|
|
|
}
|
|
|
|
|
2018-12-05 00:35:20 +00:00
|
|
|
// Ensure that the task is marked as dead, but account
|
|
|
|
// for WaitTask() closing channel before internal state is updated
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
status, err := harness.InspectTask(task.ID)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("inspecting task failed: %v", err)
|
|
|
|
}
|
|
|
|
if status.State != drivers.TaskStateExited {
|
|
|
|
return false, fmt.Errorf("task hasn't exited yet; status: %v", status.State)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
require.NoError(err)
|
|
|
|
})
|
|
|
|
|
2018-10-31 16:19:07 +00:00
|
|
|
require.NoError(harness.DestroyTask(task.ID, true))
|
|
|
|
}
|
|
|
|
|
2018-11-08 03:13:27 +00:00
|
|
|
func TestJavaDriver_Class_Start_Wait(t *testing.T) {
|
2018-10-31 16:19:07 +00:00
|
|
|
javaCompatible(t)
|
|
|
|
if !testutil.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
|
|
|
|
require := require.New(t)
|
|
|
|
d := NewDriver(testlog.HCLogger(t))
|
2018-11-27 19:03:58 +00:00
|
|
|
harness := dtestutil.NewDriverHarness(t, d)
|
2018-10-31 16:19:07 +00:00
|
|
|
|
|
|
|
task := basicTask(t, "demo-app", map[string]interface{}{
|
2018-11-08 02:00:34 +00:00
|
|
|
"class": "Hello",
|
|
|
|
"args": []string{"1"},
|
2018-10-31 16:19:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
cleanup := harness.MkAllocDir(task, true)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
copyFile("./test-resources/Hello.class", filepath.Join(task.TaskDir().Dir, "Hello.class"), t)
|
|
|
|
|
|
|
|
handle, _, err := harness.StartTask(task)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
ch, err := harness.WaitTask(context.Background(), handle.Config.ID)
|
|
|
|
require.NoError(err)
|
|
|
|
result := <-ch
|
|
|
|
require.Nil(result.Err)
|
|
|
|
|
|
|
|
require.Zero(result.ExitCode)
|
|
|
|
|
|
|
|
// Get the stdout of the process and assert that it's not empty
|
|
|
|
stdout, err := ioutil.ReadFile(filepath.Join(task.TaskDir().LogDir, "demo-app.stdout.0"))
|
|
|
|
require.NoError(err)
|
|
|
|
require.Contains(string(stdout), "Hello")
|
|
|
|
|
|
|
|
require.NoError(harness.DestroyTask(task.ID, true))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJavaCmdArgs(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
cfg TaskConfig
|
|
|
|
expected []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"jar_path_full",
|
|
|
|
TaskConfig{
|
|
|
|
JvmOpts: []string{"-Xmx512m", "-Xms128m"},
|
|
|
|
JarPath: "/jar-path.jar",
|
|
|
|
Args: []string{"hello", "world"},
|
|
|
|
},
|
|
|
|
[]string{"-Xmx512m", "-Xms128m", "-jar", "/jar-path.jar", "hello", "world"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"class_full",
|
|
|
|
TaskConfig{
|
|
|
|
JvmOpts: []string{"-Xmx512m", "-Xms128m"},
|
|
|
|
Class: "ClassName",
|
|
|
|
ClassPath: "/classpath",
|
|
|
|
Args: []string{"hello", "world"},
|
|
|
|
},
|
|
|
|
[]string{"-Xmx512m", "-Xms128m", "-cp", "/classpath", "ClassName", "hello", "world"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"jar_path_slim",
|
|
|
|
TaskConfig{
|
|
|
|
JarPath: "/jar-path.jar",
|
|
|
|
},
|
|
|
|
[]string{"-jar", "/jar-path.jar"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"class_slim",
|
|
|
|
TaskConfig{
|
|
|
|
Class: "ClassName",
|
|
|
|
},
|
|
|
|
[]string{"ClassName"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
found := javaCmdArgs(c.cfg)
|
|
|
|
require.Equal(t, c.expected, found)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func basicTask(t *testing.T, name string, taskConfig map[string]interface{}) *drivers.TaskConfig {
|
2018-11-08 02:00:34 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2018-10-31 16:19:07 +00:00
|
|
|
task := &drivers.TaskConfig{
|
|
|
|
ID: uuid.Generate(),
|
|
|
|
Name: name,
|
|
|
|
Resources: &drivers.Resources{
|
|
|
|
NomadResources: &structs.Resources{
|
|
|
|
MemoryMB: 128,
|
|
|
|
CPU: 100,
|
|
|
|
},
|
|
|
|
LinuxResources: &drivers.LinuxResources{
|
|
|
|
MemoryLimitBytes: 134217728,
|
|
|
|
CPUShares: 100,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
encodeDriverHelper(t, task, taskConfig)
|
|
|
|
return task
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeDriverHelper(t *testing.T, task *drivers.TaskConfig, taskConfig map[string]interface{}) {
|
2018-11-08 02:00:34 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2018-10-31 16:19:07 +00:00
|
|
|
evalCtx := &hcl.EvalContext{
|
|
|
|
Functions: shared.GetStdlibFuncs(),
|
|
|
|
}
|
|
|
|
spec, diag := hclspec.Convert(taskConfigSpec)
|
|
|
|
require.False(t, diag.HasErrors())
|
|
|
|
taskConfigCtyVal, diag := shared.ParseHclInterface(taskConfig, spec, evalCtx)
|
2018-11-08 02:00:34 +00:00
|
|
|
require.Empty(t, diag.Errs())
|
2018-10-31 16:19:07 +00:00
|
|
|
err := task.EncodeDriverConfig(taskConfigCtyVal)
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// copyFile moves an existing file to the destination
|
|
|
|
func copyFile(src, dst string, t *testing.T) {
|
|
|
|
in, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("copying %v -> %v failed: %v", src, dst, err)
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
out, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("copying %v -> %v failed: %v", src, dst, err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := out.Close(); err != nil {
|
|
|
|
t.Fatalf("copying %v -> %v failed: %v", src, dst, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if _, err = io.Copy(out, in); err != nil {
|
|
|
|
t.Fatalf("copying %v -> %v failed: %v", src, dst, err)
|
|
|
|
}
|
|
|
|
if err := out.Sync(); err != nil {
|
|
|
|
t.Fatalf("copying %v -> %v failed: %v", src, dst, err)
|
|
|
|
}
|
|
|
|
}
|