Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.opentelemetry.smoketest;

import java.util.function.Consumer;
import org.junit.jupiter.api.TestInstance;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public abstract class AbstractSmokeTest {
public abstract void configureTelemetryRetriever(Consumer<JavaTelemetryRetriever> action);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public List<SpanData> waitForTraces() {
convert(requests, ExportTraceServiceRequest::getResourceSpansList));
}

public Collection<MetricData> waitForMetrics() {
public List<MetricData> waitForMetrics() {
Collection<ExportMetricsServiceRequest> requests =
waitForTelemetry("get-metrics", ExportMetricsServiceRequest::newBuilder);
return TelemetryConverter.getMetricsData(
convert(requests, ExportMetricsServiceRequest::getResourceMetricsList));
}

public Collection<LogRecordData> waitForLogs() {
public List<LogRecordData> waitForLogs() {
Collection<ExportLogsServiceRequest> requests =
waitForTelemetry("get-logs", ExportLogsServiceRequest::newBuilder);
return TelemetryConverter.getLogRecordData(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.opentelemetry.smoketest;

import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import org.junit.jupiter.api.extension.ExtensionContext;

public class SmokeTestInstrumentationExtension extends InstrumentationExtension {
private SmokeTestInstrumentationExtension() {
super(SmokeTestRunner.instance());
}

public static SmokeTestInstrumentationExtension create() {
return new SmokeTestInstrumentationExtension();
}

@Override
public void beforeEach(ExtensionContext extensionContext) {
Object testInstance = extensionContext.getRequiredTestInstance();

if (!(testInstance instanceof AbstractSmokeTest)) {
throw new AssertionError(
"SmokeTestInstrumentationExtension can only be applied to a subclass of "
+ "AbstractSmokeTest");
}

SmokeTestRunner smokeTestRunner = (SmokeTestRunner) getTestRunner();
((AbstractSmokeTest) testInstance).configureTelemetryRetriever(
smokeTestRunner::setTelemetryRetriever);

super.beforeEach(extensionContext);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.opentelemetry.smoketest;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.testing.InstrumentationTestRunner;
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.trace.data.SpanData;
import java.util.List;

public class SmokeTestRunner extends InstrumentationTestRunner {

private static final SmokeTestRunner INSTANCE = new SmokeTestRunner();

private JavaTelemetryRetriever telemetryRetriever;

public static SmokeTestRunner instance() {
return INSTANCE;
}

private SmokeTestRunner() {
super(OpenTelemetry.noop());
}

void setTelemetryRetriever(JavaTelemetryRetriever telemetryRetriever) {
this.telemetryRetriever = telemetryRetriever;
}

@Override
public void beforeTestClass() {
}

@Override
public void afterTestClass() {
}

@Override
public void clearAllExportedData() {
telemetryRetriever.clearTelemetry();
}

@Override
public OpenTelemetry getOpenTelemetry() {
throw new UnsupportedOperationException();
}

@Override
public List<SpanData> getExportedSpans() {
return telemetryRetriever.waitForTraces();
}

@Override
public List<MetricData> getExportedMetrics() {
return telemetryRetriever.waitForMetrics();
}

@Override
public List<LogRecordData> getExportedLogRecords() {
return telemetryRetriever.waitForLogs();
}

@Override
public boolean forceFlushCalled() {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.testcontainers.containers.output.OutputFrame;

public abstract class JavaSmokeTest {
public abstract class JavaSmokeTest extends AbstractSmokeTest {
protected static final TestContainerManager containerManager = createContainerManager();
private JavaTelemetryRetriever telemetryRetriever;

Expand Down Expand Up @@ -59,7 +59,7 @@ protected List<Integer> getExtraPorts() {
return emptyList();
}

@BeforeEach
@BeforeAll
void setUp() {
containerManager.startEnvironmentOnce();
telemetryRetriever = new JavaTelemetryRetriever(containerManager.getBackendMappedPort());
Expand Down Expand Up @@ -132,4 +132,9 @@ private static TestContainerManager createContainerManager() {
? new WindowsTestContainerManager()
: new LinuxTestContainerManager();
}

@Override
public void configureTelemetryRetriever(Consumer<JavaTelemetryRetriever> action) {
action.accept(telemetryRetriever);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@

package io.opentelemetry.smoketest;

import static io.opentelemetry.sdk.testing.assertj.TracesAssert.assertThat;

import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.condition.DisabledIf;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@DisabledIf("io.opentelemetry.smoketest.TestContainerManager#useWindowsContainers")
class SecurityManagerSmokeTest extends JavaSmokeTest {
@RegisterExtension static final InstrumentationExtension testing = SmokeTestInstrumentationExtension.create();
@RegisterExtension static final AutoCleanupExtension autoCleanup = AutoCleanupExtension.create();

@Override
protected String getTargetImage(String jdk) {
return "ghcr.io/open-telemetry/opentelemetry-java-instrumentation/smoke-test-security-manager:jdk"
Expand All @@ -30,12 +34,10 @@ protected Map<String, String> getExtraEnv() {

@ParameterizedTest
@ValueSource(ints = {8, 11, 17, 21, 23})
void securityManagerSmokeTest(int jdk) throws Exception {
withTarget(
jdk,
() ->
assertThat(waitForTraces())
.hasTracesSatisfyingExactly(
trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("test"))));
void securityManagerSmokeTest(int jdk) {
startTarget(jdk);
autoCleanup.deferCleanup(this::stopTarget);

testing.waitAndAssertTraces(trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("test")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static void assertScopeVersion(List<List<SpanData>> traces) {
for (List<SpanData> trace : traces) {
for (SpanData span : trace) {
InstrumentationScopeInfo scopeInfo = span.getInstrumentationScopeInfo();
if (!scopeInfo.getName().equals("test")) {
if (!scopeInfo.getName().startsWith("test")) {
assertThat(scopeInfo.getVersion())
.as(
"Instrumentation version of module %s was empty; make sure that the "
Expand Down
Loading