Search in sources :

Example 16 with RegistryException

use of com.google.cloud.tools.jib.api.RegistryException in project jib by google.

the class JibBuildRunnerTest method testBuildImage_other.

@Test
public void testBuildImage_other() throws InterruptedException, IOException, CacheDirectoryCreationException, RegistryException, ExecutionException {
    Mockito.doThrow(new RegistryException("messagePrefix")).when(mockJibContainerBuilder).containerize(mockContainerizer);
    try {
        testJibBuildRunner.runBuild();
        Assert.fail();
    } catch (BuildStepsExecutionException ex) {
        Assert.assertEquals(TEST_HELPFUL_SUGGESTIONS.none(), ex.getMessage());
    }
}
Also used : InsecureRegistryException(com.google.cloud.tools.jib.api.InsecureRegistryException) RegistryException(com.google.cloud.tools.jib.api.RegistryException) Test(org.junit.Test)

Example 17 with RegistryException

use of com.google.cloud.tools.jib.api.RegistryException 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)

Example 18 with RegistryException

use of com.google.cloud.tools.jib.api.RegistryException in project jib by google.

the class ObtainBaseImageLayerStepTest method setUp.

@Before
public void setUp() throws IOException, RegistryException, DigestException {
    existingLayerDigest = DescriptorDigest.fromHash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    freshLayerDigest = DescriptorDigest.fromHash("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
    DescriptorDigest diffId = Mockito.mock(DescriptorDigest.class);
    existingLayer = new ReferenceLayer(new BlobDescriptor(existingLayerDigest), diffId);
    freshLayer = new ReferenceLayer(new BlobDescriptor(freshLayerDigest), diffId);
    Mockito.when(registryClient.checkBlob(existingLayerDigest)).thenReturn(Optional.of(Mockito.mock(BlobDescriptor.class)));
    Mockito.when(registryClient.checkBlob(freshLayerDigest)).thenReturn(Optional.empty());
    // necessary to prevent error from classes dealing with progress report
    Answer3<Blob, DescriptorDigest, Consumer<Long>, Consumer<Long>> progressSizeSetter = (ignored1, progressSizeConsumer, ignored2) -> {
        progressSizeConsumer.accept(Long.valueOf(12345));
        return null;
    };
    Mockito.when(registryClient.pullBlob(Mockito.any(), Mockito.any(), Mockito.any())).thenAnswer(AdditionalAnswers.answer(progressSizeSetter));
}
Also used : StateInTarget(com.google.cloud.tools.jib.builder.steps.PreparedLayer.StateInTarget) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) DigestException(java.security.DigestException) Before(org.junit.Before) Answer3(org.mockito.stubbing.Answer3) Layer(com.google.cloud.tools.jib.image.Layer) Answers(org.mockito.Answers) Blob(com.google.cloud.tools.jib.blob.Blob) IOException(java.io.IOException) Test(org.junit.Test) RegistryException(com.google.cloud.tools.jib.api.RegistryException) ReferenceLayer(com.google.cloud.tools.jib.image.ReferenceLayer) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) AdditionalAnswers(org.mockito.AdditionalAnswers) CacheCorruptedException(com.google.cloud.tools.jib.cache.CacheCorruptedException) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) Consumer(java.util.function.Consumer) Mockito(org.mockito.Mockito) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) Optional(java.util.Optional) Assert(org.junit.Assert) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) Blob(com.google.cloud.tools.jib.blob.Blob) Consumer(java.util.function.Consumer) ReferenceLayer(com.google.cloud.tools.jib.image.ReferenceLayer) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Before(org.junit.Before)

Example 19 with RegistryException

use of com.google.cloud.tools.jib.api.RegistryException in project jib by GoogleContainerTools.

the class BlobPusherIntegrationTest method testPush.

@Test
public void testPush() throws DigestException, IOException, RegistryException {
    Blob testBlob = Blobs.from("crepecake");
    // Known digest for 'crepecake'
    DescriptorDigest testBlobDigest = DescriptorDigest.fromHash("52a9e4d4ba4333ce593707f98564fee1e6d898db0d3602408c0b2a6a424d357c");
    RegistryClient registryClient = RegistryClient.factory(EventHandlers.NONE, "localhost:5000", "testimage", httpClient).newRegistryClient();
    Assert.assertFalse(registryClient.pushBlob(testBlobDigest, testBlob, null, ignored -> {
    }));
}
Also used : Blob(com.google.cloud.tools.jib.blob.Blob) FailoverHttpClient(com.google.cloud.tools.jib.http.FailoverHttpClient) DigestException(java.security.DigestException) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Blobs(com.google.cloud.tools.jib.blob.Blobs) IOException(java.io.IOException) Test(org.junit.Test) RegistryException(com.google.cloud.tools.jib.api.RegistryException) Assert(org.junit.Assert) ClassRule(org.junit.ClassRule) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Blob(com.google.cloud.tools.jib.blob.Blob) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Test(org.junit.Test)

Example 20 with RegistryException

use of com.google.cloud.tools.jib.api.RegistryException in project jib by GoogleContainerTools.

the class PullBaseImageStepTest method testCall_allMirrorsFail.

@Test
public void testCall_allMirrorsFail() throws InvalidImageReferenceException, IOException, RegistryException, LayerPropertyNotFoundException, LayerCountMismatchException, BadContainerConfigurationFormatException, CacheCorruptedException, CredentialRetrievalException {
    Mockito.when(imageConfiguration.getImage()).thenReturn(ImageReference.parse("registry/repo"));
    Mockito.when(imageConfiguration.getImageRegistry()).thenReturn("registry");
    Mockito.when(buildContext.getRegistryMirrors()).thenReturn(ImmutableListMultimap.of("registry", "quay.io", "registry", "gcr.io"));
    Mockito.when(buildContext.newBaseImageRegistryClientFactory(Mockito.any())).thenReturn(registryClientFactory);
    Mockito.when(registryClient.pullManifest(Mockito.any())).thenThrow(new RegistryException("not found"));
    RegistryClient.Factory dockerHubRegistryClientFactory = setUpWorkingRegistryClientFactory();
    Mockito.when(buildContext.newBaseImageRegistryClientFactory()).thenReturn(dockerHubRegistryClientFactory);
    ImagesAndRegistryClient result = pullBaseImageStep.call();
    Assert.assertEquals(dockerHubRegistryClientFactory.newRegistryClient(), result.registryClient);
    InOrder inOrder = Mockito.inOrder(eventHandlers);
    inOrder.verify(eventHandlers).dispatch(LogEvent.info("trying mirror quay.io for the base image"));
    inOrder.verify(eventHandlers).dispatch(LogEvent.debug("failed to get manifest from mirror quay.io: not found"));
    inOrder.verify(eventHandlers).dispatch(LogEvent.info("trying mirror gcr.io for the base image"));
    inOrder.verify(eventHandlers).dispatch(LogEvent.debug("failed to get manifest from mirror gcr.io: not found"));
}
Also used : InOrder(org.mockito.InOrder) ImagesAndRegistryClient(com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient) ImagesAndRegistryClient(com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) RegistryException(com.google.cloud.tools.jib.api.RegistryException) Test(org.junit.Test)

Aggregations

RegistryException (com.google.cloud.tools.jib.api.RegistryException)23 IOException (java.io.IOException)15 Test (org.junit.Test)12 ExecutionException (java.util.concurrent.ExecutionException)9 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)8 CacheDirectoryCreationException (com.google.cloud.tools.jib.api.CacheDirectoryCreationException)7 DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)6 Blob (com.google.cloud.tools.jib.blob.Blob)6 ImagesAndRegistryClient (com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient)6 EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)6 DigestException (java.security.DigestException)6 ExecutorService (java.util.concurrent.ExecutorService)6 Assert (org.junit.Assert)6 InsecureRegistryException (com.google.cloud.tools.jib.api.InsecureRegistryException)4 InvalidImageReferenceException (com.google.cloud.tools.jib.api.InvalidImageReferenceException)4 Blobs (com.google.cloud.tools.jib.blob.Blobs)4 ProgressEventDispatcher (com.google.cloud.tools.jib.builder.ProgressEventDispatcher)4 ProgressEvent (com.google.cloud.tools.jib.event.events.ProgressEvent)4 ProgressEventHandler (com.google.cloud.tools.jib.event.progress.ProgressEventHandler)4 FailoverHttpClient (com.google.cloud.tools.jib.http.FailoverHttpClient)4