github.com/jmitchell/nomad@v0.1.3-0.20151007230021-7ab84c2862d8/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 11 ctestutils "github.com/hashicorp/nomad/client/testutil" 12 ) 13 14 // javaLocated checks whether java is installed so we can run java stuff. 15 func javaLocated() bool { 16 _, err := exec.Command("java", "-version").CombinedOutput() 17 return err == nil 18 } 19 20 // The fingerprinter test should always pass, even if Java is not installed. 21 func TestJavaDriver_Fingerprint(t *testing.T) { 22 ctestutils.ExecCompatible(t) 23 d := NewJavaDriver(testDriverContext("")) 24 node := &structs.Node{ 25 Attributes: make(map[string]string), 26 } 27 apply, err := d.Fingerprint(&config.Config{}, node) 28 if err != nil { 29 t.Fatalf("err: %v", err) 30 } 31 if apply != javaLocated() { 32 t.Fatalf("Fingerprinter should detect Java when it is installed") 33 } 34 if node.Attributes["driver.java"] != "1" { 35 t.Fatalf("missing driver") 36 } 37 for _, key := range []string{"driver.java.version", "driver.java.runtime", "driver.java.vm"} { 38 if node.Attributes[key] == "" { 39 t.Fatalf("missing driver key (%s)", key) 40 } 41 } 42 } 43 44 /* 45 TODO: This test is disabled til a follow-up api changes the restore state interface. 46 The driver/executor interface will be changed from Open to Cleanup, in which 47 clean-up tears down previous allocs. 48 func TestJavaDriver_StartOpen_Wait(t *testing.T) { 49 ctestutils.ExecCompatible(t) 50 task := &structs.Task{ 51 Name: "demo-app", 52 Config: map[string]string{ 53 "jar_source": "https://dl.dropboxusercontent.com/u/47675/jar_thing/demoapp.jar", 54 // "jar_source": "https://s3-us-west-2.amazonaws.com/java-jar-thing/demoapp.jar", 55 // "args": "-d64", 56 }, 57 Resources: basicResources, 58 } 59 60 driverCtx := testDriverContext(task.Name) 61 ctx := testDriverExecContext(task, driverCtx) 62 defer ctx.AllocDir.Destroy() 63 d := NewJavaDriver(driverCtx) 64 65 handle, err := d.Start(ctx, task) 66 if err != nil { 67 t.Fatalf("err: %v", err) 68 } 69 if handle == nil { 70 t.Fatalf("missing handle") 71 } 72 73 // Attempt to open 74 handle2, err := d.Open(ctx, handle.ID()) 75 if err != nil { 76 t.Fatalf("err: %v", err) 77 } 78 if handle2 == nil { 79 t.Fatalf("missing handle") 80 } 81 82 time.Sleep(2 * time.Second) 83 // need to kill long lived process 84 err = handle.Kill() 85 if err != nil { 86 t.Fatalf("Error: %s", err) 87 } 88 } 89 */ 90 91 func TestJavaDriver_Start_Wait(t *testing.T) { 92 if !javaLocated() { 93 t.Skip("Java not found; skipping") 94 } 95 96 ctestutils.ExecCompatible(t) 97 task := &structs.Task{ 98 Name: "demo-app", 99 Config: map[string]string{ 100 "jar_source": "https://dl.dropboxusercontent.com/u/47675/jar_thing/demoapp.jar", 101 // "jar_source": "https://s3-us-west-2.amazonaws.com/java-jar-thing/demoapp.jar", 102 // "args": "-d64", 103 }, 104 Resources: basicResources, 105 } 106 107 driverCtx := testDriverContext(task.Name) 108 ctx := testDriverExecContext(task, driverCtx) 109 defer ctx.AllocDir.Destroy() 110 d := NewJavaDriver(driverCtx) 111 112 handle, err := d.Start(ctx, 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 err := <-handle.WaitCh(): 123 if err != nil { 124 t.Fatalf("err: %v", err) 125 } 126 case <-time.After(2 * 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 if !javaLocated() { 140 t.Skip("Java not found; skipping") 141 } 142 143 ctestutils.ExecCompatible(t) 144 task := &structs.Task{ 145 Name: "demo-app", 146 Config: map[string]string{ 147 "jar_source": "https://dl.dropboxusercontent.com/u/47675/jar_thing/demoapp.jar", 148 // "jar_source": "https://s3-us-west-2.amazonaws.com/java-jar-thing/demoapp.jar", 149 // "args": "-d64", 150 }, 151 Resources: basicResources, 152 } 153 154 driverCtx := testDriverContext(task.Name) 155 ctx := testDriverExecContext(task, driverCtx) 156 defer ctx.AllocDir.Destroy() 157 d := NewJavaDriver(driverCtx) 158 159 handle, err := d.Start(ctx, task) 160 if err != nil { 161 t.Fatalf("err: %v", err) 162 } 163 if handle == nil { 164 t.Fatalf("missing handle") 165 } 166 167 go func() { 168 time.Sleep(100 * time.Millisecond) 169 err := handle.Kill() 170 if err != nil { 171 t.Fatalf("err: %v", err) 172 } 173 }() 174 175 // Task should terminate quickly 176 select { 177 case err := <-handle.WaitCh(): 178 if err == nil { 179 t.Fatal("should err") 180 } 181 case <-time.After(2 * time.Second): 182 t.Fatalf("timeout") 183 } 184 185 // need to kill long lived process 186 err = handle.Kill() 187 if err != nil { 188 t.Fatalf("Error: %s", err) 189 } 190 }