Skip to content

Commit 9f1486e

Browse files
authored
Fix random test failure by removing dependency on Alpine package server. (#505)
* Fix random test failure by removing dependency on Alpine package server. This fixes an occasional random failure during docker image build, seemingly due to a failure to download alpine package indices: ``` ERROR: http://dl-cdn.alpinelinux.org/alpine/v3.3/main: temporary error (try again later) ... ERROR 🐳 [testcontainers/cbwpthsp79aee52s] - The command '/bin/sh -c apk add --update nginx' returned a non-zero code: 1 ```
1 parent ad68a83 commit 9f1486e

File tree

1 file changed

+23
-34
lines changed

1 file changed

+23
-34
lines changed
Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
package org.testcontainers.junit;
22

3-
import org.apache.http.client.methods.CloseableHttpResponse;
4-
import org.apache.http.client.methods.HttpGet;
5-
import org.apache.http.impl.client.CloseableHttpClient;
6-
import org.apache.http.impl.client.HttpClientBuilder;
73
import org.junit.Rule;
84
import org.junit.Test;
95
import org.junit.runner.RunWith;
106
import org.junit.runners.Parameterized;
11-
import org.rnorth.ducttape.unreliables.Unreliables;
127
import org.testcontainers.containers.GenericContainer;
138
import org.testcontainers.images.builder.ImageFromDockerfile;
149

15-
import java.io.IOException;
16-
import java.util.concurrent.TimeUnit;
17-
18-
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
1910
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;
2011

2112
/**
@@ -25,40 +16,38 @@
2516
@RunWith(Parameterized.class)
2617
public class ParameterizedDockerfileContainerTest {
2718

28-
@Parameterized.Parameters(name = "{0}")
29-
public static Object[] data() {
30-
return new Object[] { "alpine:3.2", "alpine:3.3" };
31-
}
19+
private final String expectedVersion;
20+
21+
@Rule
22+
public GenericContainer container;
3223

33-
public ParameterizedDockerfileContainerTest(String baseImage) {
24+
public ParameterizedDockerfileContainerTest(String baseImage, String expectedVersion) {
3425
container = new GenericContainer(new ImageFromDockerfile().withDockerfileFromBuilder(builder -> {
3526
builder
3627
.from(baseImage)
37-
.run("apk add --update nginx")
38-
.cmd("nginx", "-g", "daemon off;")
28+
// Could potentially customise the image here, e.g. adding files, running
29+
// commands, etc.
3930
.build();
40-
})).withExposedPorts(80);
31+
})).withCommand("top");
32+
this.expectedVersion = expectedVersion;
4133
}
4234

43-
@Rule
44-
public GenericContainer container;
35+
@Parameterized.Parameters(name = "{0}")
36+
public static Object[][] data() {
37+
return new Object[][] {
38+
{ "alpine:3.2", "3.2"},
39+
{ "alpine:3.3", "3.3"},
40+
{ "alpine:3.4", "3.4"},
41+
{ "alpine:3.5", "3.5"},
42+
{ "alpine:3.6", "3.6"}
43+
};
44+
}
4545

4646
@Test
47-
public void simpleTest() throws IOException {
48-
String address = String.format("http://%s:%s", container.getContainerIpAddress(), container.getMappedPort(80));
49-
50-
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
51-
HttpGet get = new HttpGet(address);
47+
public void simpleTest() throws Exception {
48+
final String release = container.execInContainer("cat", "/etc/alpine-release").getStdout();
5249

53-
Unreliables.retryUntilSuccess(5, TimeUnit.SECONDS, () -> {
54-
try (CloseableHttpResponse response = httpClient.execute(get)) {
55-
assertEquals("A container built from a dockerfile can run nginx as expected, and returns a good status code",
56-
200,
57-
response.getStatusLine().getStatusCode());
58-
assertTrue("A container built from a dockerfile can run nginx as expected, and returns an expected Server header",
59-
response.getHeaders("Server")[0].getValue().contains("nginx"));
60-
}
61-
return true;
62-
});
50+
assertTrue("/etc/alpine-release starts with " + expectedVersion,
51+
release.startsWith(expectedVersion));
6352
}
6453
}

0 commit comments

Comments
 (0)