Skip to content

Commit d0f360e

Browse files
committed
reuse existing tests wherever possible
1 parent 048a594 commit d0f360e

File tree

8 files changed

+84
-152
lines changed

8 files changed

+84
-152
lines changed

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

Lines changed: 0 additions & 122 deletions
This file was deleted.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ public void setUp() {
3737
@Test
3838
public void testProfileOption() {
3939
try (
40+
// composeContainerWithLocalCompose {
4041
ComposeContainer compose = new ComposeContainer(COMPOSE_FILE)
41-
.withOptions("--profile=cache")
4242
.withLocalCompose(true)
43+
// }
44+
.withOptions("--profile=cache")
4345
) {
4446
compose.start();
4547
assertThat(compose.listChildContainers()).hasSize(1);

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,30 @@
1818
public class ComposeContainerTest extends BaseComposeTest {
1919

2020
@Rule
21+
// composeContainerConstructor {
2122
public ComposeContainer environment = new ComposeContainer(
2223
new File("src/test/resources/composev2/compose-test.yml")
2324
)
2425
.withExposedService("redis-1", REDIS_PORT)
2526
.withExposedService("db-1", 3306);
2627

28+
// }
29+
2730
@Override
2831
protected ComposeContainer getEnvironment() {
2932
return environment;
3033
}
3134

3235
@Test
33-
public void testGetServicePort() {
36+
public void testGetServiceHostAndPort() {
37+
// getServiceHostAndPort {
38+
String serviceHost = environment.getServiceHost("redis-1", REDIS_PORT);
3439
int serviceWithInstancePort = environment.getServicePort("redis-1", REDIS_PORT);
40+
// }
41+
42+
assertThat(serviceHost).as("Service host is not blank").isNotBlank();
3543
assertThat(serviceWithInstancePort).as("Port is set for service with instance number").isNotNull();
44+
3645
int serviceWithoutInstancePort = environment.getServicePort("redis", REDIS_PORT);
3746
assertThat(serviceWithoutInstancePort).as("Port is set for service with instance number").isNotNull();
3847
assertThat(serviceWithoutInstancePort).as("Service ports are the same").isEqualTo(serviceWithInstancePort);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ public void testWithFileCopyInclusionUsingFilePath() throws IOException {
4949
@Test
5050
public void testWithFileCopyInclusionUsingDirectoryPath() throws IOException {
5151
try (
52+
// composeContainerWithCopyFiles {
5253
ComposeContainer environment = new ComposeContainer(
5354
new File("src/test/resources/compose-file-copy-inclusions/compose-test-only.yml")
5455
)
5556
.withExposedService("app", 8080)
5657
.withCopyFilesInContainer("Dockerfile", "EnvVariableRestEndpoint.java", "test")
58+
// }
5759
) {
5860
environment.start();
5961

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.testcontainers.junit;
2+
3+
import org.junit.Test;
4+
import org.testcontainers.containers.ComposeContainer;
5+
import org.testcontainers.containers.wait.strategy.Wait;
6+
7+
import java.io.File;
8+
import java.time.Duration;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
12+
public class ComposeContainerWithWaitStrategies {
13+
14+
private static final int REDIS_PORT = 6379;
15+
16+
@Test
17+
public void testComposeContainerConstructor() {
18+
try (
19+
// composeContainerWithCombinedWaitStrategies {
20+
ComposeContainer compose = new ComposeContainer(new File("src/test/resources/composev2/compose-test.yml"))
21+
.withExposedService("redis-1", REDIS_PORT, Wait.forSuccessfulCommand("redis-cli ping"))
22+
.withExposedService("db-1", 3306, Wait.forLogMessage(".*ready for connections.*\\n", 1))
23+
// }
24+
) {
25+
compose.start();
26+
containsStartedServices(compose, "redis-1", "db-1");
27+
}
28+
}
29+
30+
@Test
31+
public void testComposeContainerWaitForPortWithTimeout() {
32+
try (
33+
// composeContainerWaitForPortWithTimeout {
34+
ComposeContainer compose = new ComposeContainer(new File("src/test/resources/composev2/compose-test.yml"))
35+
.withExposedService(
36+
"redis-1",
37+
REDIS_PORT,
38+
Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(30))
39+
)
40+
// }
41+
) {
42+
compose.start();
43+
containsStartedServices(compose, "redis-1");
44+
}
45+
}
46+
47+
private void containsStartedServices(ComposeContainer compose, String... expectedServices) {
48+
for (String serviceName : expectedServices) {
49+
assertThat(compose.getContainerByServiceName(serviceName))
50+
.as("Container should be found by service name %s", serviceName)
51+
.isPresent();
52+
}
53+
}
54+
}

core/src/test/resources/v2-compose-test-doc.env

Lines changed: 0 additions & 3 deletions
This file was deleted.

core/src/test/resources/v2-compose-test-doc.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

docs/modules/docker_compose.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,24 @@ The [`ComposeContainer`](http://static.javadoc.io/org.testcontainers/testcontain
1313
A single class `ComposeContainer`, defined based on a `docker-compose.yml` file, should be sufficient to launch any number of services required by our tests:
1414

1515
<!--codeinclude-->
16-
[Create a ComposeContainer](../../core/src/test/java/org/testcontainers/containers/ComposeContainerDocTest.java) inside_block:composeContainerConstructor
16+
[Create a ComposeContainer](../../core/src/test/java/org/testcontainers/junit/ComposeContainerTest.java) inside_block:composeContainerConstructor
1717
<!--/codeinclude-->
1818

1919
!!! note
2020
Make sure the service names use a `-` rather than `_` as separator.
2121

22-
In this example, Docker Compose file should have content such as:
22+
2323
```yaml
24-
version: '3.8'
24+
!include ../../core/src/test/resources/composev2/compose-test.yml
25+
```
2526

27+
In this example, Docker Compose file should have content such as:
28+
```yaml
2629
services:
2730
redis:
28-
image: redis:7
29-
postgres:
30-
image: postgres:17-alpine
31-
environment:
32-
POSTGRES_USER: postgres
31+
image: redis
32+
db:
33+
image: mysql:8.0.36
3334
```
3435
3536
Note that it is not necessary to define ports to be exposed in the YAML file, as this would inhibit the reuse/inclusion of the file in other contexts.
@@ -55,7 +56,7 @@ ComposeContainer provides methods for discovering how your tests can interact wi
5556

5657
Let's use this API to create the URL that will enable our tests to access the Redis service:
5758
<!--codeinclude-->
58-
[Access a Service's host and port](../../core/src/test/java/org/testcontainers/containers/ComposeContainerDocTest.java) inside_block:getServiceHostAndPort
59+
[Access a Service's host and port](../../core/src/test/java/org/testcontainers/junit/ComposeContainerTest.java) inside_block:getServiceHostAndPort
5960
<!--/codeinclude-->
6061

6162
## Wait Strategies and Startup Timeouts
@@ -67,15 +68,15 @@ There are overloaded `withExposedService` methods that take a [`WaitStrategy`](h
6768

6869
For instance, we can wait for exposed port and set a custom timeout:
6970
<!--codeinclude-->
70-
[Wait for the exposed port and use a custom timeout](../../core/src/test/java/org/testcontainers/containers/ComposeContainerDocTest.java) inside_block:composeContainerWaitForPortWithTimeout
71+
[Wait for the exposed port and use a custom timeout](../../core/src/test/java/org/testcontainers/junit/ComposeContainerWithWaitStrategies.java) inside_block:composeContainerWaitForPortWithTimeout
7172
<!--/codeinclude-->
7273

7374
Needless to say, we can define different strategies for each service in our Docker Compose setup.
7475

75-
For example, our Redis container can wait for a successful redis-cli command, while Postgres waits for a specific log message:
76+
For example, our Redis container can wait for a successful redis-cli command, while our db service waits for a specific log message:
7677

7778
<!--codeinclude-->
78-
[Wait for a custom command and a log message](../../core/src/test/java/org/testcontainers/containers/ComposeContainerDocTest.java) inside_block:composeContainerWithCombinedWaitStrategies
79+
[Wait for a custom command and a log message](../../core/src/test/java/org/testcontainers/junit/ComposeContainerWithWaitStrategies.java) inside_block:composeContainerWithCombinedWaitStrategies
7980
<!--/codeinclude-->
8081

8182
!!! More on Wait Strategies
@@ -89,15 +90,15 @@ We can override Testcontainers' default behaviour and make it use a `docker-comp
8990
This will generally yield an experience that is closer to running _docker compose_ locally, with the caveat that Docker Compose needs to be present on dev and CI machines.
9091

9192
<!--codeinclude-->
92-
[Use ComposeContainer in 'Local Compose' mode](../../core/src/test/java/org/testcontainers/containers/ComposeContainerDocTest.java) inside_block:composeContainerWithLocalCompose
93+
[Use ComposeContainer in 'Local Compose' mode](../../core/src/test/java/org/testcontainers/containers/ComposeProfilesOptionTest.java) inside_block:composeContainerWithLocalCompose
9394
<!--/codeinclude-->
9495

9596
## Build Working Directory
9697

9798
We can select what files should be copied only via `withCopyFilesInContainer`:
9899

99100
<!--codeinclude-->
100-
[Use ComposeContainer in 'Local Compose' mode](../../core/src/test/java/org/testcontainers/containers/ComposeContainerDocTest.java) inside_block:composeContainerWithCopyFiles
101+
[Use ComposeContainer in 'Local Compose' mode](../../core/src/test/java/org/testcontainers/junit/ComposeContainerWithCopyFilesTest.java) inside_block:composeContainerWithCopyFiles
101102
<!--/codeinclude-->
102103

103104
In this example, only docker compose and env files are copied over into the container that will run the Docker Compose file.

0 commit comments

Comments
 (0)