|
10 | 10 | import java.io.IOException; |
11 | 11 | import java.net.InetSocketAddress; |
12 | 12 | import java.net.Socket; |
| 13 | +import java.nio.file.Paths; |
13 | 14 | import java.util.Map; |
14 | 15 | import org.jetbrains.annotations.NotNull; |
15 | 16 | import org.junit.jupiter.api.TestInfo; |
16 | 17 | import org.slf4j.Logger; |
17 | 18 | import org.slf4j.LoggerFactory; |
18 | 19 | import org.testcontainers.containers.Network; |
| 20 | +import org.testcontainers.images.builder.ImageFromDockerfile; |
19 | 21 | import org.testcontainers.mongodb.MongoDBContainer; |
20 | 22 | import org.testcontainers.toxiproxy.ToxiproxyContainer; |
21 | 23 | import org.testcontainers.utility.DockerImageName; |
|
40 | 42 | public class MongoService implements Closeable { |
41 | 43 | // We do logging in some static initializers, so this needs to be initialized first |
42 | 44 | private static final Logger LOGGER = LoggerFactory.getLogger(MongoService.class); |
43 | | - |
44 | | - private static final DockerImageName MONGODB_IMAGE_NAME = DockerImageName.parse("mongo:8.0"); |
45 | | - |
46 | 45 | private final MongoClient mongoClient = MongoClients.create(normalClientSettings); |
47 | 46 |
|
48 | 47 | // Expensive stuff shared among instances as much as possible, hence static |
49 | 48 | private static final Network NETWORK = Network.newNetwork(); |
| 49 | + private static final DockerImageName MONGO_IMAGE = newImageFromDockerfile( |
| 50 | + "mongo", |
| 51 | + "src/test/resources/mongo.dockerfile"); |
50 | 52 | private static final MongoDBContainer MONGO_CONTAINER = mongoContainer(); |
51 | 53 | private static final MongoClientSettings normalClientSettings = mongoClientSettings( |
52 | 54 | new ServerAddress(MONGO_CONTAINER.getHost(), MONGO_CONTAINER.getFirstMappedPort()) |
53 | 55 | ); |
54 | 56 |
|
| 57 | + public static final DockerImageName TOXIPROXY_IMAGE = newImageFromDockerfile( |
| 58 | + "shopify/toxiproxy", |
| 59 | + "src/test/resources/toxiproxy.dockerfile"); |
| 60 | + |
55 | 61 | private static final ToxiproxyContainer TOXIPROXY_CONTAINER = toxiproxyContainer(); |
56 | 62 | private static final ToxiproxyClient TOXIPROXY_CLIENT = createToxiproxyClient(); |
57 | 63 |
|
@@ -113,18 +119,22 @@ private static MongoDBContainer mongoContainer() { |
113 | 119 | * @return just the {@link MongoDBContainer} with no options, and not yet started |
114 | 120 | */ |
115 | 121 | static MongoDBContainer newPlainMongoContainer() { |
116 | | - return new MongoDBContainer(MONGODB_IMAGE_NAME); |
| 122 | + return new MongoDBContainer(MONGO_IMAGE); |
117 | 123 | } |
118 | 124 |
|
119 | 125 | private static ToxiproxyContainer toxiproxyContainer() { |
120 | | - ToxiproxyContainer result = new ToxiproxyContainer( |
121 | | - DockerImageName.parse("ghcr.io/shopify/toxiproxy:2.12.0") |
122 | | - .asCompatibleSubstituteFor("shopify/toxiproxy")) |
| 126 | + ToxiproxyContainer result = new ToxiproxyContainer(TOXIPROXY_IMAGE) |
123 | 127 | .withNetwork(NETWORK); |
124 | 128 | result.start(); |
125 | 129 | return result; |
126 | 130 | } |
127 | 131 |
|
| 132 | + private static DockerImageName newImageFromDockerfile(String otherImageName, String filePath) { |
| 133 | + return DockerImageName.parse(new ImageFromDockerfile() |
| 134 | + .withDockerfile(Paths.get(filePath)).get() |
| 135 | + ).asCompatibleSubstituteFor(otherImageName); |
| 136 | + } |
| 137 | + |
128 | 138 | private static ToxiproxyClient createToxiproxyClient() { |
129 | 139 | return new ToxiproxyClient( |
130 | 140 | TOXIPROXY_CONTAINER.getHost(), |
|
0 commit comments