use of com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient in project jib by GoogleContainerTools.
the class PullBaseImageStepTest method testCall_offlineMode_cached.
@Test
public void testCall_offlineMode_cached() throws LayerPropertyNotFoundException, RegistryException, LayerCountMismatchException, BadContainerConfigurationFormatException, CacheCorruptedException, CredentialRetrievalException, InvalidImageReferenceException, IOException {
ImageReference imageReference = ImageReference.parse("cat");
Mockito.when(imageConfiguration.getImage()).thenReturn(imageReference);
Mockito.when(buildContext.isOffline()).thenReturn(true);
ContainerConfigurationTemplate containerConfigJson = new ContainerConfigurationTemplate();
containerConfigJson.setArchitecture("slim arch");
containerConfigJson.setOs("fat system");
ManifestAndConfigTemplate manifestAndConfig = new ManifestAndConfigTemplate(new V22ManifestTemplate(), containerConfigJson, "sha256:digest");
ImageMetadataTemplate imageMetadata = new ImageMetadataTemplate(null, Arrays.asList(manifestAndConfig));
Mockito.when(cache.retrieveMetadata(imageReference)).thenReturn(Optional.of(imageMetadata));
ImagesAndRegistryClient result = pullBaseImageStep.call();
Assert.assertEquals("fat system", result.images.get(0).getOs());
Assert.assertNull(result.registryClient);
Mockito.verify(buildContext, Mockito.never()).newBaseImageRegistryClientFactory();
}
use of com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient in project jib by GoogleContainerTools.
the class PullBaseImageStepTest method testCall_scratch_multiplePlatforms.
@Test
public void testCall_scratch_multiplePlatforms() throws LayerPropertyNotFoundException, IOException, RegistryException, LayerCountMismatchException, BadContainerConfigurationFormatException, CacheCorruptedException, CredentialRetrievalException {
Mockito.when(imageConfiguration.getImage()).thenReturn(ImageReference.scratch());
Mockito.when(containerConfig.getPlatforms()).thenReturn(ImmutableSet.of(new Platform("architecture1", "os1"), new Platform("architecture2", "os2")));
ImagesAndRegistryClient result = pullBaseImageStep.call();
Assert.assertEquals(2, result.images.size());
Assert.assertEquals("architecture1", result.images.get(0).getArchitecture());
Assert.assertEquals("os1", result.images.get(0).getOs());
Assert.assertEquals("architecture2", result.images.get(1).getArchitecture());
Assert.assertEquals("os2", result.images.get(1).getOs());
Assert.assertNull(result.registryClient);
}
use of com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient in project jib by GoogleContainerTools.
the class PullBaseImageStepTest method testTryMirrors_multipleMirrors.
@Test
public void testTryMirrors_multipleMirrors() throws LayerCountMismatchException, BadContainerConfigurationFormatException, IOException, RegistryException, InvalidImageReferenceException {
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("quay.io")).thenReturn(registryClientFactory);
Mockito.when(registryClient.pullManifest(Mockito.any())).thenThrow(new RegistryException("not found"));
RegistryClient.Factory gcrRegistryClientFactory = setUpWorkingRegistryClientFactory();
Mockito.when(buildContext.newBaseImageRegistryClientFactory("gcr.io")).thenReturn(gcrRegistryClientFactory);
Optional<ImagesAndRegistryClient> result = pullBaseImageStep.tryMirrors(buildContext, progressDispatcherFactory);
Assert.assertTrue(result.isPresent());
Assert.assertEquals(gcrRegistryClientFactory.newRegistryClient(), result.get().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.info("pulled manifest from mirror gcr.io"));
}
use of com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient in project jib by GoogleContainerTools.
the class StepsRunnerTest method testObtainBaseImageLayers_skipObtainingDuplicateLayers.
@Test
public void testObtainBaseImageLayers_skipObtainingDuplicateLayers() throws DigestException, InterruptedException, ExecutionException {
Mockito.when(executorService.submit(Mockito.any(PullBaseImageStep.class))).thenReturn(Futures.immediateFuture(new ImagesAndRegistryClient(null, null)));
// Pretend that a thread pulling base images returned some (meaningless) result.
stepsRunner.pullBaseImages(progressDispatcherFactory);
DescriptorDigest digest1 = DescriptorDigest.fromHash("1111111111111111111111111111111111111111111111111111111111111111");
DescriptorDigest digest2 = DescriptorDigest.fromHash("2222222222222222222222222222222222222222222222222222222222222222");
DescriptorDigest digest3 = DescriptorDigest.fromHash("3333333333333333333333333333333333333333333333333333333333333333");
DigestOnlyLayer layer1 = new DigestOnlyLayer(digest1);
DigestOnlyLayer layer2 = new DigestOnlyLayer(digest2);
DigestOnlyLayer layer3 = new DigestOnlyLayer(digest3);
PreparedLayer preparedLayer1 = Mockito.mock(PreparedLayer.class);
PreparedLayer preparedLayer2 = Mockito.mock(PreparedLayer.class);
PreparedLayer preparedLayer3 = Mockito.mock(PreparedLayer.class);
Mockito.when(executorService.submit(Mockito.any(ObtainBaseImageLayerStep.class))).thenReturn(Futures.immediateFuture(preparedLayer1)).thenReturn(Futures.immediateFuture(preparedLayer2)).thenReturn(Futures.immediateFuture(preparedLayer3));
Map<DescriptorDigest, Future<PreparedLayer>> preparedLayersCache = new HashMap<>();
// 1. Should schedule two threads to obtain new layers.
Image image = Mockito.mock(Image.class);
Mockito.when(image.getLayers()).thenReturn(ImmutableList.of(layer1, layer2));
stepsRunner.obtainBaseImageLayers(image, true, preparedLayersCache, progressDispatcherFactory);
// two new layers cached
Assert.assertEquals(2, preparedLayersCache.size());
Assert.assertEquals(preparedLayer1, preparedLayersCache.get(digest1).get());
Assert.assertEquals(preparedLayer2, preparedLayersCache.get(digest2).get());
// 2. Should not schedule threads for existing layers.
stepsRunner.obtainBaseImageLayers(image, true, preparedLayersCache, progressDispatcherFactory);
// no new layers cached (still 2)
Assert.assertEquals(2, preparedLayersCache.size());
Assert.assertEquals(preparedLayer1, preparedLayersCache.get(digest1).get());
Assert.assertEquals(preparedLayer2, preparedLayersCache.get(digest2).get());
// 3. Another image with one duplicate layer.
Mockito.when(image.getLayers()).thenReturn(ImmutableList.of(layer3, layer2));
stepsRunner.obtainBaseImageLayers(image, true, preparedLayersCache, progressDispatcherFactory);
// one new layer cached
Assert.assertEquals(3, preparedLayersCache.size());
Assert.assertEquals(preparedLayer1, preparedLayersCache.get(digest1).get());
Assert.assertEquals(preparedLayer2, preparedLayersCache.get(digest2).get());
Assert.assertEquals(preparedLayer3, preparedLayersCache.get(digest3).get());
// Total three threads scheduled for the three unique layers.
Mockito.verify(executorService, Mockito.times(3)).submit(Mockito.any(ObtainBaseImageLayerStep.class));
}
use of com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient in project jib by GoogleContainerTools.
the class PullBaseImageStep method tryMirrors.
@VisibleForTesting
Optional<ImagesAndRegistryClient> tryMirrors(BuildContext buildContext, ProgressEventDispatcher.Factory progressDispatcherFactory) throws LayerCountMismatchException, BadContainerConfigurationFormatException {
EventHandlers eventHandlers = buildContext.getEventHandlers();
Collection<Map.Entry<String, String>> mirrorEntries = buildContext.getRegistryMirrors().entries();
try (ProgressEventDispatcher progressDispatcher1 = progressDispatcherFactory.create("trying mirrors", mirrorEntries.size());
TimerEventDispatcher ignored1 = new TimerEventDispatcher(eventHandlers, "trying mirrors")) {
for (Map.Entry<String, String> entry : mirrorEntries) {
String registry = entry.getKey();
String mirror = entry.getValue();
eventHandlers.dispatch(LogEvent.debug("mirror config: " + registry + " --> " + mirror));
if (!buildContext.getBaseImageConfiguration().getImageRegistry().equals(registry)) {
progressDispatcher1.dispatchProgress(1);
continue;
}
eventHandlers.dispatch(LogEvent.info("trying mirror " + mirror + " for the base image"));
try (ProgressEventDispatcher progressDispatcher2 = progressDispatcher1.newChildProducer().create("trying mirror " + mirror, 2)) {
RegistryClient registryClient = buildContext.newBaseImageRegistryClientFactory(mirror).newRegistryClient();
List<Image> images = pullPublicImages(registryClient, progressDispatcher2);
eventHandlers.dispatch(LogEvent.info("pulled manifest from mirror " + mirror));
return Optional.of(new ImagesAndRegistryClient(images, registryClient));
} catch (IOException | RegistryException ex) {
// Ignore errors from this mirror and continue.
eventHandlers.dispatch(LogEvent.debug("failed to get manifest from mirror " + mirror + ": " + ex.getMessage()));
}
}
return Optional.empty();
}
}
Aggregations