11package org .testcontainers .containers ;
22
3- import org .testcontainers .containers .wait .strategy .HttpWaitStrategy ;
3+ import org .testcontainers .containers .wait .strategy .Wait ;
4+ import org .testcontainers .containers .wait .strategy .WaitAllStrategy ;
45import org .testcontainers .utility .ComparableVersion ;
56import org .testcontainers .utility .DockerImageName ;
67
@@ -66,19 +67,31 @@ public CockroachContainer(final String dockerImageName) {
6667 public CockroachContainer (final DockerImageName dockerImageName ) {
6768 super (dockerImageName );
6869 dockerImageName .assertCompatibleWith (DEFAULT_IMAGE_NAME );
69- isVersionGreaterThanOrEqualTo221 = isVersionGreaterThanOrEqualTo221 (dockerImageName );
70+ this . isVersionGreaterThanOrEqualTo221 = isVersionGreaterThanOrEqualTo221 (dockerImageName );
7071
71- withExposedPorts (REST_API_PORT , DB_PORT );
72- waitingFor (
73- new HttpWaitStrategy ()
74- .forPath ("/health" )
75- .forPort (REST_API_PORT )
76- .forStatusCode (200 )
77- .withStartupTimeout (Duration .ofMinutes (1 ))
72+ WaitAllStrategy waitStrategy = new WaitAllStrategy ();
73+ waitStrategy .withStrategy (
74+ Wait .forHttp ("/health" ).forPort (REST_API_PORT ).forStatusCode (200 ).withStartupTimeout (Duration .ofMinutes (1 ))
7875 );
76+ if (this .isVersionGreaterThanOrEqualTo221 ) {
77+ waitStrategy .withStrategy (Wait .forSuccessfulCommand ("[ -f ./init_success ] || { exit 1; }" ));
78+ }
79+
80+ withExposedPorts (REST_API_PORT , DB_PORT );
81+ waitingFor (waitStrategy );
7982 withCommand ("start-single-node --insecure" );
8083 }
8184
85+ @ Override
86+ protected void configure () {
87+ withEnv ("COCKROACH_USER" , this .username );
88+ withEnv ("COCKROACH_PASSWORD" , this .password );
89+ if (this .password != null && !this .password .isEmpty ()) {
90+ withCommand ("start-single-node" );
91+ }
92+ withEnv ("COCKROACH_DATABASE" , this .databaseName );
93+ }
94+
8295 @ Override
8396 public String getDriverClassName () {
8497 return JDBC_DRIVER_CLASS_NAME ;
@@ -123,21 +136,21 @@ public String getTestQueryString() {
123136 public CockroachContainer withUsername (String username ) {
124137 validateIfVersionSupportsUsernameOrPasswordOrDatabase ("username" );
125138 this .username = username ;
126- return withEnv ( "COCKROACH_USER" , username ) ;
139+ return this ;
127140 }
128141
129142 @ Override
130143 public CockroachContainer withPassword (String password ) {
131144 validateIfVersionSupportsUsernameOrPasswordOrDatabase ("password" );
132145 this .password = password ;
133- return withEnv ( "COCKROACH_PASSWORD" , password ). withCommand ( "start-single-node" ) ;
146+ return this ;
134147 }
135148
136149 @ Override
137150 public CockroachContainer withDatabaseName (final String databaseName ) {
138151 validateIfVersionSupportsUsernameOrPasswordOrDatabase ("databaseName" );
139152 this .databaseName = databaseName ;
140- return withEnv ( "COCKROACH_DATABASE" , databaseName ) ;
153+ return this ;
141154 }
142155
143156 private boolean isVersionGreaterThanOrEqualTo221 (DockerImageName dockerImageName ) {
@@ -152,4 +165,9 @@ private void validateIfVersionSupportsUsernameOrPasswordOrDatabase(String parame
152165 );
153166 }
154167 }
168+
169+ @ Override
170+ protected void waitUntilContainerStarted () {
171+ getWaitStrategy ().waitUntilReady (this );
172+ }
155173}
0 commit comments