use of com.google.cloud.tools.jib.http.ResponseException in project jib by GoogleContainerTools.
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());
}
}
use of com.google.cloud.tools.jib.http.ResponseException in project jib by GoogleContainerTools.
the class BlobCheckerTest method testHandleHttpResponseException_notBlobUnknown.
@Test
public void testHandleHttpResponseException_notBlobUnknown() throws IOException {
ResponseException mockResponseException = Mockito.mock(ResponseException.class);
Mockito.when(mockResponseException.getStatusCode()).thenReturn(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
ErrorResponseTemplate emptyErrorResponseTemplate = new ErrorResponseTemplate();
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);
}
}
use of com.google.cloud.tools.jib.http.ResponseException in project jib by GoogleContainerTools.
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);
}
}
use of com.google.cloud.tools.jib.http.ResponseException in project jib by GoogleContainerTools.
the class BlobCheckerTest method testHandleHttpResponseException.
@Test
public void testHandleHttpResponseException() 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"));
Mockito.when(mockResponseException.getContent()).thenReturn(JsonTemplateMapper.toUtf8String(emptyErrorResponseTemplate));
Assert.assertFalse(testBlobChecker.handleHttpResponseException(mockResponseException).isPresent());
}
use of com.google.cloud.tools.jib.http.ResponseException in project jib by GoogleContainerTools.
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");
}
Aggregations