|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package co.elastic.otel.config; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | + |
| 23 | +import java.io.BufferedWriter; |
| 24 | +import java.io.File; |
| 25 | +import java.io.FileWriter; |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.InputStream; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import org.junit.jupiter.api.AfterAll; |
| 32 | +import org.junit.jupiter.api.BeforeAll; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | + |
| 35 | +public class ConfigLoggingAgentListenerTest { |
| 36 | + static final String TARGET_CLASS_NAME = "TempTest321"; |
| 37 | + static String[] identifyingStrings = { |
| 38 | + "ConfigLoggingAgentListener - AutoConfiguredOpenTelemetrySdk{", |
| 39 | + "tracerProvider=SdkTracerProvider" |
| 40 | + }; |
| 41 | + |
| 42 | + private static String agentJarFile; |
| 43 | + private static File testTargetClass; |
| 44 | + |
| 45 | + @BeforeAll |
| 46 | + public static void setup() throws IOException { |
| 47 | + agentJarFile = getAgentJarFile(); |
| 48 | + testTargetClass = createTestTarget(); |
| 49 | + } |
| 50 | + |
| 51 | + @AfterAll |
| 52 | + public static void teardown() throws IOException { |
| 53 | + testTargetClass.delete(); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void checkLogConfigPresent() throws IOException { |
| 58 | + String output = executeCommand(createTestTargetCommand(true), 20); |
| 59 | + for (String identifyingString : identifyingStrings) { |
| 60 | + assertThat(output).contains(identifyingString); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void checkLogConfigAbsent() throws IOException { |
| 66 | + String output = executeCommand(createTestTargetCommand(false), 20); |
| 67 | + for (String identifyingString : identifyingStrings) { |
| 68 | + assertThat(output).doesNotContain(identifyingString); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + static List<String> createTestTargetCommand(boolean logConfig) throws IOException { |
| 73 | + List<String> command = new ArrayList<>(); |
| 74 | + command.add("java"); |
| 75 | + command.add("-Xmx32m"); |
| 76 | + command.add("-javaagent:" + agentJarFile); |
| 77 | + // Only on false, ie test the 'true' default with no option |
| 78 | + if (!logConfig) { |
| 79 | + command.add("-D" + ConfigLoggingAgentListener.LOG_THE_CONFIG + "=false"); |
| 80 | + } |
| 81 | + command.add(testTargetClass.getAbsolutePath()); |
| 82 | + return command; |
| 83 | + } |
| 84 | + |
| 85 | + static File createTestTarget() throws IOException { |
| 86 | + String targetClass = |
| 87 | + "public class " |
| 88 | + + TARGET_CLASS_NAME |
| 89 | + + " {\n" |
| 90 | + + " public static void main(String[] args) {\n" |
| 91 | + + " System.out.println(\"Test started\");\n" |
| 92 | + // 15 seconds to give the agent plenty of time to start and be fully initialized |
| 93 | + + " try {Thread.sleep(15_000L);} catch (InterruptedException e) {}\n" |
| 94 | + + " System.out.println(\"Test ended\");\n" |
| 95 | + + " }\n" |
| 96 | + + "}\n"; |
| 97 | + File targetClassFile = |
| 98 | + new File(System.getProperty("java.io.tmpdir"), TARGET_CLASS_NAME + ".java"); |
| 99 | + targetClassFile.deleteOnExit(); |
| 100 | + try (BufferedWriter writer = new BufferedWriter(new FileWriter(targetClassFile))) { |
| 101 | + writer.write(targetClass); |
| 102 | + } |
| 103 | + return targetClassFile; |
| 104 | + } |
| 105 | + |
| 106 | + static String getAgentJarFile() { |
| 107 | + String path = System.getProperty("elastic.otel.agent.jar.path"); |
| 108 | + if (path == null) { |
| 109 | + throw new IllegalStateException("elastic.otel.agent.jar.path system property is not set"); |
| 110 | + } |
| 111 | + return path; |
| 112 | + } |
| 113 | + |
| 114 | + public static String executeCommand(List<String> command, int timeoutSeconds) throws IOException { |
| 115 | + ProcessBuilder pb = new ProcessBuilder(command); |
| 116 | + pb.redirectErrorStream(true); |
| 117 | + Process childProcess = pb.start(); |
| 118 | + |
| 119 | + StringBuilder commandOutput = new StringBuilder(); |
| 120 | + |
| 121 | + boolean isAlive = true; |
| 122 | + byte[] buffer = new byte[64 * 1000]; |
| 123 | + InputStream in = childProcess.getInputStream(); |
| 124 | + // stop trying if the time elapsed exceeds the timeout |
| 125 | + while (isAlive && (timeoutSeconds > 0)) { |
| 126 | + while (in.available() > 0) { |
| 127 | + int lengthRead = in.read(buffer, 0, buffer.length); |
| 128 | + commandOutput.append(new String(buffer, 0, lengthRead)); |
| 129 | + } |
| 130 | + pauseSeconds(1); |
| 131 | + timeoutSeconds--; |
| 132 | + isAlive = childProcess.isAlive(); |
| 133 | + } |
| 134 | + // it can die but still have output available buffered |
| 135 | + while (in.available() > 0) { |
| 136 | + int lengthRead = in.read(buffer, 0, buffer.length); |
| 137 | + commandOutput.append(new String(buffer, 0, lengthRead)); |
| 138 | + } |
| 139 | + |
| 140 | + // Cleanup as well as I can |
| 141 | + boolean exited = false; |
| 142 | + try { |
| 143 | + exited = childProcess.waitFor(3, TimeUnit.SECONDS); |
| 144 | + } catch (InterruptedException e) { |
| 145 | + } |
| 146 | + if (!exited) { |
| 147 | + childProcess.destroy(); |
| 148 | + pauseSeconds(1); |
| 149 | + if (childProcess.isAlive()) { |
| 150 | + childProcess.destroyForcibly(); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return commandOutput.toString(); |
| 155 | + } |
| 156 | + |
| 157 | + private static void pauseSeconds(int seconds) { |
| 158 | + try { |
| 159 | + Thread.sleep(seconds * 1_000L); |
| 160 | + } catch (InterruptedException e) { |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments