Skip to content

Commit 9cf78a5

Browse files
authored
Removes use of Runtime.exec. (GoogleContainerTools#143)
1 parent 3ba2762 commit 9cf78a5

5 files changed

Lines changed: 56 additions & 53 deletions

File tree

jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/DockerImageRunner.java renamed to jib-core/src/integration-test/java/com/google/cloud/tools/jib/Command.java

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,44 @@
1414
* the License.
1515
*/
1616

17-
package com.google.cloud.tools.jib.builder;
17+
package com.google.cloud.tools.jib;
1818

1919
import com.google.common.io.CharStreams;
2020
import java.io.IOException;
2121
import java.io.InputStreamReader;
22+
import java.io.OutputStream;
2223
import java.nio.charset.StandardCharsets;
2324
import java.util.Arrays;
25+
import java.util.List;
26+
import javax.annotation.Nullable;
2427

25-
/** Test utility for running an image with Docker. */
26-
public class DockerImageRunner {
28+
/** Test utility to run shell commands for integration tests. */
29+
public class Command {
2730

28-
/** Runs a command with naive tokenization by whitespace. */
29-
private static String runCommand(String... command) throws IOException, InterruptedException {
30-
Process process = new ProcessBuilder(Arrays.asList(command)).start();
31+
private final List<String> command;
3132

33+
/** Instantiate with a command. */
34+
public Command(String... command) {
35+
this.command = Arrays.asList(command);
36+
}
37+
38+
/** Runs the command. */
39+
public String run() throws IOException, InterruptedException {
40+
return run(null);
41+
}
42+
43+
/** Runs the command and pipes in {@code stdin}. */
44+
public String run(@Nullable byte[] stdin) throws IOException, InterruptedException {
45+
Process process = new ProcessBuilder(command).start();
46+
47+
if (stdin != null) {
48+
// Write out stdin.
49+
try (OutputStream outputStream = process.getOutputStream()) {
50+
outputStream.write(stdin);
51+
}
52+
}
53+
54+
// Read in stdout.
3255
try (InputStreamReader inputStreamReader =
3356
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)) {
3457
String output = CharStreams.toString(inputStreamReader);
@@ -40,16 +63,4 @@ private static String runCommand(String... command) throws IOException, Interrup
4063
return output;
4164
}
4265
}
43-
44-
private final String imageReference;
45-
46-
public DockerImageRunner(String imageReference) {
47-
this.imageReference = imageReference;
48-
}
49-
50-
/** Pulls and runs the image and returns the output. */
51-
public String run() throws IOException, InterruptedException {
52-
runCommand("docker", "pull", imageReference);
53-
return runCommand("docker", "run", imageReference);
54-
}
5566
}

‎jib-core/src/integration-test/java/com/google/cloud/tools/jib/builder/BuildImageStepsIntegrationTest.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.tools.jib.builder;
1818

19+
import com.google.cloud.tools.jib.Command;
1920
import com.google.cloud.tools.jib.image.ImageReference;
2021
import com.google.cloud.tools.jib.registry.LocalRegistry;
2122
import org.junit.Assert;
@@ -56,7 +57,8 @@ public void testSteps() throws Exception {
5657
buildImageSteps.run();
5758
logger.info("Secondary build time: " + ((System.nanoTime() - lastTime) / 1_000_000));
5859

59-
Assert.assertEquals(
60-
"Hello world\n", new DockerImageRunner("localhost:5000/testimage:testtag").run());
60+
String imageReference = "localhost:5000/testimage:testtag";
61+
new Command("docker", "pull", imageReference).run();
62+
Assert.assertEquals("Hello world\n", new Command("docker", "run", imageReference).run());
6163
}
6264
}

‎jib-core/src/integration-test/java/com/google/cloud/tools/jib/registry/LocalRegistry.java‎

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
package com.google.cloud.tools.jib.registry;
1818

19+
import com.google.cloud.tools.jib.Command;
1920
import com.google.common.io.CharStreams;
2021
import java.io.IOException;
2122
import java.io.InputStreamReader;
2223
import java.nio.charset.StandardCharsets;
23-
import java.util.Arrays;
2424
import java.util.UUID;
2525
import org.junit.rules.ExternalResource;
2626
import org.junit.rules.TestRule;
@@ -41,49 +41,43 @@ public LocalRegistry(int port) {
4141
@Override
4242
protected void before() throws Throwable {
4343
// Runs the Docker registry.
44-
runCommand(
45-
"docker",
46-
"run",
47-
"-d",
48-
"-p",
49-
port + ":5000",
50-
"--restart=always",
51-
"--name",
52-
containerName,
53-
"registry:2");
44+
new Command(
45+
"docker",
46+
"run",
47+
"-d",
48+
"-p",
49+
port + ":5000",
50+
"--restart=always",
51+
"--name",
52+
containerName,
53+
"registry:2")
54+
.run();
5455

5556
// Pulls 'busybox'.
56-
runCommand("docker", "pull", "busybox");
57+
new Command("docker", "pull", "busybox").run();
5758

5859
// Tags 'busybox' to push to our local registry.
59-
runCommand("docker", "tag", "busybox", "localhost:" + port + "/busybox");
60+
new Command("docker", "tag", "busybox", "localhost:" + port + "/busybox").run();
6061

6162
// Pushes 'busybox' to our local registry.
62-
runCommand("docker", "push", "localhost:" + port + "/busybox");
63+
new Command("docker", "push", "localhost:" + port + "/busybox").run();
6364
}
6465

6566
/** Stops the local registry. */
6667
@Override
6768
protected void after() {
6869
try {
6970
// Stops the registry.
70-
runCommand("docker", "stop", containerName);
71+
new Command("docker", "stop", containerName).run();
7172

7273
// Removes the container.
73-
runCommand("docker", "rm", "-v", containerName);
74+
new Command("docker", "rm", "-v", containerName).run();
7475

7576
} catch (InterruptedException | IOException ex) {
7677
throw new RuntimeException("Could not stop local registry fully: " + containerName, ex);
7778
}
7879
}
7980

80-
/** Runs a command with naive tokenization by whitespace. */
81-
private void runCommand(String... command) throws IOException, InterruptedException {
82-
if (new ProcessBuilder(Arrays.asList(command)).start().waitFor() != 0) {
83-
throw new IOException("Command '" + String.join(" ", command) + "' failed");
84-
}
85-
}
86-
8781
private void printLogs() throws IOException, InterruptedException {
8882
Process process = Runtime.getRuntime().exec("docker logs " + containerName);
8983
try (InputStreamReader inputStreamReader =

‎jib-core/src/integration-test/java/com/google/cloud/tools/jib/registry/credentials/DockerCredentialHelperIntegrationTest.java‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
package com.google.cloud.tools.jib.registry.credentials;
1818

19+
import com.google.cloud.tools.jib.Command;
1920
import com.google.cloud.tools.jib.http.Authorization;
2021
import com.google.common.io.Resources;
2122
import java.io.IOException;
22-
import java.io.OutputStream;
2323
import java.net.URISyntaxException;
2424
import java.nio.file.Files;
2525
import java.nio.file.Paths;
@@ -35,13 +35,8 @@ public class DockerCredentialHelperIntegrationTest {
3535
public void testRetrieveGCR()
3636
throws IOException, NonexistentServerUrlDockerCredentialHelperException,
3737
NonexistentDockerCredentialHelperException, URISyntaxException, InterruptedException {
38-
// TODO: Refactor
39-
Process process = Runtime.getRuntime().exec("docker-credential-gcr store");
40-
try (OutputStream outputStream = process.getOutputStream()) {
41-
outputStream.write(
42-
Files.readAllBytes(Paths.get(Resources.getResource("credentials.json").toURI())));
43-
}
44-
process.waitFor();
38+
new Command("docker-credential-gcr", "store")
39+
.run(Files.readAllBytes(Paths.get(Resources.getResource("credentials.json").toURI())));
4540

4641
DockerCredentialHelper dockerCredentialHelper =
4742
new DockerCredentialHelperFactory("myregistry").withCredentialHelperSuffix("gcr");

‎jib-maven-plugin/src/test/java/com/google/cloud/tools/jib/maven/BuildImageMojoIT.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.google.cloud.tools.jib.maven;
1818

19-
import com.google.cloud.tools.jib.builder.DockerImageRunner;
19+
import com.google.cloud.tools.jib.Command;
2020
import java.io.IOException;
2121
import java.nio.file.Path;
2222
import org.apache.maven.it.VerificationException;
@@ -72,6 +72,7 @@ private String buildAndRun(Path projectRoot, String imageReference)
7272

7373
Assert.assertTrue(timeOne > timeTwo);
7474

75-
return new DockerImageRunner(imageReference).run();
75+
new Command("docker", "pull", imageReference).run();
76+
return new Command("docker", "run", imageReference).run();
7677
}
7778
}

0 commit comments

Comments
 (0)