|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
| 10 | +import java.io.File; |
| 11 | +import java.util.Collections; |
| 12 | +import jvmbootstraptest.AgentLoadedChecker; |
| 13 | +import jvmbootstraptest.MyClassLoaderIsNotBootstrap; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +class AgentLoadedIntoBootstrapTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + void agentLoadsInWhenSeparateJvmIsLaunched() throws Exception { |
| 20 | + int exitCode = IntegrationTestUtils.runOnSeparateJvm( |
| 21 | + AgentLoadedChecker.class.getName(), |
| 22 | + new String[0], |
| 23 | + new String[0], |
| 24 | + Collections.emptyMap(), |
| 25 | + true); |
| 26 | + |
| 27 | + assertThat(exitCode).isEqualTo(0); |
| 28 | + } |
| 29 | + |
| 30 | + // this tests the case where someone adds the contents of opentelemetry-javaagent.jar by mistake |
| 31 | + // to their application's "uber.jar" |
| 32 | + // |
| 33 | + // the reason this can cause issues is because we locate the agent jar based on the CodeSource of |
| 34 | + // the OpenTelemetryAgent class, and then we add that jar file to the bootstrap class path |
| 35 | + // |
| 36 | + // but if we find the OpenTelemetryAgent class in an uber jar file, and we add that (whole) uber |
| 37 | + // jar file to the bootstrap class loader, that can cause some applications to break, as there's a |
| 38 | + // lot of application and library code that doesn't handle getClassLoader() returning null |
| 39 | + // (e.g. https://github.com/qos-ch/logback/pull/291) |
| 40 | + @Test |
| 41 | + void applicationUberJarShouldNotBeAddedToTheBootstrapClassLoader() throws Exception { |
| 42 | + String mainClassName = MyClassLoaderIsNotBootstrap.class.getName(); |
| 43 | + String pathToJar = IntegrationTestUtils.createJarWithClasses( |
| 44 | + mainClassName, |
| 45 | + MyClassLoaderIsNotBootstrap.class, |
| 46 | + OpenTelemetryAgent.class).getPath(); |
| 47 | + |
| 48 | + try { |
| 49 | + int exitCode = IntegrationTestUtils.runOnSeparateJvm( |
| 50 | + mainClassName, |
| 51 | + new String[0], |
| 52 | + new String[0], |
| 53 | + Collections.emptyMap(), |
| 54 | + pathToJar, |
| 55 | + true); |
| 56 | + |
| 57 | + assertThat(exitCode).isEqualTo(0); |
| 58 | + } finally { |
| 59 | + new File(pathToJar).delete(); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments