|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.smoketest |
| 7 | + |
| 8 | +import io.opentelemetry.proto.trace.v1.Span |
| 9 | +import org.slf4j.Logger |
| 10 | +import org.slf4j.LoggerFactory |
| 11 | +import org.testcontainers.containers.Container |
| 12 | +import org.testcontainers.containers.GenericContainer |
| 13 | +import org.testcontainers.containers.Network |
| 14 | +import org.testcontainers.containers.output.Slf4jLogConsumer |
| 15 | +import org.testcontainers.containers.wait.strategy.Wait |
| 16 | +import org.testcontainers.utility.DockerImageName |
| 17 | +import org.testcontainers.utility.MountableFile |
| 18 | +import spock.lang.IgnoreIf |
| 19 | +import spock.lang.Specification |
| 20 | + |
| 21 | +import java.time.Duration |
| 22 | + |
| 23 | +import static io.opentelemetry.smoketest.TestContainerManager.useWindowsContainers |
| 24 | + |
| 25 | +@IgnoreIf({ useWindowsContainers() }) |
| 26 | +class IbmHttpsUrlConnectionTest extends Specification { |
| 27 | + private static final Logger logger = LoggerFactory.getLogger(IbmHttpsUrlConnectionTest) |
| 28 | + |
| 29 | + private static final String TARGET_AGENT_FILENAME = "opentelemetry-javaagent.jar" |
| 30 | + private static final String agentPath = System.getProperty("io.opentelemetry.smoketest.agent.shadowJar.path") |
| 31 | + private static final int BACKEND_PORT = 8080 |
| 32 | + private static final String BACKEND_ALIAS = "backend" |
| 33 | + |
| 34 | + private final Network network = Network.newNetwork() |
| 35 | + |
| 36 | + def "test https url connection"() { |
| 37 | + setup: |
| 38 | + GenericContainer backend = |
| 39 | + new GenericContainer<>( |
| 40 | + DockerImageName.parse( |
| 41 | + "ghcr.io/open-telemetry/opentelemetry-java-instrumentation/smoke-test-fake-backend:20221127.3559314891")) |
| 42 | + .withExposedPorts(BACKEND_PORT) |
| 43 | + .withEnv("JAVA_TOOL_OPTIONS", "-Xmx128m") |
| 44 | + .waitingFor(Wait.forHttp("/health").forPort(BACKEND_PORT)) |
| 45 | + .withNetwork(network) |
| 46 | + .withNetworkAliases(BACKEND_ALIAS) |
| 47 | + .withLogConsumer(new Slf4jLogConsumer(logger)) |
| 48 | + backend.start() |
| 49 | + |
| 50 | + def telemetryRetriever = new TelemetryRetriever(backend.getMappedPort(BACKEND_PORT)) |
| 51 | + |
| 52 | + GenericContainer target = |
| 53 | + new GenericContainer<>(DockerImageName.parse("ibmjava:8-sdk")) |
| 54 | + .withStartupTimeout(Duration.ofMinutes(5)) |
| 55 | + .withNetwork(network) |
| 56 | + .withLogConsumer(new Slf4jLogConsumer(logger)) |
| 57 | + .withCopyFileToContainer( |
| 58 | + MountableFile.forHostPath(agentPath), "/" + TARGET_AGENT_FILENAME) |
| 59 | + .withCopyFileToContainer( |
| 60 | + MountableFile.forClasspathResource("ibmhttpsurlconnection/IbmHttpsUrlConnectionTest.java"), "/IbmHttpsUrlConnectionTest.java") |
| 61 | + .withCopyFileToContainer( |
| 62 | + MountableFile.forClasspathResource("ibmhttpsurlconnection/start.sh", 777), "/start.sh") |
| 63 | + .withCopyFileToContainer( |
| 64 | + MountableFile.forClasspathResource("ibmhttpsurlconnection/test.sh", 777), "/test.sh") |
| 65 | + .waitingFor( |
| 66 | + Wait.forLogMessage(".*started.*\\n", 1) |
| 67 | + ) |
| 68 | + .withCommand("/bin/sh", "-c", "/start.sh") |
| 69 | + target.start() |
| 70 | + |
| 71 | + when: |
| 72 | + Container.ExecResult result = target.execInContainer("/bin/sh", "-c", "/test.sh") |
| 73 | + then: |
| 74 | + result.getExitCode() == 0 |
| 75 | + |
| 76 | + TraceInspector traces = new TraceInspector(telemetryRetriever.waitForTraces()) |
| 77 | + Set<String> traceIds = traces.traceIds |
| 78 | + |
| 79 | + then: "There is one trace" |
| 80 | + traceIds.size() == 1 |
| 81 | + |
| 82 | + and: "Client span in the distributed trace" |
| 83 | + traces.countSpansByKind(Span.SpanKind.SPAN_KIND_CLIENT) == 1 |
| 84 | + |
| 85 | + and: "Expected span names" |
| 86 | + traces.countSpansByName("GET") == 1 |
| 87 | + |
| 88 | + cleanup: |
| 89 | + System.err.println(result.toString()) |
| 90 | + target.stop() |
| 91 | + backend.stop() |
| 92 | + } |
| 93 | +} |
0 commit comments