|
1 | 1 | package org.testcontainers.junit; |
2 | 2 |
|
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; |
7 | 3 | import org.junit.Rule; |
8 | 4 | import org.junit.Test; |
9 | 5 | import org.junit.runner.RunWith; |
10 | 6 | import org.junit.runners.Parameterized; |
11 | | -import org.rnorth.ducttape.unreliables.Unreliables; |
12 | 7 | import org.testcontainers.containers.GenericContainer; |
13 | 8 | import org.testcontainers.images.builder.ImageFromDockerfile; |
14 | 9 |
|
15 | | -import java.io.IOException; |
16 | | -import java.util.concurrent.TimeUnit; |
17 | | - |
18 | | -import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals; |
19 | 10 | import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue; |
20 | 11 |
|
21 | 12 | /** |
|
25 | 16 | @RunWith(Parameterized.class) |
26 | 17 | public class ParameterizedDockerfileContainerTest { |
27 | 18 |
|
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; |
32 | 23 |
|
33 | | - public ParameterizedDockerfileContainerTest(String baseImage) { |
| 24 | + public ParameterizedDockerfileContainerTest(String baseImage, String expectedVersion) { |
34 | 25 | container = new GenericContainer(new ImageFromDockerfile().withDockerfileFromBuilder(builder -> { |
35 | 26 | builder |
36 | 27 | .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. |
39 | 30 | .build(); |
40 | | - })).withExposedPorts(80); |
| 31 | + })).withCommand("top"); |
| 32 | + this.expectedVersion = expectedVersion; |
41 | 33 | } |
42 | 34 |
|
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 | + } |
45 | 45 |
|
46 | 46 | @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(); |
52 | 49 |
|
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)); |
63 | 52 | } |
64 | 53 | } |
0 commit comments