Skip to content

Commit ba4661f

Browse files
nickkkcccArtDu
andauthored
Add instruction how to use env and build args
Closes #12 --------- Co-authored-by: Artyom Dubinin <[email protected]>
1 parent cce5219 commit ba4661f

File tree

3 files changed

+110
-13
lines changed

3 files changed

+110
-13
lines changed

README.md

Lines changed: 49 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+
// Tarantool URI, optional. Default is "localhost"
171+
.withRouterHost("localhost")
172+
// Binary port, optional. Default is 3301
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,41 @@ 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+
nothing that almost all build arguments listed here are passed into environment variables of the same name. At the
205+
moment, 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 is `cartridge`. **Only as a build argument.**
209+
- `TARANTOOL_WORKDIR` - a directory where all data will be stored: snapshots, wal logs and cartridge config files.
210+
Defaults is `/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 is `/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 is `/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 is `/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 is `./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. See example: https://github.com/tarantool/testcontainers-java-tarantool/blob/355d1e985bd10beca83bc7ca77f919a288709419/src/test/java/org/testcontainers/containers/TarantoolCartridgeBootstrapFromLuaWithFixedPortsTest.java#L111-L119
224+
225+
###### Environment variables:
226+
227+
To set an environment variable, use the `withEnv(...)` method of testcontainers API. Full list of variables the
228+
environments used in cartridge can be found here [link](https://www.tarantool.io/ru/doc/2.11/book/cartridge/cartridge_api/modules/cartridge/#cfg-opts-box-opts).
229+
230+
***Note:*** As shown in the previous section, some build arguments are converted to environment variables and used to
231+
cartridge build at the image build stage.
232+
233+
An example of how to set the `TARANTOOL_CLUSTER_COOKIE` parameter: https://github.com/tarantool/testcontainers-java-tarantool/blob/355d1e985bd10beca83bc7ca77f919a288709419/src/test/java/org/testcontainers/containers/TarantoolCartridgeBootstrapFromLuaWithFixedPortsTest.java#L57-L82
234+
196235
## License
197236

198237
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 isEnvInStdout(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.isEnvInStdout(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.isEnvInStdout(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)