/** * Set environment variable "kafkaServer" to the host and port specified by * docker */ public static void setKafkaEnvironment(DockerComposeRule docker) { DockerPort kafka = docker.containers().container("kafka").port(9092); Map<String, String> newEnv = new HashMap<>(); newEnv.put(ServiceProperties.KAFKA_SERVER_KEY, kafka.inFormat("$HOST:$EXTERNAL_PORT")); newEnv.putAll(System.getenv()); setEnv(newEnv); }
/** * 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); }
/** * Set environment variable "registryServer" to the host and port specified by * docker */ public static void setConsulEnvironment(DockerComposeRule docker) { DockerPort consul = docker.containers().container("consul").port(8500); Map<String, String> newEnv = new HashMap<>(); newEnv.put(ServiceProperties.REGISTRY_SERVER_KEY, consul.inFormat("$HOST:$EXTERNAL_PORT")); newEnv.putAll(System.getenv()); setEnv(newEnv); }
/** * Get the server name of the postgres docker instance * * @param docker The used docker compose rule * @return postgresServerName */ public static String getPostgresServerName(DockerComposeRule docker) { DockerPort postgres = docker.containers().container("postgres").port(5432); return postgres.inFormat("$HOST"); }
/** * Get the external port of the postgres docker instance * * @param docker The used docker compose rule * @return postgresExternalPort */ public static String getPostgresExternalPort(DockerComposeRule docker) { DockerPort postgres = docker.containers().container("postgres").port(5432); return postgres.inFormat("$EXTERNAL_PORT"); }
private ServiceProperties fillServiceProperties() { DockerPort kafka1 = docker.containers().container("kafka1").port(9092); DockerPort kafka2 = docker.containers().container("kafka2").port(9092); DockerPort kafka3 = docker.containers().container("kafka2").port(9092); StringBuilder kafkaServer = new StringBuilder(); kafkaServer.append(kafka2.inFormat("$HOST:$EXTERNAL_PORT")); kafkaServer.append(","); kafkaServer.append(kafka1.inFormat("$HOST:$EXTERNAL_PORT")); kafkaServer.append(","); kafkaServer.append(kafka3.inFormat("$HOST:$EXTERNAL_PORT")); String[] args = new String[2]; args[0] = "-" + ServiceProperties.KAFKA_SERVER_KEY; args[1] = kafkaServer.toString(); ServiceProperties serviceProperties = new ServiceProperties(); serviceProperties.initialize(args); return serviceProperties; }
@Test public void throttleTest() throws InterruptedException { int messageCount = 200; CountDownLatch latch = new CountDownLatch(messageCount); DockerPort kafka = docker.containers().container("kafka").port(9092); ServiceProperties props = new ServiceProperties(); props.addProperty(KAFKA_SERVER_KEY, kafka.inFormat("$HOST:$EXTERNAL_PORT")); String topic = "throttle-test"; KafkaPublisherFactory publisherFactory = new KafkaPublisherFactory(props, null); KafkaPublisher publisher = publisherFactory.newBuilder(topic).build(); KafkaSubscriberFactory<String> subscriberFactory = new KafkaSubscriberFactory<>(props, null, null); EventReceivedCallback<String> callback = (message, topicInfo) -> { latch.countDown(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } }; subscriberFactory.newBuilder(topic, callback).withPollTime(50).withAutoCommit(true).build(); for (int i = 0; i < messageCount; i++) { publisher.publishSync("message " + i + randomData()); } latch.await(); }
@Override public boolean attempt() throws Exception { final DockerPort dockerPort = container.port(port); final String jdbcUrl = dockerPort.inFormat(jdbcUrlFormat); log.trace("Container Ip = {}", dockerPort.getIp()); if (!dockerPort.isListeningNow()) { log.trace("Port {} is not listening on container {}.", port, containerName); return false; } log.trace("Attempting to authenticate to {} with user {}.", jdbcUrl, username); try (Connection connection = DriverManager.getConnection( jdbcUrl, username, password )) { return true; } catch (Exception ex) { log.trace("Exception thrown", ex); Thread.sleep(2500); return false; } } }
@Test public void allow_building_an_externally_accessible_address() { assertThat( dockerPort.inFormat("http://$HOST:$EXTERNAL_PORT/api"), is("http://hostname:1234/api")); }
@Test public void allow_multiple_copies_of_each_substitution() { assertThat( dockerPort.inFormat("$HOST,$HOST,$INTERNAL_PORT,$INTERNAL_PORT,$EXTERNAL_PORT,$EXTERNAL_PORT"), is("hostname,hostname,4321,4321,1234,1234")); }
@Test public void have_no_effect_on_a_string_with_no_substitutions() { assertThat( dockerPort.inFormat("no substitutions"), is("no substitutions")); }
@Test public void allow_building_an_address_with_an_internal_port() { assertThat( dockerPort.inFormat("http://localhost:$INTERNAL_PORT/api"), is("http://localhost:4321/api")); }
private Object dockerFormatString(Class<?> parameterContext, Cluster cluster, FormatString formatString) { Container container = cluster.container(formatString.container()); Preconditions.checkNotNull(container, "Could not find container '%s'", formatString.container()); DockerPort port = container.port(formatString.internalPort()); Preconditions.checkNotNull(container, "Could not find internalPort '%s' for container '%s'", formatString.internalPort(), formatString.container()); final String format = port.inFormat(formatString.format()); if (String.class.equals(parameterContext)) { return format; } else if (URI.class.equals(parameterContext)) { return URI.create(format); } else if (URL.class.equals(parameterContext)) { try { return new URL(format); } catch (MalformedURLException e) { throw new ParameterResolutionException("Could not create URL", e); } } else { return null; } }
private Object dockerFormatString(DockerComposeRule docker, Annotation annotation) { DockerFormatString dockerFormatString = (DockerFormatString) annotation; Container container = docker.containers().container(dockerFormatString.container()); DockerPort dockerPort = container.port(dockerFormatString.port()); return dockerPort.inFormat(dockerFormatString.format()); }