|
| 1 | +package org.testcontainers.consul; |
| 2 | + |
| 3 | +import com.github.dockerjava.api.command.InspectContainerResponse; |
| 4 | +import com.github.dockerjava.api.model.Capability; |
| 5 | +import org.testcontainers.containers.GenericContainer; |
| 6 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 7 | +import org.testcontainers.utility.DockerImageName; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.List; |
| 13 | +import java.util.stream.Collectors; |
| 14 | + |
| 15 | +/** |
| 16 | + * Testcontainers implementation for Consul. |
| 17 | + */ |
| 18 | +public class ConsulContainer extends GenericContainer<ConsulContainer> { |
| 19 | + |
| 20 | + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("consul"); |
| 21 | + |
| 22 | + private static final int CONSUL_HTTP_PORT = 8500; |
| 23 | + |
| 24 | + private static final int CONSUL_GRPC_PORT = 8502; |
| 25 | + |
| 26 | + private List<String> initCommands = new ArrayList<>(); |
| 27 | + |
| 28 | + private String[] startConsulCmd = new String[] { "agent", "-dev", "-client", "0.0.0.0" }; |
| 29 | + |
| 30 | + public ConsulContainer(String dockerImageName) { |
| 31 | + this(DockerImageName.parse(dockerImageName)); |
| 32 | + } |
| 33 | + |
| 34 | + public ConsulContainer(final DockerImageName dockerImageName) { |
| 35 | + super(dockerImageName); |
| 36 | + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); |
| 37 | + |
| 38 | + // Use the status leader endpoint to verify if consul is running. |
| 39 | + setWaitStrategy(Wait.forHttp("/v1/status/leader").forPort(CONSUL_HTTP_PORT).forStatusCode(200)); |
| 40 | + |
| 41 | + withCreateContainerCmdModifier(cmd -> cmd.getHostConfig().withCapAdd(Capability.IPC_LOCK)); |
| 42 | + withEnv("CONSUL_ADDR", "http://0.0.0.0:" + CONSUL_HTTP_PORT); |
| 43 | + withCommand(startConsulCmd); |
| 44 | + withExposedPorts(CONSUL_HTTP_PORT, CONSUL_GRPC_PORT); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + protected void containerIsStarted(InspectContainerResponse containerInfo) { |
| 49 | + runConsulCommands(); |
| 50 | + } |
| 51 | + |
| 52 | + private void runConsulCommands() { |
| 53 | + if (!initCommands.isEmpty()) { |
| 54 | + String commands = initCommands |
| 55 | + .stream() |
| 56 | + .map(command -> "consul " + command) |
| 57 | + .collect(Collectors.joining(" && ")); |
| 58 | + try { |
| 59 | + ExecResult execResult = this.execInContainer(new String[] { "/bin/sh", "-c", commands }); |
| 60 | + if (execResult.getExitCode() != 0) { |
| 61 | + logger() |
| 62 | + .error( |
| 63 | + "Failed to execute these init commands {}. Exit code {}. Stdout {}. Stderr {}", |
| 64 | + initCommands, |
| 65 | + execResult.getExitCode(), |
| 66 | + execResult.getStdout(), |
| 67 | + execResult.getStderr() |
| 68 | + ); |
| 69 | + } |
| 70 | + } catch (IOException | InterruptedException e) { |
| 71 | + logger() |
| 72 | + .error( |
| 73 | + "Failed to execute these init commands {}. Exception message: {}", |
| 74 | + initCommands, |
| 75 | + e.getMessage() |
| 76 | + ); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Run consul commands using the consul cli. |
| 83 | + * |
| 84 | + * Useful for enableing more secret engines like: |
| 85 | + * <pre> |
| 86 | + * .withConsulCommand("secrets enable pki") |
| 87 | + * .withConsulCommand("secrets enable transit") |
| 88 | + * </pre> |
| 89 | + * or register specific K/V like: |
| 90 | + * <pre> |
| 91 | + * .withConsulCommand("kv put config/testing1 value123") |
| 92 | + * </pre> |
| 93 | + * @param commands The commands to send to the consul cli |
| 94 | + * @return this |
| 95 | + */ |
| 96 | + public ConsulContainer withConsulCommand(String... commands) { |
| 97 | + initCommands.addAll(Arrays.asList(commands)); |
| 98 | + return self(); |
| 99 | + } |
| 100 | +} |
0 commit comments