protected GenericContainer startContainerWithCommand(String shellCommand, WaitStrategy waitStrategy, Integer... ports) { // apply WaitStrategy to container return new GenericContainer(IMAGE_NAME) .withExposedPorts(ports) .withCommand("sh", "-c", shellCommand) .waitingFor(waitStrategy.withStartupTimeout(Duration.ofMillis(WAIT_TIMEOUT_MILLIS))); }
@Before public void setUp() { // Using a Dockerfile here, since Dockerfile builder DSL doesn't support HEALTHCHECK container = new GenericContainer(new ImageFromDockerfile() .withFileFromClasspath("write_file_and_loop.sh", "health-wait-strategy-dockerfile/write_file_and_loop.sh") .withFileFromClasspath("Dockerfile", "health-wait-strategy-dockerfile/Dockerfile")) .waitingFor(Wait.forHealthcheck().withStartupTimeout(Duration.ofSeconds(3))); }
public SwarmGenericContainer waitingFor(WaitStrategy waitStrategy) { container.waitingFor(waitStrategy); return this; }
@BeforeClass public static void setUp() throws Exception { TestUtil.ignoreException(() -> { mongo = new GenericContainer("mongo:3") .withNetworkAliases("mongo-" + Base58.randomString(6)) .withExposedPorts(MONGO_DEFAULT_PORT) .waitingFor(new HttpWaitStrategy() .forPort(MONGO_DEFAULT_PORT) .forStatusCodeMatching(response -> response == HTTP_OK || response == HTTP_UNAUTHORIZED) .withStartupTimeout(Duration.ofMinutes(2))); mongo.start(); }, Exception.class); assumeNotNull(mongo); assumeTrue("Mongo DB must be running", mongo.isRunning()); MongoClient mongoClient = new MongoClient(mongo.getContainerIpAddress(), mongo.getMappedPort(MONGO_DEFAULT_PORT)); HOST = String.format("mongodb://%s:%s", mongo.getContainerIpAddress(), mongo.getMappedPort(MONGO_DEFAULT_PORT)); params = map("host", HOST, "db", "test", "collection", "test"); MongoDatabase database = mongoClient.getDatabase("test"); collection = database.getCollection("test"); collection.deleteMany(new Document()); collection.insertOne(new Document(map("name", "testDocument", "date", currentTime, "longValue", longValue))); db = new TestGraphDatabaseFactory() .newImpermanentDatabaseBuilder() .newGraphDatabase(); TestUtil.registerProcedure(db, MongoDB.class); mongoClient.close(); }
public SwarmGenericContainer waitingFor(WaitStrategy waitStrategy) { container.waitingFor(waitStrategy); return this; }
@Bean(name = DynamoDBProperties.BEAN_NAME_EMBEDDED_DYNAMODB, destroyMethod = "stop") public GenericContainer mariadb(ConfigurableEnvironment environment, DynamoDBProperties properties) throws Exception { log.info("Starting mariadb server. Docker image: {}", properties.dockerImage); GenericContainer mariadb = new GenericContainer(properties.dockerImage) .withLogConsumer(containerLogsConsumer(log)) .withExposedPorts(properties.port) .waitingFor(new HostPortWaitStrategy()) .withStartupTimeout(properties.getTimeoutDuration()); mariadb.start(); registerDynamodbEnvironment(mariadb, environment, properties); return mariadb; }
@Bean(name = BEAN_NAME_EMBEDDED_VOLTDB, destroyMethod = "stop") public GenericContainer voltDB(ConfigurableEnvironment environment, VoltDBProperties properties, VoltDBStatusCheck voltDbStatusCheck) { log.info("Starting VoltDB server. Docker image: {}", properties.dockerImage); GenericContainer voltDB = new GenericContainer(properties.dockerImage) .withEnv("HOST_COUNT", "1") .withLogConsumer(containerLogsConsumer(log)) .withExposedPorts(properties.port) .waitingFor(voltDbStatusCheck) .withStartupTimeout(properties.getTimeoutDuration()); voltDB.start(); registerVoltDBEnvironment(voltDB, environment, properties); return voltDB; }
@Bean(name = BEAN_NAME_EMBEDDED_MEMSQL, destroyMethod = "stop") public GenericContainer memsql(ConfigurableEnvironment environment, MemSqlProperties properties, MemSqlStatusCheck memSqlStatusCheck) { log.info("Starting memsql server. Docker image: {}", properties.dockerImage); GenericContainer memsql = new GenericContainer<>(properties.dockerImage) .withEnv("IGNORE_MIN_REQUIREMENTS", "1") .withLogConsumer(containerLogsConsumer(log)) .withExposedPorts(properties.port) .withCopyFileToContainer(MountableFile.forClasspathResource("mem.sql"), "/schema.sql") .waitingFor(memSqlStatusCheck) .withStartupTimeout(properties.getTimeoutDuration()); memsql.start(); registerMemSqlEnvironment(memsql, environment, properties); return memsql; }
@Bean(name = BEAN_NAME_EMBEDDED_REDIS, destroyMethod = "stop") public GenericContainer redis(ConfigurableEnvironment environment, RedisProperties properties, RedisStatusCheck redisStatusCheck) throws Exception { log.info("Starting redis server. Docker image: {}", properties.dockerImage); GenericContainer redis = new GenericContainer(properties.dockerImage) .withLogConsumer(containerLogsConsumer(log)) .withExposedPorts(properties.port) .withEnv("REDIS_USER", properties.getUser()) .withEnv("REDIS_PASSWORD", properties.getPassword()) .withCommand("redis-server", "--requirepass", properties.getPassword()) .waitingFor(redisStatusCheck) .withStartupTimeout(properties.getTimeoutDuration()); redis.start(); registerRedisEnvironment(redis, environment, properties); return redis; }
@SuppressWarnings("resource") private DockerRabbitMQ(Optional<String> hostNamePrefix, Optional<String> clusterIdentity, Optional<String> erlangCookie, Optional<Network> net) { this.hostNameSuffix = clusterIdentity.orElse(UUID.randomUUID().toString()); this.rabbitHostName = hostName(hostNamePrefix); this.container = new GenericContainer<>(Images.RABBITMQ) .withCreateContainerCmdModifier(cmd -> cmd.withName(this.rabbitHostName)) .withCreateContainerCmdModifier(cmd -> cmd.withHostName(this.rabbitHostName)) .withExposedPorts(DEFAULT_RABBITMQ_PORT, DEFAULT_RABBITMQ_ADMIN_PORT) .waitingFor(waitStrategy()) .withLogConsumer(frame -> LOGGER.debug(frame.getUtf8String())) .withCreateContainerCmdModifier(cmd -> cmd.getHostConfig() .withTmpFs(ImmutableMap.of("/var/lib/rabbitmq/mnesia", "rw,noexec,nosuid,size=100m"))); net.ifPresent(this.container::withNetwork); erlangCookie.ifPresent(cookie -> this.container.withEnv(RABBITMQ_ERLANG_COOKIE, cookie)); this.nodeName = DEFAULT_RABBIT_NODE_NAME_PREFIX + "@" + this.rabbitHostName; this.container.withEnv(RABBITMQ_NODENAME, this.nodeName); }
@Bean(name = BEAN_NAME_EMBEDDED_POSTGRESQL, destroyMethod = "stop") public GenericContainer postgresql(ConfigurableEnvironment environment, PostgreSQLProperties properties, PostgreSQLStatusCheck postgreSQLStatusCheck) { log.info("Starting postgresql server. Docker image: {}", properties.dockerImage); GenericContainer postgresql = new GenericContainer(properties.dockerImage) .withEnv("POSTGRES_USER", properties.getUser()) .withEnv("POSTGRES_PASSWORD", properties.getPassword()) .withEnv("PGPASSWORD", properties.password) // for health check .withEnv("POSTGRES_DB", properties.getDatabase()) .withCommand("postgres") .withLogConsumer(containerLogsConsumer(log)) .withExposedPorts(properties.port) .waitingFor(postgreSQLStatusCheck) .withStartupTimeout(properties.getTimeoutDuration()); postgresql.start(); registerPostgresqlEnvironment(postgresql, environment, properties); return postgresql; }
public static GenericContainer<?> createTestContainer(final int testPort) { return new GenericContainer<>("localstack/localstack:latest") .withEnv("DEBUG", "1") .waitingFor(new HttpWaitStrategy().forStatusCode(200).forPort(testPort)); }
private GenericContainer<?> getContainer(String image, int port, Network network, String logWaitRegex, int logWaitTimes, boolean matchExposedPort) { GenericContainer<?> container = new GenericContainer<>(image) .withExposedPorts(port) .waitingFor( Wait.forLogMessage(logWaitRegex, logWaitTimes).withStartupTimeout(Duration.ofMinutes(5L)) ); if (network != null) { container.withNetwork(network); } if(matchExposedPort) { container.withCreateContainerCmdModifier( command -> command.withPortBindings(PortBinding.parse(String.format("%d:%d", port, port))) ); } return container; }
static GenericContainer create(ElasticSearchProperties properties, Logger containerLogger) { return new FixedHostPortGenericContainer(properties.dockerImage) .withExposedPorts(properties.httpPort, properties.transportPort) .withEnv("cluster.name", properties.getClusterName()) .withEnv("discovery.type", "single-node") .withEnv("ES_JAVA_OPTS", getJavaOpts(properties)) .withLogConsumer(containerLogsConsumer(containerLogger)) .waitingFor(getCompositeWaitStrategy(properties)) .withStartupTimeout(properties.getTimeoutDuration()); }
@Bean(name = BEAN_NAME_EMBEDDED_MARIADB, destroyMethod = "stop") public GenericContainer mariadb(ConfigurableEnvironment environment, MariaDBProperties properties, MariaDBStatusCheck mariaDBStatusCheck) throws Exception { log.info("Starting mariadb server. Docker image: {}", properties.dockerImage); GenericContainer mariadb = new GenericContainer(properties.dockerImage) .withEnv("MYSQL_ALLOW_EMPTY_PASSWORD", "true") .withEnv("MYSQL_USER", properties.getUser()) .withEnv("MYSQL_PASSWORD", properties.getPassword()) .withEnv("MYSQL_DATABASE", properties.getDatabase()) .withCommand( "--character-set-server=" + properties.getEncoding(), "--collation-server=" + properties.getCollation()) .withLogConsumer(containerLogsConsumer(log)) .withExposedPorts(properties.port) .waitingFor(mariaDBStatusCheck) .withStartupTimeout(properties.getTimeoutDuration()); mariadb.start(); registerMariadbEnvironment(mariadb, environment, properties); return mariadb; }
@SuppressWarnings("resource") public DockerCassandra() { client = DockerClientFactory.instance().client(); cassandraContainer = new GenericContainer<>("cassandra:3.11.3") .withCreateContainerCmdModifier(cmd -> cmd.getHostConfig().withTmpFs(ImmutableMap.of("/var/lib/cassandra", "rw,noexec,nosuid,size=200m"))) .withExposedPorts(CASSANDRA_PORT) .withLogConsumer(DockerCassandra::displayDockerLog); cassandraContainer .waitingFor(new CassandraWaitStrategy(cassandraContainer)); }
/** Starts a docker container optionally. For example, skips if Docker is unavailable. */ @SuppressWarnings("rawtypes") @BeforeClass public static void startContainer() { try { container = new GenericContainer(JAEGER_IMAGE) .withExposedPorts(JAEGER_HTTP_PORT, JAEGER_HTTP_PORT_THRIFT) .waitingFor(new HttpWaitStrategy()); container.start(); } catch (RuntimeException e) { throw new AssumptionViolatedException("could not start docker container", e); } }
@SuppressWarnings("resource") public DockerCassandra() { client = DockerClientFactory.instance().client(); cassandraContainer = new GenericContainer<>("cassandra:3.11.3") .withCreateContainerCmdModifier(cmd -> cmd.getHostConfig().withTmpFs(ImmutableMap.of("/var/lib/cassandra", "rw,noexec,nosuid,size=200m"))) .withExposedPorts(CASSANDRA_PORT) .withLogConsumer(DockerCassandra::displayDockerLog); cassandraContainer .waitingFor(new CassandraWaitStrategy(cassandraContainer)); }
public SpamAssassinExtension() { spamAssassinContainer = new GenericContainer<>( new ImageFromDockerfile() .withFileFromClasspath("Dockerfile", "docker/spamassassin/Dockerfile") .withFileFromClasspath("local.cf", "docker/spamassassin/local.cf") .withFileFromClasspath("run.sh", "docker/spamassassin/run.sh") .withFileFromClasspath("spamd.sh", "docker/spamassassin/spamd.sh") .withFileFromClasspath("rule-update.sh", "docker/spamassassin/rule-update.sh") .withFileFromClasspath("bayes_pg.sql", "docker/spamassassin/bayes_pg.sql")); spamAssassinContainer.waitingFor(new SpamAssassinWaitStrategy(spamAssassinContainer)); }
public DockerSwiftContainer() { this.swiftContainer = new GenericContainer<>(SWIFT_DOCKER_IMAGE); this.swiftContainer .withExposedPorts(KEYSTONE_ADMIN_PORT) .withExposedPorts(SWIFT_PORT) .withLogConsumer(DockerSwiftContainer::displayDockerLog) .waitingFor( new WaitAllStrategy() .withStrategy( forHttp("/v3") .forPort(KEYSTONE_ADMIN_PORT) .forStatusCode(200) .withRateLimiter(RateLimiters.TWENTIES_PER_SECOND) ).withStrategy( forHttp("/info") .forPort(SWIFT_PORT) .forStatusCode(200) .withRateLimiter(RateLimiters.TWENTIES_PER_SECOND) ) ); }