5
5
6
6
import java .net .URL ;
7
7
import java .util .Arrays ;
8
+ import java .util .Collections ;
8
9
import java .util .List ;
10
+ import java .util .Map ;
9
11
import java .util .concurrent .CompletableFuture ;
10
12
import java .util .concurrent .ExecutionException ;
11
13
import java .util .concurrent .Future ;
@@ -98,6 +100,7 @@ public class TarantoolCartridgeContainer extends GenericContainer<TarantoolCartr
98
100
private static final String ENV_TARANTOOL_WORKDIR = "TARANTOOL_WORKDIR" ;
99
101
private static final String ENV_TARANTOOL_RUNDIR = "TARANTOOL_RUNDIR" ;
100
102
private static final String ENV_TARANTOOL_DATADIR = "TARANTOOL_DATADIR" ;
103
+ private boolean useFixedPorts = false ;
101
104
102
105
private String routerHost = ROUTER_HOST ;
103
106
private int routerPort = ROUTER_PORT ;
@@ -106,7 +109,6 @@ public class TarantoolCartridgeContainer extends GenericContainer<TarantoolCartr
106
109
private String routerPassword = CARTRIDGE_PASSWORD ;
107
110
private String directoryResourcePath = SCRIPT_RESOURCE_DIRECTORY ;
108
111
private String instanceDir = INSTANCE_DIR ;
109
- private final String instancesFile ;
110
112
private final CartridgeConfigParser instanceFileParser ;
111
113
private final String topologyConfigurationFile ;
112
114
private final TarantoolContainerClientHelper clientHelper ;
@@ -119,7 +121,7 @@ public class TarantoolCartridgeContainer extends GenericContainer<TarantoolCartr
119
121
* @param topologyConfigurationFile path to a topology bootstrap script, relative to the classpath resources
120
122
*/
121
123
public TarantoolCartridgeContainer (String instancesFile , String topologyConfigurationFile ) {
122
- this (withArguments ( buildImage ()) , instancesFile , topologyConfigurationFile );
124
+ this (DOCKERFILE , instancesFile , topologyConfigurationFile );
123
125
}
124
126
125
127
/**
@@ -130,7 +132,7 @@ public TarantoolCartridgeContainer(String instancesFile, String topologyConfigur
130
132
* @param topologyConfigurationFile path to a topology bootstrap script, relative to the classpath resources
131
133
*/
132
134
public TarantoolCartridgeContainer (String dockerFile , String instancesFile , String topologyConfigurationFile ) {
133
- this (withArguments ( buildImage ( dockerFile )) , instancesFile , topologyConfigurationFile );
135
+ this (dockerFile , "" , instancesFile , topologyConfigurationFile );
134
136
}
135
137
136
138
/**
@@ -145,21 +147,43 @@ public TarantoolCartridgeContainer(String dockerFile, String instancesFile, Stri
145
147
*/
146
148
public TarantoolCartridgeContainer (String dockerFile , String buildImageName ,
147
149
String instancesFile , String topologyConfigurationFile ) {
148
- this (withArguments (buildImage (dockerFile , buildImageName )), instancesFile , topologyConfigurationFile );
150
+ this (dockerFile , buildImageName , instancesFile , topologyConfigurationFile , Collections .emptyMap ());
151
+ }
152
+
153
+
154
+ /**
155
+ * Create a container with specified image and specified instances file from the classpath resources. By providing
156
+ * the result Cartridge container image name, you can cache the image and avoid rebuilding on each test run (the
157
+ * image is tagged with the provided name and not deleted after tests finishing).
158
+ *
159
+ * @param dockerFile URL resource path to a Dockerfile which configures Cartridge
160
+ * and other necessary services
161
+ * @param buildImageName Specify a stable image name for the test container to prevent rebuilds
162
+ * @param instancesFile URL resource path to instances.yml relative in the classpath
163
+ * @param topologyConfigurationFile URL resource path to a topology bootstrap script in the classpath
164
+ * @param buildArgs a map of arguments that will be passed to docker ARG commands on image build.
165
+ * This values can be overriden by environment.
166
+ */
167
+ public TarantoolCartridgeContainer (String dockerFile , String buildImageName , String instancesFile ,
168
+ String topologyConfigurationFile , final Map <String , String > buildArgs ) {
169
+ this (withArguments (buildImage (dockerFile , buildImageName ), buildArgs ),
170
+ instancesFile , topologyConfigurationFile );
149
171
}
150
172
151
173
private TarantoolCartridgeContainer (Future <String > image , String instancesFile , String topologyConfigurationFile ) {
152
174
super (image );
153
175
if (instancesFile == null || instancesFile .isEmpty ()) {
154
176
throw new IllegalArgumentException ("Instance file name must not be null or empty" );
155
177
}
156
- this .instancesFile = instancesFile ;
157
178
this .instanceFileParser = new CartridgeConfigParser (instancesFile );
158
179
this .topologyConfigurationFile = topologyConfigurationFile ;
159
180
this .clientHelper = new TarantoolContainerClientHelper (this );
160
181
}
161
182
162
- private static Future <String > withArguments (ImageFromDockerfile image ) {
183
+ private static Future <String > withArguments (ImageFromDockerfile image , final Map <String , String > buildArgs ) {
184
+ if (!buildArgs .isEmpty ()) {
185
+ image .withBuildArgs (buildArgs );
186
+ }
163
187
for (String envVariable : Arrays .asList (
164
188
ENV_TARANTOOL_VERSION ,
165
189
ENV_TARANTOOL_SERVER_USER ,
@@ -178,17 +202,12 @@ private static Future<String> withArguments(ImageFromDockerfile image) {
178
202
return image ;
179
203
}
180
204
181
- private static ImageFromDockerfile buildImage () {
182
- return buildImage (DOCKERFILE );
183
- }
184
-
185
- private static ImageFromDockerfile buildImage (String dockerFile ) {
186
- return new ImageFromDockerfile ().withFileFromClasspath ("Dockerfile" , dockerFile );
187
- }
188
-
189
205
private static ImageFromDockerfile buildImage (String dockerFile , String buildImageName ) {
190
- return new ImageFromDockerfile (buildImageName , false )
191
- .withFileFromClasspath ("Dockerfile" , dockerFile );
206
+ if (buildImageName != null && !buildImageName .isEmpty ()) {
207
+ return new ImageFromDockerfile (buildImageName , false )
208
+ .withFileFromClasspath ("Dockerfile" , dockerFile );
209
+ }
210
+ return new ImageFromDockerfile ().withFileFromClasspath ("Dockerfile" , dockerFile );
192
211
}
193
212
194
213
/**
@@ -206,6 +225,9 @@ public String getRouterHost() {
206
225
* @return router mapped port
207
226
*/
208
227
public int getRouterPort () {
228
+ if (useFixedPorts ) {
229
+ return routerPort ;
230
+ }
209
231
return getMappedPort (routerPort );
210
232
}
211
233
@@ -312,9 +334,23 @@ public TarantoolCartridgeContainer withDirectoryBinding(String directoryResource
312
334
* @return HTTP API port
313
335
*/
314
336
public int getAPIPort () {
337
+ if (useFixedPorts ) {
338
+ return apiPort ;
339
+ }
315
340
return getMappedPort (apiPort );
316
341
}
317
342
343
+ /**
344
+ * Use fixed ports binding.
345
+ * Defaults to false.
346
+ *
347
+ * @return HTTP API port
348
+ */
349
+ public TarantoolCartridgeContainer withUseFixedPorts (boolean useFixedPorts ) {
350
+ this .useFixedPorts = useFixedPorts ;
351
+ return this ;
352
+ }
353
+
318
354
/**
319
355
* Set Cartridge router hostname
320
356
*
@@ -377,8 +413,17 @@ public TarantoolCartridgeContainer withRouterPassword(String routerPassword) {
377
413
378
414
@ Override
379
415
protected void configure () {
380
- withFileSystemBind (getDirectoryBinding (), getInstanceDir (), BindMode .READ_WRITE );
381
- withExposedPorts (instanceFileParser .getExposablePorts ());
416
+ if (!getDirectoryBinding ().isEmpty ()) {
417
+ withFileSystemBind (getDirectoryBinding (), getInstanceDir (), BindMode .READ_WRITE );
418
+ }
419
+
420
+ if (useFixedPorts ) {
421
+ for (Integer port : instanceFileParser .getExposablePorts ()) {
422
+ addFixedExposedPort (port , port );
423
+ }
424
+ } else {
425
+ withExposedPorts (instanceFileParser .getExposablePorts ());
426
+ }
382
427
}
383
428
384
429
@ Override
0 commit comments