Search in sources :

Example 31 with ResponseException

use of com.google.cloud.tools.jib.http.ResponseException in project jib by google.

the class ManifestPusherTest method testHandleHttpResponseException_quayIo.

/**
 * Quay.io returns an undocumented 415 / MANIFEST_INVALID.
 */
@Test
public void testHandleHttpResponseException_quayIo() throws ResponseException {
    ResponseException exception = Mockito.mock(ResponseException.class);
    Mockito.when(exception.getStatusCode()).thenReturn(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE);
    Mockito.when(exception.getContent()).thenReturn("{\"errors\":[{\"code\":\"MANIFEST_INVALID\"," + "\"detail\":{\"message\":\"manifest schema version not supported\"}," + "\"message\":\"manifest invalid\"}]}");
    try {
        testManifestPusher.handleHttpResponseException(exception);
        Assert.fail();
    } catch (RegistryErrorException ex) {
        MatcherAssert.assertThat(ex.getMessage(), CoreMatchers.containsString("Registry may not support pushing OCI Manifest or " + "Docker Image Manifest Version 2, Schema 2"));
    }
}
Also used : ResponseException(com.google.cloud.tools.jib.http.ResponseException) Test(org.junit.Test)

Example 32 with ResponseException

use of com.google.cloud.tools.jib.http.ResponseException in project jib by google.

the class ManifestPusherTest method testHandleHttpResponseException_dockerRegistry_tagInvalid.

/**
 * Docker Registry 2.0 and 2.1 return 400 / TAG_INVALID.
 */
@Test
public void testHandleHttpResponseException_dockerRegistry_tagInvalid() throws ResponseException {
    ResponseException exception = Mockito.mock(ResponseException.class);
    Mockito.when(exception.getStatusCode()).thenReturn(HttpStatus.SC_BAD_REQUEST);
    Mockito.when(exception.getContent()).thenReturn("{\"errors\":[{\"code\":\"TAG_INVALID\"," + "\"message\":\"manifest tag did not match URI\"}]}");
    try {
        testManifestPusher.handleHttpResponseException(exception);
        Assert.fail();
    } catch (RegistryErrorException ex) {
        MatcherAssert.assertThat(ex.getMessage(), CoreMatchers.containsString("Registry may not support pushing OCI Manifest or " + "Docker Image Manifest Version 2, Schema 2"));
    }
}
Also used : ResponseException(com.google.cloud.tools.jib.http.ResponseException) Test(org.junit.Test)

Example 33 with ResponseException

use of com.google.cloud.tools.jib.http.ResponseException in project jib by google.

the class ManifestPusherTest method testHandleHttpResponseException_dockerRegistry_manifestInvalid.

/**
 * Docker Registry 2.2 returns a 400 / MANIFEST_INVALID.
 */
@Test
public void testHandleHttpResponseException_dockerRegistry_manifestInvalid() throws ResponseException {
    ResponseException exception = Mockito.mock(ResponseException.class);
    Mockito.when(exception.getStatusCode()).thenReturn(HttpStatus.SC_BAD_REQUEST);
    Mockito.when(exception.getContent()).thenReturn("{\"errors\":[{\"code\":\"MANIFEST_INVALID\"," + "\"message\":\"manifest invalid\",\"detail\":{}}]}");
    try {
        testManifestPusher.handleHttpResponseException(exception);
        Assert.fail();
    } catch (RegistryErrorException ex) {
        MatcherAssert.assertThat(ex.getMessage(), CoreMatchers.containsString("Registry may not support pushing OCI Manifest or " + "Docker Image Manifest Version 2, Schema 2"));
    }
}
Also used : ResponseException(com.google.cloud.tools.jib.http.ResponseException) Test(org.junit.Test)

Example 34 with ResponseException

use of com.google.cloud.tools.jib.http.ResponseException in project jib by google.

the class RegistryAuthenticatorTest method testAuthorizationCleared.

@Test
public void testAuthorizationCleared() throws RegistryAuthenticationFailedException, IOException {
    ResponseException responseException = Mockito.mock(ResponseException.class);
    Mockito.when(responseException.getStatusCode()).thenReturn(401);
    Mockito.when(responseException.requestAuthorizationCleared()).thenReturn(true);
    Mockito.when(httpClient.call(Mockito.any(), Mockito.any(), Mockito.any())).thenThrow(responseException);
    try {
        registryAuthenticator.authenticatePush(null);
        Assert.fail();
    } catch (RegistryCredentialsNotSentException ex) {
        Assert.assertEquals("Required credentials for someserver/someimage were not sent because the connection was " + "over HTTP", ex.getMessage());
    }
}
Also used : ResponseException(com.google.cloud.tools.jib.http.ResponseException) Test(org.junit.Test)

Example 35 with ResponseException

use of com.google.cloud.tools.jib.http.ResponseException in project jib by google.

the class JibBuildRunner method runBuild.

/**
 * Runs the Jib build.
 *
 * @return the built {@link JibContainer}
 * @throws BuildStepsExecutionException if another exception is thrown during the build
 * @throws IOException if an I/O exception occurs
 * @throws CacheDirectoryCreationException if the cache directory could not be created
 */
public JibContainer runBuild() throws BuildStepsExecutionException, IOException, CacheDirectoryCreationException {
    try {
        logger.accept(LogEvent.lifecycle(""));
        logger.accept(LogEvent.lifecycle(startupMessage));
        JibContainer jibContainer = jibContainerBuilder.containerize(containerizer);
        logger.accept(LogEvent.lifecycle(""));
        logger.accept(LogEvent.lifecycle(successMessage));
        // when an image is built, write out the digest and id
        if (imageDigestOutputPath != null) {
            String imageDigest = jibContainer.getDigest().toString();
            Files.write(imageDigestOutputPath, imageDigest.getBytes(StandardCharsets.UTF_8));
        }
        if (imageIdOutputPath != null) {
            String imageId = jibContainer.getImageId().toString();
            Files.write(imageIdOutputPath, imageId.getBytes(StandardCharsets.UTF_8));
        }
        if (imageJsonOutputPath != null) {
            ImageMetadataOutput metadataOutput = ImageMetadataOutput.fromJibContainer(jibContainer);
            String imageJson = metadataOutput.toJson();
            Files.write(imageJsonOutputPath, imageJson.getBytes(StandardCharsets.UTF_8));
        }
        return jibContainer;
    } catch (HttpHostConnectException ex) {
        // Failed to connect to registry.
        throw new BuildStepsExecutionException(helpfulSuggestions.forHttpHostConnect(), ex);
    } catch (RegistryUnauthorizedException ex) {
        handleRegistryUnauthorizedException(ex, helpfulSuggestions);
    } catch (RegistryCredentialsNotSentException ex) {
        throw new BuildStepsExecutionException(helpfulSuggestions.forCredentialsNotSent(), ex);
    } catch (RegistryAuthenticationFailedException ex) {
        if (ex.getCause() instanceof ResponseException) {
            handleRegistryUnauthorizedException(new RegistryUnauthorizedException(ex.getServerUrl(), ex.getImageName(), (ResponseException) ex.getCause()), helpfulSuggestions);
        } else {
            // Unknown cause
            throw new BuildStepsExecutionException(helpfulSuggestions.none(), ex);
        }
    } catch (UnknownHostException ex) {
        throw new BuildStepsExecutionException(helpfulSuggestions.forUnknownHost(), ex);
    } catch (InsecureRegistryException ex) {
        throw new BuildStepsExecutionException(helpfulSuggestions.forInsecureRegistry(), ex);
    } catch (RegistryException ex) {
        // keep null-away happy
        String message = Verify.verifyNotNull(ex.getMessage());
        throw new BuildStepsExecutionException(message, ex);
    } catch (ExecutionException ex) {
        String message = ex.getCause().getMessage();
        throw new BuildStepsExecutionException(message == null ? "(null exception message)" : message, ex.getCause());
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
        throw new BuildStepsExecutionException(helpfulSuggestions.none(), ex);
    }
    throw new IllegalStateException("unreachable");
}
Also used : RegistryAuthenticationFailedException(com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException) UnknownHostException(java.net.UnknownHostException) ResponseException(com.google.cloud.tools.jib.http.ResponseException) JibContainer(com.google.cloud.tools.jib.api.JibContainer) InsecureRegistryException(com.google.cloud.tools.jib.api.InsecureRegistryException) RegistryException(com.google.cloud.tools.jib.api.RegistryException) InsecureRegistryException(com.google.cloud.tools.jib.api.InsecureRegistryException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) RegistryUnauthorizedException(com.google.cloud.tools.jib.api.RegistryUnauthorizedException) RegistryCredentialsNotSentException(com.google.cloud.tools.jib.registry.RegistryCredentialsNotSentException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ResponseException (com.google.cloud.tools.jib.http.ResponseException)44 Test (org.junit.Test)32 NoHttpResponseException (org.apache.http.NoHttpResponseException)18 RegistryUnauthorizedException (com.google.cloud.tools.jib.api.RegistryUnauthorizedException)8 ErrorResponseTemplate (com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate)6 InsecureRegistryException (com.google.cloud.tools.jib.api.InsecureRegistryException)4 RegistryAuthenticationFailedException (com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException)4 Request (com.google.cloud.tools.jib.http.Request)4 Response (com.google.cloud.tools.jib.http.Response)4 ErrorEntryTemplate (com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate)4 IOException (java.io.IOException)4 JibContainer (com.google.cloud.tools.jib.api.JibContainer)2 RegistryException (com.google.cloud.tools.jib.api.RegistryException)2 BlobHttpContent (com.google.cloud.tools.jib.http.BlobHttpContent)2 ManifestTemplate (com.google.cloud.tools.jib.image.json.ManifestTemplate)2 V22ManifestTemplate (com.google.cloud.tools.jib.image.json.V22ManifestTemplate)2 RegistryCredentialsNotSentException (com.google.cloud.tools.jib.registry.RegistryCredentialsNotSentException)2 URL (java.net.URL)2 UnknownHostException (java.net.UnknownHostException)2 ExecutionException (java.util.concurrent.ExecutionException)2