1
1
package org .testcontainers .containers ;
2
2
3
- import org .testcontainers .containers .wait .strategy .HttpWaitStrategy ;
3
+ import org .testcontainers .containers .wait .strategy .Wait ;
4
+ import org .testcontainers .containers .wait .strategy .WaitAllStrategy ;
4
5
import org .testcontainers .utility .ComparableVersion ;
5
6
import org .testcontainers .utility .DockerImageName ;
6
7
@@ -66,19 +67,31 @@ public CockroachContainer(final String dockerImageName) {
66
67
public CockroachContainer (final DockerImageName dockerImageName ) {
67
68
super (dockerImageName );
68
69
dockerImageName .assertCompatibleWith (DEFAULT_IMAGE_NAME );
69
- isVersionGreaterThanOrEqualTo221 = isVersionGreaterThanOrEqualTo221 (dockerImageName );
70
+ this . isVersionGreaterThanOrEqualTo221 = isVersionGreaterThanOrEqualTo221 (dockerImageName );
70
71
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 ))
78
75
);
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 );
79
82
withCommand ("start-single-node --insecure" );
80
83
}
81
84
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
+
82
95
@ Override
83
96
public String getDriverClassName () {
84
97
return JDBC_DRIVER_CLASS_NAME ;
@@ -123,21 +136,21 @@ public String getTestQueryString() {
123
136
public CockroachContainer withUsername (String username ) {
124
137
validateIfVersionSupportsUsernameOrPasswordOrDatabase ("username" );
125
138
this .username = username ;
126
- return withEnv ( "COCKROACH_USER" , username ) ;
139
+ return this ;
127
140
}
128
141
129
142
@ Override
130
143
public CockroachContainer withPassword (String password ) {
131
144
validateIfVersionSupportsUsernameOrPasswordOrDatabase ("password" );
132
145
this .password = password ;
133
- return withEnv ( "COCKROACH_PASSWORD" , password ). withCommand ( "start-single-node" ) ;
146
+ return this ;
134
147
}
135
148
136
149
@ Override
137
150
public CockroachContainer withDatabaseName (final String databaseName ) {
138
151
validateIfVersionSupportsUsernameOrPasswordOrDatabase ("databaseName" );
139
152
this .databaseName = databaseName ;
140
- return withEnv ( "COCKROACH_DATABASE" , databaseName ) ;
153
+ return this ;
141
154
}
142
155
143
156
private boolean isVersionGreaterThanOrEqualTo221 (DockerImageName dockerImageName ) {
@@ -152,4 +165,9 @@ private void validateIfVersionSupportsUsernameOrPasswordOrDatabase(String parame
152
165
);
153
166
}
154
167
}
168
+
169
+ @ Override
170
+ protected void waitUntilContainerStarted () {
171
+ getWaitStrategy ().waitUntilReady (this );
172
+ }
155
173
}
0 commit comments