@@ -167,16 +167,20 @@ public class SomeOtherTest {
167
167
private static final TarantoolCartridgeContainer container =
168
168
// Pass the classpath-relative paths of the instances configuration and topology script files
169
169
new TarantoolCartridgeContainer (" cartridge/instances.yml" , " cartridge/topology.lua" )
170
- // Point out the classpath-relative directory where the application files reside
171
- .withDirectoryBinding(" cartridge" )
172
- .withRouterHost(" localhost" ) // Optional, "localhost" is default
173
- .withRouterPort(3301 ) // Binary port, optional, 3301 is default
174
- .withAPIPort(8801 ) // Cartridge HTTP API port, optional, 8081 is default
175
- .withRouterUsername(" admin" ) // Specify the actual username, default is "admin"
176
- .withRouterPassword(" testapp-cluster-cookie" ) // Specify the actual password, see the "cluster_cookie" parameter
177
- // in the cartridge.cfg({...}) call in your application.
178
- // Usually it can be found in the init.lua module
179
- .withReuse(true ); // allows to reuse the container build once for faster testing
170
+ // Optional, "localhost" is default
171
+ .withRouterHost(" localhost" )
172
+ // Binary port, optional, 3301 is default
173
+ .withRouterPort(3301 )
174
+ // Cartridge HTTP API port, optional, 8081 is default
175
+ .withAPIPort(8801 )
176
+ // Specify the actual username, default is "admin"
177
+ .withRouterUsername(" admin" )
178
+ // Specify the actual password, see the "cluster_cookie" parameter
179
+ // in the cartridge.cfg({...}) call in your application.
180
+ // Usually it can be found in the init.lua module
181
+ .withRouterPassword(" secret-cluster-cookie" )
182
+ // allows to reuse the container build once for faster testing
183
+ .withReuse(true );
180
184
181
185
// Use the created container in tests
182
186
public void testFoo () {
@@ -193,6 +197,185 @@ public class SomeOtherTest {
193
197
}
194
198
```
195
199
200
+ ##### Environment variables of cartridge container and build arguments:
201
+ ###### Build arguments:
202
+
203
+ This section describes the Docker image build arguments and environment variables inside the container. It is worth
204
+ noting that all build arguments listed here are passed into environment variables of the same name. At the moment,
205
+ the following arguments are available to build the image:
206
+
207
+ - `CARTRIDGE_SRC_DIR ` - directory on the host machine that contains all the .lua scripts needed to initialize and run
208
+ cartridge. Defaults to `cartridge`. Does not convert to an environment variable.
209
+ - `TARANTOOL_WORKDIR ` - a directory where all data will be stored: snapshots, wal logs and cartridge config file.
210
+ Defaults to `/ app`. Converts to an environment variable. It is not recommended to override via the `withEnv(... )` method.
211
+ - `TARANTOOL_RUNDIR ` - a directory where PID and socket files are stored. Defaults to `/ tmp/ run`. Converts to an
212
+ environment variable. It is not recommended to override via the `withEnv(... )` method.
213
+ - `TARANTOOL_DATADIR ` - a directory containing the instances working directories. Defaults to `/ tmp/ data`. Converts to
214
+ an environment variable. It is not recommended to override via the `withEnv(... )` method.
215
+ - `TARANTOOL_LOGDIR ` - the directory where log files are stored. Defaults to `/ tmp/ log`. Converts to an environment
216
+ - variable. It is not recommended to override via the `withEnv(... )` method.
217
+ - `TARANTOOL_INSTANCES_FILE ` - path to the configuration file. Defaults to `. / instances. yml`. Converts to an environment
218
+ variable. It is not recommended to override via the `withEnv(... )` method.
219
+ - `START_DELAY ` - the time after which cartridge will actually run after the container has started. Converts to an
220
+ environment variable. It is not recommended to override via the `withEnv(... )` method.
221
+
222
+ You can set the Docker image build arguments using a map, which is passed as an input argument to the constructor when
223
+ creating a container in Java code:
224
+
225
+ ```java
226
+ import java.util.HashMap;
227
+ import java.util. Map ;
228
+
229
+ import org.junit. Test ;
230
+
231
+ @Testcontainers
232
+ public class BuildArgsTest {
233
+
234
+ private static final Map<String , String > buildArgs = new HashMap<> () {{
235
+ // Set src directory
236
+ put(" CARTRIDGE_SRC_DIR" , " cartridge" );
237
+
238
+ // Set Tarantool work directory (has an environment variable of the same name)
239
+ put(" TARANTOOL_WORKDIR" , " /app" );
240
+
241
+ // Set Tarantool run directory (has an environment variable of the same name)
242
+ put(" TARANTOOL_RUNDIR" , " /tmp/new_run" );
243
+
244
+ // Set Tarantool data directory (has an environment variable of the same name)
245
+ put(" TARANTOOL_DATADIR" , " /tmp/new_data" );
246
+
247
+ // Set Tarantool log files directory (has an environment variable of the same name)
248
+ put(" TARANTOOL_LOGDIR" , " /tmp/log" );
249
+
250
+ // Path to the configuration file (has an environment variable of the same name)
251
+ put(" TARANTOOL_INSTANCES_FILE" , " ./instances.yml" );
252
+
253
+ // Set container start delay (has an environment variable of the same name)
254
+ put(" START_DELAY" , " 1s" );
255
+ }};
256
+
257
+
258
+ @Container
259
+ // Create container with build arguments
260
+ private static final TarantoolCartridgeContainer newContainer = new TarantoolCartridgeContainer (
261
+ " Dockerfile" ,
262
+ " build_args_test" ,
263
+ " cartridge/instances.yml" ,
264
+ " cartridge/replicasets.yml" ,
265
+ buildArgs)
266
+ .withStartupTimeout(Duration . ofMinutes(5 ))
267
+ .withLogConsumer(new Slf4jLogConsumer (
268
+ LoggerFactory . getLogger(TarantoolCartridgeBootstrapFromYamlTest . class)));
269
+
270
+
271
+ @Test
272
+ public void testBuildArgs () {
273
+ // Start container
274
+ newContainer. start();
275
+
276
+ // Get environment variables from container
277
+ ExecResult res = newContainer. execInContainer(" env" );
278
+
279
+ // Remove src directory to create expected env map
280
+ buildArgs. remove(" CARTRIDGE_SRC_DIR" , " cartridge" );
281
+
282
+ // Check that environment variables from container contains expected env map
283
+ assertTrue(envIsContainsInStdout(res. getStdout(), buildArgs));
284
+
285
+ // Check cartridge functionality
286
+ List<Object > result = newContainer. executeCommandDecoded(" return true" );
287
+ assertEquals(1 , result. size());
288
+ assertTrue((boolean ) result. get(0 ));
289
+ }
290
+
291
+ public bolean envIsContainsInStdout (String stdout , Map<String , String > env ) {
292
+ Map<String , String > envMap = Arrays . stream(stdout. split(" \n " ))
293
+ .collect(Collectors . toMap(toKey - > toKey. split(" =" )[0 ],
294
+ toValue - > {
295
+ String [] pair = toValue. split(" =" );
296
+ if (pair. length == 1 ) {
297
+ return " null" ;
298
+ }
299
+ return pair[1 ];
300
+ }));
301
+
302
+ return envMap. entrySet(). containsAll(env. entrySet());
303
+ }
304
+ }
305
+ ```
306
+
307
+ ###### Environment variables:
308
+
309
+ To set an environment variable, use the `withEnv (...)` method of testcontainers API. Full list of variables the
310
+ environments used in cartridge can be found here [link](https:// www.tarantool.io/ru/doc/2.11/book/cartridge/cartridge_api/modules/cartridge/).
311
+
312
+ ***Note:*** As shown in the previous section, some build arguments are converted to environment variables and used to
313
+ cartridge build at the image build stage.
314
+
315
+ An example of how to set the `TARANTOOL_CLUSTER_COOKIE` parameter:
316
+
317
+ ```java
318
+ import java.util.HashMap;
319
+ import java.util. Map ;
320
+
321
+ import org.junit. Test ;
322
+
323
+ @Testcontainers
324
+ public class EnvTest {
325
+
326
+ @Container
327
+ // Create container with env
328
+ private static final TarantoolCartridgeContainer newContainer = new TarantoolCartridgeContainer (
329
+ " Dockerfile" ,
330
+ " cartridge" ,
331
+ " cartridge/instances.yml" ,
332
+ " cartridge/replicasets.yml" )
333
+ // Set environment
334
+ .withEnv(TarantoolCartridgeContainer . ENV_TARANTOOL_CLUSTER_COOKIE , " secret" )
335
+ .withRouterUsername(" admin" )
336
+ .withRouterPassword(" secret" )
337
+ .withStartupTimeout(Duration . ofMinutes(5 ))
338
+ .withLogConsumer(new Slf4jLogConsumer (
339
+ LoggerFactory . getLogger(TarantoolCartridgeBootstrapFromYamlTest . class)));
340
+
341
+
342
+ @Test
343
+ public void testEnv () {
344
+
345
+ // Start container
346
+ newContainer. start();
347
+
348
+ // Get environment variables from container
349
+ ExecResult res = newContainer. execInContainer(" env" );
350
+
351
+ // Check that environment variables from container contains expected env map
352
+ assertTrue(envIsContainsInStdout(res. getStdout(), new HashMap<String , String > (){{
353
+ put(" TARANTOOL_CLUSTER_COOKIE" , " secret" );
354
+ }}));
355
+
356
+ // Check cartridge functionality
357
+ List<Object > result = newContainer. executeCommandDecoded(" return true" );
358
+ assertEquals(1 , result. size());
359
+ assertTrue((boolean ) result. get(0 ));
360
+ }
361
+
362
+ public bolean envIsContainsInStdout (String stdout , Map<String , String > env ) {
363
+ Map<String , String > envMap = Arrays . stream(stdout. split(" \n " ))
364
+ .collect(Collectors . toMap(toKey - > toKey. split(" =" )[0 ],
365
+ toValue - > {
366
+ String [] pair = toValue. split(" =" );
367
+ if (pair. length == 1 ) {
368
+ return " null" ;
369
+ }
370
+ return pair[1 ];
371
+ }));
372
+
373
+ return envMap. entrySet(). containsAll(env. entrySet());
374
+ }
375
+ }
376
+
377
+ ```
378
+
196
379
## License
197
380
198
381
See [LICENSE](LICENSE).
0 commit comments