Repository navigation
Expand file tree
/
Copy pathjmx_test.go
More file actions
276 lines (236 loc) · 8.16 KB
/
Copy pathjmx_test.go
File metadata and controls
276 lines (236 loc) · 8.16 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
package frameworks_test
import (
"fmt"
"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 newJmxContext(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{},
}
}
var _ = Describe("JMX", func() {
var (
fw *frameworks.JmxFramework
buildDir string
cacheDir string
depsDir string
)
BeforeEach(func() {
var err error
buildDir, err = os.MkdirTemp("", "jmx-build")
Expect(err).NotTo(HaveOccurred())
cacheDir, err = os.MkdirTemp("", "jmx-cache")
Expect(err).NotTo(HaveOccurred())
depsDir, err = os.MkdirTemp("", "jmx-deps")
Expect(err).NotTo(HaveOccurred())
Expect(os.MkdirAll(filepath.Join(depsDir, "0"), 0755)).To(Succeed())
fw = frameworks.NewJmxFramework(newJmxContext(buildDir, cacheDir, depsDir))
})
AfterEach(func() {
os.RemoveAll(buildDir)
os.RemoveAll(cacheDir)
os.RemoveAll(depsDir)
os.Unsetenv("BPL_JMX_ENABLED")
os.Unsetenv("BPL_JMX_PORT")
os.Unsetenv("JBP_CONFIG_JMX")
})
Describe("Detect", func() {
Context("with no configuration (default)", func() {
It("returns empty string (disabled by default)", func() {
name, err := fw.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(BeEmpty())
})
})
Context("with BPL_JMX_ENABLED=true", func() {
BeforeEach(func() {
os.Setenv("BPL_JMX_ENABLED", "true")
})
It("returns 'JMX'", func() {
name, err := fw.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(Equal("JMX"))
})
})
Context("with BPL_JMX_ENABLED=1", func() {
BeforeEach(func() {
os.Setenv("BPL_JMX_ENABLED", "1")
})
It("returns 'JMX'", func() {
name, err := fw.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(Equal("JMX"))
})
})
Context("with BPL_JMX_ENABLED=false", func() {
BeforeEach(func() {
os.Setenv("BPL_JMX_ENABLED", "false")
})
It("returns empty string", func() {
name, err := fw.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(BeEmpty())
})
})
Context("with BPL_JMX_ENABLED=0", func() {
BeforeEach(func() {
os.Setenv("BPL_JMX_ENABLED", "0")
})
It("returns empty string", func() {
name, err := fw.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(BeEmpty())
})
})
Context("with JBP_CONFIG_JMX enabled: true", func() {
BeforeEach(func() {
os.Setenv("JBP_CONFIG_JMX", "enabled: true")
})
It("returns 'JMX'", func() {
name, err := fw.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(Equal("JMX"))
})
})
Context("with JBP_CONFIG_JMX enabled: false", func() {
BeforeEach(func() {
os.Setenv("JBP_CONFIG_JMX", "enabled: false")
})
It("returns empty string", func() {
name, err := fw.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(BeEmpty())
})
})
Context("when BPL_JMX_ENABLED overrides JBP_CONFIG_JMX", func() {
BeforeEach(func() {
os.Setenv("JBP_CONFIG_JMX", "enabled: false")
os.Setenv("BPL_JMX_ENABLED", "true")
})
It("BPL_JMX_ENABLED takes precedence — returns 'JMX'", func() {
name, err := fw.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(Equal("JMX"))
})
})
Context("when BPL_JMX_ENABLED=false overrides JBP_CONFIG_JMX enabled: true", func() {
BeforeEach(func() {
os.Setenv("JBP_CONFIG_JMX", "enabled: true")
os.Setenv("BPL_JMX_ENABLED", "false")
})
It("BPL_JMX_ENABLED takes precedence — returns empty string", func() {
name, err := fw.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(BeEmpty())
})
})
})
Describe("Finalize", func() {
Context("with default port (5000)", func() {
BeforeEach(func() {
os.Setenv("BPL_JMX_ENABLED", "true")
})
It("writes the opts file", func() {
Expect(fw.Finalize()).To(Succeed())
Expect(filepath.Join(depsDir, "0", "java_opts", "29_jmx.opts")).To(BeAnExistingFile())
})
It("opts file contains the default port 5000", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "29_jmx.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Dcom.sun.management.jmxremote.port=5000"))
Expect(string(content)).To(ContainSubstring("-Dcom.sun.management.jmxremote.rmi.port=5000"))
})
It("opts file disables authentication and SSL", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "29_jmx.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Dcom.sun.management.jmxremote.authenticate=false"))
Expect(string(content)).To(ContainSubstring("-Dcom.sun.management.jmxremote.ssl=false"))
})
It("opts file sets RMI hostname to 127.0.0.1", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "29_jmx.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Djava.rmi.server.hostname=127.0.0.1"))
})
})
Context("with BPL_JMX_PORT set", func() {
BeforeEach(func() {
os.Setenv("BPL_JMX_ENABLED", "true")
os.Setenv("BPL_JMX_PORT", "9090")
})
It("uses the port from BPL_JMX_PORT", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "29_jmx.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Dcom.sun.management.jmxremote.port=9090"))
Expect(string(content)).To(ContainSubstring("-Dcom.sun.management.jmxremote.rmi.port=9090"))
})
})
Context("with port set via JBP_CONFIG_JMX", func() {
BeforeEach(func() {
os.Setenv("JBP_CONFIG_JMX", "enabled: true\nport: 7777")
})
It("uses the port from JBP_CONFIG_JMX", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "29_jmx.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Dcom.sun.management.jmxremote.port=7777"))
Expect(string(content)).To(ContainSubstring("-Dcom.sun.management.jmxremote.rmi.port=7777"))
})
})
Context("when BPL_JMX_PORT overrides JBP_CONFIG_JMX port", func() {
BeforeEach(func() {
os.Setenv("JBP_CONFIG_JMX", "enabled: true\nport: 7777")
os.Setenv("BPL_JMX_PORT", "8888")
})
It("BPL_JMX_PORT takes precedence", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "29_jmx.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring(fmt.Sprintf("-Dcom.sun.management.jmxremote.port=%d", 8888)))
Expect(string(content)).NotTo(ContainSubstring("7777"))
})
})
Context("with an invalid BPL_JMX_PORT value", func() {
BeforeEach(func() {
os.Setenv("BPL_JMX_ENABLED", "true")
os.Setenv("BPL_JMX_PORT", "not-a-number")
})
It("falls back to the default port 5000", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "29_jmx.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-Dcom.sun.management.jmxremote.port=5000"))
})
})
Context("opts file naming and priority", func() {
BeforeEach(func() {
os.Setenv("BPL_JMX_ENABLED", "true")
})
It("uses priority prefix 29 in the filename", func() {
Expect(fw.Finalize()).To(Succeed())
optsDir := filepath.Join(depsDir, "0", "java_opts")
entries, err := os.ReadDir(optsDir)
Expect(err).NotTo(HaveOccurred())
Expect(entries).To(HaveLen(1))
Expect(entries[0].Name()).To(Equal("29_jmx.opts"))
})
})
})
})