-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathjacoco_agent_test.go
More file actions
351 lines (303 loc) · 11.6 KB
/
jacoco_agent_test.go
File metadata and controls
351 lines (303 loc) · 11.6 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
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 newJacocoContext(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{},
}
}
// jacocoVCAPServices builds a VCAP_SERVICES JSON for a JaCoCo service.
// address is the required credential; extraCreds is an optional comma-separated
// list of additional JSON key:value pairs added to credentials.
func jacocoVCAPServices(label, name string, tags []string, address, extraCreds string) string {
tagJSON := "[]"
if len(tags) > 0 {
parts := make([]string, len(tags))
for i, t := range tags {
parts[i] = fmt.Sprintf("%q", t)
}
tagJSON = "[" + joinJacocoStrings(parts) + "]"
}
creds := fmt.Sprintf(`"address":%q`, address)
if extraCreds != "" {
creds += "," + extraCreds
}
return fmt.Sprintf(`{%q:[{"name":%q,"label":%q,"tags":%s,"credentials":{%s}}]}`,
label, name, label, tagJSON, creds)
}
func joinJacocoStrings(ss []string) string {
result := ""
for i, s := range ss {
if i > 0 {
result += ","
}
result += s
}
return result
}
// installJacocoAgent creates jacocoagent.jar at the expected path under depsDir.
func installJacocoAgent(depsDir string) {
agentDir := filepath.Join(depsDir, "0", "jacoco_agent")
Expect(os.MkdirAll(agentDir, 0755)).To(Succeed())
Expect(os.WriteFile(filepath.Join(agentDir, "jacocoagent.jar"), []byte("fake jar"), 0644)).To(Succeed())
}
var _ = Describe("JaCoCo Agent", func() {
var (
fw *frameworks.JacocoAgentFramework
buildDir string
cacheDir string
depsDir string
)
BeforeEach(func() {
var err error
buildDir, err = os.MkdirTemp("", "jacoco-build")
Expect(err).NotTo(HaveOccurred())
cacheDir, err = os.MkdirTemp("", "jacoco-cache")
Expect(err).NotTo(HaveOccurred())
depsDir, err = os.MkdirTemp("", "jacoco-deps")
Expect(err).NotTo(HaveOccurred())
Expect(os.MkdirAll(filepath.Join(depsDir, "0"), 0755)).To(Succeed())
fw = frameworks.NewJacocoAgentFramework(newJacocoContext(buildDir, cacheDir, depsDir))
})
AfterEach(func() {
os.RemoveAll(buildDir)
os.RemoveAll(cacheDir)
os.RemoveAll(depsDir)
os.Unsetenv("VCAP_SERVICES")
})
Describe("Detect", func() {
Context("with no VCAP_SERVICES set", func() {
It("returns empty string", func() {
name, err := fw.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(BeEmpty())
})
})
Context("with service bound by label 'jacoco' and address credential", func() {
BeforeEach(func() {
os.Setenv("VCAP_SERVICES", jacocoVCAPServices("jacoco", "my-jacoco", nil, "localhost:6300", ""))
})
It("returns 'JaCoCo Agent'", func() {
name, err := fw.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(Equal("JaCoCo Agent"))
})
})
Context("with service name containing 'jacoco' and address credential", func() {
BeforeEach(func() {
os.Setenv("VCAP_SERVICES", jacocoVCAPServices("user-provided", "prod-jacoco-coverage", nil, "localhost:6300", ""))
})
It("returns 'JaCoCo Agent'", func() {
name, err := fw.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(Equal("JaCoCo Agent"))
})
})
Context("with service bound by label 'jacoco' but address credential missing", func() {
BeforeEach(func() {
os.Setenv("VCAP_SERVICES", `{"jacoco":[{"name":"my-jacoco","label":"jacoco","tags":[],"credentials":{"other":"value"}}]}`)
})
It("returns empty string", func() {
name, err := fw.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(BeEmpty())
})
})
Context("with an unrelated service bound", func() {
BeforeEach(func() {
os.Setenv("VCAP_SERVICES", jacocoVCAPServices("newrelic", "my-newrelic", []string{"apm"}, "some-addr", ""))
})
It("returns empty string", func() {
name, err := fw.Detect()
Expect(err).NotTo(HaveOccurred())
Expect(name).To(BeEmpty())
})
})
Context("with invalid VCAP_SERVICES JSON", func() {
BeforeEach(func() {
os.Setenv("VCAP_SERVICES", "{invalid json")
})
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 and required address credential", func() {
BeforeEach(func() {
installJacocoAgent(depsDir)
os.Setenv("VCAP_SERVICES", jacocoVCAPServices("jacoco", "my-jacoco", nil, "jacoco-server.example.com:6300", ""))
})
It("writes the opts file", func() {
Expect(fw.Finalize()).To(Succeed())
Expect(filepath.Join(depsDir, "0", "java_opts", "26_jacoco.opts")).To(BeAnExistingFile())
})
It("opts file contains -javaagent pointing to the runtime jacocoagent.jar path", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "26_jacoco.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("-javaagent:"))
Expect(string(content)).To(ContainSubstring("$DEPS_DIR/0/jacoco_agent/jacocoagent.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", "26_jacoco.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).NotTo(ContainSubstring(depsDir))
Expect(string(content)).To(ContainSubstring("$DEPS_DIR"))
})
It("uses priority prefix 26 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("26_jacoco.opts"))
})
It("opts file contains address property", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "26_jacoco.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("address=jacoco-server.example.com:6300"))
})
It("opts file contains default output=tcpclient", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "26_jacoco.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("output=tcpclient"))
})
It("opts file contains sessionid=$CF_INSTANCE_GUID", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "26_jacoco.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("sessionid=$CF_INSTANCE_GUID"))
})
})
Context("with optional 'excludes' credential", func() {
BeforeEach(func() {
installJacocoAgent(depsDir)
os.Setenv("VCAP_SERVICES", jacocoVCAPServices("jacoco", "my-jacoco", nil, "host:6300",
`"excludes":"com.example.generated.*"`))
})
It("opts file contains excludes property", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "26_jacoco.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("excludes=com.example.generated.*"))
})
})
Context("with optional 'includes' credential", func() {
BeforeEach(func() {
installJacocoAgent(depsDir)
os.Setenv("VCAP_SERVICES", jacocoVCAPServices("jacoco", "my-jacoco", nil, "host:6300",
`"includes":"com.example.*"`))
})
It("opts file contains includes property", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "26_jacoco.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("includes=com.example.*"))
})
})
Context("with optional 'port' credential", func() {
BeforeEach(func() {
installJacocoAgent(depsDir)
os.Setenv("VCAP_SERVICES", jacocoVCAPServices("jacoco", "my-jacoco", nil, "host:6300",
`"port":"6301"`))
})
It("opts file contains port property", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "26_jacoco.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("port=6301"))
})
})
Context("with optional 'output' credential overriding the default", func() {
BeforeEach(func() {
installJacocoAgent(depsDir)
os.Setenv("VCAP_SERVICES", jacocoVCAPServices("jacoco", "my-jacoco", nil, "host:6300",
`"output":"file"`))
})
It("opts file contains the overridden output property", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "26_jacoco.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("output=file"))
Expect(string(content)).NotTo(ContainSubstring("output=tcpclient"))
})
})
Context("with service detected via name pattern", func() {
BeforeEach(func() {
installJacocoAgent(depsDir)
os.Setenv("VCAP_SERVICES", jacocoVCAPServices("user-provided", "prod-jacoco-svc", nil, "host:6300", ""))
})
It("writes the opts file successfully", func() {
Expect(fw.Finalize()).To(Succeed())
Expect(filepath.Join(depsDir, "0", "java_opts", "26_jacoco.opts")).To(BeAnExistingFile())
})
})
Context("when VCAP_SERVICES has no jacoco service", func() {
BeforeEach(func() {
installJacocoAgent(depsDir)
os.Setenv("VCAP_SERVICES", `{}`)
})
It("returns an error", func() {
err := fw.Finalize()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("JaCoCo service binding not found"))
})
})
Context("when address credential is missing", func() {
BeforeEach(func() {
installJacocoAgent(depsDir)
os.Setenv("VCAP_SERVICES", `{"jacoco":[{"name":"my-jacoco","label":"jacoco","tags":[],"credentials":{"other":"value"}}]}`)
})
It("returns an error", func() {
err := fw.Finalize()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("address"))
})
})
Context("when jacocoagent.jar is not present", func() {
BeforeEach(func() {
os.Setenv("VCAP_SERVICES", jacocoVCAPServices("jacoco", "my-jacoco", nil, "host:6300", ""))
})
It("returns an error", func() {
err := fw.Finalize()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("jacocoagent.jar"))
})
})
Context("with jacocoagent.jar installed under a lib subdirectory", func() {
BeforeEach(func() {
libDir := filepath.Join(depsDir, "0", "jacoco_agent", "lib")
Expect(os.MkdirAll(libDir, 0755)).To(Succeed())
Expect(os.WriteFile(filepath.Join(libDir, "jacocoagent.jar"), []byte("fake jar"), 0644)).To(Succeed())
os.Setenv("VCAP_SERVICES", jacocoVCAPServices("jacoco", "my-jacoco", nil, "host:6300", ""))
})
It("resolves the JAR from the lib subdirectory", func() {
Expect(fw.Finalize()).To(Succeed())
content, err := os.ReadFile(filepath.Join(depsDir, "0", "java_opts", "26_jacoco.opts"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("$DEPS_DIR/0/jacoco_agent/lib/jacocoagent.jar"))
})
})
})
})