/** * Set environment variable "etcd_endpoints" to the host and port specified by * docker */ public static void setEtcdEnvironment(DockerComposeRule docker) { DockerPort etcd = docker.containers().container("etcd").port(2379); Map<String, String> newEnv = new HashMap<>(); newEnv.put("etcd_endpoints", etcd.inFormat("http://$HOST:$EXTERNAL_PORT")); newEnv.putAll(System.getenv()); setEnv(newEnv); }
private static void waitForCassandraContainer(Container container) { org.awaitility.Awaitility.await() .atMost(60, TimeUnit.SECONDS) .pollInterval(1, TimeUnit.SECONDS) .until(() -> { // TODO (jkong): hack String curlOutput = EteSetup.execCliCommand("ete1", String.format("bash -c 'curl %s:%s; echo $?; exit 0;'", container.getContainerName(), CASSANDRA_PORT)); return curlOutput.contains("52"); }); } }
@Override public Container container(String containerName) { return new Container(containerName, this); }
@Test public void testStateChanges_withoutHealthCheck() throws IOException, InterruptedException { DockerCompose dockerCompose = new DefaultDockerCompose( DockerComposeFiles.from("src/test/resources/no-healthcheck.yaml"), dockerMachine, ProjectName.random()); // The noHealthcheck service has no healthcheck specified; it should be immediately healthy Container container = new Container("noHealthcheck", docker, dockerCompose); assertEquals(State.DOWN, container.state()); container.up(); assertEquals(State.HEALTHY, container.state()); container.kill(); assertEquals(State.DOWN, container.state()); }
@Test public void return_updated_external_port_on_restart() throws IOException, InterruptedException { int internalPort = 5432; env.ephemeralPort("service", IP, internalPort); DockerPort port = container.port(internalPort); int prePort = port.getExternalPort(); DockerPort samePort = container.port(internalPort); assertThat(prePort, is(samePort.getExternalPort())); container.stop(); container.start(); DockerPort updatedPort = container.port(internalPort); assertThat(prePort, not(is(updatedPort.getExternalPort()))); }
@Test public void can_kill_and_start_containers() { forEachContainer(containerName -> { try { Container container = docker.containers().container(containerName); container.kill(); assertThat(container.state(), is(State.DOWN)); container.start(); assertThat(container.state(), is(State.HEALTHY)); } catch (IOException | InterruptedException e) { propagate(e); } }); }
/** * Returns a check that the native "healthcheck" status of the docker containers is not unhealthy. * * <p>Does not wait for DOWN or PAUSED containers, or containers with no healthcheck defined. */ static ClusterHealthCheck nativeHealthChecks() { return cluster -> { Set<String> unhealthyContainers = new LinkedHashSet<>(); try { for (Container container : cluster.allContainers()) { State state = container.state(); if (state == State.UNHEALTHY) { unhealthyContainers.add(container.getContainerName()); } } if (!unhealthyContainers.isEmpty()) { return SuccessOrFailure.failure( "The following containers are not healthy: " + unhealthyContainers.stream().collect(joining(", "))); } return SuccessOrFailure.success(); } catch (IOException e) { return SuccessOrFailure.fromException(e); } }; }
private void awaitContainerStart(Container container) { LOG.INFO("Waiting for '%s' to become ready...", container.getContainerName()); try { int retries = MAX_WAIT_SECONDS; while (container.areAllPortsOpen().failed()) { Wait.seconds(1); if (retries-- <= 0) { LOG.WARN("Failed to start '%s' - Ports: %s", container.getContainerName(), container.ports().stream().map(Object::toString).collect(Collectors.joining(", "))); return; } } LOG.INFO("Container '%s' is ONLINE - Ports: %s", container.getContainerName(), container.ports().stream().map(Object::toString).collect(Collectors.joining(", "))); } catch (Exception e) { LOG.SEVERE(e); } }
public SuccessOrFailure portIsListeningOnHttp(int internalPort, Function<DockerPort, String> urlFunction) { return portIsListeningOnHttp(internalPort, urlFunction, false); }
@Test public void can_stop_and_start_containers() { forEachContainer(containerName -> { try { Container container = docker.containers().container(containerName); container.stop(); assertThat(container.state(), is(State.DOWN)); container.start(); assertThat(container.state(), is(State.HEALTHY)); } catch (IOException | InterruptedException e) { propagate(e); } }); }
@Test public void kill_can_be_run_on_killed_container() { forEachContainer(containerName -> { try { Container container = docker.containers().container(containerName); container.kill(); assertThat(container.state(), is(State.DOWN)); container.kill(); } catch (IOException | InterruptedException e) { propagate(e); } }); }
@Test public void start_can_be_run_on_running_container() { forEachContainer(containerName -> { try { Container container = docker.containers().container(containerName); container.start(); assertThat(container.state(), is(State.HEALTHY)); } catch (IOException | InterruptedException e) { propagate(e); } }); }
public static void killCassandraContainer(String containerName) { Container container = EteSetup.getContainer(containerName); try { container.kill(); } catch (IOException | InterruptedException e) { throw Throwables.propagate(e); } }
@Test public void stop_can_be_run_on_stopped_container() { forEachContainer(containerName -> { try { Container container = docker.containers().container(containerName); container.stop(); assertThat(container.state(), is(State.DOWN)); container.stop(); } catch (IOException | InterruptedException e) { propagate(e); } }); }
public DockerPort portOnContainerWithExternalMapping(String container, int portNumber) throws IOException, InterruptedException { return rule().containers().container(container).portMappedExternallyTo(portNumber); }
final Container container = cluster.container(containerName); try { Preconditions.checkState(State.Up == container.state(), "container %s should be up", containerName); } catch (IOException e) { log.error("exception thrown", e);
/** * This test is not currently enabled in Circle as it does not provide a sufficiently recent version of docker-compose. * * @see <a href="https://github.com/palantir/docker-compose-rule/issues/156">Issue #156</a> */ @Test public void testStateChanges_withHealthCheck() throws IOException, InterruptedException { assumeThat("docker version", Docker.version(), new GreaterOrEqual<>(Version.forIntegers(1, 12, 0))); assumeThat("docker-compose version", DockerCompose.version(), new GreaterOrEqual<>(Version.forIntegers(1, 10, 0))); DockerCompose dockerCompose = new DefaultDockerCompose( DockerComposeFiles.from("src/test/resources/native-healthcheck.yaml"), dockerMachine, ProjectName.random()); // The withHealthcheck service's healthcheck checks every 100ms whether the file "healthy" exists Container container = new Container("withHealthcheck", docker, dockerCompose); assertEquals(State.DOWN, container.state()); container.up(); assertEquals(State.UNHEALTHY, container.state()); dockerCompose.exec(noOptions(), "withHealthcheck", arguments("touch", "healthy")); wait.until(container::state, equalTo(State.HEALTHY)); dockerCompose.exec(noOptions(), "withHealthcheck", arguments("rm", "healthy")); wait.until(container::state, equalTo(State.UNHEALTHY)); container.kill(); assertEquals(State.DOWN, container.state()); } }
public static HealthCheck<Container> toRespondOverHttp(int internalPort, Function<DockerPort, String> urlFunction) { return container -> container.portIsListeningOnHttp(internalPort, urlFunction); }
@Override public void after() { if (!initialized.get()) { return; } try { kvs.close(); containers.getContainer(containerInstance.getServiceName()).kill(); } catch (IOException | InterruptedException e) { throw Throwables.rewrapAndThrowUncheckedException(e); } }