Skip to content

Commit

Permalink
getLivenessCheckPortNumbers() should return mapped port (#5734)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Wittek <kiview@users.noreply.github.com>
  • Loading branch information
aidando73 and kiview committed Sep 28, 2022
1 parent 9847d59 commit f54a29a
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 16 deletions.
Expand Up @@ -6,7 +6,6 @@

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.HashSet;
import java.util.Set;

public class Db2Container extends JdbcDatabaseContainer<Db2Container> {
Expand Down Expand Up @@ -54,9 +53,14 @@ public Db2Container(final DockerImageName dockerImageName) {
addExposedPort(DB2_PORT);
}

/**
* @return the ports on which to check if the container is ready
* @deprecated use {@link #getLivenessCheckPortNumbers()} instead
*/
@Override
@Deprecated
protected Set<Integer> getLivenessCheckPorts() {
return new HashSet<>(getMappedPort(DB2_PORT));
return super.getLivenessCheckPorts();
}

@Override
Expand Down
Expand Up @@ -21,6 +21,7 @@ public void testSimple() throws SQLException {

int resultSetInt = resultSet.getInt(1);
assertThat(resultSetInt).as("A basic SELECT query succeeds").isEqualTo(1);
assertHasCorrectExposedAndLivenessCheckPorts(db2);
}
}

Expand All @@ -37,4 +38,9 @@ public void testWithAdditionalUrlParamInJdbcUrl() {
assertThat(jdbcUrl).contains(":sslConnection=false;");
}
}

private void assertHasCorrectExposedAndLivenessCheckPorts(Db2Container db2) {
assertThat(db2.getExposedPorts()).containsExactly(Db2Container.DB2_PORT);
assertThat(db2.getLivenessCheckPortNumbers()).containsExactly(db2.getMappedPort(Db2Container.DB2_PORT));
}
}
@@ -1,6 +1,5 @@
package org.testcontainers.containers;

import com.google.common.collect.Sets;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.LicenseAcceptance;

Expand Down Expand Up @@ -64,7 +63,7 @@ public MSSQLServerContainer(final DockerImageName dockerImageName) {

@Override
public Set<Integer> getLivenessCheckPortNumbers() {
return Sets.newHashSet(MS_SQL_SERVER_PORT);
return super.getLivenessCheckPortNumbers();
}

@Override
Expand Down
Expand Up @@ -25,6 +25,7 @@ public void testSimple() throws SQLException {

int resultSetInt = resultSet.getInt(1);
assertThat(resultSetInt).as("A basic SELECT query succeeds").isEqualTo(1);
assertHasCorrectExposedAndLivenessCheckPorts(mssqlServer);
}
}

Expand Down Expand Up @@ -64,4 +65,10 @@ public void testSetupDatabase() throws SQLException {
assertThat(resultSetInt).as("A basic SELECT query succeeds").isEqualTo(3);
}
}

private void assertHasCorrectExposedAndLivenessCheckPorts(MSSQLServerContainer<?> mssqlServer) {
assertThat(mssqlServer.getExposedPorts()).containsExactly(MSSQLServerContainer.MS_SQL_SERVER_PORT);
assertThat(mssqlServer.getLivenessCheckPortNumbers())
.containsExactly(mssqlServer.getMappedPort(MSSQLServerContainer.MS_SQL_SERVER_PORT));
}
}
Expand Up @@ -3,7 +3,6 @@
import org.jetbrains.annotations.NotNull;
import org.testcontainers.utility.DockerImageName;

import java.util.HashSet;
import java.util.Set;

/**
Expand Down Expand Up @@ -56,10 +55,15 @@ public MySQLContainer(final DockerImageName dockerImageName) {
addExposedPort(MYSQL_PORT);
}

/**
* @return the ports on which to check if the container is ready
* @deprecated use {@link #getLivenessCheckPortNumbers()} instead
*/
@NotNull
@Override
@Deprecated
protected Set<Integer> getLivenessCheckPorts() {
return new HashSet<>(getMappedPort(MYSQL_PORT));
return super.getLivenessCheckPorts();
}

@Override
Expand Down
Expand Up @@ -57,6 +57,7 @@ public void testSimple() throws SQLException {
int resultSetInt = resultSet.getInt(1);

assertThat(resultSetInt).as("A basic SELECT query succeeds").isEqualTo(1);
assertHasCorrectExposedAndLivenessCheckPorts(mysql);
}
}

Expand Down Expand Up @@ -237,4 +238,9 @@ public void testWithAdditionalUrlParamInJdbcUrl() {
mysql.stop();
}
}

private void assertHasCorrectExposedAndLivenessCheckPorts(MySQLContainer<?> mysql) {
assertThat(mysql.getExposedPorts()).containsExactly(MySQLContainer.MYSQL_PORT);
assertThat(mysql.getLivenessCheckPortNumbers()).containsExactly(mysql.getMappedPort(MySQLContainer.MYSQL_PORT));
}
}
Expand Up @@ -6,7 +6,6 @@

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Set;

/**
Expand Down Expand Up @@ -42,10 +41,15 @@ public NginxContainer(final DockerImageName dockerImageName) {
setCommand("nginx", "-g", "daemon off;");
}

/**
* @return the ports on which to check if the container is ready
* @deprecated use {@link #getLivenessCheckPortNumbers()} instead
*/
@NotNull
@Override
@Deprecated
protected Set<Integer> getLivenessCheckPorts() {
return Collections.singleton(getMappedPort(80));
return super.getLivenessCheckPorts();
}

public URL getBaseUrl(String scheme, int port) throws MalformedURLException {
Expand Down
Expand Up @@ -58,6 +58,12 @@ public void testSimple() throws Exception {
.as("An HTTP GET from the Nginx server returns the index.html from the custom content directory")
.contains("Hello World!");
// }
assertHasCorrectExposedAndLivenessCheckPorts(nginx);
}

private void assertHasCorrectExposedAndLivenessCheckPorts(NginxContainer<?> nginxContainer) throws Exception {
assertThat(nginxContainer.getExposedPorts()).containsExactly(80);
assertThat(nginxContainer.getLivenessCheckPortNumbers()).containsExactly(nginxContainer.getMappedPort(80));
}

private static String responseFromNginx(URL baseUrl) throws IOException {
Expand Down
Expand Up @@ -6,7 +6,6 @@

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.Set;

/**
Expand Down Expand Up @@ -62,10 +61,15 @@ public PostgreSQLContainer(final DockerImageName dockerImageName) {
addExposedPort(POSTGRESQL_PORT);
}

/**
* @return the ports on which to check if the container is ready
* @deprecated use {@link #getLivenessCheckPortNumbers()} instead
*/
@NotNull
@Override
@Deprecated
protected Set<Integer> getLivenessCheckPorts() {
return Collections.singleton(getMappedPort(POSTGRESQL_PORT));
return super.getLivenessCheckPorts();
}

@Override
Expand Down
Expand Up @@ -26,6 +26,7 @@ public void testSimple() throws SQLException {
ResultSet resultSet = performQuery(postgres, "SELECT 1");
int resultSetInt = resultSet.getInt(1);
assertThat(resultSetInt).as("A basic SELECT query succeeds").isEqualTo(1);
assertHasCorrectExposedAndLivenessCheckPorts(postgres);
}
}

Expand Down Expand Up @@ -86,4 +87,10 @@ public void testWithAdditionalUrlParamInJdbcUrl() {
assertThat(jdbcUrl).contains("charSet=UNICODE");
}
}

private void assertHasCorrectExposedAndLivenessCheckPorts(PostgreSQLContainer<?> postgres) {
assertThat(postgres.getExposedPorts()).containsExactly(PostgreSQLContainer.POSTGRESQL_PORT);
assertThat(postgres.getLivenessCheckPortNumbers())
.containsExactly(postgres.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT));
}
}
Expand Up @@ -9,7 +9,6 @@
import java.sql.SQLException;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.HashSet;
import java.util.Set;

/**
Expand Down Expand Up @@ -56,10 +55,15 @@ public PrestoContainer(final DockerImageName dockerImageName) {
addExposedPort(PRESTO_PORT);
}

/**
* @return the ports on which to check if the container is ready
* @deprecated use {@link #getLivenessCheckPortNumbers()} instead
*/
@NotNull
@Override
@Deprecated
protected Set<Integer> getLivenessCheckPorts() {
return new HashSet<>(getMappedPort(PRESTO_PORT));
return super.getLivenessCheckPorts();
}

@Override
Expand Down
Expand Up @@ -32,6 +32,7 @@ public void testSimple() throws Exception {
assertThat(resultSet.getString("node_version"))
.as("Presto version")
.isEqualTo(PrestoContainer.DEFAULT_TAG);
assertHasCorrectExposedAndLivenessCheckPorts(prestoSql);
}
}
}
Expand Down Expand Up @@ -149,4 +150,10 @@ public void testTcJdbcUri() throws Exception {
.isEqualTo(Connection.TRANSACTION_READ_UNCOMMITTED);
}
}

private void assertHasCorrectExposedAndLivenessCheckPorts(PrestoContainer<?> prestoSql) {
assertThat(prestoSql.getExposedPorts()).containsExactly(PrestoContainer.PRESTO_PORT);
assertThat(prestoSql.getLivenessCheckPortNumbers())
.containsExactly(prestoSql.getMappedPort(PrestoContainer.PRESTO_PORT));
}
}
Expand Up @@ -6,7 +6,6 @@
import org.testcontainers.utility.DockerImageName;

import java.time.Duration;
import java.util.HashSet;
import java.util.Set;

/**
Expand Down Expand Up @@ -51,10 +50,15 @@ public TiDBContainer(final DockerImageName dockerImageName) {
);
}

/**
* @return the ports on which to check if the container is ready
* @deprecated use {@link #getLivenessCheckPortNumbers()} instead
*/
@NotNull
@Override
@Deprecated
protected Set<Integer> getLivenessCheckPorts() {
return new HashSet<>(getMappedPort(TIDB_PORT));
return super.getLivenessCheckPorts();
}

@Override
Expand Down
Expand Up @@ -21,6 +21,7 @@ public void testSimple() throws SQLException {

int resultSetInt = resultSet.getInt(1);
assertThat(resultSetInt).isEqualTo(1);
assertHasCorrectExposedAndLivenessCheckPorts(tidb);
}
}

Expand Down Expand Up @@ -51,4 +52,13 @@ public void testWithAdditionalUrlParamInJdbcUrl() {
tidb.stop();
}
}

private void assertHasCorrectExposedAndLivenessCheckPorts(TiDBContainer tidb) {
Integer tidbPort = 4000;
Integer restApiPort = 10080;

assertThat(tidb.getExposedPorts()).containsExactlyInAnyOrder(tidbPort, restApiPort);
assertThat(tidb.getLivenessCheckPortNumbers())
.containsExactlyInAnyOrder(tidb.getMappedPort(tidbPort), tidb.getMappedPort(restApiPort));
}
}
Expand Up @@ -7,7 +7,6 @@

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;

public class TrinoContainer extends JdbcDatabaseContainer<TrinoContainer> {
Expand Down Expand Up @@ -37,10 +36,15 @@ public TrinoContainer(final DockerImageName dockerImageName) {
addExposedPort(TRINO_PORT);
}

/**
* @return the ports on which to check if the container is ready
* @deprecated use {@link #getLivenessCheckPortNumbers()} instead
*/
@NotNull
@Override
@Deprecated
protected Set<Integer> getLivenessCheckPorts() {
return new HashSet<>(getMappedPort(TRINO_PORT));
return super.getLivenessCheckPorts();
}

@Override
Expand Down
Expand Up @@ -24,6 +24,7 @@ public void testSimple() throws Exception {
assertThat(resultSet.getString("node_version"))
.as("Trino version")
.isEqualTo(TrinoContainer.DEFAULT_TAG);
assertContainerHasCorrectExposedAndLivenessCheckPorts(trino);
}
}
}
Expand Down Expand Up @@ -61,4 +62,9 @@ public void testInitScript() throws Exception {
}
}
}

private void assertContainerHasCorrectExposedAndLivenessCheckPorts(TrinoContainer trino) {
assertThat(trino.getExposedPorts()).containsExactly(8080);
assertThat(trino.getLivenessCheckPortNumbers()).containsExactly(trino.getMappedPort(8080));
}
}

0 comments on commit f54a29a

Please sign in to comment.