-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathjava_memory_assistant_test.go
More file actions
334 lines (288 loc) · 12.3 KB
/
java_memory_assistant_test.go
File metadata and controls
334 lines (288 loc) · 12.3 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
package frameworks_test
import (
"os"
"path/filepath"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/cloudfoundry/java-buildpack/src/java/common"
"github.com/cloudfoundry/java-buildpack/src/java/frameworks"
"github.com/cloudfoundry/libbuildpack"
)
func newJMAContext(buildDir, cacheDir, depsDir string) *common.Context {
logger := libbuildpack.NewLogger(GinkgoWriter)
manifest := &libbuildpack.Manifest{}
installer := &libbuildpack.Installer{}
stager := libbuildpack.NewStager([]string{buildDir, cacheDir, depsDir, "0"}, logger, manifest)
return &common.Context{
Stager: stager,
Manifest: manifest,
Installer: installer,
Log: logger,
Command: &libbuildpack.Command{},
}
}
// installJMAAgent creates a versioned JMA JAR under depsDir.
func installJMAAgent(depsDir, version string) {
agentDir := filepath.Join(depsDir, "0", "java_memory_assistant")
Expect(os.MkdirAll(agentDir, 0755)).To(Succeed())
Expect(os.WriteFile(
filepath.Join(agentDir, "java-memory-assistant-"+version+".jar"),
[]byte("fake jar"), 0644,
)).To(Succeed())
}
var _ = Describe("Java Memory Assistant", func() {
var (
fw *frameworks.JavaMemoryAssistantFramework
buildDir string
cacheDir string
depsDir string
)
BeforeEach(func() {
var err error
buildDir, err = os.MkdirTemp("", "jma-build")
Expect(err).NotTo(HaveOccurred())
cacheDir, err = os.MkdirTemp("", "jma-cache")
Expect(err).NotTo(HaveOccurred())
depsDir, err = os.MkdirTemp("", "jma-deps")
Expect(err).NotTo(HaveOccurred())
Expect(os.MkdirAll(filepath.Join(depsDir, "0"), 0755)).To(Succeed())
fw = frameworks.NewJavaMemoryAssistantFramework(newJMAContext(buildDir, cacheDir, depsDir))
})
AfterEach(func() {
os.RemoveAll(buildDir)
os.RemoveAll(cacheDir)
os.RemoveAll(depsDir)
os.Unsetenv("JBP_CONFIG_JAVA_MEMORY_ASSISTANT")
os.Unsetenv("VCAP_SERVICES")
os.Unsetenv("JAVA_HOME")
})
Describe("Detect", func() {
Context("with no configuration set", func() {
It("returns empty string (disabled by default)", func() {
name, err := fw.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(BeEmpty())
})
})
Context("with JBP_CONFIG_JAVA_MEMORY_ASSISTANT enabled: true", func() {
BeforeEach(func() {
os.Setenv("JBP_CONFIG_JAVA_MEMORY_ASSISTANT", "enabled: true")
})
It("returns 'Java Memory Assistant'", func() {
name, err := fw.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(Equal("Java Memory Assistant"))
})
})
Context("with JBP_CONFIG_JAVA_MEMORY_ASSISTANT enabled: false", func() {
BeforeEach(func() {
os.Setenv("JBP_CONFIG_JAVA_MEMORY_ASSISTANT", "enabled: false")
})
It("returns empty string", func() {
name, err := fw.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(BeEmpty())
})
})
Context("with invalid JBP_CONFIG_JAVA_MEMORY_ASSISTANT YAML", func() {
BeforeEach(func() {
os.Setenv("JBP_CONFIG_JAVA_MEMORY_ASSISTANT", "enabled: [not a bool")
})
It("returns empty string without error", func() {
name, err := fw.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(BeEmpty())
})
})
})
Describe("Finalize", func() {
Context("with agent JAR present", func() {
BeforeEach(func() {
installJMAAgent(depsDir, "1.2.3")
})
It("writes the opts file", func() {
Expect(fw.Finalize()).To(Succeed())
Expect(filepath.Join(depsDir, "0", "java_opts", "28_java_memory_assistant.opts")).To(BeAnExistingFile())
})
It("opts file contains -javaagent pointing to the runtime JAR path", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "28_java_memory_assistant.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-javaagent:"))
Expect(string(content)).To(ContainSubstring("$DEPS_DIR/0/java_memory_assistant/java-memory-assistant-1.2.3.jar"))
})
It("opts file does not embed the staging-time absolute path", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "28_java_memory_assistant.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).NotTo(ContainSubstring(depsDir))
Expect(string(content)).To(ContainSubstring("$DEPS_DIR"))
})
It("uses priority prefix 28 in the filename", func() {
Expect(fw.Finalize()).To(Succeed())
entries, err := os.ReadDir(filepath.Join(depsDir, "0", "java_opts"))
Expect(err).NotTo(HaveOccurred())
Expect(entries).To(HaveLen(1))
Expect(entries[0].Name()).To(Equal("28_java_memory_assistant.opts"))
})
It("opts file contains default check interval", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "28_java_memory_assistant.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Djma.check_interval=5s"))
})
It("opts file contains default max frequency", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "28_java_memory_assistant.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Djma.max_frequency=1/1m"))
})
It("opts file contains default old_gen threshold", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "28_java_memory_assistant.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Djma.thresholds.old_gen=>600MB"))
})
It("opts file contains heap dump folder defaulting to $PWD", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "28_java_memory_assistant.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Djma.heap_dump_folder=$PWD"))
})
})
Context("with custom check_interval via JBP_CONFIG_JAVA_MEMORY_ASSISTANT", func() {
BeforeEach(func() {
installJMAAgent(depsDir, "1.2.3")
os.Setenv("JBP_CONFIG_JAVA_MEMORY_ASSISTANT", "agent:\n check_interval: 10s")
})
It("opts file contains the configured check interval", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "28_java_memory_assistant.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Djma.check_interval=10s"))
})
})
Context("with custom max_frequency via JBP_CONFIG_JAVA_MEMORY_ASSISTANT", func() {
BeforeEach(func() {
installJMAAgent(depsDir, "1.2.3")
os.Setenv("JBP_CONFIG_JAVA_MEMORY_ASSISTANT", "agent:\n max_frequency: 2/5m")
})
It("opts file contains the configured max frequency", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "28_java_memory_assistant.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Djma.max_frequency=2/5m"))
})
})
Context("with custom old_gen threshold via JBP_CONFIG_JAVA_MEMORY_ASSISTANT", func() {
BeforeEach(func() {
installJMAAgent(depsDir, "1.2.3")
os.Setenv("JBP_CONFIG_JAVA_MEMORY_ASSISTANT", "agent:\n thresholds:\n old_gen: \">80%\"")
})
It("opts file contains the configured old_gen threshold", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "28_java_memory_assistant.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Djma.thresholds.old_gen=>80%"))
})
})
Context("with heap threshold set via JBP_CONFIG_JAVA_MEMORY_ASSISTANT", func() {
BeforeEach(func() {
installJMAAgent(depsDir, "1.2.3")
os.Setenv("JBP_CONFIG_JAVA_MEMORY_ASSISTANT", "agent:\n thresholds:\n heap: \">90%\"")
})
It("opts file contains -Djma.thresholds.heap", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "28_java_memory_assistant.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Djma.thresholds.heap=>90%"))
})
})
Context("with log_level set via JBP_CONFIG_JAVA_MEMORY_ASSISTANT", func() {
BeforeEach(func() {
installJMAAgent(depsDir, "1.2.3")
os.Setenv("JBP_CONFIG_JAVA_MEMORY_ASSISTANT", "agent:\n log_level: DEBUG")
})
It("opts file contains -Djma.log_level=DEBUG", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "28_java_memory_assistant.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Djma.log_level=DEBUG"))
})
})
Context("with a heap-dump volume service in VCAP_SERVICES", func() {
BeforeEach(func() {
installJMAAgent(depsDir, "1.2.3")
os.Setenv("VCAP_SERVICES", `{"user-provided":[{"name":"heap-dump","label":"user-provided","tags":[],"credentials":{}}]}`)
})
It("opts file contains heap dump folder pointing to the volume mount", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "28_java_memory_assistant.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Djma.heap_dump_folder=$HEAP_DUMP_VOLUME/heapdumps"))
})
})
Context("with Java 9+ (JAVA_HOME set to a mock java 11 installation)", func() {
BeforeEach(func() {
installJMAAgent(depsDir, "1.2.3")
// Create a fake java binary that reports version 11
javaHome := filepath.Join(depsDir, "java11")
javabin := filepath.Join(javaHome, "bin")
Expect(os.MkdirAll(javabin, 0755)).To(Succeed())
releaseFile := filepath.Join(javaHome, "release")
Expect(os.WriteFile(releaseFile, []byte(`JAVA_VERSION="11.0.2"`), 0644)).To(Succeed())
os.Setenv("JAVA_HOME", javaHome)
})
It("opts file contains --add-opens flag", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "28_java_memory_assistant.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("--add-opens jdk.management/com.sun.management.internal=ALL-UNNAMED"))
})
})
Context("without JAVA_HOME set", func() {
BeforeEach(func() {
installJMAAgent(depsDir, "1.2.3")
os.Unsetenv("JAVA_HOME")
})
It("succeeds and writes the opts file without --add-opens", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "28_java_memory_assistant.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).NotTo(ContainSubstring("--add-opens"))
})
})
Context("when the agent JAR is not present", func() {
It("returns an error", func() {
err := fw.Finalize()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Java Memory Assistant JAR not found"))
})
})
Context("with the exact user config: enabled, heap threshold 80%, heap_dump_folder /home/vcap/, check_interval 5m", func() {
BeforeEach(func() {
installJMAAgent(depsDir, "1.2.3")
os.Setenv("JBP_CONFIG_JAVA_MEMORY_ASSISTANT",
`{enabled : true, agent: { thresholds : { heap: "80%" }, heap_dump_folder: /home/vcap/, check_interval: 5m } }`)
})
It("opts file contains -Djma.thresholds.heap=80%", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "28_java_memory_assistant.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Djma.thresholds.heap=80%"))
})
It("opts file contains -Djma.heap_dump_folder=/home/vcap/", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "28_java_memory_assistant.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Djma.heap_dump_folder=/home/vcap/"))
})
It("opts file contains -Djma.check_interval=5m", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "28_java_memory_assistant.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Djma.check_interval=5m"))
})
})
})
})