-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathjava_main_test.go
More file actions
505 lines (429 loc) · 15.4 KB
/
Copy pathjava_main_test.go
File metadata and controls
505 lines (429 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package containers_test
import (
"archive/zip"
"bytes"
"os"
"path/filepath"
"github.com/cloudfoundry/java-buildpack/src/java/common"
"github.com/cloudfoundry/java-buildpack/src/java/containers"
"github.com/cloudfoundry/libbuildpack"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
// createJar writes a JAR file at jarPath containing META-INF/MANIFEST.MF with the given content.
func createJar(jarPath, manifestContent string) error {
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
f, err := w.Create("META-INF/MANIFEST.MF")
if err != nil {
return err
}
if _, err := f.Write([]byte(manifestContent)); err != nil {
return err
}
if err := w.Close(); err != nil {
return err
}
return os.WriteFile(jarPath, buf.Bytes(), 0644)
}
var _ = Describe("Java Main Container", func() {
var (
ctx *common.Context
container *containers.JavaMainContainer
buildDir string
depsDir string
cacheDir string
)
BeforeEach(func() {
var err error
buildDir, err = os.MkdirTemp("", "build")
Expect(err).NotTo(HaveOccurred())
depsDir, err = os.MkdirTemp("", "deps")
Expect(err).NotTo(HaveOccurred())
cacheDir, err = os.MkdirTemp("", "cache")
Expect(err).NotTo(HaveOccurred())
err = os.MkdirAll(filepath.Join(depsDir, "0"), 0755)
Expect(err).NotTo(HaveOccurred())
logger := libbuildpack.NewLogger(os.Stdout)
manifest := &libbuildpack.Manifest{}
installer := &libbuildpack.Installer{}
stager := libbuildpack.NewStager([]string{buildDir, cacheDir, depsDir, "0"}, logger, manifest)
command := &libbuildpack.Command{}
ctx = &common.Context{
Stager: stager,
Manifest: manifest,
Installer: installer,
Log: logger,
Command: command,
}
container = containers.NewJavaMainContainer(ctx)
})
AfterEach(func() {
os.RemoveAll(buildDir)
os.RemoveAll(depsDir)
os.RemoveAll(cacheDir)
})
Describe("Detect", func() {
Context("with JAR file containing Main-Class manifest", func() {
BeforeEach(func() {
Expect(createJar(
filepath.Join(buildDir, "app.jar"),
"Manifest-Version: 1.0\nMain-Class: com.example.Main\n",
)).To(Succeed())
})
It("detects as Java Main", func() {
name, err := container.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(Equal("Java Main"))
})
})
Context("with JAR file without Main-Class manifest", func() {
BeforeEach(func() {
Expect(createJar(
filepath.Join(buildDir, "lib.jar"),
"Manifest-Version: 1.0\nCreated-By: test\n",
)).To(Succeed())
})
It("does not detect via JAR alone", func() {
name, err := container.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(BeEmpty())
})
})
Context("with .class files", func() {
BeforeEach(func() {
os.WriteFile(filepath.Join(buildDir, "Main.class"), []byte{}, 0644)
})
It("detects as Java Main", func() {
name, err := container.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(Equal("Java Main"))
})
})
Context("with valid MANIFEST.MF", func() {
BeforeEach(func() {
metaInfDir := filepath.Join(buildDir, "META-INF")
os.MkdirAll(metaInfDir, 0755)
manifest := "Manifest-Version: 1.0\nMain-Class: com.example.Main\n"
os.WriteFile(filepath.Join(metaInfDir, "MANIFEST.MF"), []byte(manifest), 0644)
})
It("detects main class", func() {
jarFile := filepath.Join(buildDir, "app.jar")
os.WriteFile(jarFile, []byte("fake"), 0644)
name, err := container.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(Equal("Java Main"))
})
})
Context("with manifest without Main-Class", func() {
BeforeEach(func() {
metaInfDir := filepath.Join(buildDir, "META-INF")
os.MkdirAll(metaInfDir, 0755)
manifest := "Manifest-Version: 1.0\nCreated-By: test\n"
os.WriteFile(filepath.Join(metaInfDir, "MANIFEST.MF"), []byte(manifest), 0644)
})
It("does not detect", func() {
name, err := container.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(BeEmpty())
})
})
})
Describe("Release", func() {
expectQuotedEval := func(cmd string) {
Expect(cmd).To(MatchRegexp(`eval "exec .*\$JAVA_OPTS`))
}
Context("with JAR file", func() {
BeforeEach(func() {
Expect(createJar(
filepath.Join(buildDir, "app.jar"),
"Manifest-Version: 1.0\nMain-Class: com.example.Main\n",
)).To(Succeed())
container.Detect()
})
It("uses java -jar", func() {
cmd, err := container.Release()
Expect(err).NotTo(HaveOccurred())
Expect(cmd).To(ContainSubstring("java"))
Expect(cmd).To(ContainSubstring("-jar"))
Expect(cmd).To(ContainSubstring("app.jar"))
})
It("does not require JAVA_MAIN_CLASS", func() {
cmd, err := container.Release()
Expect(err).NotTo(HaveOccurred())
Expect(cmd).NotTo(ContainSubstring("JAVA_MAIN_CLASS"))
})
})
Context("with JAVA_MAIN_CLASS env variable", func() {
BeforeEach(func() {
os.Setenv("JAVA_MAIN_CLASS", "com.example.CustomMain")
os.WriteFile(filepath.Join(buildDir, "Main.class"), []byte("fake"), 0644)
})
AfterEach(func() {
os.Unsetenv("JAVA_MAIN_CLASS")
})
It("uses -cp with main class", func() {
container.Detect()
cmd, err := container.Release()
Expect(err).NotTo(HaveOccurred())
Expect(cmd).To(ContainSubstring("-cp"))
Expect(cmd).To(ContainSubstring("com.example.CustomMain"))
})
})
Context("with JBP_CONFIG_JAVA_MAIN java_main_class overriding manifest Main-Class", func() {
// Ruby parity: config[MAIN_CLASS_PROPERTY] takes precedence over manifest Main-Class
// This is how PropertiesLauncher is used with Spring Boot exploded JARs
BeforeEach(func() {
os.Setenv("JBP_CONFIG_JAVA_MAIN", "{java_main_class: org.springframework.boot.loader.launch.PropertiesLauncher}")
Expect(createJar(
filepath.Join(buildDir, "app.jar"),
"Manifest-Version: 1.0\nMain-Class: org.springframework.boot.loader.JarLauncher\n",
)).To(Succeed())
})
AfterEach(func() {
os.Unsetenv("JBP_CONFIG_JAVA_MAIN")
})
It("uses the configured java_main_class instead of the manifest Main-Class", func() {
container.Detect()
cmd, err := container.Release()
Expect(err).NotTo(HaveOccurred())
Expect(cmd).To(ContainSubstring("org.springframework.boot.loader.launch.PropertiesLauncher"))
Expect(cmd).NotTo(ContainSubstring("JarLauncher"))
})
It("uses classpath mode (not java -jar) so the overridden main class is actually invoked", func() {
container.Detect()
cmd, err := container.Release()
Expect(err).NotTo(HaveOccurred())
Expect(cmd).NotTo(ContainSubstring("-jar"))
Expect(cmd).To(ContainSubstring("-cp"))
})
})
Context("with JBP_CONFIG_JAVA_MAIN java_main_class on app with no manifest Main-Class", func() {
BeforeEach(func() {
os.Setenv("JBP_CONFIG_JAVA_MAIN", "{java_main_class: com.example.CustomMain}")
// App has no Main-Class in manifest — detection still works via JBP_CONFIG_JAVA_MAIN
os.WriteFile(filepath.Join(buildDir, "app.jar"), []byte("fake"), 0644)
})
AfterEach(func() {
os.Unsetenv("JBP_CONFIG_JAVA_MAIN")
})
It("detects as Java Main application", func() {
name, err := container.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(Equal("Java Main"))
})
It("uses the configured main class", func() {
container.Detect()
cmd, err := container.Release()
Expect(err).NotTo(HaveOccurred())
Expect(cmd).To(ContainSubstring("com.example.CustomMain"))
})
})
Context("with JBP_CONFIG_JAVA_MAIN arguments", func() {
AfterEach(func() {
os.Unsetenv("JBP_CONFIG_JAVA_MAIN")
})
It("appends arguments after main class when using java_main_class", func() {
os.Setenv("JBP_CONFIG_JAVA_MAIN", `{java_main_class: com.example.Main, arguments: "--server.port=$PORT"}`)
container.Detect()
cmd, err := container.Release()
Expect(err).NotTo(HaveOccurred())
Expect(cmd).To(ContainSubstring("com.example.Main --server.port=$PORT"))
})
It("appends arguments after main class when using manifest Main-Class", func() {
os.Setenv("JBP_CONFIG_JAVA_MAIN", `{arguments: "--foo=bar"}`)
Expect(createJar(
filepath.Join(buildDir, "app.jar"),
"Manifest-Version: 1.0\nMain-Class: com.example.Main\n",
)).To(Succeed())
container.Detect()
cmd, err := container.Release()
Expect(err).NotTo(HaveOccurred())
Expect(cmd).To(ContainSubstring("--foo=bar"))
})
It("escapes double quotes in arguments to avoid breaking the eval string", func() {
os.Setenv("JBP_CONFIG_JAVA_MAIN", `{arguments: "--spring.datasource.url=\"jdbc:h2:mem:test\""}`)
Expect(createJar(
filepath.Join(buildDir, "app.jar"),
"Manifest-Version: 1.0\nMain-Class: com.example.Main\n",
)).To(Succeed())
container.Detect()
cmd, err := container.Release()
Expect(err).NotTo(HaveOccurred())
// Double quotes in arguments must be backslash-escaped so they
// don't terminate the outer eval "..." string.
Expect(cmd).To(ContainSubstring(`--spring.datasource.url=\"jdbc:h2:mem:test\"`))
})
})
// Regression tests for issue #1301: start command must use eval "exec ... $JAVA_OPTS"
// (quoted string) so that glob chars in JAVA_OPTS are not expanded by bash before eval.
Context("with JAR file (eval quoting)", func() {
BeforeEach(func() {
Expect(createJar(
filepath.Join(buildDir, "app.jar"),
"Manifest-Version: 1.0\nMain-Class: com.example.Main\n",
)).To(Succeed())
container.Detect()
})
It("uses quoted eval to protect $JAVA_OPTS from glob expansion", func() {
cmd, err := container.Release()
Expect(err).NotTo(HaveOccurred())
expectQuotedEval(cmd)
})
})
Context("with JAVA_MAIN_CLASS env variable (eval quoting)", func() {
BeforeEach(func() {
os.Setenv("JAVA_MAIN_CLASS", "com.example.Main")
os.WriteFile(filepath.Join(buildDir, "Main.class"), []byte("fake"), 0644)
container.Detect()
})
AfterEach(func() {
os.Unsetenv("JAVA_MAIN_CLASS")
})
It("uses quoted eval to protect $JAVA_OPTS from glob expansion", func() {
cmd, err := container.Release()
Expect(err).NotTo(HaveOccurred())
expectQuotedEval(cmd)
})
})
Context("with JBP_CONFIG_JAVA_MAIN java_main_class (eval quoting)", func() {
BeforeEach(func() {
os.Setenv("JBP_CONFIG_JAVA_MAIN", "{java_main_class: com.example.Main}")
Expect(createJar(
filepath.Join(buildDir, "app.jar"),
"Manifest-Version: 1.0\nMain-Class: com.example.Main\n",
)).To(Succeed())
container.Detect()
})
AfterEach(func() {
os.Unsetenv("JBP_CONFIG_JAVA_MAIN")
})
It("uses quoted eval to protect $JAVA_OPTS from glob expansion", func() {
cmd, err := container.Release()
Expect(err).NotTo(HaveOccurred())
expectQuotedEval(cmd)
})
})
Context("without main class or JAR", func() {
It("returns error", func() {
_, err := container.Release()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("no main class"))
})
})
})
Describe("buildClasspath", func() {
Context("with JARs in root and lib/", func() {
BeforeEach(func() {
os.WriteFile(filepath.Join(buildDir, "app.jar"), []byte("fake"), 0644)
os.WriteFile(filepath.Join(buildDir, "util.jar"), []byte("fake"), 0644)
os.MkdirAll(filepath.Join(buildDir, "lib"), 0755)
os.WriteFile(filepath.Join(buildDir, "lib", "dep1.jar"), []byte("fake"), 0644)
os.WriteFile(filepath.Join(buildDir, "lib", "dep2.jar"), []byte("fake"), 0644)
})
It("includes all JARs in classpath", func() {
os.Setenv("JAVA_MAIN_CLASS", "com.example.Main")
defer os.Unsetenv("JAVA_MAIN_CLASS")
os.WriteFile(filepath.Join(buildDir, "Main.class"), []byte("fake"), 0644)
os.Remove(filepath.Join(buildDir, "app.jar"))
os.Remove(filepath.Join(buildDir, "util.jar"))
os.WriteFile(filepath.Join(buildDir, "lib", "dep1.jar"), []byte("fake"), 0644)
os.WriteFile(filepath.Join(buildDir, "lib", "dep2.jar"), []byte("fake"), 0644)
cmd, err := container.Release()
Expect(err).NotTo(HaveOccurred())
Expect(cmd).To(ContainSubstring("-cp"))
Expect(cmd).To(ContainSubstring("com.example.Main"))
})
})
Context("with no lib/ directory", func() {
BeforeEach(func() {
os.WriteFile(filepath.Join(buildDir, "Main.class"), []byte("fake"), 0644)
os.Setenv("JAVA_MAIN_CLASS", "com.example.Main")
})
AfterEach(func() {
os.Unsetenv("JAVA_MAIN_CLASS")
})
It("uses classpath with main class", func() {
cmd, err := container.Release()
Expect(err).NotTo(HaveOccurred())
Expect(cmd).To(ContainSubstring("-cp"))
Expect(cmd).To(ContainSubstring("com.example.Main"))
Expect(cmd).NotTo(ContainSubstring("lib/"))
})
})
Context("with empty directory", func() {
BeforeEach(func() {
os.Setenv("JAVA_MAIN_CLASS", "com.example.Main")
})
AfterEach(func() {
os.Unsetenv("JAVA_MAIN_CLASS")
})
It("returns classpath with current directory", func() {
cmd, err := container.Release()
Expect(err).NotTo(HaveOccurred())
Expect(cmd).To(ContainSubstring("."))
})
})
})
Describe("Finalize", func() {
Context("with JARs in root and lib/", func() {
BeforeEach(func() {
os.WriteFile(filepath.Join(buildDir, "app.jar"), []byte("fake"), 0644)
os.WriteFile(filepath.Join(buildDir, "util.jar"), []byte("fake"), 0644)
os.MkdirAll(filepath.Join(buildDir, "lib"), 0755)
os.WriteFile(filepath.Join(buildDir, "lib", "dep1.jar"), []byte("fake"), 0644)
os.WriteFile(filepath.Join(buildDir, "lib", "dep2.jar"), []byte("fake"), 0644)
})
It("builds correct classpath", func() {
os.WriteFile(filepath.Join(buildDir, "Main.class"), []byte("fake"), 0644)
container.Detect()
err := container.Finalize()
Expect(err).NotTo(HaveOccurred())
})
})
Context("with only lib/ directory", func() {
BeforeEach(func() {
os.MkdirAll(filepath.Join(buildDir, "lib"), 0755)
os.WriteFile(filepath.Join(buildDir, "lib", "dep1.jar"), []byte("fake"), 0644)
os.WriteFile(filepath.Join(buildDir, "Main.class"), []byte("fake"), 0644)
})
It("includes lib JARs in classpath", func() {
container.Detect()
err := container.Finalize()
Expect(err).NotTo(HaveOccurred())
})
})
Context("with Spring Boot launcher in JBP_CONFIG_JAVA_MAIN", func() {
BeforeEach(func() {
os.Setenv("JBP_CONFIG_JAVA_MAIN", `{java_main_class: "org.springframework.boot.loader.launch.PropertiesLauncher"}`)
os.WriteFile(filepath.Join(buildDir, "app.jar"), []byte("fake"), 0644)
})
AfterEach(func() {
os.Unsetenv("JBP_CONFIG_JAVA_MAIN")
})
It("writes SERVER_PORT=$PORT to profile.d for Ruby parity", func() {
container.Detect()
err := container.Finalize()
Expect(err).NotTo(HaveOccurred())
profileScript := filepath.Join(depsDir, "0", "profile.d", "java_main.sh")
data, err := os.ReadFile(profileScript)
Expect(err).NotTo(HaveOccurred())
Expect(string(data)).To(ContainSubstring("export SERVER_PORT=$PORT\n"))
})
})
Context("with non-Spring-Boot main class", func() {
BeforeEach(func() {
os.WriteFile(filepath.Join(buildDir, "Main.class"), []byte("fake"), 0644)
})
It("does not write SERVER_PORT to profile.d", func() {
container.Detect()
err := container.Finalize()
Expect(err).NotTo(HaveOccurred())
profileScript := filepath.Join(depsDir, "0", "profile.d", "java_main.sh")
data, err := os.ReadFile(profileScript)
Expect(err).NotTo(HaveOccurred())
Expect(string(data)).NotTo(ContainSubstring("SERVER_PORT"))
})
})
})
})