|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.contrib.jmxscraper; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.nio.file.Paths; |
| 15 | +import java.time.Duration; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.stream.Collectors; |
| 19 | +import org.testcontainers.containers.GenericContainer; |
| 20 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 21 | +import org.testcontainers.shaded.com.google.errorprone.annotations.CanIgnoreReturnValue; |
| 22 | +import org.testcontainers.utility.MountableFile; |
| 23 | + |
| 24 | +/** Test container that allows to execute {@link TestApp} in an isolated container */ |
| 25 | +public class TestAppContainer extends GenericContainer<TestAppContainer> { |
| 26 | + |
| 27 | + private final Map<String, String> properties; |
| 28 | + private int port; |
| 29 | + private String login; |
| 30 | + private String pwd; |
| 31 | + |
| 32 | + public TestAppContainer() { |
| 33 | + super("openjdk:8u272-jre-slim"); |
| 34 | + |
| 35 | + this.properties = new HashMap<>(); |
| 36 | + |
| 37 | + String appJar = System.getProperty("app.jar.path"); |
| 38 | + assertThat(Paths.get(appJar)).isNotEmptyFile().isReadable(); |
| 39 | + |
| 40 | + this.withCopyFileToContainer(MountableFile.forHostPath(appJar), "/app.jar") |
| 41 | + .waitingFor( |
| 42 | + Wait.forLogMessage(TestApp.APP_STARTED_MSG + "\\n", 1) |
| 43 | + .withStartupTimeout(Duration.ofSeconds(5))) |
| 44 | + .withCommand("java", "-jar", "/app.jar"); |
| 45 | + } |
| 46 | + |
| 47 | + @CanIgnoreReturnValue |
| 48 | + public TestAppContainer withJmxPort(int port) { |
| 49 | + this.port = port; |
| 50 | + properties.put("com.sun.management.jmxremote.port", Integer.toString(port)); |
| 51 | + return this.withExposedPorts(port); |
| 52 | + } |
| 53 | + |
| 54 | + @CanIgnoreReturnValue |
| 55 | + public TestAppContainer withUserAuth(String login, String pwd) { |
| 56 | + this.login = login; |
| 57 | + this.pwd = pwd; |
| 58 | + return this; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void start() { |
| 63 | + |
| 64 | + // TODO: add support for ssl |
| 65 | + properties.put("com.sun.management.jmxremote.ssl", "false"); |
| 66 | + |
| 67 | + if (pwd == null) { |
| 68 | + properties.put("com.sun.management.jmxremote.authenticate", "false"); |
| 69 | + } else { |
| 70 | + properties.put("com.sun.management.jmxremote.authenticate", "true"); |
| 71 | + |
| 72 | + Path pwdFile = createPwdFile(login, pwd); |
| 73 | + this.withCopyFileToContainer(MountableFile.forHostPath(pwdFile), "/jmx.password"); |
| 74 | + properties.put("com.sun.management.jmxremote.password.file", "/jmx.password"); |
| 75 | + |
| 76 | + Path accessFile = createAccessFile(login); |
| 77 | + this.withCopyFileToContainer(MountableFile.forHostPath(accessFile), "/jmx.access"); |
| 78 | + properties.put("com.sun.management.jmxremote.access.file", "/jmx.access"); |
| 79 | + } |
| 80 | + |
| 81 | + String confArgs = |
| 82 | + properties.entrySet().stream() |
| 83 | + .map( |
| 84 | + e -> { |
| 85 | + String s = "-D" + e.getKey(); |
| 86 | + if (!e.getValue().isEmpty()) { |
| 87 | + s += "=" + e.getValue(); |
| 88 | + } |
| 89 | + return s; |
| 90 | + }) |
| 91 | + .collect(Collectors.joining(" ")); |
| 92 | + |
| 93 | + this.withEnv("JAVA_TOOL_OPTIONS", confArgs); |
| 94 | + |
| 95 | + logger().info("Test application JAVA_TOOL_OPTIONS = " + confArgs); |
| 96 | + |
| 97 | + super.start(); |
| 98 | + |
| 99 | + logger().info("Test application JMX port mapped to {}:{}", getHost(), getMappedPort(port)); |
| 100 | + } |
| 101 | + |
| 102 | + private static Path createPwdFile(String login, String pwd) { |
| 103 | + try { |
| 104 | + Path path = Files.createTempFile("test", ".pwd"); |
| 105 | + writeLine(path, String.format("%s %s", login, pwd)); |
| 106 | + return path; |
| 107 | + } catch (IOException e) { |
| 108 | + throw new RuntimeException(e); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private static Path createAccessFile(String login) { |
| 113 | + try { |
| 114 | + Path path = Files.createTempFile("test", ".pwd"); |
| 115 | + writeLine(path, String.format("%s %s", login, "readwrite")); |
| 116 | + return path; |
| 117 | + } catch (IOException e) { |
| 118 | + throw new RuntimeException(e); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static void writeLine(Path path, String line) throws IOException { |
| 123 | + line = line + "\n"; |
| 124 | + Files.write(path, line.getBytes(StandardCharsets.UTF_8)); |
| 125 | + } |
| 126 | +} |
0 commit comments