forked from cloudfoundry/java-buildpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtomcat_test.go
More file actions
445 lines (369 loc) · 18.5 KB
/
tomcat_test.go
File metadata and controls
445 lines (369 loc) · 18.5 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
package integration_test
import (
"path/filepath"
"testing"
"github.com/cloudfoundry/switchblade"
"github.com/cloudfoundry/switchblade/matchers"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testTomcat(platform switchblade.Platform, fixtures string) func(*testing.T, spec.G, spec.S) {
return func(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
Eventually = NewWithT(t).Eventually
name string
)
it.Before(func() {
var err error
name, err = switchblade.RandomName()
Expect(err).NotTo(HaveOccurred())
})
it.After(func() {
if t.Failed() && name != "" {
t.Logf("❌ FAILED TEST - App/Container: %s", name)
t.Logf(" Platform: %s", settings.Platform)
}
if name != "" && (!settings.KeepFailedContainers || !t.Failed()) {
Expect(platform.Delete.Execute(name)).To(Succeed())
}
})
context("with a simple servlet app", func() {
it("successfully deploys and runs with Java 11 (Javax)", func() {
deployment, logs, err := platform.Deploy.
WithEnv(map[string]string{
"BP_JAVA_VERSION": "11",
"JBP_CONFIG_TOMCAT": "{ tomcat: { version: \"9.+\" }, access_logging_support: { access_logging: enabled } }",
}).
Execute(name, filepath.Join(fixtures, "containers", "tomcat_javax"))
Expect(err).NotTo(HaveOccurred(), logs.String)
// Verify embedded Cloud Foundry-optimized Tomcat configuration was installed
Expect(logs.String()).To(ContainSubstring("Installing Cloud Foundry-optimized Tomcat configuration defaults"))
Expect(logs.String()).To(ContainSubstring("Dynamic port binding (${http.port} from $PORT)"))
Expect(logs.String()).To(ContainSubstring("HTTP/2 support enabled"))
Expect(logs.String()).To(ContainSubstring("RemoteIpValve for X-Forwarded-* headers"))
Expect(logs.String()).To(ContainSubstring("CloudFoundryAccessLoggingValve with vcap_request_id"))
Expect(logs.String()).To(ContainSubstring("Stdout logging via CloudFoundryConsoleHandler"))
Eventually(deployment).Should(matchers.Serve(ContainSubstring("OK")))
// Verify runtime logs contain CloudFoundry-specific Tomcat features
// Use Eventually to wait for logs to be flushed, as they may not appear immediately
// Check for HTTP/2 support in runtime logs (Tomcat startup messages)
// These should appear quickly during Tomcat startup
Eventually(func() string {
logs, _ := deployment.RuntimeLogs()
return logs
}, "10s", "1s").Should(Or(
ContainSubstring("Http11NioProtocol"),
ContainSubstring("Starting ProtocolHandler"),
ContainSubstring("HTTP/1.1"),
))
})
it("successfully deploys and runs with Java 11 (Jakarta EE)", func() {
deployment, logs, err := platform.Deploy.
WithEnv(map[string]string{
"BP_JAVA_VERSION": "11",
"JBP_CONFIG_TOMCAT": "{ access_logging_support: { access_logging: enabled } }",
}).
Execute(name, filepath.Join(fixtures, "containers", "tomcat_jakarta"))
Expect(err).NotTo(HaveOccurred(), logs.String)
// Verify embedded Cloud Foundry-optimized Tomcat configuration was installed
Expect(logs.String()).To(ContainSubstring("Installing Cloud Foundry-optimized Tomcat configuration defaults"))
Expect(logs.String()).To(ContainSubstring("Dynamic port binding (${http.port} from $PORT)"))
Expect(logs.String()).To(ContainSubstring("HTTP/2 support enabled"))
Expect(logs.String()).To(ContainSubstring("RemoteIpValve for X-Forwarded-* headers"))
Expect(logs.String()).To(ContainSubstring("CloudFoundryAccessLoggingValve with vcap_request_id"))
Expect(logs.String()).To(ContainSubstring("Stdout logging via CloudFoundryConsoleHandler"))
Eventually(deployment).Should(matchers.Serve(ContainSubstring("OK")))
// Verify runtime logs contain CloudFoundry-specific Tomcat features
// Use Eventually to wait for logs to be flushed, as they may not appear immediately
// Check for HTTP/2 support in runtime logs (Tomcat startup messages)
// These should appear quickly during Tomcat startup
Eventually(func() string {
logs, _ := deployment.RuntimeLogs()
return logs
}, "10s", "1s").Should(Or(
ContainSubstring("Http11NioProtocol"),
ContainSubstring("Starting ProtocolHandler"),
ContainSubstring("HTTP/1.1"),
))
})
})
context("with access logging configuration", func() {
it("disables access logging by default (Ruby buildpack parity)", func() {
deployment, logs, err := platform.Deploy.
WithEnv(map[string]string{
"BP_JAVA_VERSION": "11",
}).
Execute(name, filepath.Join(fixtures, "containers", "tomcat_jakarta"))
Expect(err).NotTo(HaveOccurred(), logs.String)
// Verify buildpack mentions access logging configuration
// Should see message during Tomcat supply phase
Expect(logs.String()).To(Or(
ContainSubstring("Access logging disabled by default"),
ContainSubstring("Access logging"),
))
// Application should still work without access logs
Eventually(deployment).Should(matchers.Serve(ContainSubstring("OK")))
// Verify NO access logs are generated (default: disabled)
// Wait a bit for any potential logs to be generated
Eventually(func() string {
runtimeLogs, _ := deployment.RuntimeLogs()
return runtimeLogs
}, "5s", "1s").ShouldNot(ContainSubstring("[ACCESS]"))
})
it("enables access logging when configured via JBP_CONFIG_TOMCAT", func() {
deployment, logs, err := platform.Deploy.
WithEnv(map[string]string{
"BP_JAVA_VERSION": "11",
"JBP_CONFIG_TOMCAT": "{ access_logging_support: { access_logging: enabled } }",
}).
Execute(name, filepath.Join(fixtures, "containers", "tomcat_jakarta"))
Expect(err).NotTo(HaveOccurred(), logs.String)
// Verify buildpack detected the configuration
Expect(logs.String()).To(ContainSubstring("Access logging enabled via JBP_CONFIG_TOMCAT"))
Eventually(deployment).Should(matchers.Serve(ContainSubstring("OK")))
// Verify access logs ARE generated when enabled
Eventually(func() string {
runtimeLogs, _ := deployment.RuntimeLogs()
return runtimeLogs
}, "10s", "1s").Should(Or(
ContainSubstring("[ACCESS]"),
ContainSubstring("vcap_request_id:"),
))
})
})
context("with JRE selection", func() {
it("deploys with Java 8 (Tomcat 9 + javax.servlet)", func() {
deployment, logs, err := platform.Deploy.
WithEnv(map[string]string{
"BP_JAVA_VERSION": "8",
}).
Execute(name, filepath.Join(fixtures, "containers", "tomcat_javax"))
Expect(err).NotTo(HaveOccurred(), logs.String)
Expect(logs.String()).To(ContainSubstring("Installing OpenJDK 8."))
Expect(logs.String()).To(ContainSubstring("Tomcat 9"))
Eventually(deployment).Should(matchers.Serve(ContainSubstring("OK")))
})
it("deploys with Java 11 (Tomcat 9 + javax.servlet)", func() {
deployment, logs, err := platform.Deploy.
WithEnv(map[string]string{
"BP_JAVA_VERSION": "11",
"JBP_CONFIG_TOMCAT": "{ tomcat: { version: \"9.+\" } }",
}).
Execute(name, filepath.Join(fixtures, "containers", "tomcat_javax"))
Expect(err).NotTo(HaveOccurred(), logs.String)
Expect(logs.String()).To(ContainSubstring("Installing OpenJDK 11."))
Expect(logs.String()).To(ContainSubstring("Tomcat 9"))
Eventually(deployment).Should(matchers.Serve(ContainSubstring("OK")))
})
it("deploys with default Java (Tomcat 9 + javax.servlet)", func() {
deployment, logs, err := platform.Deploy.
WithEnv(map[string]string{
"JBP_CONFIG_TOMCAT": "{ tomcat: { version: \"9.+\" } }",
}).
Execute(name, filepath.Join(fixtures, "containers", "tomcat_javax"))
Expect(err).NotTo(HaveOccurred(), logs.String)
Expect(logs.String()).To(ContainSubstring("Installing OpenJDK 17."))
Expect(logs.String()).To(ContainSubstring("Tomcat 9"))
Eventually(deployment).Should(matchers.Serve(ContainSubstring("OK")))
})
it("fails staging with a compatibility error for Tomcat 10 with Java 8 (javax)", func() {
_, logs, err := platform.Deploy.
WithEnv(map[string]string{
"BP_JAVA_VERSION": "8",
"JBP_CONFIG_TOMCAT": "{ tomcat: { version: \"10.+\" } }",
}).
Execute(name, filepath.Join(fixtures, "containers", "tomcat_javax"))
Expect(err).To(HaveOccurred())
Expect(logs.String()).To(ContainSubstring("Tomcat 10.x requires Java 11+, but Java 8 detected"))
})
it("deploys with Java 11 (Tomcat 10 + jakarta.servlet)", func() {
deployment, logs, err := platform.Deploy.
WithEnv(map[string]string{
"BP_JAVA_VERSION": "11",
}).
Execute(name, filepath.Join(fixtures, "containers", "tomcat_jakarta"))
Expect(err).NotTo(HaveOccurred(), logs.String)
Expect(logs.String()).To(ContainSubstring("Installing OpenJDK 11."))
Expect(logs.String()).To(ContainSubstring("Tomcat 10"))
Eventually(deployment).Should(matchers.Serve(ContainSubstring("OK")))
})
it("deploys with Java 17 (Tomcat 10 + jakarta.servlet)", func() {
deployment, logs, err := platform.Deploy.
WithEnv(map[string]string{
"BP_JAVA_VERSION": "17",
}).
Execute(name, filepath.Join(fixtures, "containers", "tomcat_jakarta"))
Expect(err).NotTo(HaveOccurred(), logs.String)
Expect(logs.String()).To(ContainSubstring("Installing OpenJDK 17."))
Expect(logs.String()).To(ContainSubstring("Tomcat 10"))
Eventually(deployment).Should(matchers.Serve(ContainSubstring("OK")))
})
})
// Regression test for https://github.com/cloudfoundry/java-buildpack/issues/1219
// Staging failed with "improper constraint: 10.1.+" when using a two-segment
// minor version wildcard (e.g. 10.1.+) in JBP_CONFIG_TOMCAT. The fix normalises
// "10.1.+" → "10.1.*" before passing it to libbuildpack's FindMatchingVersion.
context("with a two-segment minor version wildcard in JBP_CONFIG_TOMCAT (issue #1219)", func() {
it("successfully stages with version: 10.1.+ and JBP_CONFIG_OPEN_JDK_JRE 17.+", func() {
deployment, logs, err := platform.Deploy.
WithEnv(map[string]string{
"JBP_CONFIG_OPEN_JDK_JRE": "{ jre: { version: 17.+ } }",
"JBP_CONFIG_TOMCAT": "{ tomcat: { version: 10.1.+ } }",
}).
Execute(name, filepath.Join(fixtures, "containers", "tomcat_jakarta"))
Expect(err).NotTo(HaveOccurred(), logs.String)
Expect(logs.String()).To(ContainSubstring("Installing OpenJDK 17."))
Expect(logs.String()).To(ContainSubstring("Tomcat 10.1."))
Eventually(deployment).Should(matchers.Serve(ContainSubstring("OK")))
})
})
context("with memory limits", func() {
it("respects memory calculator settings with JAVA_OPTS", func() {
deployment, logs, err := platform.Deploy.
WithEnv(map[string]string{
"BP_JAVA_VERSION": "11",
"JAVA_OPTS": "-Xmx256m",
}).
Execute(name, filepath.Join(fixtures, "containers", "tomcat_jakarta"))
Expect(err).NotTo(HaveOccurred(), logs.String)
Expect(logs.String()).To(ContainSubstring("Installing OpenJDK 11."))
Expect(logs.String()).To(ContainSubstring("memory"))
Eventually(deployment).Should(matchers.Serve(ContainSubstring("OK")))
})
})
context("with Java 21", func() {
it("successfully deploys WAR file with Java 21 using git buildpack", func() {
// Regression test: This deployment scenario previously failed with:
// "Failed to build droplet release: buildpack's release output invalid:
// yaml: unmarshal errors: line 1: cannot unmarshal !!str `-----> ...`"
//
// The bug: When using a git URL buildpack (not pre-packaged), the bash wrapper
// scripts (bin/detect, bin/supply, bin/finalize, bin/release) were used.
// These bash wrappers compiled Go binaries on-the-fly and had echo statements
// like "-----> Running go build release" that polluted stdout.
//
// During the release phase, Cloud Foundry expects pure YAML on stdout.
// The echo pollution caused: "cannot unmarshal !!str `-----> ...`"
//
// This test explicitly uses a git URL to ensure the bash scripts work correctly.
// The fix converted everything to pure bash (no Go wrappers) and removed all
// echo statements so only clean YAML is output.
//
// LIMITATION: Git URL buildpacks only work on CF platform because the CF CLI
// handles git cloning (see cloudfoundry/setup.go:332-335 which passes git URLs
// directly to `cf push -b <url>`). Switchblade's Docker platform only supports
// HTTP downloads via buildpacks_cache.go:64 http.Get(), not git clone.
// The test must run on CF platform to properly test git URL buildpack deployment.
//
// NOTE: The original test used #feature/go-migration but that branch was merged
// into main, so we now use the default branch (main).
if settings.Platform == "docker" {
t.Skip("Git URL buildpacks require CF platform - Docker platform cannot clone git repos")
}
deployment, logs, err := platform.Deploy.
WithBuildpacks("https://github.com/cloudfoundry/java-buildpack.git").
WithEnv(map[string]string{
"BP_JAVA_VERSION": "21",
}).
Execute(name, filepath.Join(fixtures, "containers", "tomcat_jakarta"))
Expect(err).NotTo(HaveOccurred(), logs.String)
Expect(logs.String()).To(ContainSubstring("Installing OpenJDK 21."))
Expect(logs.String()).To(ContainSubstring("Tomcat"))
// If deployment succeeds, it means:
// 1. bin/detect succeeded (detected Tomcat)
// 2. bin/supply succeeded (downloaded dependencies)
// 3. bin/finalize succeeded (configured app)
// 4. bin/release succeeded (output valid YAML) <- THIS IS THE BUG FIX
// 5. App started and responds to requests
Eventually(deployment).Should(matchers.Serve(ContainSubstring("OK")))
})
})
context("with JBP_CONFIG_OPEN_JDK_JRE version selection", func() {
it("respects JBP_CONFIG_OPEN_JDK_JRE version pattern", func() {
deployment, logs, err := platform.Deploy.
WithEnv(map[string]string{
"JBP_CONFIG_OPEN_JDK_JRE": "{jre: {version: 17.+}}",
}).
Execute(name, filepath.Join(fixtures, "containers", "tomcat_jakarta"))
Expect(err).NotTo(HaveOccurred(), logs.String)
Expect(logs.String()).To(ContainSubstring("Installing OpenJDK 17."))
Eventually(deployment).Should(matchers.Serve(ContainSubstring("OK")))
})
it("respects JBP_CONFIG_OPEN_JDK_JRE over manifest default", func() {
deployment, logs, err := platform.Deploy.
WithEnv(map[string]string{
"JBP_CONFIG_OPEN_JDK_JRE": "{jre: {version: 21.+}}",
}).
Execute(name, filepath.Join(fixtures, "containers", "tomcat_jakarta"))
Expect(err).NotTo(HaveOccurred(), logs.String)
Expect(logs.String()).To(ContainSubstring("Installing OpenJDK 21."))
Eventually(deployment).Should(matchers.Serve(ContainSubstring("OK")))
})
})
context("with JBP_CONFIG_SAP_MACHINE_JRE version selection", func() {
it("respects JBP_CONFIG_SAP_MACHINE_JRE(version 17) over manifest default", func() {
deployment, logs, err := platform.Deploy.
WithEnv(map[string]string{
"JBP_CONFIG_SAP_MACHINE_JRE": "{ jre: {version: 17.+} }",
}).
Execute(name, filepath.Join(fixtures, "containers", "tomcat_jakarta"))
Expect(err).NotTo(HaveOccurred(), logs.String)
Expect(logs.String()).To(ContainSubstring("Installing SAP Machine 17."))
Eventually(deployment).Should(matchers.Serve(ContainSubstring("OK")))
})
it("respects JBP_CONFIG_SAP_MACHINE_JRE(version 21)", func() {
deployment, logs, err := platform.Deploy.
WithEnv(map[string]string{
"JBP_CONFIG_SAP_MACHINE_JRE": "{ jre: {version: 21.+} }",
}).
Execute(name, filepath.Join(fixtures, "containers", "tomcat_jakarta"))
Expect(err).NotTo(HaveOccurred(), logs.String)
Expect(logs.String()).To(ContainSubstring("Installing SAP Machine 21."))
Eventually(deployment).Should(matchers.Serve(ContainSubstring("OK")))
})
it("respects JBP_CONFIG_SAP_MACHINE_JRE(version 25) over manifest default", func() {
deployment, logs, err := platform.Deploy.
WithEnv(map[string]string{
"JBP_CONFIG_SAP_MACHINE_JRE": "{ jre: {version: 25.+} }",
}).
Execute(name, filepath.Join(fixtures, "containers", "tomcat_jakarta"))
Expect(err).NotTo(HaveOccurred(), logs.String)
Expect(logs.String()).To(ContainSubstring("Installing SAP Machine 25."))
Eventually(deployment).Should(matchers.Serve(ContainSubstring("OK")))
})
})
context("with external Tomcat configuration", func() {
it("downloads and applies configuration from real repository", func() {
// This test verifies the external configuration workflow:
// 1. Configuration is detected from JBP_CONFIG_TOMCAT
// 2. Buildpack fetches index.yml from repository_root
// 3. Buildpack downloads the specified version's tar.gz
// 4. Configuration is extracted and applied to Tomcat
// 5. Application deploys successfully with custom configuration
deployment, logs, err := platform.Deploy.
WithEnv(map[string]string{
"BP_JAVA_VERSION": "11",
"JBP_CONFIG_TOMCAT": "{tomcat: {external_configuration_enabled: true}, external_configuration: {repository_root: \"https://tomcat-config.cfapps.eu12.hana.ondemand.com\", version: \"1.4.0\"}}",
}).
Execute(name, filepath.Join(fixtures, "containers", "tomcat_jakarta"))
// Build should succeed with real repository
Expect(err).NotTo(HaveOccurred())
// Verify external configuration was detected and parsed correctly
Expect(logs.String()).To(ContainSubstring("External Tomcat configuration is enabled"))
Expect(logs.String()).To(ContainSubstring("External configuration repository: https://tomcat-config.cfapps.eu12.hana.ondemand.com (version: 1.4.0)"))
// Verify buildpack falls back to direct download when not in manifest
Expect(logs.String()).To(ContainSubstring("External configuration not in manifest, downloading directly from repository"))
// Verify buildpack fetches index.yml successfully
Expect(logs.String()).To(ContainSubstring("Fetching external configuration index from: https://tomcat-config.cfapps.eu12.hana.ondemand.com/index.yml"))
// Verify buildpack downloads the configuration archive
Expect(logs.String()).To(ContainSubstring("Found version 1.4.0 in index"))
Expect(logs.String()).To(ContainSubstring("Extracting external configuration"))
// Verify configuration was installed successfully
Expect(logs.String()).To(ContainSubstring("Successfully installed external Tomcat configuration version 1.4.0"))
// Verify application starts successfully with custom configuration
Eventually(deployment).Should(matchers.Serve(ContainSubstring("OK")))
})
})
}
}