Skip to content

Commit 8791749

Browse files
realjeniuslanwen
andauthored
Check for a label alongside of the server version (#25)
* DRC-30 - Check for a label instead of server version * Follow the label semantics * Fix label * Better wording * label check to format message --------- Co-authored-by: Kirill Merkushev <1964214+lanwen@users.noreply.github.com>
1 parent 4ffd43f commit 8791749

File tree

1 file changed

+64
-36
lines changed

1 file changed

+64
-36
lines changed
Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
11
package cloud.testcontainers.example;
22

3-
import java.sql.Connection;
4-
import java.sql.DriverManager;
5-
import java.sql.PreparedStatement;
6-
import java.sql.ResultSet;
7-
import java.sql.SQLException;
8-
93
import com.github.dockerjava.api.DockerClient;
104
import com.github.dockerjava.api.model.Info;
115
import org.junit.jupiter.api.Test;
126
import org.junit.jupiter.api.extension.ExtendWith;
137
import org.testcontainers.DockerClientFactory;
148
import org.testcontainers.containers.PostgreSQLContainer;
159
import org.testcontainers.images.builder.Transferable;
10+
import org.testcontainers.shaded.com.google.common.collect.Streams;
11+
12+
import java.sql.Connection;
13+
import java.sql.DriverManager;
14+
import java.sql.PreparedStatement;
15+
import java.sql.ResultSet;
16+
import java.sql.SQLException;
17+
import java.util.Arrays;
18+
import java.util.List;
19+
import java.util.stream.Collectors;
20+
import java.util.stream.Stream;
1621

1722
import static org.assertj.core.api.Assertions.assertThat;
1823

1924
@ExtendWith(TccTestWatcher.class)
2025
public class TestcontainersCloudFirstTest {
2126

27+
public static final String DOCKER_CLOUD_VERSION_LABEL = "cloud.docker.run.version";
28+
29+
public static final String TESTCONTAINERS_DESKTOP_APP_NAME = "Testcontainers Desktop";
30+
31+
public static final String TESTCONTAINERS_CLOUD_VERSION_NAME = "testcontainerscloud";
32+
2233
@Test
2334
public void createPostgreSQLContainer() throws SQLException {
2435
try (PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:14-alpine")
2536
.withCopyToContainer(Transferable.of(initsql), "/docker-entrypoint-initdb.d/init.sql")) {
2637
postgreSQLContainer.start();
27-
Connection connection = DriverManager.getConnection(postgreSQLContainer.getJdbcUrl(), postgreSQLContainer.getUsername(), postgreSQLContainer.getPassword());
28-
PreparedStatement preparedStatement = connection.prepareStatement("SELECT COUNT(*) FROM guides");
29-
preparedStatement.execute();
30-
ResultSet resultSet = preparedStatement.getResultSet();
31-
resultSet.next();
32-
assertThat(resultSet.getInt(1)).isEqualTo(6);
33-
}
38+
Connection connection = DriverManager.getConnection(postgreSQLContainer.getJdbcUrl(), postgreSQLContainer.getUsername(), postgreSQLContainer.getPassword());
39+
PreparedStatement preparedStatement = connection.prepareStatement("SELECT COUNT(*) FROM guides");
40+
preparedStatement.execute();
41+
ResultSet resultSet = preparedStatement.getResultSet();
42+
resultSet.next();
43+
assertThat(resultSet.getInt(1)).isEqualTo(6);
44+
}
3445
}
3546

3647
@Test
@@ -39,38 +50,55 @@ public void testcontainersCloudDockerEngine() {
3950
Info dockerInfo = client.infoCmd().exec();
4051

4152
String serverVersion = dockerInfo.getServerVersion();
42-
assertThat(serverVersion)
53+
String[] labels = dockerInfo.getLabels();
54+
55+
List<String> info = Streams.concat(
56+
Stream.of(String.format("server.version=%s", serverVersion)),
57+
Arrays.stream(labels == null ? new String[]{} : labels)
58+
).collect(Collectors.toList());
59+
60+
assertThat(info)
4361
.as("Docker Client is configured via the Testcontainers desktop app")
44-
.satisfiesAnyOf(
45-
dockerString -> assertThat(dockerString).contains("Testcontainers Desktop"),
46-
dockerString -> assertThat(dockerString).contains("testcontainerscloud")
47-
);
62+
.anySatisfy(it -> assertThat(it).containsAnyOf(
63+
TESTCONTAINERS_DESKTOP_APP_NAME,
64+
TESTCONTAINERS_CLOUD_VERSION_NAME,
65+
DOCKER_CLOUD_VERSION_LABEL
66+
));
67+
68+
logRuntimeDetails(serverVersion != null ? serverVersion : "", dockerInfo);
69+
}
4870

71+
private static void logRuntimeDetails(String serverVersion, Info dockerInfo) {
4972
String runtimeName = "Testcontainers Cloud";
50-
if (!serverVersion.contains("testcontainerscloud")) {
73+
boolean hasCloudLabel = Stream.of(
74+
dockerInfo.getLabels() != null
75+
? dockerInfo.getLabels()
76+
: new String[]{}
77+
).anyMatch(label -> label.contains(DOCKER_CLOUD_VERSION_LABEL));
78+
if (!serverVersion.contains(TESTCONTAINERS_CLOUD_VERSION_NAME) && !hasCloudLabel) {
5179
runtimeName = dockerInfo.getOperatingSystem();
5280
}
53-
if (serverVersion.contains("Testcontainers Desktop")) {
54-
runtimeName += " via Testcontainers Desktop app";
81+
if (serverVersion.contains(TESTCONTAINERS_DESKTOP_APP_NAME)) {
82+
runtimeName += " via Testcontainers Desktop";
5583
}
5684
System.out.println(PrettyStrings.getLogo(runtimeName));
5785
}
5886

5987
private static final String initsql =
6088
"create table guides\n" +
61-
"(\n" +
62-
" id bigserial not null,\n" +
63-
" title varchar(1023) not null,\n" +
64-
" url varchar(1023) not null,\n" +
65-
" primary key (id)\n" +
66-
");\n" +
67-
"\n" +
68-
"insert into guides(title, url)\n" +
69-
"values ('Getting started with Testcontainers', 'https://testcontainers.com/getting-started/'),\n" +
70-
" ('Getting started with Testcontainers for Java', 'https://testcontainers.com/guides/getting-started-with-testcontainers-for-java/'),\n" +
71-
" ('Getting started with Testcontainers for .NET', 'https://testcontainers.com/guides/getting-started-with-testcontainers-for-dotnet/'),\n" +
72-
" ('Getting started with Testcontainers for Node.js', 'https://testcontainers.com/guides/getting-started-with-testcontainers-for-nodejs/'),\n" +
73-
" ('Getting started with Testcontainers for Go', 'https://testcontainers.com/guides/getting-started-with-testcontainers-for-go/'),\n" +
74-
" ('Testcontainers container lifecycle management using JUnit 5', 'https://testcontainers.com/guides/testcontainers-container-lifecycle/')\n" +
75-
";";
89+
"(\n" +
90+
" id bigserial not null,\n" +
91+
" title varchar(1023) not null,\n" +
92+
" url varchar(1023) not null,\n" +
93+
" primary key (id)\n" +
94+
");\n" +
95+
"\n" +
96+
"insert into guides(title, url)\n" +
97+
"values ('Getting started with Testcontainers', 'https://testcontainers.com/getting-started/'),\n" +
98+
" ('Getting started with Testcontainers for Java', 'https://testcontainers.com/guides/getting-started-with-testcontainers-for-java/'),\n" +
99+
" ('Getting started with Testcontainers for .NET', 'https://testcontainers.com/guides/getting-started-with-testcontainers-for-dotnet/'),\n" +
100+
" ('Getting started with Testcontainers for Node.js', 'https://testcontainers.com/guides/getting-started-with-testcontainers-for-nodejs/'),\n" +
101+
" ('Getting started with Testcontainers for Go', 'https://testcontainers.com/guides/getting-started-with-testcontainers-for-go/'),\n" +
102+
" ('Testcontainers container lifecycle management using JUnit 5', 'https://testcontainers.com/guides/testcontainers-container-lifecycle/')\n" +
103+
";";
76104
}

0 commit comments

Comments
 (0)