Search in sources :

Example 36 with ResponseException

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

the class BlobCheckerTest method testHandleHttpResponseException_hasOtherErrors.

@Test
public void testHandleHttpResponseException_hasOtherErrors() throws IOException {
    ResponseException mockResponseException = Mockito.mock(ResponseException.class);
    Mockito.when(mockResponseException.getStatusCode()).thenReturn(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
    ErrorResponseTemplate emptyErrorResponseTemplate = new ErrorResponseTemplate().addError(new ErrorEntryTemplate(ErrorCodes.BLOB_UNKNOWN.name(), "some message")).addError(new ErrorEntryTemplate(ErrorCodes.MANIFEST_UNKNOWN.name(), "some message"));
    Mockito.when(mockResponseException.getContent()).thenReturn(JsonTemplateMapper.toUtf8String(emptyErrorResponseTemplate));
    try {
        testBlobChecker.handleHttpResponseException(mockResponseException);
        Assert.fail("Non-BLOB_UNKNOWN errors should not be handled");
    } catch (ResponseException ex) {
        Assert.assertEquals(mockResponseException, ex);
    }
}
Also used : ResponseException(com.google.cloud.tools.jib.http.ResponseException) ErrorResponseTemplate(com.google.cloud.tools.jib.registry.json.ErrorResponseTemplate) ErrorEntryTemplate(com.google.cloud.tools.jib.registry.json.ErrorEntryTemplate) Test(org.junit.Test)

Example 37 with ResponseException

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

the class BlobCheckerTest method testHandleHttpResponseException_invalidStatusCode.

@Test
public void testHandleHttpResponseException_invalidStatusCode() {
    ResponseException mockResponseException = Mockito.mock(ResponseException.class);
    Mockito.when(mockResponseException.getStatusCode()).thenReturn(-1);
    try {
        testBlobChecker.handleHttpResponseException(mockResponseException);
        Assert.fail("Non-404 status codes should not be handled");
    } catch (ResponseException ex) {
        Assert.assertEquals(mockResponseException, ex);
    }
}
Also used : ResponseException(com.google.cloud.tools.jib.http.ResponseException) Test(org.junit.Test)

Example 38 with ResponseException

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

the class ManifestPusherIntegrationTest method testPush_missingBlobs.

@Test
public void testPush_missingBlobs() throws IOException, RegistryException {
    RegistryClient registryClient = RegistryClient.factory(EventHandlers.NONE, "gcr.io", "distroless/java", httpClient).newRegistryClient();
    ManifestTemplate manifestTemplate = registryClient.pullManifest("latest").getManifest();
    registryClient = RegistryClient.factory(EventHandlers.NONE, "localhost:5000", "ignored", httpClient).newRegistryClient();
    try {
        registryClient.pushManifest(manifestTemplate, "latest");
        Assert.fail("Pushing manifest without its BLOBs should fail");
    } catch (RegistryErrorException ex) {
        ResponseException responseException = (ResponseException) ex.getCause();
        Assert.assertEquals(HttpStatusCodes.STATUS_CODE_BAD_REQUEST, responseException.getStatusCode());
    }
}
Also used : ResponseException(com.google.cloud.tools.jib.http.ResponseException) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) Test(org.junit.Test)

Example 39 with ResponseException

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

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 40 with ResponseException

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

the class RegistryEndpointCallerTest method verifyThrowsRegistryErrorException.

/**
 * Verifies that a response with {@code httpStatusCode} throws {@link
 * RegistryUnauthorizedException}.
 */
private void verifyThrowsRegistryErrorException(int httpStatusCode) throws IOException, RegistryException {
    ResponseException errorResponse = mockResponseException(httpStatusCode);
    Mockito.when(errorResponse.getContent()).thenReturn("{\"errors\":[{\"code\":\"code\",\"message\":\"message\"}]}");
    setUpRegistryResponse(errorResponse);
    try {
        endpointCaller.call();
        Assert.fail("Call should have failed");
    } catch (RegistryErrorException ex) {
        MatcherAssert.assertThat(ex.getMessage(), CoreMatchers.startsWith("Tried to actionDescription but failed because: unknown error code: code (message)"));
    }
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) ResponseException(com.google.cloud.tools.jib.http.ResponseException)

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