Skip to content

Commit 8eb53c4

Browse files
committed
Add instruction how to use env and build args
Closes #12
1 parent cce5219 commit 8eb53c4

File tree

3 files changed

+254
-13
lines changed

3 files changed

+254
-13
lines changed

README.md

Lines changed: 193 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,20 @@ public class SomeOtherTest {
167167
private static final TarantoolCartridgeContainer container =
168168
// Pass the classpath-relative paths of the instances configuration and topology script files
169169
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);
180184

181185
// Use the created container in tests
182186
public void testFoo() {
@@ -193,6 +197,185 @@ public class SomeOtherTest {
193197
}
194198
```
195199

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+
196379
## License
197380

198381
See [LICENSE](LICENSE).

src/test/java/org/testcontainers/containers/CartridgeContainerTestUtils.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.testcontainers.containers;
22

3+
import java.util.Arrays;
34
import java.util.List;
5+
import java.util.Map;
6+
import java.util.stream.Collectors;
47

58
import static org.junit.Assert.assertEquals;
69

@@ -21,4 +24,18 @@ static public void executeProfileReplaceSmokeTest(TarantoolCartridgeContainer co
2124
assertEquals(1, result.size());
2225
assertEquals(33, ((List<?>) result.get(0)).get(3));
2326
}
27+
28+
public static boolean envIsContainsInStdout(String stdout, Map<String, String> env) {
29+
Map<String, String> envMap = Arrays.stream(stdout.split("\n"))
30+
.collect(Collectors.toMap(toKey -> toKey.split("=")[0],
31+
toValue -> {
32+
String[] pair = toValue.split("=");
33+
if (pair.length == 1) {
34+
return "null";
35+
}
36+
return pair[1];
37+
}));
38+
39+
return envMap.entrySet().containsAll(env.entrySet());
40+
}
2441
}

src/test/java/org/testcontainers/containers/TarantoolCartridgeBootstrapFromLuaWithFixedPortsTest.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.testcontainers.containers;
22

33
import java.time.Duration;
4+
import java.util.HashMap;
45
import java.util.List;
56
import java.util.Map;
67

@@ -15,6 +16,7 @@
1516
import static org.junit.jupiter.api.Assertions.assertEquals;
1617
import static org.junit.jupiter.api.Assertions.assertFalse;
1718
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
import org.testcontainers.containers.Container.ExecResult;
1820

1921

2022
/**
@@ -67,9 +69,13 @@ public void testTarantoolClusterCookieWithEnv() throws Exception {
6769
LoggerFactory.getLogger(TarantoolCartridgeBootstrapFromYamlTest.class))))
6870
{
6971
newContainer.start();
70-
Map<String, String> env = container.getEnvMap();
71-
assertFalse(env.containsKey(TarantoolCartridgeContainer.ENV_TARANTOOL_CLUSTER_COOKIE));
72-
List<Object> result = container.executeCommandDecoded("return true");
72+
ExecResult res = newContainer.execInContainer("env");
73+
assertTrue(CartridgeContainerTestUtils.envIsContainsInStdout(res.getStdout(),
74+
new HashMap<String, String>(){{
75+
put("TARANTOOL_CLUSTER_COOKIE", "secret");
76+
}}));
77+
78+
List<Object> result = newContainer.executeCommandDecoded("return true");
7379
assertEquals(1, result.size());
7480
assertTrue((boolean) result.get(0));
7581
}
@@ -97,4 +103,39 @@ public void test_retryingSetupTopology_shouldWork() {
97103
assertEquals("Failed to change the app topology after retry", cause.getMessage());
98104
}
99105
}
106+
107+
108+
@Test
109+
public void testBuildArgs() throws Exception {
110+
111+
final Map<String, String> buildArgs = new HashMap<String, String>(){{
112+
put("CARTRIDGE_SRC_DIR", "cartridge");
113+
put("TARANTOOL_WORKDIR", "/app");
114+
put("TARANTOOL_RUNDIR", "/tmp/new_run");
115+
put("TARANTOOL_DATADIR", "/tmp/new_data");
116+
put("TARANTOOL_LOGDIR", "/tmp/log");
117+
put("TARANTOOL_INSTANCES_FILE", "./instances.yml");
118+
put("START_DELAY", "1s");
119+
}};
120+
121+
try(TarantoolCartridgeContainer newContainer = new TarantoolCartridgeContainer(
122+
"Dockerfile",
123+
"build_args_test",
124+
"cartridge/instances.yml",
125+
"cartridge/replicasets.yml",
126+
buildArgs)
127+
.withStartupTimeout(Duration.ofMinutes(5))
128+
.withLogConsumer(new Slf4jLogConsumer(
129+
LoggerFactory.getLogger(TarantoolCartridgeBootstrapFromYamlTest.class)))
130+
) {
131+
newContainer.start();
132+
ExecResult res = newContainer.execInContainer("env");
133+
buildArgs.remove("CARTRIDGE_SRC_DIR", "cartridge");
134+
assertTrue(CartridgeContainerTestUtils.envIsContainsInStdout(res.getStdout(), buildArgs));
135+
136+
List<Object> result = newContainer.executeCommandDecoded("return true");
137+
assertEquals(1, result.size());
138+
assertTrue((boolean) result.get(0));
139+
}
140+
}
100141
}

0 commit comments

Comments
 (0)