|
| 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.sdk.logs.data.LogRecordData; |
| 12 | +import io.opentelemetry.sdk.metrics.data.MetricData; |
| 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 void withTarget(int jdk, TargetRunner runner) throws Exception { |
| 69 | + startTarget(jdk); |
| 70 | + try { |
| 71 | + runner.runInTarget(); |
| 72 | + } finally { |
| 73 | + stopTarget(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public Consumer<OutputFrame> startTarget(int jdk) { |
| 78 | + return startTarget(String.valueOf(jdk), null, false); |
| 79 | + } |
| 80 | + |
| 81 | + public Consumer<OutputFrame> startTarget(String jdk, String serverVersion, boolean windows) { |
| 82 | + String targetImage = getTargetImage(jdk, serverVersion, windows); |
| 83 | + return containerManager.startTarget( |
| 84 | + targetImage, |
| 85 | + agentPath, |
| 86 | + getJvmArgsEnvVarName(), |
| 87 | + getExtraEnv(), |
| 88 | + getSetServiceName(), |
| 89 | + getExtraResources(), |
| 90 | + getExtraPorts(), |
| 91 | + getWaitStrategy(), |
| 92 | + getCommand()); |
| 93 | + } |
| 94 | + |
| 95 | + protected abstract String getTargetImage(String jdk); |
| 96 | + |
| 97 | + protected String getTargetImage(String jdk, String serverVersion, boolean windows) { |
| 98 | + return getTargetImage(jdk); |
| 99 | + } |
| 100 | + |
| 101 | + protected TargetWaitStrategy getWaitStrategy() { |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + protected String[] getCommand() { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + public void cleanup() { |
| 110 | + telemetryRetriever.clearTelemetry(); |
| 111 | + } |
| 112 | + |
| 113 | + public void stopTarget() { |
| 114 | + containerManager.stopTarget(); |
| 115 | + cleanup(); |
| 116 | + } |
| 117 | + |
| 118 | + protected List<SpanData> waitForTraces() { |
| 119 | + return telemetryRetriever.waitForTraces(); |
| 120 | + } |
| 121 | + |
| 122 | + protected Collection<MetricData> waitForMetrics() { |
| 123 | + return telemetryRetriever.waitForMetrics(); |
| 124 | + } |
| 125 | + |
| 126 | + protected Collection<LogRecordData> waitForLogs() { |
| 127 | + return telemetryRetriever.waitForLogs(); |
| 128 | + } |
| 129 | + |
| 130 | + private static TestContainerManager createContainerManager() { |
| 131 | + return TestContainerManager.useWindowsContainers() |
| 132 | + ? new WindowsTestContainerManager() |
| 133 | + : new LinuxTestContainerManager(); |
| 134 | + } |
| 135 | +} |
0 commit comments