Skip to content

Commit

Permalink
ci: test suite and script changes for compatibility with Kokoro ubunt…
Browse files Browse the repository at this point in the history
…u migration (GoogleContainerTools#3886)
  • Loading branch information
emmileaf authored Apr 26, 2023
1 parent 9f2d0ba commit 3d3c02d
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.tools.jib.Command;
import com.google.cloud.tools.jib.blob.Blobs;
import com.google.cloud.tools.jib.api.HttpRequestTester;
import com.google.common.io.Resources;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
Expand All @@ -43,8 +41,6 @@ public class JarCommandTest {
@ClassRule
public static final TestProject springBootProject = new TestProject("jarTest/spring-boot");

private final String dockerHost =
System.getenv("DOCKER_IP") != null ? System.getenv("DOCKER_IP") : "localhost";
@Nullable private String containerName;

@After
Expand Down Expand Up @@ -225,7 +221,9 @@ public void testSpringBootLayeredJar_explodedMode() throws IOException, Interrup
try (JarFile jarFile = new JarFile(jarPath.toFile())) {

assertThat(jarFile.getEntry("BOOT-INF/layers.idx")).isNotNull();
assertThat(getContent(new URL("http://" + dockerHost + ":8080"))).isEqualTo("Hello world");
HttpRequestTester.verifyBody(
"Hello world",
new URL("http://" + HttpRequestTester.fetchDockerHostForHttpRequest() + ":8080"));
}
}

Expand Down Expand Up @@ -261,7 +259,9 @@ public void testSpringBootNonLayeredJar_explodedMode() throws IOException, Inter
try (JarFile jarFile = new JarFile(jarPath.toFile())) {

assertThat(jarFile.getEntry("BOOT-INF/layers.idx")).isNull();
assertThat(getContent(new URL("http://" + dockerHost + ":8080"))).isEqualTo("Hello world");
HttpRequestTester.verifyBody(
"Hello world",
new URL("http://" + HttpRequestTester.fetchDockerHostForHttpRequest() + ":8080"));
}
}

Expand Down Expand Up @@ -295,7 +295,9 @@ public void testSpringBootJar_packagedMode() throws IOException, InterruptedExce
.run();
containerName = output.trim();

assertThat(getContent(new URL("http://" + dockerHost + ":8080"))).isEqualTo("Hello world");
HttpRequestTester.verifyBody(
"Hello world",
new URL("http://" + HttpRequestTester.fetchDockerHostForHttpRequest() + ":8080"));
}

@Test
Expand All @@ -313,22 +315,4 @@ public void testJar_baseImageSpecified()
String output = new Command("docker", "run", "--rm", "cli-gcr-base").run();
assertThat(output).isEqualTo("Hello World");
}

@Nullable
private static String getContent(URL url) throws InterruptedException {
for (int i = 0; i < 40; i++) {
Thread.sleep(500);
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
try (InputStream in = connection.getInputStream()) {
return Blobs.writeToString(Blobs.from(in));
}
}
} catch (IOException ignored) {
// ignored
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@
import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.tools.jib.Command;
import com.google.cloud.tools.jib.blob.Blobs;
import com.google.cloud.tools.jib.api.HttpRequestTester;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -37,8 +35,6 @@
public class WarCommandTest {

@ClassRule public static final TestProject servletProject = new TestProject("warTest");
private final String dockerHost =
System.getenv("DOCKER_IP") != null ? System.getenv("DOCKER_IP") : "localhost";

@Nullable private String containerName;

Expand Down Expand Up @@ -100,8 +96,9 @@ public void testWar_jetty() throws IOException, InterruptedException {
.run();
containerName = output.trim();

assertThat(getContent(new URL("http://" + dockerHost + ":8080/hello")))
.isEqualTo("Hello world");
HttpRequestTester.verifyBody(
"Hello world",
new URL("http://" + HttpRequestTester.fetchDockerHostForHttpRequest() + ":8080/hello"));
}

@Test
Expand All @@ -123,8 +120,9 @@ public void testWar_customJettySpecified() throws IOException, InterruptedExcept
.run();
containerName = output.trim();

assertThat(getContent(new URL("http://" + dockerHost + ":8080/hello")))
.isEqualTo("Hello world");
HttpRequestTester.verifyBody(
"Hello world",
new URL("http://" + HttpRequestTester.fetchDockerHostForHttpRequest() + ":8080/hello"));
}

@Test
Expand All @@ -148,25 +146,8 @@ public void testWar_tomcat() throws IOException, InterruptedException {
.run();
containerName = output.trim();

assertThat(getContent(new URL("http://" + dockerHost + ":8080/hello")))
.isEqualTo("Hello world");
}

@Nullable
private static String getContent(URL url) throws InterruptedException {
for (int i = 0; i < 40; i++) {
Thread.sleep(500);
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
try (InputStream in = connection.getInputStream()) {
return Blobs.writeToString(Blobs.from(in));
}
}
} catch (IOException ignored) {
// ignored
}
}
return null;
HttpRequestTester.verifyBody(
"Hello world",
new URL("http://" + HttpRequestTester.fetchDockerHostForHttpRequest() + ":8080/hello"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* the License.
*/

package com.google.cloud.tools.jib.maven;
package com.google.cloud.tools.jib.api;

import com.google.cloud.tools.jib.blob.Blobs;
import java.io.IOException;
Expand All @@ -24,17 +24,30 @@
import javax.annotation.Nullable;
import org.junit.Assert;

/** Verifies the response of HTTP GET. */
class HttpGetVerifier {
/** Test helpers for making HTTP requests. */
public class HttpRequestTester {

/**
* Verifies the response body. Repeatedly tries {@code url} at the interval of .5 seconds for up
* to 20 seconds until getting OK HTTP response code.
*/
static void verifyBody(String expectedBody, URL url) throws InterruptedException {
public static void verifyBody(String expectedBody, URL url) throws InterruptedException {
Assert.assertEquals(expectedBody, getContent(url));
}

/** Fetches the host to use for the http request. */
public static String fetchDockerHostForHttpRequest() {
if (System.getenv("KOKORO_JOB_CLUSTER") != null
&& System.getenv("KOKORO_JOB_CLUSTER").equals("MACOS_EXTERNAL")) {
return System.getenv("DOCKER_IP");
} else if (System.getenv("KOKORO_JOB_CLUSTER") != null
&& System.getenv("KOKORO_JOB_CLUSTER").equals("GCP_UBUNTU_DOCKER")) {
return System.getenv("DOCKER_IP_UBUNTU");
} else {
return "localhost";
}
}

@Nullable
private static String getContent(URL url) throws InterruptedException {
for (int i = 0; i < 40; i++) {
Expand All @@ -52,4 +65,6 @@ private static String getContent(URL url) throws InterruptedException {
}
return null;
}

private HttpRequestTester() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,14 @@ public void start() throws IOException, InterruptedException {
Path tempFolder = Files.createTempDirectory(Paths.get("/tmp"), "", attrs);
Files.write(
tempFolder.resolve("htpasswd"), credentialString.getBytes(StandardCharsets.UTF_8));
boolean isOnKokoroCI =
System.getenv("KOKORO_JOB_CLUSTER") != null
&& System.getenv("KOKORO_JOB_CLUSTER").equals("MACOS_EXTERNAL");
String tempAuthFile = tempFolder + ":/auth";
String authenticationVolume = isOnKokoroCI ? "/home/docker/auth:/auth" : tempAuthFile;
String authenticationVolume = tempFolder + ":/auth";
if (System.getenv("KOKORO_JOB_CLUSTER") != null
&& System.getenv("KOKORO_JOB_CLUSTER").equals("MACOS_EXTERNAL")) {
authenticationVolume = "/home/docker/auth:/auth";
} else if (System.getenv("KOKORO_JOB_CLUSTER") != null
&& System.getenv("KOKORO_JOB_CLUSTER").equals("GCP_UBUNTU_DOCKER")) {
authenticationVolume = "/tmpfs/auth:/auth";
}
// Run the Docker registry
dockerTokens.addAll(
Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,22 +548,23 @@ public ImmutableListMultimap<String, String> getRegistryMirrors() {
}

/**
* Creates a new {@link RegistryClient.Factory} for the base image with fields from the build
* configuration. The server URL is derived from the base {@link ImageConfiguration}.
* Creates a new {@link com.google.cloud.tools.jib.registry.RegistryClient.Factory} for the base
* image with fields from the build configuration. The server URL is derived from the base {@link
* ImageConfiguration}.
*
* @return a new {@link RegistryClient.Factory}
* @return a new {@link com.google.cloud.tools.jib.registry.RegistryClient.Factory}
*/
public RegistryClient.Factory newBaseImageRegistryClientFactory() {
return newBaseImageRegistryClientFactory(baseImageConfiguration.getImageRegistry());
}

/**
* Creates a new {@link RegistryClient.Factory} for the base image repository on the registry
* {@code serverUrl}. Compared to @link #newBaseImageRegistryClientFactory()), this method is
* useful to try a mirror.
* Creates a new {@link com.google.cloud.tools.jib.registry.RegistryClient.Factory} for the base
* image repository on the registry {@code serverUrl}. Compared to @link
* #newBaseImageRegistryClientFactory()), this method is useful to try a mirror.
*
* @param serverUrl the server URL for the registry (for example, {@code gcr.io})
* @return a new {@link RegistryClient.Factory}
* @return a new {@link com.google.cloud.tools.jib.registry.RegistryClient.Factory}
*/
public RegistryClient.Factory newBaseImageRegistryClientFactory(String serverUrl) {
return RegistryClient.factory(
Expand All @@ -572,10 +573,10 @@ public RegistryClient.Factory newBaseImageRegistryClientFactory(String serverUrl
}

/**
* Creates a new {@link RegistryClient.Factory} for the target image with fields from the build
* configuration.
* Creates a new {@link com.google.cloud.tools.jib.registry.RegistryClient.Factory} for the target
* image with fields from the build configuration.
*
* @return a new {@link RegistryClient.Factory}
* @return a new {@link com.google.cloud.tools.jib.registry.RegistryClient.Factory}
*/
public RegistryClient.Factory newTargetImageRegistryClientFactory() {
// if base and target are on the same registry, try enabling cross-repository mounts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@
import com.google.cloud.tools.jib.api.DescriptorDigest;
import com.google.cloud.tools.jib.api.ImageReference;
import com.google.cloud.tools.jib.api.InvalidImageReferenceException;
import com.google.cloud.tools.jib.blob.Blobs;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -35,7 +31,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Nullable;
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.BuildTask;
import org.gradle.testkit.runner.TaskOutcome;
Expand All @@ -46,24 +41,6 @@
/** Helper class to run integration tests. */
public class JibRunHelper {

@Nullable
static String getContent(URL url) throws InterruptedException {
for (int i = 0; i < 40; i++) {
Thread.sleep(500);
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
try (InputStream in = connection.getInputStream()) {
return Blobs.writeToString(Blobs.from(in));
}
}
} catch (IOException ignored) {
// ignored
}
}
return null;
}

static String buildAndRun(TestProject testProject, String imageReference)
throws IOException, InterruptedException, DigestException {
return buildAndRun(testProject, imageReference, "build.gradle");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.cloud.tools.jib.Command;
import com.google.cloud.tools.jib.IntegrationTestingConfiguration;
import com.google.cloud.tools.jib.api.HttpRequestTester;
import java.io.IOException;
import java.net.URL;
import java.security.DigestException;
Expand All @@ -34,9 +35,6 @@ public class SpringBootProjectIntegrationTest {

@Nullable private String containerName;

private final String dockerHost =
System.getenv("DOCKER_IP") != null ? System.getenv("DOCKER_IP") : "localhost";

@After
public void tearDown() throws IOException, InterruptedException {
if (containerName != null) {
Expand All @@ -57,10 +55,11 @@ public void testBuild_packagedMode() throws IOException, InterruptedException, D
"-c",
"/app/classpath/spring-boot-original.jar")
.run();
Assert.assertEquals("1360 /app/classpath/spring-boot-original.jar\n", output);

Assert.assertEquals(
"Hello world", JibRunHelper.getContent(new URL("http://" + dockerHost + ":8080")));
Assert.assertEquals("1360 /app/classpath/spring-boot-original.jar\n", output);
HttpRequestTester.verifyBody(
"Hello world",
new URL("http://" + HttpRequestTester.fetchDockerHostForHttpRequest() + ":8080"));
}

private void buildAndRunWebApp(String label, String gradleBuildFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

import com.google.cloud.tools.jib.Command;
import com.google.cloud.tools.jib.IntegrationTestingConfiguration;
import com.google.cloud.tools.jib.api.HttpRequestTester;
import java.io.IOException;
import java.net.URL;
import java.security.DigestException;
import javax.annotation.Nullable;
import org.junit.After;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Test;

Expand All @@ -34,9 +34,6 @@ public class WarProjectIntegrationTest {

@Nullable private String containerName;

private final String dockerHost =
System.getenv("DOCKER_IP") != null ? System.getenv("DOCKER_IP") : "localhost";

@After
public void tearDown() throws IOException, InterruptedException {
if (containerName != null) {
Expand All @@ -63,7 +60,8 @@ private void verifyBuildAndRun(TestProject project, String label, String gradleB
JibRunHelper.buildAndRun(project, targetImage, gradleBuildFile, "--detach", "-p8080:8080");
containerName = output.trim();

Assert.assertEquals(
"Hello world", JibRunHelper.getContent(new URL("http://" + dockerHost + ":8080/hello")));
HttpRequestTester.verifyBody(
"Hello world",
new URL("http://" + HttpRequestTester.fetchDockerHostForHttpRequest() + ":8080/hello"));
}
}
Loading

0 comments on commit 3d3c02d

Please sign in to comment.