Skip to content

Commit d7226ad

Browse files
committed
Extend PostgresDumpTest: connect via an IP address of a container
1 parent 5f14e21 commit d7226ad

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--
2+
-- PostgreSQL database dump
3+
--
4+
5+
-- Dumped from database version 17.0 (Debian 17.0-1.pgdg120+1)
6+
-- Dumped by pg_dump version 17.0 (Debian 17.0-1.pgdg120+1)
7+
8+
SET statement_timeout = 0;
9+
SET lock_timeout = 0;
10+
SET idle_in_transaction_session_timeout = 0;
11+
SET transaction_timeout = 0;
12+
SET client_encoding = 'UTF8';
13+
SET standard_conforming_strings = on;
14+
SELECT pg_catalog.set_config('search_path', '', false);
15+
SET check_function_bodies = false;
16+
SET xmloption = content;
17+
SET client_min_messages = warning;
18+
SET row_security = off;
19+
20+
--
21+
-- PostgreSQL database dump complete
22+
--

src/test/java/de/cronn/postgres/snapshot/util/PostgresDumpTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@
88
import java.nio.file.Files;
99
import java.nio.file.Path;
1010
import java.util.List;
11+
import java.util.regex.Matcher;
12+
import java.util.regex.Pattern;
1113

1214
import org.junit.jupiter.api.AfterAll;
1315
import org.junit.jupiter.api.BeforeAll;
1416
import org.junit.jupiter.api.Nested;
1517
import org.junit.jupiter.api.Test;
1618
import org.junit.jupiter.api.io.TempDir;
1719
import org.testcontainers.containers.ContainerLaunchException;
20+
import org.testcontainers.containers.GenericContainer;
21+
import org.testcontainers.containers.Network;
1822
import org.testcontainers.containers.PostgreSQLContainer;
23+
import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy;
1924
import org.testcontainers.utility.DockerImageName;
2025

2126
import de.cronn.assertions.validationfile.normalization.SimpleRegexReplacement;
@@ -173,4 +178,34 @@ void testOtherPostgresVersion() {
173178
compareActualWithValidationFile(schema);
174179
}
175180
}
181+
182+
@Test
183+
void testConnectViaDockerContainerIpAddress() {
184+
String networkAlias = "postgres-db";
185+
try (Network network = Network.newNetwork();
186+
PostgreSQLContainer<?> postgresInNetworkContainer = createPostgresContainer(DockerImageName.parse("postgres:17.0"))
187+
.withNetwork(network)
188+
.withNetworkAliases(networkAlias)) {
189+
postgresInNetworkContainer.start();
190+
191+
String ipAddress = resolveHostname(network, networkAlias);
192+
193+
String jdbcUrl = "jdbc:postgresql://%s/test-db".formatted(ipAddress);
194+
String schema = PostgresDump.dumpToString(jdbcUrl, USERNAME, PASSWORD);
195+
compareActualWithValidationFile(schema);
196+
}
197+
}
198+
199+
private String resolveHostname(Network network, String hostname) {
200+
try (GenericContainer<?> dnsContainer = new GenericContainer<>("alpine:3.20.3")
201+
.withNetwork(network)
202+
.withCommand("getent hosts " + hostname)
203+
.withStartupCheckStrategy(new OneShotStartupCheckStrategy())) {
204+
dnsContainer.start();
205+
String logs = dnsContainer.getLogs();
206+
Matcher matcher = Pattern.compile("(\\d+\\.\\d+\\.\\d+\\.\\d+)\\s+" + hostname + "\\s+" + hostname + "\\n").matcher(logs);
207+
assertThat(matcher.matches()).describedAs("Failed to parse '%s'", logs).isTrue();
208+
return matcher.group(1);
209+
}
210+
}
176211
}

0 commit comments

Comments
 (0)