@@ -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 ;
0 commit comments