github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/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 "jvm_options": "-Xmx2048m -Xms256m", 104 }, 105 Resources: basicResources, 106 } 107 108 driverCtx := testDriverContext(task.Name) 109 ctx := testDriverExecContext(task, driverCtx) 110 defer ctx.AllocDir.Destroy() 111 d := NewJavaDriver(driverCtx) 112 113 handle, err := d.Start(ctx, task) 114 if err != nil { 115 t.Fatalf("err: %v", err) 116 } 117 if handle == nil { 118 t.Fatalf("missing handle") 119 } 120 121 // Task should terminate quickly 122 select { 123 case err := <-handle.WaitCh(): 124 if err != nil { 125 t.Fatalf("err: %v", err) 126 } 127 case <-time.After(2 * time.Second): 128 // expect the timeout b/c it's a long lived process 129 break 130 } 131 132 // need to kill long lived process 133 err = handle.Kill() 134 if err != nil { 135 t.Fatalf("Error: %s", err) 136 } 137 } 138 139 func TestJavaDriver_Start_Kill_Wait(t *testing.T) { 140 if !javaLocated() { 141 t.Skip("Java not found; skipping") 142 } 143 144 ctestutils.ExecCompatible(t) 145 task := &structs.Task{ 146 Name: "demo-app", 147 Config: map[string]string{ 148 "jar_source": "https://dl.dropboxusercontent.com/u/47675/jar_thing/demoapp.jar", 149 // "jar_source": "https://s3-us-west-2.amazonaws.com/java-jar-thing/demoapp.jar", 150 // "args": "-d64", 151 }, 152 Resources: basicResources, 153 } 154 155 driverCtx := testDriverContext(task.Name) 156 ctx := testDriverExecContext(task, driverCtx) 157 defer ctx.AllocDir.Destroy() 158 d := NewJavaDriver(driverCtx) 159 160 handle, err := d.Start(ctx, task) 161 if err != nil { 162 t.Fatalf("err: %v", err) 163 } 164 if handle == nil { 165 t.Fatalf("missing handle") 166 } 167 168 go func() { 169 time.Sleep(100 * time.Millisecond) 170 err := handle.Kill() 171 if err != nil { 172 t.Fatalf("err: %v", err) 173 } 174 }() 175 176 // Task should terminate quickly 177 select { 178 case err := <-handle.WaitCh(): 179 if err == nil { 180 t.Fatal("should err") 181 } 182 case <-time.After(2 * time.Second): 183 t.Fatalf("timeout") 184 } 185 186 // need to kill long lived process 187 err = handle.Kill() 188 if err != nil { 189 t.Fatalf("Error: %s", err) 190 } 191 }