11package org .parallaxsecond .testcontainers ;
2- import lombok .SneakyThrows ;
3- import lombok .extern .slf4j .Slf4j ;
4- import org .junit .jupiter .api .Assertions ;
5- import org .testcontainers .containers .BindMode ;
6- import org .testcontainers .containers .GenericContainer ;
7- import org .testcontainers .utility .DockerImageName ;
82
93import java .net .ServerSocket ;
104import java .net .URI ;
115import java .nio .file .Files ;
126import java .nio .file .Path ;
137import java .nio .file .attribute .PosixFilePermissions ;
8+ import java .time .Duration ;
9+ import static java .util .Collections .singletonList ;
1410import java .util .Locale ;
1511import java .util .concurrent .ExecutorService ;
1612import java .util .concurrent .Executors ;
1713
18- import static java .util .Collections .singletonList ;
14+ import org .junit .jupiter .api .Assertions ;
15+ import org .slf4j .LoggerFactory ;
16+ import org .testcontainers .containers .GenericContainer ;
17+ import org .testcontainers .containers .output .Slf4jLogConsumer ;
18+ import org .testcontainers .containers .wait .strategy .ShellStrategy ;
19+ import org .testcontainers .shaded .org .awaitility .Awaitility ;
20+ import org .testcontainers .utility .DockerImageName ;
21+
22+ import lombok .SneakyThrows ;
23+ import lombok .extern .slf4j .Slf4j ;
1924
2025@ Slf4j
2126public class ParsecContainer extends GenericContainer <ParsecContainer > {
2227 private static final String IMAGE_NAME = "parallaxsecond/parsec" ;
2328 private static final String PARSEC_SOCKET_FILE = "parsec.sock" ;
24- private static final String PARSEC_RUN_DIR = "/run/ parsec/" ;
29+ private static final String PARSEC_RUN_DIR = "/parsec/quickstart /" ;
2530 private static final String PARSEC_SOCKET = PARSEC_RUN_DIR + PARSEC_SOCKET_FILE ;
2631 private static final int PARSEC_TCP_PORT = 4444 ;
2732 private final int localPort = findFreePort ();
@@ -30,20 +35,47 @@ public class ParsecContainer extends GenericContainer<ParsecContainer> {
3035 private Process cmd ;
3136 private ExecutorService executor ;
3237
38+ public Path runDir ;
39+
3340 @ SneakyThrows
3441 public ParsecContainer (final DockerImageName dockerImageName ) {
3542 super (dockerImageName );
36- Path runDir = Files .createTempDirectory ("ps" );
43+ this .setWaitStrategy (new ShellStrategy ().withCommand ("parsec-tool ping" )
44+ .withStartupTimeout (Duration .ofSeconds (10 )));
45+ runDir = Files .createTempDirectory ("parsec_" );
3746 Files .setPosixFilePermissions (runDir , PosixFilePermissions .fromString ("rwxrwxrwx" ));
3847 this .parsecSock = runDir .resolve (PARSEC_SOCKET_FILE );
3948 this .parsecSockSocat = runDir .resolve ("ps_socat.sock" );
40- this .withFileSystemBind (
41- runDir .toFile ().getAbsoluteFile ().getAbsolutePath (), PARSEC_RUN_DIR , BindMode .READ_WRITE );
49+
4250 this .setPortBindings (singletonList (localPort + ":" + PARSEC_TCP_PORT ));
51+ this .withLogConsumer (new Slf4jLogConsumer (LoggerFactory .getLogger (ParsecContainer .class )));
52+ }
53+
54+ public boolean isRunning () {
55+ if (getContainerId () == null ) {
56+ return false ;
57+ }
58+
59+ try {
60+ Boolean running = getCurrentContainerInfo ().getState ().getRunning ();
61+ return Boolean .TRUE .equals (running );
62+ } catch (Throwable e ) {
63+ return false ;
64+ }
4365 }
4466
67+ @ SneakyThrows
4568 public static ParsecContainer withVersion (String version ) {
46- return new ParsecContainer (DockerImageName .parse (IMAGE_NAME + ":" + version ));
69+ ParsecContainer parsecContainer =
70+ new ParsecContainer (DockerImageName .parse (IMAGE_NAME + ":" + version ));
71+
72+ parsecContainer .start ();
73+
74+ // Wait for container to be running
75+ Awaitility .await ().until (parsecContainer ::isRunning );
76+
77+
78+ return parsecContainer ;
4779 }
4880
4981 @ SneakyThrows
@@ -63,20 +95,20 @@ public URI getSocketUri() {
6395
6496 @ Override
6597 public void start () {
66- super .start ();
6798 if (isOsx ()) {
6899 useSocat ();
100+ } else {
69101 }
102+ super .start ();
70103 }
104+
71105 private static boolean isOsx () {
72106 String os = System .getProperty ("os.name" , "generic" ).toLowerCase (Locale .ENGLISH );
73107 return ((os .contains ("mac" )) || (os .contains ("darwin" )));
74108 }
75109
76-
77110 @ Override
78111 public void close () {
79- super .close ();
80112 if (this .cmd != null ) {
81113 this .cmd .destroyForcibly ();
82114 this .cmd = null ;
@@ -90,29 +122,21 @@ public void close() {
90122 @ SneakyThrows
91123 public void useSocat () {
92124 executor = Executors .newSingleThreadExecutor ();
93- executor .submit (
94- () -> {
95- log .info ("starting socat in docker container" );
96- ParsecContainer .this .execInContainer (
97- "socat" ,
98- "TCP4-LISTEN:" + PARSEC_TCP_PORT + ",reuseaddr,fork" ,
99- "UNIX-CONNECT:" + PARSEC_SOCKET );
100- log .info ("started socat in docker container" );
101- return null ;
102- });
103-
104- log .info ("starting socat on local machine" );
105- cmd =
106- Runtime .getRuntime ()
107- .exec (
108- new String [] {
109- "socat" ,
110- "UNIX-LISTEN:"
111- + this .parsecSockSocat .toFile ().getAbsolutePath ()
112- + ",fork,reuseaddr" ,
113- "TCP4:localhost:" + this .localPort
114- });
115- log .info ("started socat on local machine" );
125+ executor .submit (() -> {
126+ ParsecContainer .this .execInContainer ("socat" , "-d" , "-d" , // Add debug output
127+ "TCP4-LISTEN:" + PARSEC_TCP_PORT + ",reuseaddr,fork" , "UNIX-CONNECT:" + PARSEC_SOCKET );
128+ return null ;
129+ });
130+
131+ cmd = Runtime .getRuntime ().exec (new String [] {"socat" , "-d" , "-d" , // Add debug output
132+ "UNIX-LISTEN:" + this .parsecSockSocat .toFile ().getAbsolutePath () + ",fork,reuseaddr" ,
133+ "TCP4:localhost:" + this .localPort });
134+
135+ // wait until this.parsecSockSocat is created
136+ Awaitility .await ().until (() -> {
137+ return Files .exists (this .parsecSockSocat );
138+ });
139+
116140 }
117141
118142 @ SneakyThrows
@@ -121,6 +145,7 @@ public void parsecTool(String... args) {
121145 System .arraycopy (args , 0 , args_ , 1 , args .length );
122146 args_ [0 ] = "parsec-tool" ;
123147 ExecResult r = execInContainer (args_ );
124- Assertions .assertEquals (0 , r .getExitCode (), getLogs () + "\n " + r .getStdout () + "\n " + r .getStderr ());
148+ Assertions .assertEquals (0 , r .getExitCode (),
149+ getLogs () + "\n " + r .getStdout () + "\n " + r .getStderr ());
125150 }
126151}
0 commit comments