Skip to content

Commit 462d9ea

Browse files
authored
Merge branch 'main' into test-java-versions
2 parents 5b2659a + 49b13d8 commit 462d9ea

File tree

27 files changed

+526
-231
lines changed

27 files changed

+526
-231
lines changed

.github/settings.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ repository:
1313
homepage: https://testcontainers.org
1414

1515
# A comma-separated list of topics to set on the repository
16-
topics: java,testing,docker,docker-compose,jvm,test-automation,junit,hacktoberfest
16+
topics: java,testing,docker,docker-compose,jvm,test-automation,junit,hacktoberfest,integration-testing
1717

1818
# Either `true` to make the repository private, or `false` to make it public.
1919
private: false

core/src/main/java/org/testcontainers/containers/ParsedDockerComposeFile.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import org.yaml.snakeyaml.LoaderOptions;
1212
import org.yaml.snakeyaml.Yaml;
1313
import org.yaml.snakeyaml.constructor.SafeConstructor;
14+
import org.yaml.snakeyaml.nodes.Node;
15+
import org.yaml.snakeyaml.nodes.Tag;
1416
import org.yaml.snakeyaml.representer.Representer;
1517
import org.yaml.snakeyaml.resolver.Resolver;
1618

@@ -44,13 +46,17 @@ class ParsedDockerComposeFile {
4446
LoaderOptions options = new LoaderOptions();
4547
options.setMaxAliasesForCollections(1_000);
4648
DumperOptions dumperOptions = new DumperOptions();
47-
Yaml yaml = new Yaml(
48-
new SafeConstructor(options),
49-
new Representer(dumperOptions),
50-
dumperOptions,
51-
options,
52-
new Resolver()
53-
);
49+
50+
SafeConstructor constructor = new SafeConstructor(options) {
51+
@Override
52+
protected Object constructObject(Node node) {
53+
if (node.getTag().equals(new Tag("!reset"))) {
54+
return null;
55+
}
56+
return super.constructObject(node);
57+
}
58+
};
59+
Yaml yaml = new Yaml(constructor, new Representer(dumperOptions), dumperOptions, options, new Resolver());
5460
try (FileInputStream fileInputStream = FileUtils.openInputStream(composeFile)) {
5561
composeFileContent = yaml.load(fileInputStream);
5662
} catch (Exception e) {

core/src/test/java/org/testcontainers/containers/DockerComposeProfilesOptionTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public void setUp() {
3131
.assumeThat(CommandLine.executableExists(DockerComposeContainer.COMPOSE_EXECUTABLE))
3232
.as("docker-compose executable exists")
3333
.isTrue();
34+
Assumptions
35+
.assumeThat(CommandLine.runShellCommand("docker-compose", "--version"))
36+
.doesNotStartWith("Docker Compose version v2");
3437
}
3538
}
3639

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.testcontainers.junit;
2+
3+
import com.github.dockerjava.api.command.InspectContainerResponse;
4+
import org.junit.Test;
5+
import org.testcontainers.containers.ComposeContainer;
6+
import org.testcontainers.containers.ContainerState;
7+
8+
import java.io.File;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
12+
public class ComposeContainerOverrideTest {
13+
14+
private static final File BASE = new File("src/test/resources/compose-override/compose.yml");
15+
16+
private static final File OVERRIDE = new File("src/test/resources/compose-override/compose-override.yml");
17+
18+
@Test
19+
public void readEnvironment() {
20+
try (ComposeContainer compose = new ComposeContainer(BASE).withExposedService("redis", 6379)) {
21+
compose.start();
22+
InspectContainerResponse container = compose
23+
.getContainerByServiceName("redis-1")
24+
.map(ContainerState::getContainerInfo)
25+
.get();
26+
assertThat(container.getConfig().getEnv()).contains("foo=bar");
27+
}
28+
}
29+
30+
@Test
31+
public void resetEnvironment() {
32+
try (ComposeContainer compose = new ComposeContainer(BASE, OVERRIDE).withExposedService("redis", 6379)) {
33+
compose.start();
34+
InspectContainerResponse container = compose
35+
.getContainerByServiceName("redis-1")
36+
.map(ContainerState::getContainerInfo)
37+
.get();
38+
assertThat(container.getConfig().getEnv()).doesNotContain("foo=bar");
39+
}
40+
}
41+
}

core/src/test/java/org/testcontainers/junit/DockerComposeContainerWithOptionsTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ public void setUp() {
8383
.assumeThat(CommandLine.executableExists(DockerComposeContainer.COMPOSE_EXECUTABLE))
8484
.as("docker-compose executable exists")
8585
.isTrue();
86+
Assumptions
87+
.assumeThat(CommandLine.runShellCommand("docker-compose", "--version"))
88+
.doesNotStartWith("Docker Compose version v2");
8689
}
8790
}
8891

core/src/test/java/org/testcontainers/junit/FixedHostPortContainerTest.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.testcontainers.junit;
22

3-
import com.google.common.util.concurrent.Uninterruptibles;
3+
import org.awaitility.Awaitility;
44
import org.junit.Test;
5-
import org.rnorth.ducttape.unreliables.Unreliables;
65
import org.testcontainers.TestImages;
76
import org.testcontainers.containers.FixedHostPortGenericContainer;
87
import org.testcontainers.containers.GenericContainer;
@@ -11,7 +10,7 @@
1110
import java.io.IOException;
1211
import java.io.InputStreamReader;
1312
import java.net.Socket;
14-
import java.util.concurrent.TimeUnit;
13+
import java.time.Duration;
1514

1615
import static org.assertj.core.api.Assertions.assertThat;
1716

@@ -71,7 +70,7 @@ public void testFixedHostPortMapping() throws IOException {
7170
.as("Port mapping does not seem to match given fixed port")
7271
.isEqualTo(unusedHostPort);
7372

74-
final String content = this.readResponse(echoServer, unusedHostPort);
73+
final String content = readResponse(echoServer, unusedHostPort);
7574
assertThat(content).as("Returned echo from fixed port does not match expected").isEqualTo(TEST_RESPONSE);
7675
}
7776
}
@@ -86,15 +85,11 @@ public void testFixedHostPortMapping() throws IOException {
8685
*/
8786
private String readResponse(GenericContainer container, Integer port) throws IOException {
8887
try (
89-
final BufferedReader reader = Unreliables.retryUntilSuccess(
90-
10,
91-
TimeUnit.SECONDS,
92-
() -> {
93-
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
94-
final Socket socket = new Socket(container.getHost(), port);
95-
return new BufferedReader(new InputStreamReader(socket.getInputStream()));
96-
}
97-
)
88+
Socket socket = Awaitility
89+
.await()
90+
.pollDelay(Duration.ofSeconds(1))
91+
.until(() -> new Socket(container.getHost(), port), Socket::isConnected);
92+
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))
9893
) {
9994
return reader.readLine();
10095
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
redis:
3+
image: redis:6-alpine
4+
ports:
5+
- 6379
6+
environment:
7+
foo: !reset null
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
services:
2+
redis:
3+
image: redis:6-alpine
4+
ports:
5+
- 6379
6+
environment:
7+
foo: bar

docs/modules/databases/cassandra.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ This example connects to the Cassandra cluster:
66

77
1. Define a container:
88
<!--codeinclude-->
9-
[Container definition](../../../modules/cassandra/src/test/java/org/testcontainers/cassandra/CassandraDriver4Test.java) inside_block:container-definition
9+
[Container definition](../../../modules/cassandra/src/test/java/org/testcontainers/cassandra/CassandraContainerTest.java) inside_block:container-definition
1010
<!--/codeinclude-->
1111

1212
2. Build a `CqlSession`:
1313
<!--codeinclude-->
14-
[Building CqlSession](../../../modules/cassandra/src/test/java/org/testcontainers/cassandra/CassandraDriver4Test.java) inside_block:cql-session
14+
[Building CqlSession](../../../modules/cassandra/src/test/java/org/testcontainers/cassandra/CassandraContainerTest.java) inside_block:cql-session
1515
<!--/codeinclude-->
1616

1717
3. Define a container with custom `cassandra.yaml` located in a directory `cassandra-auth-required-configuration`:

docs/modules/databases/mongodb.md

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# MongoDB Module
22

3-
!!! note
4-
This module is INCUBATING. While it is ready for use and operational in the current version of Testcontainers, it is possible that it may receive breaking changes in the future. See [our contributing guidelines](/contributing/#incubating-modules) for more information on our incubating modules policy.
3+
The MongoDB module provides two Testcontainers for MongoDB unit testing:
4+
5+
* [MongoDBContainer](#mongodbcontainer) - the core MongoDB database
6+
* [MongoDBAtlasLocalContainer](#mongodbatlaslocalcontainer) - the core MongoDB database combined with MongoDB Atlas Search + Atlas Vector Search
7+
8+
## MongoDBContainer
59

6-
## Usage example
10+
### Usage example
711

812
The following example shows how to create a MongoDBContainer:
913

@@ -36,6 +40,37 @@ For instance, to initialize a single node replica set on fixed ports via Docker,
3640
As we can see, there is a lot of operations to execute and we even haven't touched a non-fixed port approach.
3741
That's where the MongoDBContainer might come in handy.
3842

43+
## MongoDBAtlasLocalContainer
44+
45+
### Usage example
46+
47+
The following example shows how to create a MongoDBAtlasLocalContainer:
48+
49+
<!--codeinclude-->
50+
[Creating a MongoDB Atlas Local Container](../../../modules/mongodb/src/test/java/org/testcontainers/mongodb/MongoDBAtlasLocalContainerTest.java) inside_block:creatingAtlasLocalContainer
51+
<!--/codeinclude-->
52+
53+
And how to start it:
54+
55+
<!--codeinclude-->
56+
[Start the Container](../../../modules/mongodb/src/test/java/org/testcontainers/mongodb/MongoDBAtlasLocalContainerTest.java) inside_block:startingAtlasLocalContainer
57+
<!--/codeinclude-->
58+
59+
The connection string provided by the MongoDBAtlasLocalContainer's getConnectionString() method includes the dynamically allocated port:
60+
61+
<!--codeinclude-->
62+
[Get the Connection String](../../../modules/mongodb/src/test/java/org/testcontainers/mongodb/MongoDBAtlasLocalContainerTest.java) inside_block:getConnectionStringAtlasLocalContainer
63+
<!--/codeinclude-->
64+
65+
e.g. `mongodb://localhost:12345/?directConnection=true`
66+
67+
### References
68+
MongoDB Atlas Local combines the MongoDB database engine with MongoT, a sidecar process for advanced searching capabilities built by MongoDB and powered by [Apache Lucene](https://lucene.apache.org/).
69+
70+
The container (mongodb/mongodb-atlas-local) documentation can be found [here](https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-deploy-docker/).
71+
72+
General information about Atlas Search can be found [here](https://www.mongodb.com/docs/atlas/atlas-search/).
73+
3974
## Adding this module to your project dependencies
4075

4176
Add the following dependency to your `pom.xml`/`build.gradle` file:
@@ -55,7 +90,7 @@ Add the following dependency to your `pom.xml`/`build.gradle` file:
5590
```
5691

5792
!!! hint
58-
Adding this Testcontainers library JAR will not automatically add a database driver JAR to your project. You should ensure that your project also has a suitable database driver as a dependency
93+
Adding this Testcontainers library JAR will not automatically add a database driver JAR to your project. You should ensure that your project also has a suitable database driver as a dependency
5994

6095
#### Copyright
6196
Copyright (c) 2019 Konstantin Silaev <[email protected]>

0 commit comments

Comments
 (0)