Skip to content

Commit 871af8f

Browse files
authored
Configure cluster from yaml (#40)
Configure cluster from yaml
1 parent 5072e54 commit 871af8f

File tree

4 files changed

+123
-22
lines changed

4 files changed

+123
-22
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,4 @@
263263
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
264264
</repository>
265265
</distributionManagement>
266-
</project>
266+
</project>

src/main/java/org/testcontainers/containers/TarantoolCartridgeContainer.java

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,18 @@ public class TarantoolCartridgeContainer extends GenericContainer<TarantoolCartr
102102
private static final String ENV_TARANTOOL_RUNDIR = "TARANTOOL_RUNDIR";
103103
private static final String ENV_TARANTOOL_DATADIR = "TARANTOOL_DATADIR";
104104
private static final String ENV_TARANTOOL_INSTANCES_FILE = "TARANTOOL_INSTANCES_FILE";
105+
private final CartridgeConfigParser instanceFileParser;
106+
private final TarantoolContainerClientHelper clientHelper;
105107
private boolean useFixedPorts = false;
106-
107108
private String routerHost = ROUTER_HOST;
108109
private int routerPort = ROUTER_PORT;
109110
private int apiPort = API_PORT;
110111
private String routerUsername = CARTRIDGE_USERNAME;
111112
private String routerPassword = CARTRIDGE_PASSWORD;
112113
private String directoryResourcePath = SCRIPT_RESOURCE_DIRECTORY;
113114
private String instanceDir = INSTANCE_DIR;
114-
private final CartridgeConfigParser instanceFileParser;
115-
private final String topologyConfigurationFile;
116-
private final TarantoolContainerClientHelper clientHelper;
115+
private String topologyConfigurationFile;
116+
private String replicasetsFileName;
117117

118118
/**
119119
* Create a container with default image and specified instances file from the classpath resources. Assumes that
@@ -166,7 +166,6 @@ public TarantoolCartridgeContainer(String dockerFile, String buildImageName,
166166
this(dockerFile, buildImageName, instancesFile, topologyConfigurationFile, Collections.emptyMap());
167167
}
168168

169-
170169
/**
171170
* Create a container with specified image and specified instances file from the classpath resources. By providing
172171
* the result Cartridge container image name, you can cache the image and avoid rebuilding on each test run (the
@@ -186,13 +185,22 @@ public TarantoolCartridgeContainer(String dockerFile, String buildImageName, Str
186185
instancesFile, topologyConfigurationFile);
187186
}
188187

188+
189189
private TarantoolCartridgeContainer(Future<String> image, String instancesFile, String topologyConfigurationFile) {
190190
super(image);
191191
if (instancesFile == null || instancesFile.isEmpty()) {
192192
throw new IllegalArgumentException("Instance file name must not be null or empty");
193193
}
194+
if (topologyConfigurationFile == null || topologyConfigurationFile.isEmpty()) {
195+
throw new IllegalArgumentException("Topology configuration file must not be null or empty");
196+
}
197+
String fileType = topologyConfigurationFile.substring(topologyConfigurationFile.lastIndexOf('.') + 1);
198+
if (fileType.equals("lua")) {
199+
this.topologyConfigurationFile = topologyConfigurationFile;
200+
}else{
201+
this.replicasetsFileName = topologyConfigurationFile.substring(topologyConfigurationFile.lastIndexOf('/')+1);
202+
}
194203
this.instanceFileParser = new CartridgeConfigParser(instancesFile);
195-
this.topologyConfigurationFile = topologyConfigurationFile;
196204
this.clientHelper = new TarantoolContainerClientHelper(this);
197205
}
198206

@@ -435,7 +443,6 @@ protected void configure() {
435443
if (!getDirectoryBinding().isEmpty()) {
436444
withFileSystemBind(getDirectoryBinding(), getInstanceDir(), BindMode.READ_WRITE);
437445
}
438-
439446
if (useFixedPorts) {
440447
for (Integer port : instanceFileParser.getExposablePorts()) {
441448
addFixedExposedPort(port, port);
@@ -451,21 +458,55 @@ protected void containerIsStarting(InspectContainerResponse containerInfo) {
451458
}
452459

453460
private boolean setupTopology() {
454-
try {
455-
executeScript(topologyConfigurationFile).get();
456-
// The client connection will be closed after that command
457-
} catch (Exception e) {
458-
if (e instanceof ExecutionException) {
459-
if (e.getCause() instanceof TimeoutException) {
460-
return true;
461-
// Do nothing, the cluster is reloading
462-
} else if (e.getCause() instanceof TarantoolConnectionException) {
463-
// Probably cluster is not ready
464-
logger().error("Failed to setup topology: {}", e.getMessage());
465-
return false;
461+
if (topologyConfigurationFile == null) {
462+
String runDirPath = null;
463+
try {
464+
Container.ExecResult envVariablesContainer = this.execInContainer("env");
465+
String stdout = envVariablesContainer.getStdout();
466+
int exitCode = envVariablesContainer.getExitCode();
467+
if (exitCode != 0) {
468+
logger().error("Failed to bootstrap replica sets topology: {}", stdout);
469+
}
470+
int startInd = stdout.lastIndexOf(ENV_TARANTOOL_RUNDIR + "=");
471+
try {
472+
runDirPath = stdout.substring(startInd,
473+
stdout.indexOf('\n', startInd)).split("=")[1];
474+
} catch (Exception e) {
475+
logger().error("Missing dir-run environment variable: {}", e.getMessage());
476+
}
477+
478+
} catch (Exception e) {
479+
logger().error("Failed to get environment variables: {}", e.getMessage());
480+
}
481+
482+
try {
483+
Container.ExecResult lsResult = this.execInContainer("cartridge", "replicasets", "--run-dir=" + runDirPath,
484+
"--file=" + this.replicasetsFileName, "setup", "--bootstrap-vshard");
485+
String stdout = lsResult.getStdout();
486+
int exitCode = lsResult.getExitCode();
487+
if (exitCode != 0) {
488+
logger().error("Failed to bootstrap replica sets topology: {}", stdout);
489+
}
490+
} catch (Exception e) {
491+
logger().error("Failed to bootstrap replica sets topology: {}", e.getMessage());
492+
}
493+
} else {
494+
try {
495+
executeScript(topologyConfigurationFile).get();
496+
// The client connection will be closed after that command
497+
} catch (Exception e) {
498+
if (e instanceof ExecutionException) {
499+
if (e.getCause() instanceof TimeoutException) {
500+
return true;
501+
// Do nothing, the cluster is reloading
502+
} else if (e.getCause() instanceof TarantoolConnectionException) {
503+
// Probably cluster is not ready
504+
logger().error("Failed to setup topology: {}", e.getMessage());
505+
return false;
506+
}
507+
} else {
508+
throw new RuntimeException("Failed to change the app topology", e);
466509
}
467-
} else {
468-
throw new RuntimeException("Failed to change the app topology", e);
469510
}
470511
}
471512
return true;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.testcontainers.containers;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.slf4j.LoggerFactory;
5+
import org.testcontainers.containers.output.Slf4jLogConsumer;
6+
import org.testcontainers.containers.wait.strategy.Wait;
7+
import org.testcontainers.utility.MountableFile;
8+
9+
import java.time.Duration;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
public class TarantoolCartridgeContainerReplicasetsTest {
14+
15+
@Test
16+
public void test_ClusterContainer_StartsSuccessfully_ifFilesAreCopiedUnderRoot() throws Exception {
17+
Map<String, String> buildArgs = new HashMap<String, String>() {
18+
{
19+
put("TARANTOOL_SERVER_USER", "root");
20+
put("TARANTOOL_SERVER_UID", "0");
21+
put("TARANTOOL_SERVER_GROUP", "root");
22+
put("TARANTOOL_SERVER_GID", "0");
23+
}
24+
};
25+
TarantoolCartridgeContainer container =
26+
new TarantoolCartridgeContainer(
27+
"Dockerfile",
28+
"testcontainers-java-tarantool:test",
29+
"cartridge/instances.yml",
30+
"cartridge/replicasets.yml",
31+
buildArgs)
32+
.withCopyFileToContainer(MountableFile.forClasspathResource("cartridge"), "/app")
33+
.withStartupTimeout(Duration.ofSeconds(300))
34+
.withLogConsumer(new Slf4jLogConsumer(
35+
LoggerFactory.getLogger(TarantoolCartridgeContainerReplicasetsTest.class)));
36+
37+
container.start();
38+
CartridgeContainerTestUtils.executeProfileReplaceSmokeTest(container);
39+
if(container.isRunning())
40+
container.stop();
41+
}
42+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
router:
2+
instances:
3+
- router
4+
roles:
5+
- failover-coordinator
6+
- vshard-router
7+
- app.roles.custom
8+
- app.roles.api_router
9+
all_rw: false
10+
s-1:
11+
instances:
12+
- s1-master
13+
roles:
14+
- vshard-storage
15+
- app.roles.api_storage
16+
weight: 1
17+
all_rw: false
18+
vshard_group: default

0 commit comments

Comments
 (0)