Java Examples for org.testcontainers.containers.GenericContainer

The following java examples will help you to understand the usage of org.testcontainers.containers.GenericContainer. These source code samples are taken from different open source projects.

Example 1
Project: testcontainers-java-master  File: GenericContainerRuleTest.java View source code
//TODO investigate intermittent failures
@Test
//TODO investigate intermittent failures
@Ignore
public void failFastWhenContainerHaltsImmediately() throws Exception {
    long startingTimeMs = System.currentTimeMillis();
    final GenericContainer failsImmediately = new GenericContainer("alpine:3.2").withCommand("/bin/sh", "-c", "return false").withMinimumRunningDuration(Duration.ofMillis(100));
    try {
        assertThrows("When we start a container that halts immediately, an exception is thrown", RetryCountExceededException.class, () -> {
            failsImmediately.start();
            return null;
        });
        // Check how long it took, to verify that we ARE bailing out early.
        // Want to strike a balance here; too short and this test will fail intermittently
        // on slow systems and/or due to GC variation, too long and we won't properly test
        // what we're intending to test.
        int allowedSecondsToFailure = GenericContainer.CONTAINER_RUNNING_TIMEOUT_SEC / 2;
        long completedTimeMs = System.currentTimeMillis();
        assertTrue("container should not take long to start up", completedTimeMs - startingTimeMs < 1000L * allowedSecondsToFailure);
    } finally {
        failsImmediately.stop();
    }
}
Example 2
Project: zipkin-master  File: LazyElasticsearchHttpStorage.java View source code
@Override
protected ElasticsearchHttpStorage compute() {
    try {
        container = new GenericContainer(image).withExposedPorts(9200).waitingFor(new HttpWaitStrategy().forPath("/"));
        container.start();
        System.out.println("Will use TestContainers Elasticsearch instance");
    } catch (Exception e) {
    }
    ElasticsearchHttpStorage result = computeStorageBuilder().build();
    Component.CheckResult check = result.check();
    if (check.ok) {
        return result;
    } else {
        throw new AssumptionViolatedException(check.exception.getMessage(), check.exception);
    }
}
Example 3
Project: elasticsearch-readonlyrest-plugin-master  File: LdapContainer.java View source code
private WaitStrategy ldapWaitStrategy(File initialDataLdif) {
    return new GenericContainer.AbstractWaitStrategy() {

        @Override
        protected void waitUntilReady() {
            logger.info("Waiting for LDAP container ...");
            Optional<LDAPConnection> connection = tryConnect(getLdapHost(), getLdapPort());
            if (connection.isPresent()) {
                try {
                    initLdap(connection.get(), initialDataLdif);
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                } finally {
                    connection.get().close();
                }
            } else {
                throw new IllegalStateException("Cannot connect");
            }
            logger.info("LDAP container stated");
        }

        private LDAPConnection createConnection() {
            LDAPConnectionOptions options = new LDAPConnectionOptions();
            options.setConnectTimeoutMillis((int) LDAP_CONNECT_TIMEOUT.toMillis());
            return new LDAPConnection(options);
        }

        private Optional<LDAPConnection> tryConnect(String address, Integer port) {
            final Instant startTime = Instant.now();
            final LDAPConnection connection = createConnection();
            do {
                try {
                    connection.connect(address, port);
                    Thread.sleep(WAIT_BETWEEN_RETRIES.toMillis());
                } catch (Exception ignored) {
                }
            } while (!connection.isConnected() && !checkTimeout(startTime, startupTimeout));
            return Optional.of(connection);
        }

        private void initLdap(LDAPConnection connection, File initialDataLdif) throws Exception {
            LDAPConnection bindedConnection = bind(connection);
            LDIFReader r = new LDIFReader(initialDataLdif.getAbsoluteFile());
            Entry readEntry;
            while ((readEntry = r.readEntry()) != null) {
                bindedConnection.add(new AddRequest(readEntry.toLDIF()));
            }
        }

        private LDAPConnection bind(LDAPConnection connection) throws Exception {
            SearchingUserConfig bindDNAndPassword = getSearchingUserConfig();
            BindResult bindResult = connection.bind(bindDNAndPassword.getDn(), bindDNAndPassword.getPassword());
            if (!ResultCode.SUCCESS.equals(bindResult.getResultCode())) {
                throw new ContainerCreationException("Cannot init LDAP due to bind problem");
            }
            return connection;
        }
    };
}
Example 4
Project: datacollector-master  File: LdapAuthenticationBaseIT.java View source code
static LdapConnection setupLdapServer(GenericContainer server, String setupFile) {
    // setup Ldap server 1
    LdapConnection connection = new LdapNetworkConnection(server.getContainerIpAddress(), server.getMappedPort(LDAP_PORT));
    try {
        connection.bind(BIND_DN, BIND_PWD);
        LdifReader reader = new LdifReader(Resources.getResource(setupFile).getFile());
        for (LdifEntry entry : reader) {
            connection.add(entry.getEntry());
        }
    } catch (LdapException e) {
        LOG.error("Setup server 1 failed " + e);
    }
    return connection;
}
Example 5
Project: spring-data-aerospike-master  File: EmbeddedAerospikeAutoConfiguration.java View source code
@Bean(initMethod = "start", destroyMethod = "stop")
public GenericContainer aerosike() {
    GenericContainer aerosike = new GenericContainer("aerospike:latest").withExposedPorts(TestConstants.AS_PORT).withClasspathResourceMapping("aerospike.conf", "/etc/aerospike/aerospike.conf", BindMode.READ_ONLY);
    return aerosike;
}