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 ;
8
7
9
8
import java .net .URL ;
10
- import java .util .Arrays ;
11
- import java .util .Collections ;
12
- import java .util .HashMap ;
13
- import java .util .List ;
14
- import java .util .Map ;
15
- import java .util .concurrent .CompletableFuture ;
9
+ import java .util .*;
16
10
import java .util .concurrent .ExecutionException ;
17
11
import java .util .concurrent .TimeoutException ;
18
12
import java .util .function .Supplier ;
82
76
* specified in the http_port options, will be exposed.
83
77
*
84
78
* @author Alexey Kuzin
79
+ * @authorm Artyom Dubinin
80
+ * @author Ivan Dneprov
81
+ *
85
82
*/
86
83
public class TarantoolCartridgeContainer extends GenericContainer <TarantoolCartridgeContainer >
87
84
implements TarantoolContainerOperations <TarantoolCartridgeContainer > {
@@ -119,6 +116,9 @@ public class TarantoolCartridgeContainer extends GenericContainer<TarantoolCartr
119
116
private String directoryResourcePath = SCRIPT_RESOURCE_DIRECTORY ;
120
117
private String instanceDir = INSTANCE_DIR ;
121
118
private String topologyConfigurationFile ;
119
+ private Boolean sslIsActive = false ;
120
+ private String keyFile = "" ;
121
+ private String certFile = "" ;
122
122
123
123
/**
124
124
* Create a container with default image and specified instances file from the classpath resources. Assumes that
@@ -246,6 +246,33 @@ private static ImageFromDockerfile buildImage(String dockerFile, String buildIma
246
246
return new ImageFromDockerfile ().withFileFromClasspath ("Dockerfile" , dockerFile );
247
247
}
248
248
249
+ /**
250
+ * Specify SSL as connection transport.
251
+ * Warning! SSL must be set as default transport on your tarantool cluster.
252
+ * Supported only in Tarantool Enterprise
253
+ *
254
+ * @return this container instance
255
+ */
256
+ public TarantoolCartridgeContainer withSsl () {
257
+ checkNotRunning ();
258
+ this .sslIsActive = true ;
259
+ return this ;
260
+ }
261
+
262
+ /**
263
+ * Specify path to key and cert files inside your container for SSL connection.
264
+ * Warning! SSL must be set as default transport on your tarantool cluster.
265
+ * Supported only in Tarantool Enterprise
266
+ *
267
+ * @return this container instance
268
+ */
269
+ public TarantoolCartridgeContainer withKeyAndCertFiles (String keyFile , String certFile ) {
270
+ checkNotRunning ();
271
+ this .keyFile = keyFile ;
272
+ this .certFile = certFile ;
273
+ return this ;
274
+ }
275
+
249
276
/**
250
277
* Get the router host
251
278
*
@@ -328,6 +355,11 @@ public String getInstanceDir() {
328
355
return instanceDir ;
329
356
}
330
357
358
+ @ Override
359
+ public int getInternalPort () {
360
+ return routerPort ;
361
+ }
362
+
331
363
/**
332
364
* Get Cartridge router HTTP API hostname
333
365
*
@@ -489,7 +521,7 @@ private boolean setupTopology() {
489
521
490
522
} else {
491
523
try {
492
- List <?> res = executeScript (topologyConfigurationFile ). get ( );
524
+ ArrayList <?> res = executeScriptDecoded (topologyConfigurationFile );
493
525
if (res .size () >= 2 && res .get (1 ) != null && res .get (1 ) instanceof Map ) {
494
526
HashMap <?, ?> error = ((HashMap <?, ?>) res .get (1 ));
495
527
// that means topology already exists
@@ -501,10 +533,6 @@ private boolean setupTopology() {
501
533
if (e .getCause () instanceof TimeoutException ) {
502
534
return true ;
503
535
// Do nothing, the cluster is reloading
504
- } else if (e .getCause () instanceof TarantoolConnectionException ) {
505
- // Probably cluster is not ready
506
- logger ().error ("Failed to setup topology: {}" , e .getMessage ());
507
- return false ;
508
536
}
509
537
} else {
510
538
throw new CartridgeTopologyException (e );
@@ -530,7 +558,7 @@ private void retryingSetupTopology() {
530
558
531
559
private void bootstrapVshard () {
532
560
try {
533
- executeCommand (VSHARD_BOOTSTRAP_COMMAND ). get () ;
561
+ executeCommand (VSHARD_BOOTSTRAP_COMMAND );
534
562
} catch (Exception e ) {
535
563
logger ().error ("Failed to bootstrap vshard cluster" , e );
536
564
throw new RuntimeException (e );
@@ -580,8 +608,8 @@ private boolean routerIsUp() {
580
608
String healthyCmd = " local cartridge = package.loaded['cartridge']" +
581
609
" return assert(cartridge ~= nil)" ;
582
610
try {
583
- List <?> result = executeCommand (healthyCmd ). get ( );
584
- return ( Boolean ) result . get ( 0 );
611
+ Container . ExecResult result = executeCommand (healthyCmd );
612
+ return result . getStdout (). equals ( "--- \n - true \n ... \n \n " );
585
613
} catch (Exception e ) {
586
614
logger ().warn ("Error while waiting for router instance to be up: " + e .getMessage ());
587
615
return false ;
@@ -592,21 +620,31 @@ private boolean isCartridgeHealthy() {
592
620
String healthyCmd = " local cartridge = package.loaded['cartridge']" +
593
621
" return assert(cartridge) and assert(cartridge.is_healthy())" ;
594
622
try {
595
- List <?> result = executeCommand (healthyCmd ). get ( );
596
- return ( Boolean ) result . get ( 0 );
623
+ Container . ExecResult result = executeCommand (healthyCmd );
624
+ return result . getStdout (). equals ( "--- \n - true \n ... \n \n " );
597
625
} catch (Exception e ) {
598
626
logger ().warn ("Error while waiting for cartridge healthy state: " + e .getMessage ());
599
627
return false ;
600
628
}
601
629
}
602
630
603
631
@ Override
604
- public CompletableFuture <List <?>> executeScript (String scriptResourcePath ) throws Exception {
605
- return clientHelper .executeScript (scriptResourcePath );
632
+ public Container .ExecResult executeScript (String scriptResourcePath ) throws Exception {
633
+ return clientHelper .executeScript (scriptResourcePath , this .sslIsActive , this .keyFile , this .certFile );
634
+ }
635
+
636
+ @ Override
637
+ public <T > T executeScriptDecoded (String scriptResourcePath ) throws Exception {
638
+ return clientHelper .executeScriptDecoded (scriptResourcePath , this .sslIsActive , this .keyFile , this .certFile );
639
+ }
640
+
641
+ @ Override
642
+ public Container .ExecResult executeCommand (String command ) throws Exception {
643
+ return clientHelper .executeCommand (command , this .sslIsActive , this .keyFile , this .certFile );
606
644
}
607
645
608
646
@ Override
609
- public CompletableFuture < List <?>> executeCommand (String command , Object ... arguments ) throws Exception {
610
- return clientHelper .executeCommand (command , arguments );
647
+ public < T > T executeCommandDecoded (String command ) throws Exception {
648
+ return clientHelper .executeCommandDecoded (command , this . sslIsActive , this . keyFile , this . certFile );
611
649
}
612
650
}
0 commit comments