1
1
package org .testcontainers .containers ;
2
2
3
3
import com .github .dockerjava .api .command .InspectContainerResponse ;
4
- import io .tarantool .driver .exceptions .TarantoolConnectionException ;
5
4
6
5
import org .testcontainers .containers .exceptions .CartridgeTopologyException ;
7
6
import org .testcontainers .images .builder .ImageFromDockerfile ;
12
11
import java .util .HashMap ;
13
12
import java .util .List ;
14
13
import java .util .Map ;
15
- import java .util .concurrent .CompletableFuture ;
16
14
import java .util .concurrent .ExecutionException ;
17
15
import java .util .concurrent .TimeoutException ;
18
16
import java .util .function .Supplier ;
82
80
* specified in the http_port options, will be exposed.
83
81
*
84
82
* @author Alexey Kuzin
83
+ * @authorm Artyom Dubinin
84
+ * @author Ivan Dneprov
85
+ *
85
86
*/
86
87
public class TarantoolCartridgeContainer extends GenericContainer <TarantoolCartridgeContainer >
87
88
implements TarantoolContainerOperations <TarantoolCartridgeContainer > {
@@ -119,6 +120,7 @@ public class TarantoolCartridgeContainer extends GenericContainer<TarantoolCartr
119
120
private String directoryResourcePath = SCRIPT_RESOURCE_DIRECTORY ;
120
121
private String instanceDir = INSTANCE_DIR ;
121
122
private String topologyConfigurationFile ;
123
+ private SslContext sslContext ;
122
124
123
125
/**
124
126
* Create a container with default image and specified instances file from the classpath resources. Assumes that
@@ -189,6 +191,22 @@ public TarantoolCartridgeContainer(String dockerFile, String buildImageName, Str
189
191
this (buildImage (dockerFile , buildImageName , buildArgs ), instancesFile , topologyConfigurationFile , buildArgs );
190
192
}
191
193
194
+ // todo add SSL and mTLS cartridge test
195
+ // /**
196
+ // * Create a container with specified image and specified instances file from the classpath resources. By providing
197
+ // * the result Cartridge container image name, you can cache the image and avoid rebuilding on each test run (the
198
+ // * image is tagged with the provided name and not deleted after tests finishing).
199
+ // *
200
+ // * @param tarantoolImageParams params for cached image creating
201
+ // * @param instancesFile URL resource path to instances.yml relative in the classpath
202
+ // * @param topologyConfigurationFile URL resource path to a topology bootstrap script in the classpath
203
+ // */
204
+ // public TarantoolCartridgeContainer(TarantoolImageParams tarantoolImageParams, String instancesFile,
205
+ // String topologyConfigurationFile) {
206
+ // this(new ImageFromDockerfile(TarantoolContainerImageHelper.getImage(tarantoolImageParams)), instancesFile,
207
+ // topologyConfigurationFile, tarantoolImageParams.getBuildArgs());
208
+ // }
209
+
192
210
private TarantoolCartridgeContainer (ImageFromDockerfile image , String instancesFile , String topologyConfigurationFile ,
193
211
Map <String , String > buildArgs ) {
194
212
super (withBuildArgs (image , buildArgs ));
@@ -251,6 +269,20 @@ private static ImageFromDockerfile buildImage(String dockerFile, String buildIma
251
269
"cartridge" : buildArgs .get ("CARTRIDGE_SRC_DIR" ));
252
270
}
253
271
272
+ // todo add SSL and mTLS cartridge test
273
+ // /**
274
+ // * Specify SSL as connection transport. And path to key and cert files inside your container for mTLS connection
275
+ // * Warning! SSL must be set as default transport on your tarantool cluster.
276
+ // * Supported only in Tarantool Enterprise
277
+ // *
278
+ // * @return this container instance
279
+ // */
280
+ // public TarantoolCartridgeContainer withSslContext(SslContext sslContext) {
281
+ // checkNotRunning();
282
+ // this.sslContext = sslContext;
283
+ // return this;
284
+ // }
285
+
254
286
/**
255
287
* Get the router host
256
288
*
@@ -333,6 +365,11 @@ public String getInstanceDir() {
333
365
return instanceDir ;
334
366
}
335
367
368
+ @ Override
369
+ public int getInternalPort () {
370
+ return routerPort ;
371
+ }
372
+
336
373
/**
337
374
* Get Cartridge router HTTP API hostname
338
375
*
@@ -480,7 +517,7 @@ private boolean setupTopology() {
480
517
.substring (topologyConfigurationFile .lastIndexOf ('/' ) + 1 );
481
518
482
519
try {
483
- Container . ExecResult result = execInContainer ("cartridge" ,
520
+ ExecResult result = execInContainer ("cartridge" ,
484
521
"replicasets" ,
485
522
"--run-dir=" + TARANTOOL_RUN_DIR ,
486
523
"--file=" + replicasetsFileName , "setup" , "--bootstrap-vshard" );
@@ -494,7 +531,7 @@ private boolean setupTopology() {
494
531
495
532
} else {
496
533
try {
497
- List <?> res = executeScript (topologyConfigurationFile ). get ( );
534
+ List <?> res = executeScriptDecoded (topologyConfigurationFile );
498
535
if (res .size () >= 2 && res .get (1 ) != null && res .get (1 ) instanceof Map ) {
499
536
HashMap <?, ?> error = ((HashMap <?, ?>) res .get (1 ));
500
537
// that means topology already exists
@@ -506,10 +543,6 @@ private boolean setupTopology() {
506
543
if (e .getCause () instanceof TimeoutException ) {
507
544
return true ;
508
545
// Do nothing, the cluster is reloading
509
- } else if (e .getCause () instanceof TarantoolConnectionException ) {
510
- // Probably cluster is not ready
511
- logger ().error ("Failed to setup topology: {}" , e .getMessage ());
512
- return false ;
513
546
}
514
547
} else {
515
548
throw new CartridgeTopologyException (e );
@@ -535,7 +568,7 @@ private void retryingSetupTopology() {
535
568
536
569
private void bootstrapVshard () {
537
570
try {
538
- executeCommand (VSHARD_BOOTSTRAP_COMMAND ). get () ;
571
+ executeCommand (VSHARD_BOOTSTRAP_COMMAND );
539
572
} catch (Exception e ) {
540
573
logger ().error ("Failed to bootstrap vshard cluster" , e );
541
574
throw new RuntimeException (e );
@@ -583,10 +616,10 @@ private void waitUntilTrue(int secondsToWait, Supplier<Boolean> waitFunc) {
583
616
584
617
private boolean routerIsUp () {
585
618
String healthyCmd = " local cartridge = package.loaded['cartridge']" +
586
- " return assert( cartridge ~= nil) " ;
619
+ " return cartridge ~= nil" ;
587
620
try {
588
- List <?> result = executeCommand (healthyCmd ). get ( );
589
- return (Boolean ) result .get (0 );
621
+ List <?> result = executeCommandDecoded (healthyCmd );
622
+ return result . get ( 0 ). getClass () == Boolean . class && (Boolean ) result .get (0 );
590
623
} catch (Exception e ) {
591
624
logger ().warn ("Error while waiting for router instance to be up: " + e .getMessage ());
592
625
return false ;
@@ -595,23 +628,33 @@ private boolean routerIsUp() {
595
628
596
629
private boolean isCartridgeHealthy () {
597
630
String healthyCmd = " local cartridge = package.loaded['cartridge']" +
598
- " return assert( cartridge) and assert( cartridge.is_healthy() )" ;
631
+ " return cartridge ~= nil and cartridge.is_healthy()" ;
599
632
try {
600
- List <?> result = executeCommand (healthyCmd ). get ( );
601
- return (Boolean ) result .get (0 );
633
+ List <?> result = executeCommandDecoded (healthyCmd );
634
+ return result . get ( 0 ). getClass () == Boolean . class && (Boolean ) result .get (0 );
602
635
} catch (Exception e ) {
603
636
logger ().warn ("Error while waiting for cartridge healthy state: " + e .getMessage ());
604
637
return false ;
605
638
}
606
639
}
607
640
608
641
@ Override
609
- public CompletableFuture <List <?>> executeScript (String scriptResourcePath ) throws Exception {
610
- return clientHelper .executeScript (scriptResourcePath );
642
+ public ExecResult executeScript (String scriptResourcePath ) throws Exception {
643
+ return clientHelper .executeScript (scriptResourcePath , this .sslContext );
644
+ }
645
+
646
+ @ Override
647
+ public <T > T executeScriptDecoded (String scriptResourcePath ) throws Exception {
648
+ return clientHelper .executeScriptDecoded (scriptResourcePath , this .sslContext );
649
+ }
650
+
651
+ @ Override
652
+ public ExecResult executeCommand (String command ) throws Exception {
653
+ return clientHelper .executeCommand (command , this .sslContext );
611
654
}
612
655
613
656
@ Override
614
- public CompletableFuture < List <?>> executeCommand (String command , Object ... arguments ) throws Exception {
615
- return clientHelper .executeCommand (command , arguments );
657
+ public < T > T executeCommandDecoded (String command ) throws Exception {
658
+ return clientHelper .executeCommandDecoded (command , this . sslContext );
616
659
}
617
660
}
0 commit comments