Java Code Examples for org.testcontainers.containers.GenericContainer#start()

The following examples show how to use org.testcontainers.containers.GenericContainer#start() . 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: DirectoryTarResourceTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void simpleRecursiveFileTest() {
    // 'src' is expected to be the project base directory, so all source code/resources should be copied in
    File directory = new File("src");

    GenericContainer container = new GenericContainer(
            new ImageFromDockerfile()
                    .withDockerfileFromBuilder(builder ->
                            builder.from("alpine:3.3")
                                    .copy("/tmp/foo", "/foo")
                                    .cmd("cat /foo/test/resources/test-recursive-file.txt")
                                    .build()
                    ).withFileFromFile("/tmp/foo", directory))
            .withStartupCheckStrategy(new OneShotStartupCheckStrategy());

    container.start();

    final String results = container.getLogs();

    assertTrue("The container has a file that was copied in via a recursive copy", results.contains("Used for DirectoryTarResourceTest"));
}
 
Example 2
Source File: ContainerUtils.java    From Mahuta with Apache License 2.0 6 votes vote down vote up
public static void startContainer(String name, ContainerType type, Map<String, String> customEnvVars) {
    GenericContainer<?> container = new GenericContainer<>(containersRegistry.get(type).image)
            .withExposedPorts(containersRegistry.get(type).exposedPorts);
    
    if(containersRegistry.get(type).waitStrategy != null) {
        container.waitingFor(containersRegistry.get(type).waitStrategy);
    }
    
    if(containersRegistry.get(type).envVars != null) {
        containersRegistry.get(type).envVars.forEach((key, value) -> {
            container.addEnv(key, value);
        });
    }
    
    if(customEnvVars != null) {
        customEnvVars.forEach((key, value) -> {
            container.addEnv(key, value);
        });
    }
    
    container.withLogConsumer(new Slf4jLogConsumer(log));
    
    container.start();
    
    containers.put(name, container);
}
 
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: MainIT.java    From dockerfile-maven with Apache License 2.0 6 votes vote down vote up
private GenericContainer createBackend(final Network network) {
  final String image;
  try {
    image = Resources.toString(
        Resources.getResource("META-INF/docker/com.spotify.it/backend/image-name"),
        Charsets.UTF_8).trim();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }

  final GenericContainer container = new GenericContainer(image)
      .withExposedPorts(BACKEND_PORT)
      .withNetwork(network)
      .withNetworkAliases("backend")
      .waitingFor(Wait.forHttp("/api/version"));

  // start early, since frontend needs to know the port of backend
  container.start();

  return container;
}
 
Example 5
Source File: ImagePullPolicyTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
private void expectToFailWithNotFoundException(GenericContainer<?> container) {
    try {
        container.start();
        fail("Should fail");
    } catch (ContainerLaunchException e) {
        Throwable throwable = e;
        while (throwable.getCause() != null) {
            throwable = throwable.getCause();
            if (throwable.getCause() instanceof NotFoundException) {
                VisibleAssertions.pass("Caused by NotFoundException");
                return;
            }
        }
        VisibleAssertions.fail("Caused by NotFoundException");
    }
}
 
Example 6
Source File: ContainersProvider.java    From replicator with Apache License 2.0 6 votes vote down vote up
@Override
public ServicesControl startZookeeper() {
    GenericContainer<?> zookeeper = this.getZookeeper(
            null, VersionedPipelines.defaultTags.zookeeperTag);

    zookeeper.start();


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

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

        @Override
        public int getPort() {
            return zookeeper.getMappedPort(ContainersProvider.ZOOKEEPER_PORT);
        }

    };
}
 
Example 7
Source File: ContainersProvider.java    From replicator with Apache License 2.0 6 votes vote down vote up
@Override
public ServicesControl startZookeeper(Network network, String networkAlias) {
    GenericContainer<?> zookeeper = this.getZookeeper(network, VersionedPipelines.defaultTags.zookeeperTag);
    zookeeper.withNetworkAliases(networkAlias);

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

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

        @Override
        public int getPort() {
            return zookeeper.getMappedPort(ContainersProvider.ZOOKEEPER_PORT);
        }

    };
}
 
Example 8
Source File: MongoDbPluginIT.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@Override
public void executeApp() throws Exception {
    GenericContainer<?> mongo = new GenericContainer<>("mongo:4.0.3");
    mongo.setExposedPorts(Arrays.asList(27017));
    mongo.start();
    try {
        mongoClient = MongoClients.create("mongodb://" + mongo.getContainerIpAddress() + ":"
                + mongo.getMappedPort(27017));
        beforeTransactionMarker();
        transactionMarker();
    } finally {
        mongo.close();
    }
}
 
Example 9
Source File: ShellUserGroupProviderIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
private GenericContainer createContainer(String image) throws IOException, InterruptedException {
    GenericContainer container = new GenericContainer(image)
        .withEnv("SSH_ENABLE_ROOT", "true").withExposedPorts(CONTAINER_SSH_PORT);
    container.start();

    // This can go into the docker images:
    container.execInContainer("mkdir", "-p", "/root/.ssh");
    container.copyFileToContainer(MountableFile.forHostPath(sshPubKeyFile),  CONTAINER_SSH_AUTH_KEYS);
    return container;
}
 
Example 10
Source File: PulsarCluster.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public void startService(String networkAlias,
                         GenericContainer<?> serviceContainer) {
    log.info("Starting external service {} ...", networkAlias);
    serviceContainer.withNetwork(network);
    serviceContainer.withNetworkAliases(networkAlias);
    serviceContainer.start();
    log.info("Successfully start external service {}", networkAlias);
}
 
Example 11
Source File: ContainersProvider.java    From replicator with Apache License 2.0 5 votes vote down vote up
@Override
public ServicesControl startHbase() {

    Network network = Network.newNetwork();

    GenericContainer<?> hbase = this.getContainerHBase(
            VersionedPipelines.defaultTags.hbase,
            network,
            "",
            0,
            true
    );

    hbase.start();

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

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

        @Override
        public int getPort() {
            return hbase.getMappedPort(ContainersProvider.HBASE_ZK_PORT);
        }
    };
}
 
Example 12
Source File: ContainersProvider.java    From replicator with Apache License 2.0 5 votes vote down vote up
@Override
public ServicesControl startSchemaRegistry(Network network) {

    GenericContainer<?> schemaRegistry = this.getContainer(System.getProperty(
            ContainersProvider.SCHEMA_REGISTRY_IMAGE_KEY,
            VersionedPipelines.defaultTags.schemaRegistryTag
            ),
            ContainersProvider.SCHEMA_REGISTRY_PORT,
            network,
            ContainersProvider.SCHEMA_REGISTRY_WAIT_REGEX,
            1,
            true
    ).withEnv(
            ContainersProvider.SCHEMA_REGISTRY_KAFKASTORE_CONNECTION_URL_KEY,
            String.format("%s:%d", "kafkaZk", ContainersProvider.ZOOKEEPER_PORT)
    ).withEnv(
            ContainersProvider.SCHEMA_REGISTRY_HOST_NAME_KEY,
            "localhost"
    );

    schemaRegistry.start();

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

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

        @Override
        public int getPort() {
            return schemaRegistry.getMappedPort(ContainersProvider.SCHEMA_REGISTRY_PORT);
        }
    };
}
 
Example 13
Source File: AbstractWaitStrategyTest.java    From testcontainers-java with MIT License 5 votes vote down vote up
/**
 * Expects that the WaitStrategy returns successfully after connection to a container with a listening port.
 *
 * @param container the container to start
 */
protected void waitUntilReadyAndSucceed(GenericContainer container) {
    // start() blocks until successful or timeout
    container.start();

    assertTrue(String.format("Expected container to be ready after timeout of %sms",
        WAIT_TIMEOUT_MILLIS), ready.get());
}
 
Example 14
Source File: MongoDbPluginIT.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@Override
public void executeApp() throws Exception {
    GenericContainer<?> mongo = new GenericContainer<>("mongo:4.0.3");
    mongo.setExposedPorts(Arrays.asList(27017));
    mongo.start();
    try {
        mongoClient = new Mongo(mongo.getContainerIpAddress(), mongo.getMappedPort(27017));
        transactionMarker();
    } finally {
        mongo.close();
    }
}
 
Example 15
Source File: Consumer.java    From liiklus with MIT License 5 votes vote down vote up
private static String getLiiklusTarget() {
    GenericContainer<?> liiklus = new GenericContainer<>("bsideup/liiklus:0.9.0")
            .withExposedPorts(6565)
            .withEnv("storage_records_type", "MEMORY")
            .withEnv("storage_positions_type", "MEMORY"); // Fine for testing, NOT FINE I WARNED YOU for production :D

    liiklus.start();

    log.info("Containers started");

    return String.format("%s:%d", liiklus.getContainerIpAddress(), liiklus.getFirstMappedPort());
}
 
Example 16
Source File: ContainersProvider.java    From replicator with Apache License 2.0 4 votes vote down vote up
@Override
public ServicesControl startKafka(Network network, String topic, int partitions, int replicas, String networkAlias) {

    GenericContainer<?> kafka = this.getContainer(
            System.getProperty(
                    ContainersProvider.KAFKA_DOCKER_IMAGE_KEY,
                    VersionedPipelines.defaultTags.kafkaTag
            ),
            ContainersProvider.KAFKA_PORT,
            network,
            ContainersProvider.KAFKA_STARTUP_WAIT_REGEX,
            partitions,
            true
    ).withEnv(
            ContainersProvider.KAFKA_ZOOKEEPER_CONNECT_KEY,
            String.format("%s:%d", "kafkaZk", ContainersProvider.ZOOKEEPER_PORT)
    ).withEnv(
            ContainersProvider.KAFKA_CREATE_TOPICS_KEY,
            String.format("%s:%d:%d", topic, partitions, replicas)
    ).withEnv(KAFKA_LISTENERS_KEY, String.format("PLAINTEXT://%s:29092,OUTSIDE://0.0.0.0:%d", networkAlias, KAFKA_PORT)
    ).withEnv(KAFKA_LISTENER_SECURITY_PROTOCOL_MAP_KEY, "PLAINTEXT:PLAINTEXT,OUTSIDE:PLAINTEXT"
    ).withEnv(KAFKA_ADVERTISED_LISTENERS_KEY, String.format("PLAINTEXT://%s:29092,OUTSIDE://localhost:%d", networkAlias, KAFKA_PORT)
    ).withEnv(KAFKA_INTER_BROKER_LISTENER_NAME_KEY, "PLAINTEXT");
    //   https://rmoff.net/2018/08/02/kafka-listeners-explained/

    kafka.withNetworkAliases(networkAlias);

    kafka.start();

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

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

        @Override
        public int getPort() {
            return kafka.getMappedPort(ContainersProvider.KAFKA_PORT);
        }
    };
}
 
Example 17
Source File: ContainersProvider.java    From replicator with Apache License 2.0 4 votes vote down vote up
public ServicesControl startKafka(String kafkaImageTag, String topic, int partitions, int replicas) {
    Network network = Network.newNetwork();

    GenericContainer<?> zookeeper = this.getZookeeper(
            network,
            VersionedPipelines.defaultTags.zookeeperTag
    );

    zookeeper.start();

    GenericContainer<?> kafka = this.getContainer(
            System.getProperty(
                    ContainersProvider.KAFKA_DOCKER_IMAGE_KEY,
                    kafkaImageTag
            ),
            ContainersProvider.KAFKA_PORT,
            network,
            ContainersProvider.KAFKA_STARTUP_WAIT_REGEX,
            partitions,
            true
    ).withEnv(
            ContainersProvider.KAFKA_ZOOKEEPER_CONNECT_KEY,
            String.format("%s:%d", zookeeper.getContainerInfo().getConfig().getHostName(), ContainersProvider.ZOOKEEPER_PORT)
    ).withEnv(
            ContainersProvider.KAFKA_CREATE_TOPICS_KEY,
            String.format("%s:%d:%d", topic, partitions, replicas)
    ).withEnv(
            ContainersProvider.KAFKA_ADVERTISED_HOST_NAME_KEY,
            "localhost"
    );

    kafka.start();

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

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

        @Override
        public int getPort() {
            return kafka.getMappedPort(ContainersProvider.KAFKA_PORT);
        }
    };
}
 
Example 18
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 19
Source File: KafkaIntegrationTest.java    From kop with Apache License 2.0 4 votes vote down vote up
@Test(timeOut = 3 * 60_000, dataProvider = "integrations")
void simpleProduceAndConsume(final String integration, final Optional<String> topic,
                             final boolean shouldProduce, final boolean shouldConsume) throws Exception {
    String topicName = topic.orElse(integration);
    System.out.println("starting integration " + integration + " with topicName " + topicName);

    admin.topics().createPartitionedTopic(topicName, 1);

    System.out.println("topic created");

    final GenericContainer producer = new GenericContainer<>("streamnative/kop-test-" + integration)
            .withEnv("KOP_BROKER", "host.testcontainers.internal:" + super.kafkaBrokerPort)
            .withEnv("KOP_PRODUCE", "true")
            .withEnv("KOP_TOPIC", topic.orElse(integration))
            .withEnv("KOP_LIMIT", "10")
            .withLogConsumer(new org.testcontainers.containers.output.Slf4jLogConsumer(KafkaIntegrationTest.log))
            .waitingFor(Wait.forLogMessage("starting to produce\\n", 1))
            .withNetworkMode("host");

    final GenericContainer consumer = new GenericContainer<>("streamnative/kop-test-" + integration)
            .withEnv("KOP_BROKER", "host.testcontainers.internal:" + super.kafkaBrokerPort)
            .withEnv("KOP_TOPIC", topic.orElse(integration))
            .withEnv("KOP_CONSUME", "true")
            .withEnv("KOP_LIMIT", "10")
            .withLogConsumer(new org.testcontainers.containers.output.Slf4jLogConsumer(KafkaIntegrationTest.log))
            .waitingFor(Wait.forLogMessage("starting to consume\\n", 1))
            .withNetworkMode("host");

    WaitingConsumer producerWaitingConsumer = null;
    WaitingConsumer consumerWaitingConsumer = null;
    if (shouldProduce) {
        producer.start();
        producerWaitingConsumer = KafkaIntegrationTest.createLogFollower(producer);
        System.out.println("producer started");
    }

    if (shouldConsume) {
        consumer.start();
        consumerWaitingConsumer = KafkaIntegrationTest.createLogFollower(consumer);
        System.out.println("consumer started");
    }

    if (shouldProduce) {
        producerWaitingConsumer.waitUntil(frame ->
                frame.getUtf8String().contains("ExitCode"), 30, TimeUnit.SECONDS);
        KafkaIntegrationTest.checkForErrorsInLogs(producer.getLogs());
    }

    if (shouldConsume) {
        consumerWaitingConsumer.waitUntil(frame ->
                frame.getUtf8String().contains("ExitCode"), 30, TimeUnit.SECONDS);
        KafkaIntegrationTest.checkForErrorsInLogs(consumer.getLogs());
    }
}
 
Example 20
Source File: DockerfileTest.java    From testcontainers-java with MIT License 4 votes vote down vote up
@Test
public void filePermissions() throws TimeoutException {

    WaitingConsumer consumer = new WaitingConsumer();

    ImageFromDockerfile image = new ImageFromDockerfile()
            .withFileFromTransferable("/someFile.txt", new Transferable() {
                @Override
                public long getSize() {
                    return 0;
                }

                @Override
                public byte[] getBytes() {
                    return new byte[0];
                }

                @Override
                public String getDescription() {
                    return "test file";
                }

                @Override
                public int getFileMode() {
                    return 0123;
                }


            })
            .withDockerfileFromBuilder(builder -> builder
                    .from("alpine:3.2")
                    .copy("someFile.txt", "/someFile.txt")
                    .cmd("stat -c \"%a\" /someFile.txt")
            );

    GenericContainer container = new GenericContainer(image)
            .withStartupCheckStrategy(new OneShotStartupCheckStrategy())
            .withLogConsumer(consumer);

    try {
        container.start();

        consumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("123"), 5, TimeUnit.SECONDS);
    } finally {
        container.stop();
    }
}