@@ -51,8 +51,16 @@ import (
51
51
"github.com/signalfx/splunk-otel-collector/tests/testutils"
52
52
)
53
53
54
- func cleanUp () {
55
- configureEnvironmentOnce = sync.Once {}
54
+ func cleanUp () func () {
55
+ jh := "JAVA_HOME"
56
+ existing , ok := os .LookupEnv (jh )
57
+ os .Unsetenv (jh )
58
+ return func () {
59
+ configureEnvironmentOnce = sync.Once {}
60
+ if ok {
61
+ os .Setenv (jh , existing )
62
+ }
63
+ }
56
64
}
57
65
58
66
func newReceiverCreateSettings (name string ) otelcolreceiver.CreateSettings {
@@ -101,7 +109,7 @@ func newConfig(monitorType string, intervalSeconds int) Config {
101
109
}
102
110
103
111
func TestSmartAgentReceiver (t * testing.T ) {
104
- t .Cleanup (cleanUp )
112
+ t .Cleanup (cleanUp () )
105
113
cfg := newConfig ("cpu" , 10 )
106
114
consumer := new (consumertest.MetricsSink )
107
115
receiver := newReceiver (newReceiverCreateSettings ("valid" ), cfg )
@@ -192,7 +200,7 @@ func TestStripMonitorTypePrefix(t *testing.T) {
192
200
}
193
201
194
202
func TestStartReceiverWithInvalidMonitorConfig (t * testing.T ) {
195
- t .Cleanup (cleanUp )
203
+ t .Cleanup (cleanUp () )
196
204
cfg := newConfig ("cpu" , - 123 )
197
205
receiver := newReceiver (newReceiverCreateSettings ("invalid" ), cfg )
198
206
err := receiver .Start (context .Background (), componenttest .NewNopHost ())
@@ -202,7 +210,7 @@ func TestStartReceiverWithInvalidMonitorConfig(t *testing.T) {
202
210
}
203
211
204
212
func TestStartReceiverWithUnknownMonitorType (t * testing.T ) {
205
- t .Cleanup (cleanUp )
213
+ t .Cleanup (cleanUp () )
206
214
cfg := newConfig ("notamonitortype" , 1 )
207
215
receiver := newReceiver (newReceiverCreateSettings ("invalid" ), cfg )
208
216
err := receiver .Start (context .Background (), componenttest .NewNopHost ())
@@ -212,7 +220,7 @@ func TestStartReceiverWithUnknownMonitorType(t *testing.T) {
212
220
}
213
221
214
222
func TestStartAndShutdown (t * testing.T ) {
215
- t .Cleanup (cleanUp )
223
+ t .Cleanup (cleanUp () )
216
224
cfg := newConfig ("cpu" , 1 )
217
225
receiver := newReceiver (newReceiverCreateSettings ("valid" ), cfg )
218
226
err := receiver .Start (context .Background (), componenttest .NewNopHost ())
@@ -223,7 +231,7 @@ func TestStartAndShutdown(t *testing.T) {
223
231
}
224
232
225
233
func TestOutOfOrderShutdownInvocations (t * testing.T ) {
226
- t .Cleanup (cleanUp )
234
+ t .Cleanup (cleanUp () )
227
235
cfg := newConfig ("cpu" , 1 )
228
236
receiver := newReceiver (newReceiverCreateSettings ("valid" ), cfg )
229
237
@@ -235,7 +243,7 @@ func TestOutOfOrderShutdownInvocations(t *testing.T) {
235
243
}
236
244
237
245
func TestMultipleInstancesOfSameMonitorType (t * testing.T ) {
238
- t .Cleanup (cleanUp )
246
+ t .Cleanup (cleanUp () )
239
247
cfg := newConfig ("cpu" , 1 )
240
248
fstRcvr := newReceiver (newReceiverCreateSettings ("valid" ), cfg )
241
249
@@ -250,7 +258,7 @@ func TestMultipleInstancesOfSameMonitorType(t *testing.T) {
250
258
}
251
259
252
260
func TestInvalidMonitorStateAtShutdown (t * testing.T ) {
253
- t .Cleanup (cleanUp )
261
+ t .Cleanup (cleanUp () )
254
262
cfg := newConfig ("cpu" , 1 )
255
263
receiver := newReceiver (newReceiverCreateSettings ("valid" ), cfg )
256
264
receiver .monitor = new (any )
@@ -261,7 +269,7 @@ func TestInvalidMonitorStateAtShutdown(t *testing.T) {
261
269
}
262
270
263
271
func TestConfirmStartingReceiverWithInvalidMonitorInstancesDoesntPanic (t * testing.T ) {
264
- t .Cleanup (cleanUp )
272
+ t .Cleanup (cleanUp () )
265
273
tests := []struct {
266
274
name string
267
275
monitorFactory func () any
@@ -291,7 +299,7 @@ func TestConfirmStartingReceiverWithInvalidMonitorInstancesDoesntPanic(t *testin
291
299
}
292
300
293
301
func TestFilteringNoMetadata (t * testing.T ) {
294
- t .Cleanup (cleanUp )
302
+ t .Cleanup (cleanUp () )
295
303
monitors .MonitorFactories ["fakemonitor" ] = func () any { return struct {}{} }
296
304
cfg := newConfig ("fakemonitor" , 1 )
297
305
receiver := newReceiver (newReceiverCreateSettings ("valid" ), cfg )
@@ -300,7 +308,7 @@ func TestFilteringNoMetadata(t *testing.T) {
300
308
}
301
309
302
310
func TestSmartAgentConfigProviderOverrides (t * testing.T ) {
303
- t .Cleanup (cleanUp )
311
+ t .Cleanup (cleanUp () )
304
312
cfg := newConfig ("cpu" , 1 )
305
313
observedLogger , logs := observer .New (zapcore .WarnLevel )
306
314
logger := zap .New (observedLogger )
@@ -355,6 +363,22 @@ func TestSmartAgentConfigProviderOverrides(t *testing.T) {
355
363
require .Equal (t , "/etc" , hostfs .HostEtc ())
356
364
}
357
365
366
+ func TestJavaHomeRespected (t * testing.T ) {
367
+ if runtime .GOOS == "windows" {
368
+ t .Skip ("non-windows only" )
369
+ }
370
+ t .Cleanup (cleanUp ())
371
+ os .Setenv ("JAVA_HOME" , "/existing/java/home" )
372
+ cfg := newConfig ("cpu" , 1 )
373
+ rcs := newReceiverCreateSettings ("valid" )
374
+ r := newReceiver (rcs , cfg )
375
+
376
+ require .NoError (t , r .Start (context .Background (), componenttest .NewNopHost ()))
377
+ require .NoError (t , r .Shutdown (context .Background ()))
378
+
379
+ require .Equal (t , "/existing/java/home" , os .Getenv ("JAVA_HOME" ))
380
+ }
381
+
358
382
func getSmartAgentExtensionConfig (t * testing.T ) []* smartagentextension.Config {
359
383
360
384
cfg , err := confmaptest .LoadConf (path .Join ("." , "testdata" , "extension_config.yaml" ))
0 commit comments