Skip to content

Commit 3ff0bb2

Browse files
authored
Fix tests on Windows (#5096)
* Fix tests on Windows * Fix race condition in `StartupCheckStrategyTest`
1 parent 5e016c9 commit 3ff0bb2

File tree

5 files changed

+82
-44
lines changed

5 files changed

+82
-44
lines changed

azure-pipelines.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
jobs:
22

3-
- job: azure_pipeline_tests
4-
timeoutInMinutes: 120
3+
- job: core_tests
4+
timeoutInMinutes: 60
55
steps:
66

77
# Run all core tests when running the Windows CI tests
@@ -19,6 +19,9 @@ jobs:
1919
publishJUnitResults: true
2020
testResultsFiles: '**/TEST-*.xml'
2121

22+
- job: other_tests
23+
timeoutInMinutes: 120
24+
steps:
2225
# Run all non-core tests when running the Windows CI tests
2326
- task: Gradle@2
2427
condition: eq(variables['Agent.OS'], 'Windows_NT')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.testcontainers;
2+
3+
import com.github.dockerjava.api.command.InspectContainerResponse;
4+
import lombok.Getter;
5+
import lombok.NonNull;
6+
import lombok.SneakyThrows;
7+
import org.testcontainers.containers.GenericContainer;
8+
import org.testcontainers.containers.output.FrameConsumerResultCallback;
9+
import org.testcontainers.containers.output.OutputFrame;
10+
import org.testcontainers.containers.output.WaitingConsumer;
11+
12+
import java.util.concurrent.Future;
13+
import java.util.concurrent.TimeUnit;
14+
import java.util.concurrent.atomic.AtomicInteger;
15+
import java.util.regex.Matcher;
16+
import java.util.regex.Pattern;
17+
18+
public class DockerRegistryContainer extends GenericContainer<DockerRegistryContainer> {
19+
20+
@Getter
21+
String endpoint;
22+
23+
public DockerRegistryContainer() {
24+
super(TestImages.DOCKER_REGISTRY_IMAGE);
25+
}
26+
27+
public DockerRegistryContainer(@NonNull Future<String> image) {
28+
super(image);
29+
}
30+
31+
@Override
32+
protected void configure() {
33+
super.configure();
34+
withEnv("REGISTRY_HTTP_ADDR", "127.0.0.1:0");
35+
withCreateContainerCmdModifier(cmd -> {
36+
cmd.getHostConfig().withNetworkMode("host");
37+
});
38+
}
39+
40+
@Override
41+
@SneakyThrows
42+
protected void containerIsStarting(InspectContainerResponse containerInfo) {
43+
AtomicInteger port = new AtomicInteger(-1);
44+
try (FrameConsumerResultCallback resultCallback = new FrameConsumerResultCallback()) {
45+
WaitingConsumer waitingConsumer = new WaitingConsumer();
46+
resultCallback.addConsumer(OutputFrame.OutputType.STDERR, waitingConsumer);
47+
48+
dockerClient.logContainerCmd(containerInfo.getId())
49+
.withStdErr(true)
50+
.withFollowStream(true)
51+
.exec(resultCallback);
52+
53+
Pattern pattern = Pattern.compile(".*listening on .*:(\\d+).*", Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
54+
waitingConsumer.waitUntil(it -> {
55+
String s = it.getUtf8String();
56+
Matcher matcher = pattern.matcher(s);
57+
if (matcher.matches()) {
58+
port.set(Integer.parseInt(matcher.group(1)));
59+
return true;
60+
} else {
61+
return false;
62+
}
63+
}, 10, TimeUnit.SECONDS);
64+
}
65+
66+
endpoint = getHost() + ":" + port.get();
67+
}
68+
}

core/src/test/java/org/testcontainers/images/ImagePullPolicyTest.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.mockito.Mockito;
1313
import org.rnorth.visibleassertions.VisibleAssertions;
1414
import org.testcontainers.DockerClientFactory;
15+
import org.testcontainers.DockerRegistryContainer;
1516
import org.testcontainers.containers.ContainerLaunchException;
1617
import org.testcontainers.containers.GenericContainer;
1718
import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy;
@@ -22,19 +23,17 @@
2223

2324
import static org.junit.Assert.fail;
2425
import static org.mockito.ArgumentMatchers.any;
25-
import static org.testcontainers.TestImages.DOCKER_REGISTRY_IMAGE;
2626

2727
public class ImagePullPolicyTest {
2828

2929
@ClassRule
30-
public static GenericContainer<?> registry = new GenericContainer<>(DOCKER_REGISTRY_IMAGE)
31-
.withExposedPorts(5000);
30+
public static DockerRegistryContainer registry = new DockerRegistryContainer();
3231

3332
private static DockerImageName imageName;
3433

3534
@BeforeClass
3635
public static void beforeClass() throws Exception {
37-
String testRegistryAddress = registry.getHost() + ":" + registry.getFirstMappedPort();
36+
String testRegistryAddress = registry.getEndpoint();
3837
String testImageName = testRegistryAddress + "/image-pull-policy-test";
3938
String tag = UUID.randomUUID().toString();
4039
imageName = DockerImageName.parse(testImageName).withTag(tag);

core/src/test/java/org/testcontainers/utility/AuthenticatedImagePullTest.java

+4-36
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,17 @@
1313
import org.junit.Test;
1414
import org.mockito.Mockito;
1515
import org.testcontainers.DockerClientFactory;
16+
import org.testcontainers.DockerRegistryContainer;
1617
import org.testcontainers.containers.ContainerState;
1718
import org.testcontainers.containers.DockerComposeContainer;
1819
import org.testcontainers.containers.GenericContainer;
19-
import org.testcontainers.containers.output.FrameConsumerResultCallback;
20-
import org.testcontainers.containers.output.OutputFrame;
21-
import org.testcontainers.containers.output.WaitingConsumer;
2220
import org.testcontainers.images.builder.ImageFromDockerfile;
2321

2422
import java.io.IOException;
2523
import java.nio.file.Files;
2624
import java.nio.file.Path;
2725
import java.nio.file.Paths;
2826
import java.util.concurrent.TimeUnit;
29-
import java.util.concurrent.atomic.AtomicInteger;
30-
import java.util.regex.Matcher;
31-
import java.util.regex.Pattern;
3227

3328
import static org.mockito.ArgumentMatchers.any;
3429
import static org.mockito.ArgumentMatchers.eq;
@@ -51,18 +46,14 @@ public class AuthenticatedImagePullTest {
5146
* Containerised docker image registry, with simple hardcoded credentials
5247
*/
5348
@ClassRule
54-
public static GenericContainer<?> authenticatedRegistry = new GenericContainer<>(new ImageFromDockerfile()
49+
public static DockerRegistryContainer authenticatedRegistry = new DockerRegistryContainer(new ImageFromDockerfile()
5550
.withDockerfileFromBuilder(builder -> {
5651
builder.from(DOCKER_REGISTRY_IMAGE.asCanonicalNameString())
5752
.run("htpasswd -Bbn testuser notasecret > /htpasswd")
5853
.env("REGISTRY_AUTH", "htpasswd")
5954
.env("REGISTRY_AUTH_HTPASSWD_PATH", "/htpasswd")
6055
.env("REGISTRY_AUTH_HTPASSWD_REALM", "Test");
61-
}))
62-
.withEnv("REGISTRY_HTTP_ADDR", "127.0.0.1:0")
63-
.withCreateContainerCmdModifier(cmd -> {
64-
cmd.getHostConfig().withNetworkMode("host");
65-
});
56+
}));
6657

6758
private static RegistryAuthLocator originalAuthLocatorSingleton;
6859
private static DockerClient client;
@@ -75,30 +66,7 @@ public static void setUp() throws Exception {
7566
originalAuthLocatorSingleton = RegistryAuthLocator.instance();
7667
client = DockerClientFactory.instance().client();
7768

78-
AtomicInteger port = new AtomicInteger(-1);
79-
try (FrameConsumerResultCallback resultCallback = new FrameConsumerResultCallback()) {
80-
WaitingConsumer waitingConsumer = new WaitingConsumer();
81-
resultCallback.addConsumer(OutputFrame.OutputType.STDERR, waitingConsumer);
82-
83-
client.logContainerCmd(authenticatedRegistry.getContainerId())
84-
.withStdErr(true)
85-
.withFollowStream(true)
86-
.exec(resultCallback);
87-
88-
Pattern pattern = Pattern.compile(".*listening on .*:(\\d+).*", Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
89-
waitingConsumer.waitUntil(it -> {
90-
String s = it.getUtf8String();
91-
Matcher matcher = pattern.matcher(s);
92-
if (matcher.matches()) {
93-
port.set(Integer.parseInt(matcher.group(1)));
94-
return true;
95-
} else {
96-
return false;
97-
}
98-
}, 10, TimeUnit.SECONDS);
99-
}
100-
101-
String testRegistryAddress = authenticatedRegistry.getHost() + ":" + port.get();
69+
String testRegistryAddress = authenticatedRegistry.getEndpoint();
10270
testImageName = testRegistryAddress + "/alpine";
10371

10472
final DockerImageName expectedName = DockerImageName.parse(testImageName);

docs/examples/junit4/generic/src/test/java/org/testcontainers/containers/startupcheck/StartupCheckStrategyTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ public static class MinimumDurationStrategyTest {
8484
@SneakyThrows
8585
@Test
8686
public void testCommandIsExecuted() {
87-
waitForHello(bboxWithMinimumDuration);
88-
8987
assertThat(bboxWithMinimumDuration.isRunning()).isTrue();
88+
89+
waitForHello(bboxWithMinimumDuration);
9090
}
9191
}
9292
}

0 commit comments

Comments
 (0)