|
| 1 | +/* |
| 2 | + * Copyright (C) 2024-present The Prometheus jmx_exporter Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.prometheus.jmx.test.support; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.stream.Stream; |
| 23 | +import org.testcontainers.containers.BindMode; |
| 24 | +import org.testcontainers.containers.GenericContainer; |
| 25 | +import org.testcontainers.containers.Network; |
| 26 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 27 | +import org.verifyica.api.Argument; |
| 28 | + |
| 29 | +/** Class to implement MultiExporterTestEnvironment */ |
| 30 | +public class IsolatorExporterTestEnvironment implements Argument<IsolatorExporterTestEnvironment> { |
| 31 | + |
| 32 | + private static final String BASE_URL = "http://localhost"; |
| 33 | + private static final int BASE_PORT = 8888; |
| 34 | + |
| 35 | + private final String javaDockerImage; |
| 36 | + |
| 37 | + private Class<?> testClass; |
| 38 | + private String baseUrl; |
| 39 | + private Network network; |
| 40 | + private GenericContainer<?> javaAgentApplicationContainer; |
| 41 | + |
| 42 | + /** |
| 43 | + * Constructor |
| 44 | + * |
| 45 | + * @param javaDockerImage javaDockerImage |
| 46 | + */ |
| 47 | + public IsolatorExporterTestEnvironment(String javaDockerImage) { |
| 48 | + this.javaDockerImage = javaDockerImage; |
| 49 | + this.baseUrl = BASE_URL; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public String getName() { |
| 54 | + return javaDockerImage + " / IsolatorJavaAgent"; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public IsolatorExporterTestEnvironment getPayload() { |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Method to set the base URL |
| 64 | + * |
| 65 | + * @param baseUrl baseUrl |
| 66 | + * @return the ExporterTestEnvironment |
| 67 | + */ |
| 68 | + public IsolatorExporterTestEnvironment setBaseUrl(String baseUrl) { |
| 69 | + this.baseUrl = baseUrl; |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Method to get the Java Docker image name |
| 75 | + * |
| 76 | + * @return the Java Docker image name |
| 77 | + */ |
| 78 | + public String getJavaDockerImage() { |
| 79 | + return javaDockerImage; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Method to initialize the test environment |
| 84 | + * |
| 85 | + * @param network network |
| 86 | + */ |
| 87 | + public void initialize(Class<?> testClass, Network network) { |
| 88 | + this.testClass = testClass; |
| 89 | + this.network = network; |
| 90 | + |
| 91 | + javaAgentApplicationContainer = createJavaAgentApplicationContainer(); |
| 92 | + javaAgentApplicationContainer.start(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Method to get a URL (base URL + path) |
| 97 | + * |
| 98 | + * @param index index |
| 99 | + * @param path path |
| 100 | + * @return the URL (base URL + path) |
| 101 | + */ |
| 102 | + public String getUrl(int index, String path) { |
| 103 | + return !path.startsWith("/") ? getBaseUrl(index) + "/" + path : getBaseUrl(index) + path; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Method to get the base URL |
| 108 | + * |
| 109 | + * @param index index |
| 110 | + * @return the base URL |
| 111 | + */ |
| 112 | + public String getBaseUrl(int index) { |
| 113 | + int port = javaAgentApplicationContainer.getMappedPort(BASE_PORT + index); |
| 114 | + return baseUrl + ":" + port; |
| 115 | + } |
| 116 | + |
| 117 | + /** Method to destroy the test environment */ |
| 118 | + public void destroy() { |
| 119 | + if (javaAgentApplicationContainer != null) { |
| 120 | + javaAgentApplicationContainer.stop(); |
| 121 | + javaAgentApplicationContainer = null; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Method to create an application container |
| 127 | + * |
| 128 | + * @return the return value |
| 129 | + */ |
| 130 | + private GenericContainer<?> createJavaAgentApplicationContainer() { |
| 131 | + return new GenericContainer<>(javaDockerImage) |
| 132 | + .waitingFor(Wait.forListeningPort()) |
| 133 | + .withClasspathResourceMapping("common", "/temp", BindMode.READ_ONLY) |
| 134 | + .withClasspathResourceMapping( |
| 135 | + testClass.getName().replace(".", "/") + "/JavaAgent", |
| 136 | + "/temp", |
| 137 | + BindMode.READ_ONLY) |
| 138 | + .withCreateContainerCmdModifier(TestContainerConfigureCmd.getInstance()) |
| 139 | + .withCommand("/bin/sh application.sh") |
| 140 | + .withExposedPorts(BASE_PORT, BASE_PORT + 1, BASE_PORT + 2) |
| 141 | + .withLogConsumer(TestContainerLogger.getInstance()) |
| 142 | + .withNetwork(network) |
| 143 | + .withNetworkAliases("application") |
| 144 | + .waitingFor(Wait.forLogMessage(".*JmxExampleApplication \\| Running.*\\n", 1)) |
| 145 | + .withStartupTimeout(Duration.ofMillis(60000)) |
| 146 | + .withWorkingDirectory("/temp"); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Create the MultiExporterTestEnvironment |
| 151 | + * |
| 152 | + * @return a Stream of MultiExporterTestEnvironments |
| 153 | + */ |
| 154 | + public static Stream<IsolatorExporterTestEnvironment> createMultiExporterTestEnvironments() { |
| 155 | + Collection<IsolatorExporterTestEnvironment> collection = new ArrayList<>(); |
| 156 | + |
| 157 | + JavaDockerImages.names() |
| 158 | + .forEach( |
| 159 | + dockerImageName -> { |
| 160 | + collection.add(new IsolatorExporterTestEnvironment(dockerImageName)); |
| 161 | + }); |
| 162 | + |
| 163 | + return collection.stream(); |
| 164 | + } |
| 165 | +} |
0 commit comments