Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests on Windows #5096

Merged
merged 2 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix tests on Windows
  • Loading branch information
bsideup committed Feb 19, 2022
commit c683fc60ed8da21626ed7142155286b7bb6bed69
7 changes: 5 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
jobs:

- job: azure_pipeline_tests
timeoutInMinutes: 120
- job: core_tests
timeoutInMinutes: 60
steps:

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

- job: other_tests
kiview marked this conversation as resolved.
Show resolved Hide resolved
timeoutInMinutes: 120
steps:
# Run all non-core tests when running the Windows CI tests
- task: Gradle@2
condition: eq(variables['Agent.OS'], 'Windows_NT')
Expand Down
68 changes: 68 additions & 0 deletions core/src/test/java/org/testcontainers/DockerRegistryContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.testcontainers;

import com.github.dockerjava.api.command.InspectContainerResponse;
import lombok.Getter;
import lombok.NonNull;
import lombok.SneakyThrows;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.FrameConsumerResultCallback;
import org.testcontainers.containers.output.OutputFrame;
import org.testcontainers.containers.output.WaitingConsumer;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DockerRegistryContainer extends GenericContainer<DockerRegistryContainer> {
kiview marked this conversation as resolved.
Show resolved Hide resolved

@Getter
String endpoint;

public DockerRegistryContainer() {
super(TestImages.DOCKER_REGISTRY_IMAGE);
}

public DockerRegistryContainer(@NonNull Future<String> image) {
super(image);
}

@Override
protected void configure() {
super.configure();
withEnv("REGISTRY_HTTP_ADDR", "127.0.0.1:0");
withCreateContainerCmdModifier(cmd -> {
cmd.getHostConfig().withNetworkMode("host");
});
}

@Override
@SneakyThrows
protected void containerIsStarting(InspectContainerResponse containerInfo) {
AtomicInteger port = new AtomicInteger(-1);
try (FrameConsumerResultCallback resultCallback = new FrameConsumerResultCallback()) {
WaitingConsumer waitingConsumer = new WaitingConsumer();
resultCallback.addConsumer(OutputFrame.OutputType.STDERR, waitingConsumer);

dockerClient.logContainerCmd(containerInfo.getId())
.withStdErr(true)
.withFollowStream(true)
.exec(resultCallback);

Pattern pattern = Pattern.compile(".*listening on .*:(\\d+).*", Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
kiview marked this conversation as resolved.
Show resolved Hide resolved
waitingConsumer.waitUntil(it -> {
String s = it.getUtf8String();
Matcher matcher = pattern.matcher(s);
if (matcher.matches()) {
port.set(Integer.parseInt(matcher.group(1)));
return true;
} else {
return false;
}
}, 10, TimeUnit.SECONDS);
}

endpoint = getHost() + ":" + port.get();
kiview marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.mockito.Mockito;
import org.rnorth.visibleassertions.VisibleAssertions;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.DockerRegistryContainer;
import org.testcontainers.containers.ContainerLaunchException;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy;
Expand All @@ -22,19 +23,17 @@

import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.testcontainers.TestImages.DOCKER_REGISTRY_IMAGE;

public class ImagePullPolicyTest {

@ClassRule
public static GenericContainer<?> registry = new GenericContainer<>(DOCKER_REGISTRY_IMAGE)
.withExposedPorts(5000);
public static DockerRegistryContainer registry = new DockerRegistryContainer();

private static DockerImageName imageName;

@BeforeClass
public static void beforeClass() throws Exception {
String testRegistryAddress = registry.getHost() + ":" + registry.getFirstMappedPort();
String testRegistryAddress = registry.getEndpoint();
String testImageName = testRegistryAddress + "/image-pull-policy-test";
String tag = UUID.randomUUID().toString();
imageName = DockerImageName.parse(testImageName).withTag(tag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,17 @@
import org.junit.Test;
import org.mockito.Mockito;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.DockerRegistryContainer;
import org.testcontainers.containers.ContainerState;
import org.testcontainers.containers.DockerComposeContainer;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.FrameConsumerResultCallback;
import org.testcontainers.containers.output.OutputFrame;
import org.testcontainers.containers.output.WaitingConsumer;
import org.testcontainers.images.builder.ImageFromDockerfile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand All @@ -51,18 +46,14 @@ public class AuthenticatedImagePullTest {
* Containerised docker image registry, with simple hardcoded credentials
*/
@ClassRule
public static GenericContainer<?> authenticatedRegistry = new GenericContainer<>(new ImageFromDockerfile()
public static DockerRegistryContainer authenticatedRegistry = new DockerRegistryContainer(new ImageFromDockerfile()
.withDockerfileFromBuilder(builder -> {
builder.from(DOCKER_REGISTRY_IMAGE.asCanonicalNameString())
.run("htpasswd -Bbn testuser notasecret > /htpasswd")
.env("REGISTRY_AUTH", "htpasswd")
.env("REGISTRY_AUTH_HTPASSWD_PATH", "/htpasswd")
.env("REGISTRY_AUTH_HTPASSWD_REALM", "Test");
}))
.withEnv("REGISTRY_HTTP_ADDR", "127.0.0.1:0")
.withCreateContainerCmdModifier(cmd -> {
cmd.getHostConfig().withNetworkMode("host");
});
}));

private static RegistryAuthLocator originalAuthLocatorSingleton;
private static DockerClient client;
Expand All @@ -75,30 +66,7 @@ public static void setUp() throws Exception {
originalAuthLocatorSingleton = RegistryAuthLocator.instance();
client = DockerClientFactory.instance().client();

AtomicInteger port = new AtomicInteger(-1);
try (FrameConsumerResultCallback resultCallback = new FrameConsumerResultCallback()) {
WaitingConsumer waitingConsumer = new WaitingConsumer();
resultCallback.addConsumer(OutputFrame.OutputType.STDERR, waitingConsumer);

client.logContainerCmd(authenticatedRegistry.getContainerId())
.withStdErr(true)
.withFollowStream(true)
.exec(resultCallback);

Pattern pattern = Pattern.compile(".*listening on .*:(\\d+).*", Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
waitingConsumer.waitUntil(it -> {
String s = it.getUtf8String();
Matcher matcher = pattern.matcher(s);
if (matcher.matches()) {
port.set(Integer.parseInt(matcher.group(1)));
return true;
} else {
return false;
}
}, 10, TimeUnit.SECONDS);
}

String testRegistryAddress = authenticatedRegistry.getHost() + ":" + port.get();
String testRegistryAddress = authenticatedRegistry.getEndpoint();
testImageName = testRegistryAddress + "/alpine";

final DockerImageName expectedName = DockerImageName.parse(testImageName);
Expand Down