org.testcontainers.containers.BindMode Java Examples

The following examples show how to use org.testcontainers.containers.BindMode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CopyFileToContainerTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void shouldUseCopyOnlyWithReadOnlyClasspathResources() {
    String resource = "/test_copy_to_container.txt";
    GenericContainer<?> container = new GenericContainer<>()
        .withClasspathResourceMapping(resource, "/readOnly", BindMode.READ_ONLY)
        .withClasspathResourceMapping(resource, "/readOnlyNoSelinux", BindMode.READ_ONLY)

        .withClasspathResourceMapping(resource, "/readOnlyShared", BindMode.READ_ONLY, SelinuxContext.SHARED)
        .withClasspathResourceMapping(resource, "/readWrite", BindMode.READ_WRITE);

    Map<MountableFile, String> copyMap = container.getCopyToFileContainerPathMap();
    assertTrue("uses copy for read-only", copyMap.containsValue("/readOnly"));
    assertTrue("uses copy for read-only and no Selinux", copyMap.containsValue("/readOnlyNoSelinux"));

    assertFalse("uses mount for read-only with Selinux", copyMap.containsValue("/readOnlyShared"));
    assertFalse("uses mount for read-write", copyMap.containsValue("/readWrite"));
}
 
Example #2
Source File: MySQLRule.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
private void initServer() {
  server = new GenericContainer(databaseServerInfo.getDatabaseType().toDockerImageName() + ":" + databaseServerInfo.getDockerImageTag())
    .withEnv("MYSQL_USER", "mysql")
    .withEnv("MYSQL_PASSWORD", "password")
    .withEnv("MYSQL_ROOT_PASSWORD", "password")
    .withEnv("MYSQL_DATABASE", "testschema")
    .withExposedPorts(3306)
    .withClasspathResourceMapping("init.sql", "/docker-entrypoint-initdb.d/init.sql", BindMode.READ_ONLY)
    .withReuse(true);

  if (ssl) {
    server.withClasspathResourceMapping("tls/conf", "/etc/mysql/conf.d", BindMode.READ_ONLY);
    server.withClasspathResourceMapping("tls/files", "/etc/mysql/tls", BindMode.READ_ONLY);
  } else {
    server.withClasspathResourceMapping("tls/files", "/etc/mysql/tls", BindMode.READ_ONLY);
    String cmd = "--max_allowed_packet=33554432 --max_prepared_stmt_count=16382 --local_infile=true --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci";
    if (isUsingMySQL8()) {
      // introduced in MySQL 8.0.3
      cmd += " --caching-sha2-password-public-key-path=/etc/mysql/tls/public_key.pem --caching-sha2-password-private-key-path=/etc/mysql/tls/private_key.pem";
    }
    server.withCommand(cmd);
  }
}
 
Example #3
Source File: EmbeddedGraphiteBootstrapConfiguration.java    From kayenta with Apache License 2.0 6 votes vote down vote up
@Bean(name = "graphite", destroyMethod = "stop")
public GenericContainer graphite(
    ConfigurableEnvironment environment, WaitStrategy graphiteWaitStrategy) {

  GenericContainer container =
      new GenericContainer("graphiteapp/graphite-statsd:1.1.5-12")
          .withLogConsumer(containerLogsConsumer(log))
          .withExposedPorts(PICKLE_RECEIVER_PORT)
          .waitingFor(graphiteWaitStrategy)
          .withClasspathResourceMapping(
              "/external/graphite/storage-schemas.conf",
              "/opt/graphite/conf/storage-schemas.conf",
              BindMode.READ_ONLY)
          .withStartupTimeout(Duration.ofSeconds(30));
  container.start();

  Map<String, Object> map = registerEnvironment(environment, container);
  log.info("Started Graphite server. Connection details: {}", map);
  return container;
}
 
Example #4
Source File: EnvoyContainer.java    From java-control-plane with Apache License 2.0 6 votes vote down vote up
@Override
protected void configure() {
  super.configure();

  withClasspathResourceMapping(HOST_IP_SCRIPT, HOST_IP_SCRIPT_DEST, BindMode.READ_ONLY);
  withClasspathResourceMapping(LAUNCH_ENVOY_SCRIPT, LAUNCH_ENVOY_SCRIPT_DEST, BindMode.READ_ONLY);
  withClasspathResourceMapping(config, CONFIG_DEST, BindMode.READ_ONLY);

  withCommand(
      "/bin/sh", "/usr/local/bin/launch_envoy.sh",
      Integer.toString(controlPlanePortSupplier.get()),
      CONFIG_DEST,
      "-l", "debug");

  getExposedPorts().add(0, ADMIN_PORT);
}
 
Example #5
Source File: DumpGoldenSchemaCommand.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Override
protected void onContainerCreate() throws IOException {
  // open the output file for write so we can mount it.
  new FileOutputStream(output.toFile()).close();
  postgresContainer.withFileSystemBind(
      output.toString(), CONTAINER_MOUNT_POINT, BindMode.READ_WRITE);
}
 
Example #6
Source File: CopyFileToContainerTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void shouldUseCopyForReadOnlyClasspathResources() throws Exception {
    try (
        GenericContainer container = new GenericContainer()
            .withCommand("sleep", "3000")
            .withClasspathResourceMapping("/mappable-resource/", containerPath, BindMode.READ_ONLY)
    ) {
        container.start();
        String filesList = container.execInContainer("ls", "/tmp/mappable-resource").getStdout();
        assertTrue("file list contains the file", filesList.contains(fileName));
    }
}
 
Example #7
Source File: CopyFileToContainerTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
@Test
public void shouldCreateFoldersStructureWithCopy() throws Exception {
    String resource = "/test_copy_to_container.txt";
    try (
        GenericContainer container = new GenericContainer<>()
            .withCommand("sleep", "3000")
            .withClasspathResourceMapping(resource, "/a/b/c/file", BindMode.READ_ONLY)
    ) {
        container.start();
        String filesList = container.execInContainer("ls", "/a/b/c/").getStdout();
        assertTrue("file list contains the file", filesList.contains("file"));
    }
}
 
Example #8
Source File: ServerIT.java    From presto with Apache License 2.0 5 votes vote down vote up
private static void testServer(String baseImage, String rpmHostPath, String expectedJavaVersion)
{
    String rpm = "/" + new File(rpmHostPath).getName();

    String command = "" +
            // install RPM
            "yum localinstall -q -y " + rpm + "\n" +
            // create Hive catalog file
            "mkdir /etc/presto/catalog\n" +
            "cat > /etc/presto/catalog/hive.properties <<\"EOT\"\n" +
            "connector.name=hive-hadoop2\n" +
            "hive.metastore.uri=thrift://localhost:9083\n" +
            "EOT\n" +
            // create JMX catalog file
            "cat > /etc/presto/catalog/jmx.properties <<\"EOT\"\n" +
            "connector.name=jmx\n" +
            "EOT\n" +
            // start server
            "/etc/init.d/presto start\n" +
            // allow tail to work with Docker's non-local file system
            "tail ---disable-inotify -F /var/log/presto/server.log\n";

    try (GenericContainer<?> container = new GenericContainer<>(baseImage)) {
        container.withExposedPorts(8080)
                // the RPM is hundreds MB and file system bind is much more efficient
                .withFileSystemBind(rpmHostPath, rpm, BindMode.READ_ONLY)
                .withCommand("sh", "-xeuc", command)
                .waitingFor(forLogMessage(".*SERVER STARTED.*", 1).withStartupTimeout(Duration.ofMinutes(5)))
                .start();
        QueryRunner queryRunner = new QueryRunner(container.getContainerIpAddress(), container.getMappedPort(8080));
        assertEquals(queryRunner.execute("SHOW CATALOGS"), ImmutableSet.of(asList("system"), asList("hive"), asList("jmx")));
        // TODO remove usage of assertEventually once https://github.com/prestosql/presto/issues/2214 is fixed
        assertEventually(
                new io.airlift.units.Duration(1, MINUTES),
                () -> assertEquals(queryRunner.execute("SELECT specversion FROM jmx.current.\"java.lang:type=runtime\""), ImmutableSet.of(asList(expectedJavaVersion))));
    }
}
 
Example #9
Source File: SyndesisServerContainer.java    From syndesis with Apache License 2.0 5 votes vote down vote up
protected SyndesisServerContainer(String serverJarPath, String javaOptions, boolean deleteOnExit) {
    super(new ImageFromDockerfile("syndesis-server", deleteOnExit)
            .withDockerfileFromBuilder(builder -> builder.from("fabric8/s2i-java:3.0-java8")
                         .env("JAVA_OPTIONS", javaOptions)
                         .expose(SERVER_PORT, JOLOKIA_PORT, SyndesisTestEnvironment.getDebugPort())
                         .build()));

    withClasspathResourceMapping(serverJarPath,"/deployments/server.jar", BindMode.READ_ONLY);
}
 
Example #10
Source File: KeycloakServer.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeAll(ExtensionContext extensionContext) {
    keycloak = new FixedHostPortGenericContainer("quay.io/keycloak/keycloak:7.0.1")
            .withFixedExposedPort(8180, 8080)
            .withEnv("KEYCLOAK_USER", "admin")
            .withEnv("KEYCLOAK_PASSWORD", "admin")
            .withEnv("KEYCLOAK_IMPORT", "/tmp/realm.json")
            .withClasspathResourceMapping("quarkus-realm.json", "/tmp/realm.json", BindMode.READ_ONLY)
            .waitingFor(Wait.forHttp("/auth"));
    keycloak.start();
}
 
Example #11
Source File: KeycloakServer.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, String> start() {

    keycloak = new FixedHostPortGenericContainer("quay.io/keycloak/keycloak:" + System.getProperty("keycloak.version"))
            .withFixedExposedPort(8180, 8080)
            .withEnv("KEYCLOAK_USER", "admin")
            .withEnv("KEYCLOAK_PASSWORD", "admin")
            .withEnv("KEYCLOAK_IMPORT", "/tmp/realm.json")
            .withClasspathResourceMapping("quarkus-realm.json", "/tmp/realm.json", BindMode.READ_ONLY)
            .waitingFor(Wait.forHttp("/auth"));
    keycloak.start();
    return Collections.emptyMap();
}
 
Example #12
Source File: KeycloakServer.java    From quarkus-quickstarts with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeAll(ExtensionContext extensionContext) {
    keycloak = new FixedHostPortGenericContainer("quay.io/keycloak/keycloak:" + System.getProperty("keycloak.version"))
            .withFixedExposedPort(8180, 8080)
            .withEnv("KEYCLOAK_USER", "admin")
            .withEnv("KEYCLOAK_PASSWORD", "admin")
            .withEnv("KEYCLOAK_IMPORT", "/tmp/default-tenant-realm.json,/tmp/tenant-a-realm.json")
            .withClasspathResourceMapping("default-tenant-realm.json", "/tmp/default-tenant-realm.json", BindMode.READ_ONLY)
            .withClasspathResourceMapping("tenant-a-realm.json", "/tmp/tenant-a-realm.json", BindMode.READ_ONLY)
            .waitingFor(Wait.forHttp("/auth"));
    keycloak.start();
}
 
Example #13
Source File: BaseIntegrationTest.java    From eventeum with Apache License 2.0 5 votes vote down vote up
protected static void startParity() {
    parityContainer = new FixedHostPortGenericContainer("kauriorg/parity-docker:latest");
    parityContainer.waitingFor(Wait.forListeningPort());
    parityContainer.withFixedExposedPort(8545, 8545);
    parityContainer.withFixedExposedPort(8546, 8546);
    parityContainer.withFileSystemBind(PARITY_VOLUME_PATH,
            "/root/.local/share/io.parity.ethereum/", BindMode.READ_WRITE);
    parityContainer.addEnv("NO_BLOCKS", "true");
    parityContainer.start();

    waitForParityToStart(10000, Web3j.build(new HttpService("http://localhost:8545")));
}
 
Example #14
Source File: WebLogicIT.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
public WebLogicIT(final String webLogicVersion) {
    super(new GenericContainer<>("store/oracle/weblogic:" + webLogicVersion)
            .withClasspathResourceMapping("domain.properties", "/u01/oracle/properties/domain.properties", BindMode.READ_WRITE),
        7001,
        5005,
        "weblogic-application",
        "/u01/oracle/user_projects/domains/base_domain/autodeploy",
        "weblogic");
}
 
Example #15
Source File: StatefulFunctionsAppContainers.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
@Override
protected void before() throws Throwable {
  checkpointDir = temporaryCheckpointDir();

  master.withFileSystemBind(
      checkpointDir.getAbsolutePath(), "/checkpoint-dir", BindMode.READ_WRITE);
  workers.forEach(
      worker ->
          worker.withFileSystemBind(
              checkpointDir.getAbsolutePath(), "/checkpoint-dir", BindMode.READ_WRITE));

  master.start();
  workers.forEach(GenericContainer::start);
}
 
Example #16
Source File: EtcdContainer.java    From etcd4j with Apache License 2.0 4 votes vote down vote up
public EtcdContainer(Network network, LifecycleListener listener, boolean ssl, String clusterName, String endpoint, List<String> endpoints) {
    this.endpoint = endpoint;
    this.ssl = ssl;
    this.listener = listener;

    final String name = endpoint;
    final List<String> command = new ArrayList<>();

    this.container = new FixedHostPortGenericContainer<>(ETCD_DOCKER_IMAGE_NAME);
    this.container.withExposedPorts(ETCD_CLIENT_PORT, ETCD_PEER_PORT);
    this.container.withNetwork(network);
    this.container.withNetworkAliases(name);
    this.container.waitingFor(waitStrategy());
    this.container.withLogConsumer(logConsumer());

    command.add("--name");
    command.add(name);
    command.add("--advertise-client-urls");
    command.add((ssl ? "https" : "http") + "://0.0.0.0:" + ETCD_CLIENT_PORT);
    command.add("--listen-client-urls");
    command.add((ssl ? "https" : "http") + "://0.0.0.0:" + ETCD_CLIENT_PORT);

    if (ssl) {
        this.container.withClasspathResourceMapping(
            "ssl/cert/" + name + ".pem",
            "/etc/ssl/etcd/server.pem",
            BindMode.READ_ONLY,
            SelinuxContext.SHARED);

        this.container.withClasspathResourceMapping(
            "ssl/cert/" + name + "-key.pem",
            "/etc/ssl/etcd/server-key.pem",
            BindMode.READ_ONLY,
            SelinuxContext.SHARED);

        command.add("--cert-file");
        command.add("/etc/ssl/etcd/server.pem");
        command.add("--key-file");
        command.add("/etc/ssl/etcd/server-key.pem");
    }

    if (endpoints.size() > 1) {
        command.add("--initial-advertise-peer-urls");
        command.add("http://" + name + ":" + ETCD_PEER_PORT);
        command.add("--listen-peer-urls");
        command.add("http://0.0.0.0:" + ETCD_PEER_PORT);
        command.add("--initial-cluster-token");
        command.add(clusterName);
        command.add("--initial-cluster");
        command.add(endpoints.stream().map(e -> e + "=" + "http://" + e + ":" + ETCD_PEER_PORT).collect(Collectors.joining(",")));
        command.add("--initial-cluster-state");
        command.add("new");
    }

    if (!command.isEmpty()) {
        this.container.withCommand(command.toArray(new String[command.size()]));
    }
}
 
Example #17
Source File: DockerClientTest.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public void beforeAbstractKeycloakTest() throws Exception {
    super.beforeAbstractKeycloakTest();

    // find the realm cert
    String realmCert = null;
    List<KeysMetadataRepresentation.KeyMetadataRepresentation> realmKeys = adminClient.realm(REALM_ID).keys().getKeyMetadata().getKeys();
    for (KeysMetadataRepresentation.KeyMetadataRepresentation key : realmKeys) {
        if (key.getType().equals("RSA")) {
            realmCert = key.getCertificate();
        }
    }
    if (realmCert == null) {
        throw new IllegalStateException("Cannot find public realm cert");
    }

    // save the cert to a file
    File tmpCertFile = File.createTempFile("keycloak-docker-realm-cert-", ".pem");
    tmpCertFile.deleteOnExit();
    PrintWriter tmpCertWriter = new PrintWriter(tmpCertFile);
    tmpCertWriter.println(X509Factory.BEGIN_CERT);
    tmpCertWriter.println(realmCert);
    tmpCertWriter.println(X509Factory.END_CERT);
    tmpCertWriter.close();

    final Map<String, String> environment = new HashMap<>();
    environment.put("REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY", "/tmp");
    environment.put("REGISTRY_AUTH_TOKEN_REALM", "http://" + hostIp + ":" + authServerPort + "/auth/realms/" + REALM_ID + "/protocol/docker-v2/auth");
    environment.put("REGISTRY_AUTH_TOKEN_SERVICE", CLIENT_ID);
    environment.put("REGISTRY_AUTH_TOKEN_ISSUER", "http://" + hostIp + ":" + authServerPort + "/auth/realms/" + REALM_ID);
    environment.put("REGISTRY_AUTH_TOKEN_ROOTCERTBUNDLE", "/opt/kc-certs/" + tmpCertFile.getCanonicalFile().getName());
    environment.put("INSECURE_REGISTRY", "--insecure-registry " + REGISTRY_HOSTNAME + ":" + REGISTRY_PORT);

    String dockerioPrefix = Boolean.parseBoolean(System.getProperty("docker.io-prefix-explicit")) ? "docker.io/" : "";

    dockerRegistryContainer = new GenericContainer(dockerioPrefix + "registry:2")
            .withFileSystemBind(tmpCertFile.getCanonicalPath(), "/opt/kc-certs/" + tmpCertFile.getCanonicalFile().getName(), BindMode.READ_ONLY)
            .withEnv(environment)
            .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("dockerRegistryContainer")))
            .withNetworkMode("host")
            .withPrivilegedMode(true);
    dockerRegistryContainer.start();

    dockerClientContainer = new GenericContainer(dockerioPrefix + "docker:dind")
            .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("dockerClientContainer")))
            .withNetworkMode("host")
            .withPrivilegedMode(true);
    dockerClientContainer.start();
}
 
Example #18
Source File: VaultContainer.java    From hashicorp-vault-plugin with MIT License 4 votes vote down vote up
public static VaultContainer createVaultContainer() {
    if (!hasDockerDaemon()) {
        return null;
    }

    VaultContainer container = new VaultContainer()
        .withNetwork(CONTAINER_NETWORK)
        .withNetworkAliases("vault")
        .withCopyFileToContainer(forHostPath(
            TestConstants.class.getResource("vaultTest_server.hcl").getPath()),
            CONTAINER_CONFIG_FILE)
        .withCopyFileToContainer(forHostPath(
            TestConstants.class.getResource("startup.sh").getPath()),
            CONTAINER_STARTUP_SCRIPT)
        .withCopyFileToContainer(forHostPath(
            TestConstants.class.getResource("libressl.conf").getPath()),
            CONTAINER_OPENSSL_CONFIG_FILE)
        .withFileSystemBind(SSL_DIRECTORY, CONTAINER_SSL_DIRECTORY, BindMode.READ_WRITE)
        .withCreateContainerCmdModifier(command -> command.withCapAdd(Capability.IPC_LOCK))
        .withExposedPorts(8200)
        .withCommand("/bin/sh " + CONTAINER_STARTUP_SCRIPT)
        .withLogConsumer(new Slf4jLogConsumer(LOGGER))
        .waitingFor(Wait.forLogMessage(".+Vault server started!.+", 1));

    // Additionnal SAN can be set on the Vault Certificate. This allows Docker in Docker tests through TCP socket.
    container.withEnv("ADDITIONNAL_TEST_SAN", Optional.ofNullable(System.getenv("ADDITIONNAL_TEST_SAN")).orElse(""));

    // Propagate proxy settings for Docker in Docker tests
    if (System.getProperty("http.proxyHost") != null) {
        StringBuilder http_proxy = new StringBuilder();
        http_proxy.append("http://");
        if (System.getProperty("http.proxyUser") != null) {
            http_proxy.append(System.getProperty("http.proxyUser"))
                .append(":")
                .append(System.getProperty("http.proxyPassword"))
                .append("@");
        }
        http_proxy.append(System.getProperty("http.proxyHost"));
        if (System.getProperty("http.proxyPort") != null) {
            http_proxy.append(":").append(System.getProperty("http.proxyPort"));
        }
        container.withEnv("http_proxy", http_proxy.toString());
    }
    if (System.getProperty("http.nonProxyHosts") != null) {
        container.withEnv("no_proxy", convertNonProxyHostsToNoProxy(System.getProperty("http.nonProxyHosts")));
    }
    if (System.getProperty("https.proxyHost") != null) {
        StringBuilder https_proxy = new StringBuilder();
        https_proxy.append("http://");
        if (System.getProperty("https.proxyUser") != null) {
            https_proxy.append(System.getProperty("https.proxyUser"))
                .append(":")
                .append(System.getProperty("https.proxyPassword"))
                .append("@");
        }
        https_proxy.append(System.getProperty("https.proxyHost"));
        if (System.getProperty("https.proxyPort") != null) {
            https_proxy.append(":").append(System.getProperty("https.proxyPort"));
        }
        container.withEnv("https_proxy", https_proxy.toString());
    }
    if (System.getProperty("https.nonProxyHosts") != null) {
        container.withEnv("no_proxy", convertNonProxyHostsToNoProxy(System.getProperty("https.nonProxyHosts")));
    }

    // Override with environment proxy settings
    if (System.getenv("http_proxy") != null) {
        container.withEnv("http_proxy", System.getenv("http_proxy"));
    }
    if (System.getenv("HTTP_PROXY") != null) {
        container.withEnv("http_proxy", System.getenv("HTTP_PROXY"));
    }
    if (System.getenv("https_proxy") != null) {
        container.withEnv("https_proxy", System.getenv("https_proxy"));
    }
    if (System.getenv("HTTPS_PROXY") != null) {
        container.withEnv("https_proxy", System.getenv("HTTPS_PROXY"));
    }
    if (System.getenv("no_proxy") != null) {
        container.withEnv("no_proxy", System.getenv("no_proxy"));
    }
    if (System.getenv("NO_PROXY") != null) {
        container.withEnv("no_proxy", System.getenv("NO_PROXY"));
    }

    return container;
}
 
Example #19
Source File: EtcdContainer.java    From jetcd with Apache License 2.0 4 votes vote down vote up
public EtcdContainer(Network network, LifecycleListener listener, boolean ssl, String clusterName, String endpoint,
    List<String> endpoints, String image, List<String> additionalArgs) {

    this.endpoint = endpoint;
    this.ssl = ssl;
    this.listener = listener;
    this.dataDirectory = createDataDirectory(endpoint);

    this.container = new FixedHostPortGenericContainer<>(image);
    this.container.withExposedPorts(ETCD_PEER_PORT);
    this.container.withFixedExposedPort(getAvailablePort(), ETCD_CLIENT_PORT);
    this.container.withNetwork(network);
    this.container.withNetworkAliases(endpoint);
    this.container.waitingFor(Wait.forLogMessage(".*ready to serve client requests.*", 1));
    this.container.withLogConsumer(new Slf4jLogConsumer(LOGGER).withPrefix(endpoint));
    this.container.addFileSystemBind(dataDirectory.toString(), ETCD_DATA_DIR, BindMode.READ_WRITE, SelinuxContext.SHARED);

    List<String> cmd = new ArrayList<>();
    cmd.add("etcd");
    cmd.add("--name");
    cmd.add(endpoint);
    cmd.add("--advertise-client-urls");
    cmd.add((ssl ? "https" : "http") + "://0.0.0.0:" + ETCD_CLIENT_PORT);
    cmd.add("--listen-client-urls");
    cmd.add((ssl ? "https" : "http") + "://0.0.0.0:" + ETCD_CLIENT_PORT);
    cmd.add("--data-dir");
    cmd.add(ETCD_DATA_DIR);

    if (ssl) {
        this.container.withClasspathResourceMapping(
            "ssl/cert/" + endpoint + ".pem", "/etc/ssl/etcd/server.pem",
            BindMode.READ_ONLY,
            SelinuxContext.SHARED);

        this.container.withClasspathResourceMapping(
            "ssl/cert/" + endpoint + "-key.pem", "/etc/ssl/etcd/server-key.pem",
            BindMode.READ_ONLY,
            SelinuxContext.SHARED);

        cmd.add("--cert-file");
        cmd.add("/etc/ssl/etcd/server.pem");
        cmd.add("--key-file");
        cmd.add("/etc/ssl/etcd/server-key.pem");
    }

    if (endpoints.size() > 1) {
        cmd.add("--initial-advertise-peer-urls");
        cmd.add("http://" + endpoint + ":" + ETCD_PEER_PORT);
        cmd.add("--listen-peer-urls");
        cmd.add("http://0.0.0.0:" + ETCD_PEER_PORT);
        cmd.add("--initial-cluster-token");
        cmd.add(clusterName);
        cmd.add("--initial-cluster");
        cmd.add(endpoints.stream().map(e -> e + "=http://" + e + ":" + ETCD_PEER_PORT).collect(Collectors.joining(",")));
        cmd.add("--initial-cluster-state");
        cmd.add("new");
    }

    cmd.addAll(additionalArgs);

    if (!cmd.isEmpty()) {
        this.container.withCommand(cmd.toArray(new String[0]));
    }
}
 
Example #20
Source File: PulsarCluster.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private PulsarCluster(PulsarClusterSpec spec) {

        this.spec = spec;
        this.clusterName = spec.clusterName();
        this.network = Network.newNetwork();
        this.enablePrestoWorker = spec.enablePrestoWorker();

        if (enablePrestoWorker) {
            prestoWorkerContainer = new PrestoWorkerContainer(clusterName, PrestoWorkerContainer.NAME)
                    .withNetwork(network)
                    .withNetworkAliases(PrestoWorkerContainer.NAME)
                    .withEnv("clusterName", clusterName)
                    .withEnv("zkServers", ZKContainer.NAME)
                    .withEnv("zookeeperServers", ZKContainer.NAME + ":" + ZKContainer.ZK_PORT)
                    .withEnv("pulsar.zookeeper-uri", ZKContainer.NAME + ":" + ZKContainer.ZK_PORT)
                    .withEnv("pulsar.broker-service-url", "http://pulsar-broker-0:8080");
        } else {
            prestoWorkerContainer = null;
        }


        this.zkContainer = new ZKContainer(clusterName);
        this.zkContainer
            .withNetwork(network)
            .withNetworkAliases(ZKContainer.NAME)
            .withEnv("clusterName", clusterName)
            .withEnv("zkServers", ZKContainer.NAME)
            .withEnv("configurationStore", CSContainer.NAME + ":" + CS_PORT)
            .withEnv("forceSync", "no")
            .withEnv("pulsarNode", "pulsar-broker-0");

        this.csContainer = new CSContainer(clusterName)
            .withNetwork(network)
            .withNetworkAliases(CSContainer.NAME);

        this.bookieContainers = Maps.newTreeMap();
        this.brokerContainers = Maps.newTreeMap();
        this.workerContainers = Maps.newTreeMap();

        this.proxyContainer = new ProxyContainer(clusterName, ProxyContainer.NAME)
            .withNetwork(network)
            .withNetworkAliases("pulsar-proxy")
            .withEnv("zkServers", ZKContainer.NAME)
            .withEnv("zookeeperServers", ZKContainer.NAME)
            .withEnv("configurationStoreServers", CSContainer.NAME + ":" + CS_PORT)
            .withEnv("clusterName", clusterName);

        // create bookies
        bookieContainers.putAll(
                runNumContainers("bookie", spec.numBookies(), (name) -> new BKContainer(clusterName, name)
                        .withNetwork(network)
                        .withNetworkAliases(name)
                        .withEnv("zkServers", ZKContainer.NAME)
                        .withEnv("useHostNameAsBookieID", "true")
                        // Disable fsyncs for tests since they're slow within the containers
                        .withEnv("journalSyncData", "false")
                        .withEnv("journalMaxGroupWaitMSec", "0")
                        .withEnv("clusterName", clusterName)
                        .withEnv("diskUsageThreshold", "0.99")
                )
        );

        // create brokers
        brokerContainers.putAll(
                runNumContainers("broker", spec.numBrokers(), (name) -> new BrokerContainer(clusterName, name)
                        .withNetwork(network)
                        .withNetworkAliases(name)
                        .withEnv("zkServers", ZKContainer.NAME)
                        .withEnv("zookeeperServers", ZKContainer.NAME)
                        .withEnv("configurationStoreServers", CSContainer.NAME + ":" + CS_PORT)
                        .withEnv("clusterName", clusterName)
                        .withEnv("brokerServiceCompactionMonitorIntervalInSeconds", "1")
                        // used in s3 tests
                        .withEnv("AWS_ACCESS_KEY_ID", "accesskey")
                        .withEnv("AWS_SECRET_KEY", "secretkey")
                )
        );

        spec.classPathVolumeMounts.forEach((key, value) -> {
            zkContainer.withClasspathResourceMapping(key, value, BindMode.READ_WRITE);
            proxyContainer.withClasspathResourceMapping(key, value, BindMode.READ_WRITE);

            bookieContainers.values().forEach(c -> c.withClasspathResourceMapping(key, value, BindMode.READ_WRITE));
            brokerContainers.values().forEach(c -> c.withClasspathResourceMapping(key, value, BindMode.READ_WRITE));
            workerContainers.values().forEach(c -> c.withClasspathResourceMapping(key, value, BindMode.READ_WRITE));
        });

    }
 
Example #21
Source File: BaseClickHouseTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void setup() throws IOException, InterruptedException {
  // network sharing doesn't work with ClassRule
  network = Network.newNetwork();

  zookeeper =
      new GenericContainer<>("zookeeper:3.4.13")
          .withStartupAttempts(10)
          .withExposedPorts(2181)
          .withNetwork(network)
          .withNetworkAliases("zookeeper");

  // so far zookeeper container always starts successfully, so no extra retries
  zookeeper.start();

  clickHouse =
      (ClickHouseContainer)
          new ClickHouseContainer(CLICKHOUSE_IMAGE)
              .withStartupAttempts(10)
              .withCreateContainerCmdModifier(
                  // type inference for `(CreateContainerCmd) -> cmd.` doesn't work
                  cmd ->
                      ((CreateContainerCmd) cmd)
                          .withMemory(256 * 1024 * 1024L)
                          .withMemorySwap(4L * 1024 * 1024 * 1024L))
              .withNetwork(network)
              .withClasspathResourceMapping(
                  "config.d/zookeeper_default.xml",
                  "/etc/clickhouse-server/config.d/zookeeper_default.xml",
                  BindMode.READ_ONLY);

  BackOff backOff =
      FluentBackoff.DEFAULT
          .withMaxRetries(3)
          .withInitialBackoff(Duration.standardSeconds(15))
          .backoff();

  // try to start clickhouse-server a couple of times, see BEAM-6639
  while (true) {
    try {
      Unreliables.retryUntilSuccess(
          10,
          () -> {
            DockerClientFactory.instance()
                .checkAndPullImage(DockerClientFactory.instance().client(), CLICKHOUSE_IMAGE);

            return null;
          });

      clickHouse.start();
      break;
    } catch (Exception e) {
      if (!BackOffUtils.next(Sleeper.DEFAULT, backOff)) {
        throw e;
      } else {
        List<Image> images =
            DockerClientFactory.instance().client().listImagesCmd().withShowAll(true).exec();
        String listImagesOutput = "listImagesCmd:\n" + Joiner.on('\n').join(images) + "\n";

        LOG.warn("failed to start clickhouse-server\n\n" + listImagesOutput, e);
      }
    }
  }
}
 
Example #22
Source File: ContainersProvider.java    From replicator with Apache License 2.0 4 votes vote down vote up
@Override
public ServicesControl startMySQL(MySQLConfiguration mySQLConfiguration) {

    Map<String, String> envConfigs = new HashMap<>();
    // Root password is mandatory for starting mysql docker instance
    envConfigs.put(ContainersProvider.MYSQL_ROOT_PASSWORD_KEY, mySQLConfiguration.getPassword());
    envConfigs.computeIfAbsent(ContainersProvider.MYSQL_DATABASE_KEY, val -> mySQLConfiguration.getSchema());
    envConfigs.computeIfAbsent(ContainersProvider.MYSQL_USER_KEY, val -> mySQLConfiguration.getUsername());
    envConfigs.computeIfAbsent(ContainersProvider.MYSQL_PASSWORD_KEY, val -> mySQLConfiguration.getPassword());

    GenericContainer<?> mysql = this.getContainer(
            System.getProperty(
                    ContainersProvider.MYSQL_DOCKER_IMAGE_KEY,
                    VersionedPipelines.defaultTags.mysqlReplicantTag
            ),
            ContainersProvider.MYSQL_PORT,
            mySQLConfiguration.getNetwork(),
            ContainersProvider.MYSQL_STARTUP_WAIT_REGEX,
            ContainersProvider.MYSQL_STARTUP_WAIT_TIMES,
            // Cannot match exposed port in mysql as it can have conflicts
            false)
            .withEnv(envConfigs)
            .withClasspathResourceMapping(mySQLConfiguration.getConfPath(), ContainersProvider.MYSQL_CONFIGURATION_PATH, BindMode.READ_ONLY
    );

    for (String initScript : mySQLConfiguration.getInitScripts()) {
        mysql.withClasspathResourceMapping(initScript, String.format(ContainersProvider.MYSQL_INIT_SCRIPT_PATH, initScript), BindMode.READ_ONLY);
    }

    mysql.withNetworkAliases(mySQLConfiguration.getNetworkAlias());

    mysql.start();

    return new ServicesControl() {
        @Override
        public GenericContainer<?> getContainer() { return mysql; }

        @Override
        public void close() {
            mysql.stop();
        }

        @Override
        public int getPort() {
            return mysql.getMappedPort(ContainersProvider.MYSQL_PORT);
        }
    };
}
 
Example #23
Source File: ContainersProvider.java    From replicator with Apache License 2.0 4 votes vote down vote up
public ServicesControl startCustomTagMySQL(
        String mysqlImageTag,
        String schema,
        String username,
        String password,
        String... initScripts) {
    GenericContainer<?> mysql = this.getContainer(
            System.getProperty(
                    ContainersProvider.MYSQL_DOCKER_IMAGE_KEY,
                    mysqlImageTag
            ),
            ContainersProvider.MYSQL_PORT,
            null,
            ContainersProvider.MYSQL_STARTUP_WAIT_REGEX,
            ContainersProvider.MYSQL_STARTUP_WAIT_TIMES,
            false
    ).withEnv(ContainersProvider.MYSQL_ROOT_PASSWORD_KEY, password
    ).withEnv(ContainersProvider.MYSQL_DATABASE_KEY, schema
    ).withEnv(ContainersProvider.MYSQL_USER_KEY, username
    ).withEnv(ContainersProvider.MYSQL_PASSWORD_KEY, password
    ).withClasspathResourceMapping(ContainersProvider.MYSQL_CONFIGURATION_FILE, ContainersProvider.MYSQL_CONFIGURATION_PATH, BindMode.READ_ONLY
    );

    for (String initScript : initScripts) {
        mysql.withClasspathResourceMapping(initScript, String.format(ContainersProvider.MYSQL_INIT_SCRIPT_PATH, initScript), BindMode.READ_ONLY);
    }

    mysql.start();

    return new ServicesControl() {
        @Override
        public GenericContainer<?> getContainer() { return mysql; }

        @Override
        public void close() {
            mysql.stop();
        }

        @Override
        public int getPort() {
            return mysql.getMappedPort(ContainersProvider.MYSQL_PORT);
        }
    };
}
 
Example #24
Source File: DockerContainer.java    From presto with Apache License 2.0 4 votes vote down vote up
@Override
public DockerContainer withFileSystemBind(String hostPath, String containerPath, BindMode mode)
{
    verifyHostPath(hostPath);
    return super.withFileSystemBind(hostPath, containerPath, mode);
}