22
33import com .github .dockerjava .api .DockerClient ;
44import com .github .dockerjava .api .DockerClientDelegate ;
5- import com .github .dockerjava .api .async .ResultCallback ;
65import com .github .dockerjava .api .command .CreateContainerCmd ;
76import com .github .dockerjava .api .command .PullImageCmd ;
87import com .github .dockerjava .api .exception .DockerClientException ;
98import com .github .dockerjava .api .exception .InternalServerErrorException ;
109import com .github .dockerjava .api .exception .NotFoundException ;
1110import com .github .dockerjava .api .model .AccessMode ;
1211import com .github .dockerjava .api .model .Bind ;
13- import com .github .dockerjava .api .model .Frame ;
1412import com .github .dockerjava .api .model .Info ;
1513import com .github .dockerjava .api .model .Version ;
1614import com .github .dockerjava .api .model .Volume ;
3129import org .testcontainers .utility .DockerImageName ;
3230import org .testcontainers .utility .MountableFile ;
3331import org .testcontainers .utility .ResourceReaper ;
34- import org .testcontainers .utility .RyukResourceReaper ;
3532import org .testcontainers .utility .TestcontainersConfiguration ;
3633
37- import java .io .ByteArrayOutputStream ;
38- import java .io .IOException ;
3934import java .io .InputStream ;
4035import java .net .URI ;
4136import java .util .ArrayList ;
4237import java .util .HashMap ;
4338import java .util .List ;
4439import java .util .Map ;
45- import java .util .Optional ;
4640import java .util .ServiceLoader ;
4741import java .util .UUID ;
4842import java .util .function .BiFunction ;
@@ -80,16 +74,10 @@ public class DockerClientFactory {
8074 RuntimeException cachedClientFailure ;
8175
8276 private String activeApiVersion ;
83- private String activeExecutionDriver ;
8477
8578 @ Getter (lazy = true )
8679 private final boolean fileMountingSupported = checkMountableFile ();
8780
88-
89- static {
90- System .setProperty ("org.testcontainers.shaded.io.netty.packagePrefix" , "org.testcontainers.shaded." );
91- }
92-
9381 @ VisibleForTesting
9482 DockerClientFactory () {
9583
@@ -200,20 +188,19 @@ public void close() {
200188 log .info ("Docker host IP address is {}" , strategy .getDockerHostIpAddress ());
201189
202190 Info dockerInfo = strategy .getInfo ();
191+ log .debug ("Docker info: {}" , dockerInfo .getRawValues ());
203192 Version version = client .versionCmd ().exec ();
193+ log .debug ("Docker version: {}" , version .getRawValues ());
204194 activeApiVersion = version .getApiVersion ();
205- activeExecutionDriver = dockerInfo .getExecutionDriver ();
206195 log .info ("Connected to docker: \n " +
207196 " Server Version: " + dockerInfo .getServerVersion () + "\n " +
208197 " API Version: " + activeApiVersion + "\n " +
209198 " Operating System: " + dockerInfo .getOperatingSystem () + "\n " +
210199 " Total Memory: " + dockerInfo .getMemTotal () / (1024 * 1024 ) + " MB" );
211200
212- final ResourceReaper resourceReaper ;
213201 try {
214- resourceReaper = ResourceReaper .instance ();
215202 //noinspection deprecation
216- resourceReaper .init ();
203+ ResourceReaper . instance () .init ();
217204 } catch (RuntimeException e ) {
218205 cachedClientFailure = e ;
219206 throw e ;
@@ -226,27 +213,6 @@ public void close() {
226213 try {
227214 log .info ("Checking the system..." );
228215 checkDockerVersion (version .getVersion ());
229-
230- //noinspection deprecation
231- String ryukContainerId = resourceReaper instanceof RyukResourceReaper
232- ? ((RyukResourceReaper ) resourceReaper ).getContainerId ()
233- : null ;
234-
235- if (ryukContainerId != null ) {
236- checkDiskSpace (client , ryukContainerId );
237- } else {
238- runInsideDocker (
239- createContainerCmd -> {
240- createContainerCmd .withName ("testcontainers-checks-" + SESSION_ID );
241- createContainerCmd .getHostConfig ().withAutoRemove (true );
242- createContainerCmd .withCmd ("tail" , "-f" , "/dev/null" );
243- },
244- (__ , containerId ) -> {
245- checkDiskSpace (client , containerId );
246- return "" ;
247- }
248- );
249- }
250216 } catch (RuntimeException e ) {
251217 cachedClientFailure = e ;
252218 throw e ;
@@ -263,43 +229,6 @@ private void checkDockerVersion(String dockerVersion) {
263229 check ("Docker server version should be at least 1.6.0" , versionIsSufficient );
264230 }
265231
266- private void checkDiskSpace (DockerClient dockerClient , String id ) {
267- ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
268-
269- try {
270- dockerClient
271- .execStartCmd (dockerClient .execCreateCmd (id ).withAttachStdout (true ).withCmd ("df" , "-P" ).exec ().getId ())
272- .exec (new ResultCallback .Adapter <Frame >() {
273- @ Override
274- public void onNext (Frame frame ) {
275- if (frame == null ) {
276- return ;
277- }
278- switch (frame .getStreamType ()) {
279- case RAW :
280- case STDOUT :
281- try {
282- outputStream .write (frame .getPayload ());
283- outputStream .flush ();
284- } catch (IOException e ) {
285- onError (e );
286- }
287- }
288- }
289- })
290- .awaitCompletion ();
291- } catch (Exception e ) {
292- log .debug ("Can't exec disk checking command" , e );
293- }
294-
295- DiskSpaceUsage df = parseAvailableDiskSpace (outputStream .toString ());
296-
297- check (
298- "Docker environment should have more than 2GB free disk space" ,
299- df .availableMB .map (it -> it >= 2048 ).orElse (true )
300- );
301- }
302-
303232 private void check (String message , boolean isSuccessful ) {
304233 if (isSuccessful ) {
305234 log .info ("\u2714 \ufe0e {}" , message );
@@ -389,28 +318,6 @@ <T> T runInsideDocker(DockerImageName imageName, Consumer<CreateContainerCmd> cr
389318 }
390319 }
391320
392- @ VisibleForTesting
393- static class DiskSpaceUsage {
394- Optional <Long > availableMB = Optional .empty ();
395- Optional <Integer > usedPercent = Optional .empty ();
396- }
397-
398- @ VisibleForTesting
399- DiskSpaceUsage parseAvailableDiskSpace (String dfOutput ) {
400- DiskSpaceUsage df = new DiskSpaceUsage ();
401- String [] lines = dfOutput .split ("\n " );
402- for (String line : lines ) {
403- String [] fields = line .split ("\\ s+" );
404- if (fields .length > 5 && fields [5 ].equals ("/" )) {
405- long availableKB = Long .parseLong (fields [3 ]);
406- df .availableMB = Optional .of (availableKB / 1024L );
407- df .usedPercent = Optional .of (Integer .valueOf (fields [4 ].replace ("%" , "" )));
408- break ;
409- }
410- }
411- return df ;
412- }
413-
414321 /**
415322 * @return the docker API version of the daemon that we have connected to
416323 */
@@ -423,8 +330,7 @@ public String getActiveApiVersion() {
423330 * @return the docker execution driver of the daemon that we have connected to
424331 */
425332 public String getActiveExecutionDriver () {
426- client ();
427- return activeExecutionDriver ;
333+ return getInfo ().getExecutionDriver ();
428334 }
429335
430336 /**
0 commit comments