Java源码示例:org.testcontainers.Testcontainers

示例1
@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);
    // }
}
 
示例2
@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));           
}
 
示例3
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;
}
 
示例4
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;
    }
 
示例5
@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();

}
 
示例6
@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));
}
 
示例7
@BeforeClass
public static void exposeConfluenceServerPortOnHost() {
    Testcontainers.exposeHostPorts(8090);
}
 
示例8
@BeforeClass
public static void exposeConfluenceServerPortOnHost() {
    Testcontainers.exposeHostPorts(8090);
}
 
示例9
@BeforeClass
public static void exposeConfluenceServerPortOnHost() {
    Testcontainers.exposeHostPorts(8090);
}
 
示例10
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    applicationContext.addApplicationListener((ApplicationListener<WebServerInitializedEvent>) event -> {
        Testcontainers.exposeHostPorts(event.getWebServer().getPort());
    });
}