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