org.testcontainers.Testcontainers Java Examples

The following examples show how to use org.testcontainers.Testcontainers. 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: HostPortExposedTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
    server = HttpServer.create(new InetSocketAddress(0), 0);
    server.createContext("/", exchange -> {
        byte[] content = "Hello World!".getBytes();
        exchange.sendResponseHeaders(200, content.length);
        try (OutputStream responseBody = exchange.getResponseBody()) {
            responseBody.write(content);
            responseBody.flush();
        }
    });

    server.start();
    localServerPort = server.getAddress().getPort();

    // exposePort {
    Testcontainers.exposeHostPorts(localServerPort);
    // }
}
 
Example #2
Source File: ExposedHostTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@BeforeClass
public static void setUpClass() throws Exception {
    server = HttpServer.create(new InetSocketAddress(0), 0);
    server.createContext("/", exchange -> {
        byte[] content = "Hello World!".getBytes();
        exchange.sendResponseHeaders(200, content.length);
        try (OutputStream responseBody = exchange.getResponseBody()) {
            responseBody.write(content);
            responseBody.flush();
        }
    });

    server.start();
    Testcontainers.exposeHostPorts(server.getAddress().getPort());
    
    Testcontainers.exposeHostPorts(ImmutableMap.of(server.getAddress().getPort(), 80));
    Testcontainers.exposeHostPorts(ImmutableMap.of(server.getAddress().getPort(), 81));           
}
 
Example #3
Source File: SeleniumChromeExtension.java    From apicurio-registry with Apache License 2.0 5 votes vote down vote up
private void deployChrome() {
    LOGGER.info("Deploying chrome browser");
    if (!TestUtils.isExternalRegistry()) {
        Testcontainers.exposeHostPorts(TestUtils.getRegistryPort());
    }
    chrome = new BrowserWebDriverContainer()
            .withCapabilities(new ChromeOptions());
    chrome.start();
    SeleniumProvider.getInstance().setupDriver(chrome.getWebDriver());
    SeleniumProvider.getInstance().setUiUrl(TestUtils.getRegistryUIUrl().replace("localhost", "host.testcontainers.internal"));
    deployed = true;
}
 
Example #4
Source File: BytecoderUnitTestRunner.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private static synchronized BrowserWebDriverContainer initializeSeleniumContainer() {

        if (SELENIUMCONTAINER == null) {
            java.util.logging.Logger.getLogger("org.openqa.selenium").setLevel(Level.OFF);

            final ChromeOptions theOptions = new ChromeOptions().setHeadless(true);
            theOptions.addArguments("--js-flags=experimental-wasm-eh");
            theOptions.addArguments("--enable-experimental-wasm-eh");
            theOptions.addArguments("disable-infobars"); // disabling infobars
            theOptions.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
            theOptions.addArguments("--no-sandbox"); // Bypass OS security model
            theOptions.setExperimentalOption("useAutomationExtension", false);
            final LoggingPreferences theLoggingPreferences = new LoggingPreferences();
            theLoggingPreferences.enable(LogType.BROWSER, Level.ALL);
            theOptions.setCapability(CapabilityType.LOGGING_PREFS, theLoggingPreferences);
            theOptions.setCapability("goog:loggingPrefs", theLoggingPreferences);

            Testcontainers.exposeHostPorts(getTestWebServerPort());

            SELENIUMCONTAINER = new BrowserWebDriverContainer()
                    .withCapabilities(theOptions)
                    .withRecordingMode(BrowserWebDriverContainer.VncRecordingMode.SKIP, new File("."));
            SELENIUMCONTAINER.start();

            Runtime.getRuntime().addShutdownHook(new Thread(() -> SELENIUMCONTAINER.stop()));
        }
        return SELENIUMCONTAINER;
    }
 
Example #5
Source File: KafkaConnectConverterIT.java    From apicurio-registry with Apache License 2.0 4 votes vote down vote up
@BeforeEach
public void startContainers() {
    String apicurioVersion = System.getProperty("project.version");
    assertNotNull(apicurioVersion);

    Path converterDistro = Paths.get(System.getProperty("user.dir"), "..", "distro", "connect-converter",
            "target", "apicurio-kafka-connect-converter-" + apicurioVersion + "-converter.tar.gz");

    if (Files.notExists(converterDistro)) {
        LOGGER.info("Connecter distribution {}", converterDistro.toString());
        throw new IllegalStateException("Kafka connect converter distribution is not present");
    }

    ImageFromDockerfile apicurioDebeziumImage = new ImageFromDockerfile()
            .withFileFromPath("converter-distro.tar.gz", converterDistro)
            .withDockerfileFromBuilder(builder -> builder
                    .from("debezium/connect:1.1.1.Final")
                    .env("KAFKA_CONNECT_DEBEZIUM_DIR", "$KAFKA_CONNECT_PLUGINS_DIR/debezium-connector-postgres")
                    .copy("converter-distro.tar.gz", "$KAFKA_CONNECT_DEBEZIUM_DIR/apicurio-kafka-connect-converter.tar.gz")
                    .run("cd $KAFKA_CONNECT_DEBEZIUM_DIR && tar -xvf apicurio-kafka-connect-converter.tar.gz")
                    .build());

    if (!TestUtils.isExternalRegistry()) {
        Testcontainers.exposeHostPorts(8081);
    }

    Testcontainers.exposeHostPorts(9092);
    kafka = new KafkaContainer();
    kafka.addEnv("KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR", "1");
    kafka.addEnv("KAFKA_TRANSACTION_STATE_LOG_MIN_ISR", "1");
    kafka.addExposedPorts(9092);
    kafka.withCreateContainerCmdModifier(cmd -> {
        cmd
                .withHostName("localhost")
                .withPortBindings(new PortBinding(Ports.Binding.bindPort(9092), new ExposedPort(9092)));
    });
    kafka.start();

    Network network = Network.newNetwork();

    postgres = new PostgreSQLContainer<>("debezium/postgres:11")
          .withNetwork(network)
          .withNetworkAliases("postgres");
    postgres.start();

    debeziumContainer = new DebeziumContainer("dummy-version");
    debeziumContainer.setImage(apicurioDebeziumImage);
    debeziumContainer.withNetwork(network)
          .withEnv("BOOTSTRAP_SERVERS", "host.testcontainers.internal:9092")
          .withLogConsumer(new Slf4jLogConsumer(LOGGER));
    debeziumContainer.setWaitStrategy(
            Wait.forHttp("/connectors")
            .forPort(8083)
            .forStatusCode(200)
            .withReadTimeout(Duration.ofSeconds(3))
            .withStartupTimeout(Duration.ofSeconds(300)));
    debeziumContainer.start();

}
 
Example #6
Source File: KafkaIntegrationTest.java    From kop with Apache License 2.0 4 votes vote down vote up
@BeforeClass
@Override
protected void setup() throws Exception {

    super.resetConfig();
    // in order to access PulsarBroker when using Docker for Mac, we need to adjust things:
    // - set pulsar advertized address to host IP
    // - use the `host.testcontainers.internal` address exposed by testcontainers
    String ip = InetAddress.getLocalHost().getHostAddress();
    System.out.println("exposing Pulsar broker on " + ip);
    conf.setAdvertisedAddress(ip);
    ((KafkaServiceConfiguration) conf).setListeners(
            PLAINTEXT_PREFIX + ip + ":" + kafkaBrokerPort + ","
                    + SSL_PREFIX + ip + ":" + kafkaBrokerPortTls);
    super.internalSetup();


    if (!this.admin.clusters().getClusters().contains(this.configClusterName)) {
        // so that clients can test short names
        this.admin.clusters().createCluster(this.configClusterName,
                new ClusterData("http://127.0.0.1:" + this.brokerWebservicePort));
    } else {
        this.admin.clusters().updateCluster(this.configClusterName,
                new ClusterData("http://127.0.0.1:" + this.brokerWebservicePort));
    }

    if (!this.admin.tenants().getTenants().contains("public")) {
        this.admin.tenants().createTenant("public",
                new TenantInfo(Sets.newHashSet("appid1", "appid2"), Sets.newHashSet("test")));
    } else {
        this.admin.tenants().updateTenant("public",
                new TenantInfo(Sets.newHashSet("appid1", "appid2"), Sets.newHashSet("test")));
    }
    if (!this.admin.namespaces().getNamespaces("public").contains("public/default")) {
        this.admin.namespaces().createNamespace("public/default");
        this.admin.namespaces().setNamespaceReplicationClusters("public/default", Sets.newHashSet("test"));
        this.admin.namespaces().setRetention("public/default",
                new RetentionPolicies(60, 1000));
    }
    if (!this.admin.namespaces().getNamespaces("public").contains("public/__kafka")) {
        this.admin.namespaces().createNamespace("public/__kafka");
        this.admin.namespaces().setNamespaceReplicationClusters("public/__kafka", Sets.newHashSet("test"));
        this.admin.namespaces().setRetention("public/__kafka",
                new RetentionPolicies(-1, -1));
    }
    Testcontainers.exposeHostPorts(ImmutableMap.of(super.kafkaBrokerPort, super.kafkaBrokerPort));
}
 
Example #7
Source File: AsciidocConfluencePublisherCommandLineClientIntegrationTest.java    From confluence-publisher with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void exposeConfluenceServerPortOnHost() {
    Testcontainers.exposeHostPorts(8090);
}
 
Example #8
Source File: AsciidocConfluencePublisherMojoIntegrationTest.java    From confluence-publisher with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void exposeConfluenceServerPortOnHost() {
    Testcontainers.exposeHostPorts(8090);
}
 
Example #9
Source File: DockerBasedPublishingIntegrationTest.java    From confluence-publisher with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void exposeConfluenceServerPortOnHost() {
    Testcontainers.exposeHostPorts(8090);
}
 
Example #10
Source File: SeleniumContainerTest.java    From testcontainers-java with MIT License 4 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    applicationContext.addApplicationListener((ApplicationListener<WebServerInitializedEvent>) event -> {
        Testcontainers.exposeHostPorts(event.getWebServer().getPort());
    });
}