Search in sources :

Example 1 with ResultCallback

use of com.github.dockerjava.api.async.ResultCallback in project dockerunit by qzagarese.

the class DefaultServiceBuilder method pullImage.

private CompletableFuture<Void> pullImage(CreateContainerCmd cmd, DockerClient client) {
    PullImageCmd pullImageCmd = client.pullImageCmd(cmd.getImage());
    CompletableFuture<Void> pullFut = new CompletableFuture<Void>();
    ResultCallback<PullResponseItem> resultCallback = new ResultCallback<PullResponseItem>() {

        private Closeable closeable;

        @Override
        public void close() throws IOException {
            try {
                closeable.close();
            } catch (IOException e) {
                throw new RuntimeException("Cannot close closeable " + closeable, e);
            }
        }

        @Override
        public void onStart(Closeable closeable) {
            this.closeable = closeable;
        }

        @Override
        public void onNext(PullResponseItem object) {
            if (object.getId() != null) {
                logger.info("Pulling image " + object.getId() + "...");
            }
        }

        @Override
        public void onError(Throwable throwable) {
            pullFut.completeExceptionally(new RuntimeException("Failed pulling image " + cmd.getImage(), throwable));
        }

        @Override
        public void onComplete() {
            pullFut.complete(null);
        }
    };
    pullImageCmd.exec(resultCallback);
    return pullFut;
}
Also used : ResultCallback(com.github.dockerjava.api.async.ResultCallback) CompletableFuture(java.util.concurrent.CompletableFuture) PullImageCmd(com.github.dockerjava.api.command.PullImageCmd) Closeable(java.io.Closeable) IOException(java.io.IOException) PullResponseItem(com.github.dockerjava.api.model.PullResponseItem)

Example 2 with ResultCallback

use of com.github.dockerjava.api.async.ResultCallback in project vespa by vespa-engine.

the class DockerImplTest method pullImageAsyncIfNeededSuccessfully.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void pullImageAsyncIfNeededSuccessfully() {
    final DockerImage image = new DockerImage("test:1.2.3");
    InspectImageResponse inspectImageResponse = mock(InspectImageResponse.class);
    when(inspectImageResponse.getId()).thenReturn(image.asString());
    InspectImageCmd imageInspectCmd = mock(InspectImageCmd.class);
    when(imageInspectCmd.exec()).thenThrow(new NotFoundException("Image not found")).thenReturn(inspectImageResponse);
    // Array to make it final
    ArgumentCaptor<ResultCallback> resultCallback = ArgumentCaptor.forClass(ResultCallback.class);
    PullImageCmd pullImageCmd = mock(PullImageCmd.class);
    when(pullImageCmd.exec(resultCallback.capture())).thenReturn(null);
    final DockerClient dockerClient = mock(DockerClient.class);
    when(dockerClient.inspectImageCmd(image.asString())).thenReturn(imageInspectCmd);
    when(dockerClient.pullImageCmd(eq(image.asString()))).thenReturn(pullImageCmd);
    final DockerImpl docker = new DockerImpl(dockerClient);
    docker.setMetrics(new MetricReceiverWrapper(MetricReceiver.nullImplementation));
    assertTrue("Should return true, we just scheduled the pull", docker.pullImageAsyncIfNeeded(image));
    assertTrue("Should return true, the pull i still ongoing", docker.pullImageAsyncIfNeeded(image));
    assertTrue(docker.imageIsDownloaded(image));
    resultCallback.getValue().onComplete();
    assertFalse(docker.pullImageAsyncIfNeeded(image));
}
Also used : InspectImageCmd(com.github.dockerjava.api.command.InspectImageCmd) MetricReceiverWrapper(com.yahoo.vespa.hosted.dockerapi.metrics.MetricReceiverWrapper) ExecStartResultCallback(com.github.dockerjava.core.command.ExecStartResultCallback) ResultCallback(com.github.dockerjava.api.async.ResultCallback) DockerClient(com.github.dockerjava.api.DockerClient) PullImageCmd(com.github.dockerjava.api.command.PullImageCmd) InspectImageResponse(com.github.dockerjava.api.command.InspectImageResponse) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) Test(org.junit.Test)

Example 3 with ResultCallback

use of com.github.dockerjava.api.async.ResultCallback in project vespa by vespa-engine.

the class DockerImplTest method pullImageAsyncIfNeededWithError.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void pullImageAsyncIfNeededWithError() {
    final DockerImage image = new DockerImage("test:1.2.3");
    InspectImageCmd imageInspectCmd = mock(InspectImageCmd.class);
    when(imageInspectCmd.exec()).thenThrow(new NotFoundException("Image not found"));
    // Array to make it final
    ArgumentCaptor<ResultCallback> resultCallback = ArgumentCaptor.forClass(ResultCallback.class);
    PullImageCmd pullImageCmd = mock(PullImageCmd.class);
    when(pullImageCmd.exec(resultCallback.capture())).thenReturn(null);
    final DockerClient dockerClient = mock(DockerClient.class);
    when(dockerClient.inspectImageCmd(image.asString())).thenReturn(imageInspectCmd);
    when(dockerClient.pullImageCmd(eq(image.asString()))).thenReturn(pullImageCmd);
    final DockerImpl docker = new DockerImpl(dockerClient);
    docker.setMetrics(new MetricReceiverWrapper(MetricReceiver.nullImplementation));
    assertTrue("Should return true, we just scheduled the pull", docker.pullImageAsyncIfNeeded(image));
    assertTrue("Should return true, the pull i still ongoing", docker.pullImageAsyncIfNeeded(image));
    try {
        resultCallback.getValue().onComplete();
    } catch (Exception ignored) {
    }
    assertFalse(docker.imageIsDownloaded(image));
    assertTrue("Should return true, new pull scheduled", docker.pullImageAsyncIfNeeded(image));
}
Also used : InspectImageCmd(com.github.dockerjava.api.command.InspectImageCmd) MetricReceiverWrapper(com.yahoo.vespa.hosted.dockerapi.metrics.MetricReceiverWrapper) ExecStartResultCallback(com.github.dockerjava.core.command.ExecStartResultCallback) ResultCallback(com.github.dockerjava.api.async.ResultCallback) DockerClient(com.github.dockerjava.api.DockerClient) PullImageCmd(com.github.dockerjava.api.command.PullImageCmd) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) Test(org.junit.Test)

Aggregations

ResultCallback (com.github.dockerjava.api.async.ResultCallback)3 PullImageCmd (com.github.dockerjava.api.command.PullImageCmd)3 DockerClient (com.github.dockerjava.api.DockerClient)2 InspectImageCmd (com.github.dockerjava.api.command.InspectImageCmd)2 NotFoundException (com.github.dockerjava.api.exception.NotFoundException)2 ExecStartResultCallback (com.github.dockerjava.core.command.ExecStartResultCallback)2 MetricReceiverWrapper (com.yahoo.vespa.hosted.dockerapi.metrics.MetricReceiverWrapper)2 Test (org.junit.Test)2 InspectImageResponse (com.github.dockerjava.api.command.InspectImageResponse)1 PullResponseItem (com.github.dockerjava.api.model.PullResponseItem)1 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1