Skip to content

Commit

Permalink
Add example for how the (generally deprecated) bind mount API might b…
Browse files Browse the repository at this point in the history
…e used.
  • Loading branch information
kiview committed Mar 8, 2024
1 parent b8505ba commit 93bfdc5
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 18 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.23</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
82 changes: 64 additions & 18 deletions src/test/java/org/testcontainers/repro/ReproExampleTest.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,79 @@
package org.testcontainers.repro;

import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.containers.PostgreSQLContainer;

public class ReproExampleTest {
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

private static final Logger LOG = LoggerFactory.getLogger(ReproExampleTest.class);
public class ReproExampleTest {

/**
* Placeholder for a piece of code that demonstrates the bug. You can use this as a starting point, or replace
* entirely.
* <p>
* Ideally this would be a failing test. If it's excessively difficult to form as a test (e.g. relates to log
* output, teardown or other side effects) then it would be sufficient to explain the behaviour in the issue
* description.
*/
@Test
public void demonstration() {
public void bindMountLogFiles() throws IOException {
try (
// customize the creation of a container as required
GenericContainer<?> container = new GenericContainer<>(DockerImageName.parse("redis:6.0.5"))
.withExposedPorts(6379)
GenericContainer<?> container = new GenericContainer<>("nginx:1.19.0-alpine")
.withExposedPorts(80)
.withFileSystemBind(System.getProperty("user.dir") + "/nginx",
"/var/log/nginx", BindMode.READ_WRITE)
) {
container.start();

// ...
// request against existing path
performRequestAgainstContainer(container, "/");

// request against nonsense path
long now = System.currentTimeMillis();
performRequestAgainstContainer(container, "/foobar/" + now);

// check that nginx/error.log in the project directory contains the now timestamp
String errorLog = System.getProperty("user.dir") + "/nginx/error.log";
String errorLogContents = new String(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(errorLog)));
Assert.assertTrue(errorLogContents.contains(Long.toString(now)));

}

}

@Test
public void postgresContainerWithBootstrapFiles() throws Exception {
try (
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:13.1")
.withClasspathResourceMapping("init.sql", "/docker-entrypoint-initdb.d/init.sql", BindMode.READ_WRITE)
) {
postgres.start();

try (
Connection conn = DriverManager.getConnection(postgres.getJdbcUrl(), postgres.getUsername(), postgres.getPassword());
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM test_table")
) {
// count all rows in result set
int rowCount = 0;
while (rs.next()) {
rowCount++;
}

// assert row count is 3
Assert.assertEquals(3, rowCount);
}
}
}



private static int performRequestAgainstContainer(GenericContainer<?> container, String path) throws IOException {
URL url = new URL("http://" + container.getHost() + ":" + container.getMappedPort(80) + path);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
return conn.getResponseCode();
}
}
8 changes: 8 additions & 0 deletions src/test/resources/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE test_table (
id SERIAL PRIMARY KEY,
data VARCHAR(255) NOT NULL
);

INSERT INTO test_table (data) VALUES ('Test data 1');
INSERT INTO test_table (data) VALUES ('Test data 2');
INSERT INTO test_table (data) VALUES ('Test data 3');

0 comments on commit 93bfdc5

Please sign in to comment.