Decomplicate consuming the output of docker commands in tests.

This commit is contained in:
Adrian Cole 2014-10-29 12:52:13 -07:00 committed by Adrian Cole
parent 42d881f78a
commit 8b5fcf069b
2 changed files with 10 additions and 22 deletions

View File

@ -16,6 +16,9 @@
*/
package org.jclouds.docker.compute;
import static com.google.common.base.Charsets.UTF_8;
import static org.jclouds.util.Closeables2.closeQuietly;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
@ -32,10 +35,8 @@ import org.jclouds.docker.DockerApi;
import org.jclouds.io.Payload;
import org.jclouds.io.Payloads;
import org.jclouds.sshj.config.SshjSshClientModule;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.CharStreams;
import com.google.inject.Module;
@ -60,23 +61,14 @@ public class BaseDockerApiLiveTest extends BaseApiLiveTest<DockerApi> {
return overrides;
}
protected String consumeStream(InputStream stream, boolean swallowIOException) {
String result = null;
protected String consumeStream(InputStream stream) {
try {
result = CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));
return CharStreams.toString(new InputStreamReader(stream, UTF_8));
} catch (IOException e) {
Assert.fail();
throw new AssertionError(e);
} finally {
// TODO: remove swallowIOException over-optimization and just use Closeables2.closeQuietly
try {
stream.close();
} catch (IOException e) {
if (!swallowIOException) {
throw new RuntimeException(e);
}
}
closeQuietly(stream);
}
return result;
}
public static Payload tarredDockerfile() throws IOException {

View File

@ -23,7 +23,6 @@ import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import org.jclouds.docker.compute.BaseDockerApiLiveTest;
@ -55,8 +54,7 @@ public class RemoteApiLiveTest extends BaseDockerApiLiveTest {
@Test(dependsOnMethods = "testVersion")
public void testCreateImage() throws IOException, InterruptedException {
CreateImageOptions options = CreateImageOptions.Builder.fromImage(BUSYBOX_IMAGE);
InputStream createImageStream = api().createImage(options);
consumeStream(createImageStream, false);
consumeStream(api().createImage(options));
image = api().inspectImage(BUSYBOX_IMAGE);
assertNotNull(image);
}
@ -96,15 +94,13 @@ public class RemoteApiLiveTest extends BaseDockerApiLiveTest {
@Test(dependsOnMethods = "testRemoveContainer", expectedExceptions = ResourceNotFoundException.class)
public void testDeleteImage() {
InputStream deleteImageStream = api().deleteImage(image.id());
consumeStream(deleteImageStream, false);
consumeStream(api().deleteImage(image.id()));
assertNull(api().inspectImage(image.id()));
}
public void testBuildImage() throws IOException, InterruptedException, URISyntaxException {
BuildOptions options = BuildOptions.Builder.tag("testBuildImage").verbose(false).nocache(false);
InputStream buildImageStream = api().build(tarredDockerfile(), options);
String buildStream = consumeStream(buildImageStream, false);
String buildStream = consumeStream(api().build(tarredDockerfile(), options));
Iterable<String> splitted = Splitter.on("\n").split(buildStream.replace("\r", "").trim());
String lastStreamedLine = Iterables.getLast(splitted).trim();
String rawImageId = Iterables.getLast(Splitter.on("Successfully built ").split(lastStreamedLine));