Skip to content

Commit eaccb17

Browse files
committed
convert groovy smoke tests to java
1 parent f44456e commit eaccb17

File tree

7 files changed

+386
-148
lines changed

7 files changed

+386
-148
lines changed

smoke-tests/src/test/groovy/io/opentelemetry/smoketest/AgentDebugLoggingTest.groovy

Lines changed: 0 additions & 33 deletions
This file was deleted.

smoke-tests/src/test/groovy/io/opentelemetry/smoketest/SecurityManagerSmokeTest.groovy

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.smoketest;
7+
8+
import java.time.Duration;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.condition.DisabledIf;
11+
12+
@DisabledIf("io.opentelemetry.smoketest.TestContainerManager#useWindowsContainers")
13+
public class AgentDebugLoggingTest extends JavaSmokeTest {
14+
@Override
15+
protected String getTargetImage(String jdk) {
16+
return "ghcr.io/open-telemetry/opentelemetry-java-instrumentation/smoke-test-spring-boot:jdk"
17+
+ jdk
18+
+ "-20211213.1570880324";
19+
}
20+
21+
@Override
22+
protected TargetWaitStrategy getWaitStrategy() {
23+
return new TargetWaitStrategy.Log(
24+
Duration.ofMinutes(1), ".*DEBUG io.opentelemetry.javaagent.tooling.VersionLogger.*");
25+
}
26+
27+
@Test
28+
public void verify_that_debug_logging_is_working() {
29+
startTarget(8);
30+
stopTarget();
31+
}
32+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.smoketest;
7+
8+
import static java.util.Collections.emptyList;
9+
import static java.util.Collections.emptyMap;
10+
11+
import io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest;
12+
import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest;
13+
import io.opentelemetry.sdk.trace.data.SpanData;
14+
import io.opentelemetry.smoketest.windows.WindowsTestContainerManager;
15+
import io.opentelemetry.testing.internal.armeria.client.WebClient;
16+
import java.util.Collection;
17+
import java.util.List;
18+
import java.util.Map;
19+
import java.util.function.Consumer;
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.testcontainers.containers.output.OutputFrame;
22+
23+
public abstract class JavaSmokeTest {
24+
protected static final TestContainerManager containerManager = createContainerManager();
25+
private JavaTelemetryRetriever telemetryRetriever;
26+
27+
protected String agentPath =
28+
System.getProperty("io.opentelemetry.smoketest.agent.shadowJar.path");
29+
30+
protected WebClient client() {
31+
return WebClient.of("h1c://localhost:" + containerManager.getTargetMappedPort(8080));
32+
}
33+
34+
/** Subclasses can override this method to pass jvm arguments in another environment variable */
35+
protected String getJvmArgsEnvVarName() {
36+
return "JAVA_TOOL_OPTIONS";
37+
}
38+
39+
/** Subclasses can override this method to customise target application's environment */
40+
protected Map<String, String> getExtraEnv() {
41+
return emptyMap();
42+
}
43+
44+
/** Subclasses can override this method to disable setting default service name */
45+
protected boolean getSetServiceName() {
46+
return true;
47+
}
48+
49+
/** Subclasses can override this method to provide additional files to copy to target container */
50+
protected List<ResourceMapping> getExtraResources() {
51+
return emptyList();
52+
}
53+
54+
/**
55+
* Subclasses can override this method to provide additional ports that should be exposed from the
56+
* target container
57+
*/
58+
protected List<Integer> getExtraPorts() {
59+
return emptyList();
60+
}
61+
62+
@BeforeEach
63+
void setUp() {
64+
containerManager.startEnvironmentOnce();
65+
telemetryRetriever = new JavaTelemetryRetriever(containerManager.getBackendMappedPort());
66+
}
67+
68+
public Consumer<OutputFrame> startTarget(int jdk) {
69+
return startTarget(String.valueOf(jdk), null, false);
70+
}
71+
72+
public Consumer<OutputFrame> startTarget(String jdk, String serverVersion, boolean windows) {
73+
String targetImage = getTargetImage(jdk, serverVersion, windows);
74+
return containerManager.startTarget(
75+
targetImage,
76+
agentPath,
77+
getJvmArgsEnvVarName(),
78+
getExtraEnv(),
79+
getSetServiceName(),
80+
getExtraResources(),
81+
getExtraPorts(),
82+
getWaitStrategy(),
83+
getCommand());
84+
}
85+
86+
protected abstract String getTargetImage(String jdk);
87+
88+
protected String getTargetImage(String jdk, String serverVersion, boolean windows) {
89+
return getTargetImage(jdk);
90+
}
91+
92+
protected TargetWaitStrategy getWaitStrategy() {
93+
return null;
94+
}
95+
96+
protected String[] getCommand() {
97+
return null;
98+
}
99+
100+
public void cleanup() {
101+
telemetryRetriever.clearTelemetry();
102+
}
103+
104+
public void stopTarget() {
105+
containerManager.stopTarget();
106+
}
107+
108+
protected List<SpanData> waitForTraces() {
109+
return telemetryRetriever.waitForTraces();
110+
}
111+
112+
protected Collection<ExportMetricsServiceRequest> waitForMetrics() {
113+
return telemetryRetriever.waitForMetrics();
114+
}
115+
116+
protected Collection<ExportLogsServiceRequest> waitForLogs() {
117+
return telemetryRetriever.waitForLogs();
118+
}
119+
120+
private static TestContainerManager createContainerManager() {
121+
return TestContainerManager.useWindowsContainers()
122+
? new WindowsTestContainerManager()
123+
: new LinuxTestContainerManager();
124+
}
125+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.smoketest;
7+
8+
import com.fasterxml.jackson.core.JsonProcessingException;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.google.protobuf.GeneratedMessage;
11+
import com.google.protobuf.InvalidProtocolBufferException;
12+
import com.google.protobuf.util.JsonFormat;
13+
import io.opentelemetry.javaagent.testing.common.AgentTestingExporterAccess;
14+
import io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceRequest;
15+
import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest;
16+
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest;
17+
import io.opentelemetry.sdk.trace.data.SpanData;
18+
import io.opentelemetry.testing.internal.armeria.client.WebClient;
19+
import java.util.Collection;
20+
import java.util.List;
21+
import java.util.concurrent.TimeUnit;
22+
import java.util.function.Supplier;
23+
import java.util.stream.Collectors;
24+
import java.util.stream.Stream;
25+
26+
public class JavaTelemetryRetriever {
27+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
28+
private final WebClient client;
29+
30+
public JavaTelemetryRetriever(int backendPort) {
31+
client = WebClient.of("http://localhost:" + backendPort);
32+
}
33+
34+
public void clearTelemetry() {
35+
client.get("/clear").aggregate().join();
36+
}
37+
38+
public List<SpanData> waitForTraces() {
39+
Collection<ExportTraceServiceRequest> collection =
40+
waitForTelemetry("get-traces", () -> ExportTraceServiceRequest.newBuilder());
41+
Stream<io.opentelemetry.proto.trace.v1.Span> objectStream =
42+
collection.stream()
43+
.flatMap(
44+
req ->
45+
req.getResourceSpansList().stream()
46+
.flatMap(
47+
rs ->
48+
rs.getScopeSpansList().stream()
49+
.flatMap(ss -> ss.getSpansList().stream())));
50+
51+
return AgentTestingExporterAccess.getSpanData(
52+
collection.stream().flatMap(req -> req.getResourceSpansList().stream()));
53+
}
54+
55+
public Collection<ExportMetricsServiceRequest> waitForMetrics() {
56+
return waitForTelemetry("get-metrics", () -> ExportMetricsServiceRequest.newBuilder());
57+
}
58+
59+
public Collection<ExportLogsServiceRequest> waitForLogs() {
60+
return waitForTelemetry("get-logs", () -> ExportLogsServiceRequest.newBuilder());
61+
}
62+
63+
@SuppressWarnings({"unchecked", "rawtypes"})
64+
private <T extends GeneratedMessage, B extends GeneratedMessage.Builder>
65+
Collection<T> waitForTelemetry(String path, Supplier<B> builderConstructor) {
66+
try {
67+
return OBJECT_MAPPER
68+
.readTree(waitForContent(path))
69+
.valueStream()
70+
.map(
71+
it -> {
72+
B builder = builderConstructor.get();
73+
// TODO: Register parser into object mapper to avoid de -> re -> deserialize.
74+
try {
75+
JsonFormat.parser().merge(OBJECT_MAPPER.writeValueAsString(it), builder);
76+
return (T) builder.build();
77+
} catch (InvalidProtocolBufferException | JsonProcessingException e) {
78+
throw new RuntimeException(e);
79+
}
80+
})
81+
.collect(Collectors.toList());
82+
} catch (JsonProcessingException | InterruptedException e) {
83+
throw new RuntimeException(e);
84+
}
85+
}
86+
87+
private String waitForContent(String path) throws InterruptedException {
88+
long previousSize = 0;
89+
long deadline = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(30);
90+
String content = "[]";
91+
while (System.currentTimeMillis() < deadline) {
92+
content = client.get(path).aggregate().join().contentUtf8();
93+
if (content.length() > 2 && content.length() == previousSize) {
94+
break;
95+
}
96+
97+
previousSize = content.length();
98+
System.out.println("Current content size " + previousSize);
99+
TimeUnit.MILLISECONDS.sleep(500);
100+
}
101+
102+
return content;
103+
}
104+
105+
public final WebClient getClient() {
106+
return client;
107+
}
108+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.smoketest;
7+
8+
import static io.opentelemetry.sdk.testing.assertj.TracesAssert.assertThat;
9+
10+
import java.util.Collections;
11+
import java.util.Map;
12+
import org.junit.jupiter.api.Test;
13+
import org.junit.jupiter.api.condition.DisabledIf;
14+
15+
@DisabledIf("io.opentelemetry.smoketest.TestContainerManager#useWindowsContainers")
16+
public class SecurityManagerSmokeTest extends JavaSmokeTest {
17+
@Override
18+
protected String getTargetImage(String jdk) {
19+
return "ghcr.io/open-telemetry/opentelemetry-java-instrumentation/smoke-test-security-manager:jdk"
20+
+ jdk
21+
+ "-20241021.11448062560";
22+
}
23+
24+
@Override
25+
protected Map<String, String> getExtraEnv() {
26+
return Collections.singletonMap(
27+
"OTEL_JAVAAGENT_EXPERIMENTAL_SECURITY_MANAGER_SUPPORT_ENABLED", "true");
28+
}
29+
30+
@Test
31+
public void security_manager_smoke_test_on_JDK__jdk(int jdk) {
32+
startTarget(jdk);
33+
34+
assertThat(waitForTraces())
35+
.hasTracesSatisfyingExactly(
36+
trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("test")));
37+
38+
stopTarget();
39+
40+
// where: DefaultGroovyMethods.leftShift(jdk, new ArrayList<Integer>(Arrays.asList(8, 11, 17,
41+
// 21, 23)));
42+
}
43+
}

0 commit comments

Comments
 (0)