github.com/ranjib/nomad@v0.1.1-0.20160225204057-97751b02f70b/client/driver/java_test.go (about)

     1  package driver
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"path/filepath"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/hashicorp/nomad/client/config"
    11  	"github.com/hashicorp/nomad/nomad/structs"
    12  	"github.com/hashicorp/nomad/testutil"
    13  
    14  	ctestutils "github.com/hashicorp/nomad/client/testutil"
    15  )
    16  
    17  // javaLocated checks whether java is installed so we can run java stuff.
    18  func javaLocated() bool {
    19  	_, err := exec.Command("java", "-version").CombinedOutput()
    20  	return err == nil
    21  }
    22  
    23  // The fingerprinter test should always pass, even if Java is not installed.
    24  func TestJavaDriver_Fingerprint(t *testing.T) {
    25  	t.Parallel()
    26  	ctestutils.JavaCompatible(t)
    27  	driverCtx, _ := testDriverContexts(&structs.Task{Name: "foo"})
    28  	d := NewJavaDriver(driverCtx)
    29  	node := &structs.Node{
    30  		Attributes: map[string]string{
    31  			"unique.cgroup.mountpoint": "/sys/fs/cgroups",
    32  		},
    33  	}
    34  	apply, err := d.Fingerprint(&config.Config{}, node)
    35  	if err != nil {
    36  		t.Fatalf("err: %v", err)
    37  	}
    38  	if apply != javaLocated() {
    39  		t.Fatalf("Fingerprinter should detect Java when it is installed")
    40  	}
    41  	if node.Attributes["driver.java"] != "1" {
    42  		t.Fatalf("missing driver")
    43  	}
    44  	for _, key := range []string{"driver.java.version", "driver.java.runtime", "driver.java.vm"} {
    45  		if node.Attributes[key] == "" {
    46  			t.Fatalf("missing driver key (%s)", key)
    47  		}
    48  	}
    49  }
    50  
    51  func TestJavaDriver_StartOpen_Wait(t *testing.T) {
    52  	t.Parallel()
    53  	if !javaLocated() {
    54  		t.Skip("Java not found; skipping")
    55  	}
    56  
    57  	ctestutils.JavaCompatible(t)
    58  	task := &structs.Task{
    59  		Name: "demo-app",
    60  		Config: map[string]interface{}{
    61  			"artifact_source": "https://dl.dropboxusercontent.com/u/47675/jar_thing/demoapp.jar",
    62  			"jvm_options":     []string{"-Xmx64m", "-Xms32m"},
    63  			"checksum":        "sha256:58d6e8130308d32e197c5108edd4f56ddf1417408f743097c2e662df0f0b17c8",
    64  		},
    65  		LogConfig: &structs.LogConfig{
    66  			MaxFiles:      10,
    67  			MaxFileSizeMB: 10,
    68  		},
    69  		Resources: basicResources,
    70  	}
    71  
    72  	driverCtx, execCtx := testDriverContexts(task)
    73  	defer execCtx.AllocDir.Destroy()
    74  	d := NewJavaDriver(driverCtx)
    75  
    76  	handle, err := d.Start(execCtx, task)
    77  	if err != nil {
    78  		t.Fatalf("err: %v", err)
    79  	}
    80  	if handle == nil {
    81  		t.Fatalf("missing handle")
    82  	}
    83  
    84  	// Attempt to open
    85  	handle2, err := d.Open(execCtx, handle.ID())
    86  	if err != nil {
    87  		t.Fatalf("err: %v", err)
    88  	}
    89  	if handle2 == nil {
    90  		t.Fatalf("missing handle")
    91  	}
    92  
    93  	time.Sleep(2 * time.Second)
    94  
    95  	// There is a race condition between the handle waiting and killing. One
    96  	// will return an error.
    97  	handle.Kill()
    98  	handle2.Kill()
    99  }
   100  
   101  func TestJavaDriver_Start_Wait(t *testing.T) {
   102  	t.Parallel()
   103  	if !javaLocated() {
   104  		t.Skip("Java not found; skipping")
   105  	}
   106  
   107  	ctestutils.JavaCompatible(t)
   108  	task := &structs.Task{
   109  		Name: "demo-app",
   110  		Config: map[string]interface{}{
   111  			"artifact_source": "https://dl.dropboxusercontent.com/u/47675/jar_thing/demoapp.jar",
   112  			"checksum":        "sha256:58d6e8130308d32e197c5108edd4f56ddf1417408f743097c2e662df0f0b17c8",
   113  		},
   114  		LogConfig: &structs.LogConfig{
   115  			MaxFiles:      10,
   116  			MaxFileSizeMB: 10,
   117  		},
   118  		Resources: basicResources,
   119  	}
   120  
   121  	driverCtx, execCtx := testDriverContexts(task)
   122  	defer execCtx.AllocDir.Destroy()
   123  	d := NewJavaDriver(driverCtx)
   124  
   125  	handle, err := d.Start(execCtx, task)
   126  	if err != nil {
   127  		t.Fatalf("err: %v", err)
   128  	}
   129  	if handle == nil {
   130  		t.Fatalf("missing handle")
   131  	}
   132  
   133  	// Task should terminate quickly
   134  	select {
   135  	case res := <-handle.WaitCh():
   136  		if !res.Successful() {
   137  			t.Fatalf("err: %v", res)
   138  		}
   139  	case <-time.After(time.Duration(testutil.TestMultiplier()*5) * time.Second):
   140  		// expect the timeout b/c it's a long lived process
   141  		break
   142  	}
   143  
   144  	// Get the stdout of the process and assrt that it's not empty
   145  	stdout := filepath.Join(execCtx.AllocDir.LogDir(), "demo-app.stdout.0")
   146  	fInfo, err := os.Stat(stdout)
   147  	if err != nil {
   148  		t.Fatalf("failed to get stdout of process: %v", err)
   149  	}
   150  	if fInfo.Size() == 0 {
   151  		t.Fatalf("stdout of process is empty")
   152  	}
   153  
   154  	// need to kill long lived process
   155  	err = handle.Kill()
   156  	if err != nil {
   157  		t.Fatalf("Error: %s", err)
   158  	}
   159  }
   160  
   161  func TestJavaDriver_Start_Kill_Wait(t *testing.T) {
   162  	t.Parallel()
   163  	if !javaLocated() {
   164  		t.Skip("Java not found; skipping")
   165  	}
   166  
   167  	ctestutils.JavaCompatible(t)
   168  	task := &structs.Task{
   169  		Name: "demo-app",
   170  		Config: map[string]interface{}{
   171  			"artifact_source": "https://dl.dropboxusercontent.com/u/47675/jar_thing/demoapp.jar",
   172  		},
   173  		LogConfig: &structs.LogConfig{
   174  			MaxFiles:      10,
   175  			MaxFileSizeMB: 10,
   176  		},
   177  		Resources: basicResources,
   178  	}
   179  
   180  	driverCtx, execCtx := testDriverContexts(task)
   181  	defer execCtx.AllocDir.Destroy()
   182  	d := NewJavaDriver(driverCtx)
   183  
   184  	handle, err := d.Start(execCtx, task)
   185  	if err != nil {
   186  		t.Fatalf("err: %v", err)
   187  	}
   188  	if handle == nil {
   189  		t.Fatalf("missing handle")
   190  	}
   191  
   192  	go func() {
   193  		time.Sleep(100 * time.Millisecond)
   194  		err := handle.Kill()
   195  		if err != nil {
   196  			t.Fatalf("err: %v", err)
   197  		}
   198  	}()
   199  
   200  	// Task should terminate quickly
   201  	select {
   202  	case res := <-handle.WaitCh():
   203  		if res.Successful() {
   204  			t.Fatal("should err")
   205  		}
   206  	case <-time.After(time.Duration(testutil.TestMultiplier()*10) * time.Second):
   207  		t.Fatalf("timeout")
   208  
   209  		// Need to kill long lived process
   210  		if err = handle.Kill(); err != nil {
   211  			t.Fatalf("Error: %s", err)
   212  		}
   213  	}
   214  }