Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.github.dockerjava.api.command;

import com.github.dockerjava.api.model.AuthConfigurations;
import com.github.dockerjava.api.model.EventStreamItem;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import com.github.dockerjava.api.model.AuthConfigurations;
import com.github.dockerjava.api.model.EventStreamItem;

/**
*
* Build an image from Dockerfile.
Expand Down Expand Up @@ -61,6 +61,9 @@ public interface BuildImageCmd extends DockerCmd<BuildImageCmd.Response>{
public static interface Exec extends DockerCmdExec<BuildImageCmd, BuildImageCmd.Response> {
}

/**
* @see {@link com.github.dockerjava.core.command.EventStreamReader}
*/
public static abstract class Response extends InputStream {
public abstract Iterable<EventStreamItem> getItems() throws IOException;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.dockerjava.api.command;

import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.core.command.EventStreamReader;

import java.io.InputStream;

Expand Down Expand Up @@ -33,6 +34,8 @@ public static interface Exec extends DockerCmdExec<PullImageCmd, InputStream> {
/**
* Its the responsibility of the caller to consume and/or close the {@link InputStream} to prevent
* connection leaks.
*
* @see {@link EventStreamReader}
*/
@Override
public InputStream exec();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.github.dockerjava.api.command;

import java.io.IOException;
import java.io.InputStream;

import com.github.dockerjava.api.NotFoundException;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.api.model.PushEventStreamItem;
import com.github.dockerjava.core.command.EventStreamReader;

import java.io.IOException;
import java.io.InputStream;

/**
* Push the latest image to the repository.
Expand Down Expand Up @@ -40,6 +41,9 @@ public interface PushImageCmd extends DockerCmd<PushImageCmd.Response>{
public static interface Exec extends DockerCmdExec<PushImageCmd, Response> {
}

/**
* @see {@link EventStreamReader}
*/
public static abstract class Response extends InputStream {
public abstract Iterable<PushEventStreamItem> getItems() throws IOException;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.github.dockerjava.api.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Objects;

import java.io.Serializable;

/**
* Represents an item returned from pull
*/
@JsonIgnoreProperties(ignoreUnknown=true)
public class PullEventStreamItem implements Serializable {

private static final long serialVersionUID = -5187169652557467828L;

@JsonProperty("status")
private String status;

@JsonProperty("progress")
private String progress;

@JsonProperty("progressDetail")
private ProgressDetail progressDetail;


public String getStatus() {
return status;
}

public String getProgress() {
return progress;
}

public ProgressDetail getProgressDetail() {
return progressDetail;
}

@JsonIgnoreProperties(ignoreUnknown=true)
public static class ProgressDetail implements Serializable {
@JsonProperty("current")
int current;


@Override
public String toString() {
return "current " + current;
}
}

@Override
public String toString() {
return Objects.toStringHelper(this)
.add("status", status)
.add("progress", progress)
.add("progressDetail", progressDetail)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.dockerjava.core.command;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;

public class EventStreamReader<I> implements AutoCloseable {

private final ObjectMapper objectMapper = new ObjectMapper();
private final Class<I> type;
private final InputStream inputStream;

public EventStreamReader(InputStream inputStream, Class<I> type) {
this.inputStream = inputStream;
this.type = type;
}

public I readItem() throws IOException {
try {
return objectMapper.readValue(inputStream, type);
} catch (IOException e) {
// dirty, but works
if (e.getMessage().equals("Stream closed")) {
return null;
}
throw e;
}
}

@Override
public void close() throws IOException {
inputStream.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;

/**
* Start and stop a single container for testing.
Expand All @@ -25,7 +26,7 @@ public DockerfileFixture(DockerClient dockerClient, String directory) {
this.directory = directory;
}

public void open() throws Exception {
public void open() throws IOException {

LOGGER.info("building {}", directory);
dockerClient
Expand Down Expand Up @@ -79,7 +80,7 @@ public void close() throws Exception {
.removeImageCmd(repository)
.withForce()
.exec();
} catch (InternalServerErrorException e) {
} catch (NotFoundException | InternalServerErrorException e) {
LOGGER.info("ignoring {}", e.getMessage());
}
repository = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.github.dockerjava.core.command;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.model.EventStreamItem;
import com.github.dockerjava.api.model.PullEventStreamItem;
import com.github.dockerjava.core.DockerClientBuilder;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.File;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
import static org.hamcrest.core.AllOf.allOf;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNull.nullValue;
import static org.testng.AssertJUnit.assertNull;


@Test(groups = "integration")
public class EventStreamReaderITest {

private DockerClient dockerClient;

@BeforeTest
public void setUp() throws Exception {
dockerClient = DockerClientBuilder.getInstance().build();
}

@AfterTest
public void tearDown() throws Exception {
dockerClient.close();
}

@Test
public void pullCanBeStreamed() throws Exception {

try (EventStreamReader<PullEventStreamItem> reader = new EventStreamReader<>(
dockerClient.pullImageCmd("busybox:latest").exec(),
PullEventStreamItem.class)
) {;
assertThat(reader.readItem(),
allOf(
hasProperty("status", equalTo("Pulling repository busybox")),
hasProperty("progress", nullValue()),
hasProperty("progressDetail", nullValue())
)
);
assertNull(reader.readItem());
}
}

@Test
public void buildCanBeStreamed() throws Exception {

try (EventStreamReader<EventStreamItem> reader = new EventStreamReader<>(
dockerClient.buildImageCmd(new File("src/test/resources/eventStreamReaderDockerfile")).exec(),
EventStreamItem.class)
) {
assertThat(reader.readItem(),
allOf(
hasProperty("stream", equalTo("Step 0 : FROM busybox:latest\n")),
hasProperty("error", nullValue()),
hasProperty("errorDetail", nullValue())
)
);
assertNull(reader.readItem());

}
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,37 @@
package com.github.dockerjava.core.command;


import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.StreamType;
import com.github.dockerjava.client.AbstractDockerClientTest;
import org.testng.annotations.AfterMethod;
import com.github.dockerjava.core.DockerClientBuilder;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.io.InputStream;

import static org.testng.Assert.assertEquals;
import static org.testng.AssertJUnit.assertNull;

@Test(groups = "integration")
public class FrameReaderITest extends AbstractDockerClientTest {
public class FrameReaderITest {

private DockerClient dockerClient;
private DockerfileFixture dockerfileFixture;

@BeforeTest
@Override
public void beforeTest() {
super.beforeTest();
public void beforeTest() throws Exception {
dockerClient = DockerClientBuilder.getInstance().build();
dockerfileFixture = new DockerfileFixture(dockerClient, "frameReaderDockerfile");
}

@BeforeMethod
public void createAndStartDockerContainer() throws Exception {
dockerfileFixture.open();
}

@AfterMethod
@AfterTest
public void deleteDockerContainerImage() throws Exception {
dockerfileFixture.close();
}

@AfterTest
@Override
public void afterTest() {
super.afterTest();
dockerClient.close();
}

@Test
Expand Down
5 changes: 5 additions & 0 deletions src/test/resources/eventStreamReaderDockerfile/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM busybox:latest

RUN true

CMD ["true"]