Repository navigation
Expand file tree
/
Copy pathplay_test.go
More file actions
284 lines (246 loc) · 10.2 KB
/
Copy pathplay_test.go
File metadata and controls
284 lines (246 loc) · 10.2 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
package containers_test
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"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"
)
var _ = Describe("Play Container", func() {
var (
ctx *common.Context
container *containers.PlayContainer
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())
// Create deps directory structure
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.NewPlayContainer(ctx)
})
AfterEach(func() {
os.RemoveAll(buildDir)
os.RemoveAll(depsDir)
os.RemoveAll(cacheDir)
})
Describe("Detect", func() {
Context("with Play 2.0 dist application (application-root/start)", func() {
BeforeEach(func() {
os.MkdirAll(filepath.Join(buildDir, "application-root"), 0755)
os.WriteFile(filepath.Join(buildDir, "application-root", "start"), []byte("#!/bin/sh"), 0755)
os.MkdirAll(filepath.Join(buildDir, "application-root", "lib"), 0755)
os.WriteFile(filepath.Join(buildDir, "application-root", "lib", "play.play_2.9.1-2.0.jar"), []byte("fake"), 0644)
})
It("detects as Play", func() {
name, err := container.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(Equal("Play"))
})
})
Context("with Play 2.1 dist application (application-root/start)", func() {
BeforeEach(func() {
os.MkdirAll(filepath.Join(buildDir, "application-root"), 0755)
os.WriteFile(filepath.Join(buildDir, "application-root", "start"), []byte("#!/bin/sh"), 0755)
os.MkdirAll(filepath.Join(buildDir, "application-root", "lib"), 0755)
os.WriteFile(filepath.Join(buildDir, "application-root", "lib", "play.play_2.10-2.1.4.jar"), []byte("fake"), 0644)
})
It("detects as Play", func() {
name, err := container.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(Equal("Play"))
})
})
Context("with Play 2.1 staged application (staged directory)", func() {
BeforeEach(func() {
os.MkdirAll(filepath.Join(buildDir, "staged"), 0755)
os.WriteFile(filepath.Join(buildDir, "staged", "play_2.10-2.1.4.jar"), []byte("fake"), 0644)
})
It("detects as Play", func() {
name, err := container.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(Equal("Play"))
})
})
Context("with Play 2.2 dist application (typesafe path)", func() {
BeforeEach(func() {
os.MkdirAll(filepath.Join(buildDir, "application-root", "bin"), 0755)
os.WriteFile(filepath.Join(buildDir, "application-root", "bin", "myapp"), []byte("#!/bin/sh"), 0755)
os.MkdirAll(filepath.Join(buildDir, "application-root", "lib"), 0755)
os.WriteFile(filepath.Join(buildDir, "application-root", "lib", "com.typesafe.play.play_2.10-2.2.0.jar"), []byte("fake"), 0644)
})
It("detects as Play", func() {
name, err := container.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(Equal("Play"))
})
})
Context("with Play 2.2 staged application (lib directory)", func() {
BeforeEach(func() {
os.MkdirAll(filepath.Join(buildDir, "lib"), 0755)
os.WriteFile(filepath.Join(buildDir, "lib", "com.typesafe.play.play_2.10-2.2.0.jar"), []byte("fake"), 0644)
})
It("detects as Play", func() {
name, err := container.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(Equal("Play"))
})
})
Context("with non-Play application", func() {
BeforeEach(func() {
os.WriteFile(filepath.Join(buildDir, "app.jar"), []byte("fake"), 0644)
})
It("does not detect as Play", func() {
name, err := container.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(BeEmpty())
})
})
Context("with only start script but no Play JAR", func() {
BeforeEach(func() {
os.MkdirAll(filepath.Join(buildDir, "application-root"), 0755)
os.WriteFile(filepath.Join(buildDir, "application-root", "start"), []byte("#!/bin/sh"), 0755)
})
It("does not detect as Play", func() {
name, err := container.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(BeEmpty())
})
})
})
Describe("Release", func() {
Context("with Play 2.0 dist application", func() {
BeforeEach(func() {
os.MkdirAll(filepath.Join(buildDir, "application-root"), 0755)
os.WriteFile(filepath.Join(buildDir, "application-root", "start"), []byte("#!/bin/sh"), 0755)
os.MkdirAll(filepath.Join(buildDir, "application-root", "lib"), 0755)
os.WriteFile(filepath.Join(buildDir, "application-root", "lib", "play.play_2.9.1-2.0.jar"), []byte("fake"), 0644)
_, err := container.Detect()
Expect(err).NotTo(HaveOccurred())
})
It("returns correct start command", func() {
cmd, err := container.Release()
Expect(err).NotTo(HaveOccurred())
Expect(cmd).To(Equal("$HOME/application-root/start"))
})
})
Context("with Play 2.1 staged application", func() {
BeforeEach(func() {
os.MkdirAll(filepath.Join(buildDir, "staged"), 0755)
os.WriteFile(filepath.Join(buildDir, "staged", "play_2.10-2.1.4.jar"), []byte("fake"), 0644)
_, err := container.Detect()
Expect(err).NotTo(HaveOccurred())
})
It("returns correct java command", func() {
cmd, err := container.Release()
Expect(err).NotTo(HaveOccurred())
Expect(cmd).To(ContainSubstring(`/bin/javaexec "$JAVA_HOME/bin/java" -cp`))
Expect(cmd).NotTo(ContainSubstring("eval"))
Expect(cmd).NotTo(ContainSubstring("$JAVA_OPTS"))
Expect(cmd).To(ContainSubstring("play.core.server.NettyServer $HOME"))
})
})
Context("with Play 2.2 staged application", func() {
BeforeEach(func() {
os.MkdirAll(filepath.Join(buildDir, "lib"), 0755)
os.WriteFile(filepath.Join(buildDir, "lib", "com.typesafe.play.play_2.10-2.2.0.jar"), []byte("fake"), 0644)
_, err := container.Detect()
Expect(err).NotTo(HaveOccurred())
})
It("returns correct java command", func() {
cmd, err := container.Release()
Expect(err).NotTo(HaveOccurred())
Expect(cmd).To(ContainSubstring(`/bin/javaexec "$JAVA_HOME/bin/java" -cp`))
Expect(cmd).NotTo(ContainSubstring("eval"))
Expect(cmd).NotTo(ContainSubstring("$JAVA_OPTS"))
Expect(cmd).To(ContainSubstring("play.core.server.NettyServer $HOME"))
})
})
Context("when not detected", func() {
It("returns error", func() {
_, err := container.Release()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("no Play application detected"))
})
})
})
Describe("Finalize", func() {
Context("with detected Play application", func() {
// Note: container.Finalize() (re)writes play_java_opts.sh from scratch every time it
// runs (WriteProfileD truncates and rewrites the file). Each It below gets a fresh
// buildDir/depsDir from the top-level BeforeEach, calls Finalize() itself to produce
// its own copy of the script, and only then reads/executes it — so there's no
// shared state or write/read race across tests.
BeforeEach(func() {
os.MkdirAll(filepath.Join(buildDir, "application-root"), 0755)
os.WriteFile(filepath.Join(buildDir, "application-root", "start"), []byte("#!/bin/sh"), 0755)
os.MkdirAll(filepath.Join(buildDir, "application-root", "lib"), 0755)
os.WriteFile(filepath.Join(buildDir, "application-root", "lib", "play.play_2.9.1-2.0.jar"), []byte("fake"), 0644)
_, err := container.Detect()
Expect(err).NotTo(HaveOccurred())
})
It("finalizes successfully", func() {
err := container.Finalize()
Expect(err).NotTo(HaveOccurred())
})
It("writes profile.d script that exports JAVA_OPTS with $PORT and $TMPDIR", func() {
Expect(container.Finalize()).To(Succeed())
scriptPath := filepath.Join(depsDir, "0", "profile.d", "play_java_opts.sh")
Expect(scriptPath).To(BeAnExistingFile())
content, err := os.ReadFile(scriptPath)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("export JAVA_OPTS="))
Expect(string(content)).To(ContainSubstring("$PORT"))
Expect(string(content)).To(ContainSubstring("$TMPDIR"))
})
It("appends to, rather than overwrites, any previously assembled JAVA_OPTS (#1432)", func() {
Expect(container.Finalize()).To(Succeed())
scriptPath := filepath.Join(depsDir, "0", "profile.d", "play_java_opts.sh")
content, err := os.ReadFile(scriptPath)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring(`export JAVA_OPTS="${JAVA_OPTS:+$JAVA_OPTS }`),
"must preserve JAVA_OPTS already assembled by profile.d/00_java_opts.sh, e.g. the user's own JAVA_OPTS")
})
It("actually preserves a pre-set JAVA_OPTS value at runtime, not just in the script text (#1432)", func() {
Expect(container.Finalize()).To(Succeed())
scriptPath := filepath.Join(depsDir, "0", "profile.d", "play_java_opts.sh")
// Simulate profile.d/00_java_opts.sh having already assembled and exported JAVA_OPTS
// (e.g. containing the user's own -Xss512K) before this script is sourced, exactly as
// CF's launcher sources profile.d scripts one after another in the same shell.
cmd := exec.Command("bash", "-c", fmt.Sprintf(`export JAVA_OPTS="-Xss512K"; . "%s"; echo "$JAVA_OPTS"`, scriptPath))
out, err := cmd.Output()
Expect(err).NotTo(HaveOccurred())
result := strings.TrimSpace(string(out))
Expect(result).To(ContainSubstring("-Xss512K"),
"the pre-existing JAVA_OPTS value must survive, not be wiped out by this container's own opts")
Expect(result).To(ContainSubstring("-XX:+ExitOnOutOfMemoryError"),
"this container's own opts should still be appended")
})
})
})
})