Search in sources :

Example 11 with EventHandlers

use of com.google.cloud.tools.jib.event.EventHandlers in project jib by GoogleContainerTools.

the class BuildContextTest method testBuilder_digestWarning.

@Test
public void testBuilder_digestWarning() throws CacheDirectoryCreationException, InvalidImageReferenceException {
    EventHandlers mockEventHandlers = Mockito.mock(EventHandlers.class);
    BuildContext.Builder builder = createBasicTestBuilder().setEventHandlers(mockEventHandlers);
    builder.setBaseImageConfiguration(ImageConfiguration.builder(ImageReference.parse("image@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")).build()).build();
    Mockito.verify(mockEventHandlers, Mockito.never()).dispatch(LogEvent.warn(Mockito.anyString()));
    builder.setBaseImageConfiguration(ImageConfiguration.builder(ImageReference.parse("image:tag")).build()).build();
    Mockito.verify(mockEventHandlers).dispatch(LogEvent.warn("Base image 'image:tag' does not use a specific image digest - build may not be reproducible"));
}
Also used : EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Test(org.junit.Test)

Example 12 with EventHandlers

use of com.google.cloud.tools.jib.event.EventHandlers in project jib by GoogleContainerTools.

the class MavenRawConfigurationTest method testGetters.

@Test
public void testGetters() {
    JibPluginConfiguration jibPluginConfiguration = Mockito.mock(JibPluginConfiguration.class);
    EventHandlers eventHandlers = Mockito.mock(EventHandlers.class);
    Server server = Mockito.mock(Server.class);
    Mockito.when(server.getUsername()).thenReturn("maven settings user");
    Mockito.when(server.getPassword()).thenReturn("maven settings password");
    Settings mavenSettings = Mockito.mock(Settings.class);
    Mockito.when(mavenSettings.getServer("base registry")).thenReturn(server);
    MavenSession mavenSession = Mockito.mock(MavenSession.class);
    Mockito.when(mavenSession.getSettings()).thenReturn(mavenSettings);
    FromAuthConfiguration auth = Mockito.mock(FromAuthConfiguration.class);
    Mockito.when(auth.getUsername()).thenReturn("user");
    Mockito.when(auth.getPassword()).thenReturn("password");
    Mockito.when(auth.getAuthDescriptor()).thenReturn("<from><auth>");
    Mockito.when(auth.getUsernameDescriptor()).thenReturn("<from><auth><username>");
    Mockito.when(auth.getPasswordDescriptor()).thenReturn("<from><auth><password>");
    Mockito.when(jibPluginConfiguration.getSession()).thenReturn(mavenSession);
    Mockito.when(jibPluginConfiguration.getBaseImageAuth()).thenReturn(auth);
    Mockito.when(jibPluginConfiguration.getAllowInsecureRegistries()).thenReturn(true);
    Mockito.when(jibPluginConfiguration.getAppRoot()).thenReturn("/app/root");
    Mockito.when(jibPluginConfiguration.getArgs()).thenReturn(Arrays.asList("--log", "info"));
    Mockito.when(jibPluginConfiguration.getBaseImage()).thenReturn("openjdk:15");
    CredHelperConfiguration baseImageCredHelperConfig = Mockito.mock(CredHelperConfiguration.class);
    Mockito.when(baseImageCredHelperConfig.getHelperName()).thenReturn(Optional.of("gcr"));
    Mockito.when(baseImageCredHelperConfig.getEnvironment()).thenReturn(Collections.singletonMap("ENV_VARIABLE", "Value1"));
    Mockito.when(jibPluginConfiguration.getBaseImageCredHelperConfig()).thenReturn(baseImageCredHelperConfig);
    CredHelperConfiguration targetImageCredHelperConfig = Mockito.mock(CredHelperConfiguration.class);
    Mockito.when(targetImageCredHelperConfig.getHelperName()).thenReturn(Optional.of("gcr"));
    Mockito.when(targetImageCredHelperConfig.getEnvironment()).thenReturn(Collections.singletonMap("ENV_VARIABLE", "Value2"));
    Mockito.when(jibPluginConfiguration.getTargetImageCredentialHelperConfig()).thenReturn(targetImageCredHelperConfig);
    Mockito.when(jibPluginConfiguration.getEntrypoint()).thenReturn(Arrays.asList("java", "Main"));
    Mockito.when(jibPluginConfiguration.getEnvironment()).thenReturn(new HashMap<>(ImmutableMap.of("currency", "dollar")));
    Mockito.when(jibPluginConfiguration.getExposedPorts()).thenReturn(Arrays.asList("80/tcp", "0"));
    Mockito.when(jibPluginConfiguration.getJvmFlags()).thenReturn(Arrays.asList("-cp", "."));
    Mockito.when(jibPluginConfiguration.getLabels()).thenReturn(new HashMap<>(ImmutableMap.of("unit", "cm")));
    Mockito.when(jibPluginConfiguration.getMainClass()).thenReturn("com.example.Main");
    Mockito.when(jibPluginConfiguration.getTargetImageAdditionalTags()).thenReturn(new HashSet<>(Arrays.asList("additional", "tags")));
    Mockito.when(jibPluginConfiguration.getUser()).thenReturn("admin:wheel");
    Mockito.when(jibPluginConfiguration.getFilesModificationTime()).thenReturn("2011-12-03T22:42:05Z");
    Mockito.when(jibPluginConfiguration.getDockerClientExecutable()).thenReturn(Paths.get("test"));
    Mockito.when(jibPluginConfiguration.getDockerClientEnvironment()).thenReturn(new HashMap<>(ImmutableMap.of("docker", "client")));
    Mockito.when(jibPluginConfiguration.getDigestOutputPath()).thenReturn(Paths.get("digest/path"));
    Mockito.when(jibPluginConfiguration.getImageIdOutputPath()).thenReturn(Paths.get("id/path"));
    Mockito.when(jibPluginConfiguration.getImageJsonOutputPath()).thenReturn(Paths.get("json/path"));
    Mockito.when(jibPluginConfiguration.getTarOutputPath()).thenReturn(Paths.get("tar/path"));
    MavenRawConfiguration rawConfiguration = new MavenRawConfiguration(jibPluginConfiguration);
    AuthProperty fromAuth = rawConfiguration.getFromAuth();
    Assert.assertEquals("user", fromAuth.getUsername());
    Assert.assertEquals("password", fromAuth.getPassword());
    Assert.assertEquals("<from><auth>", fromAuth.getAuthDescriptor());
    Assert.assertEquals("<from><auth><username>", fromAuth.getUsernameDescriptor());
    Assert.assertEquals("<from><auth><password>", fromAuth.getPasswordDescriptor());
    Assert.assertTrue(rawConfiguration.getAllowInsecureRegistries());
    Assert.assertEquals(Arrays.asList("java", "Main"), rawConfiguration.getEntrypoint().get());
    Assert.assertEquals(new HashMap<>(ImmutableMap.of("currency", "dollar")), rawConfiguration.getEnvironment());
    Assert.assertEquals("/app/root", rawConfiguration.getAppRoot());
    Assert.assertEquals("gcr", rawConfiguration.getFromCredHelper().getHelperName().get());
    Assert.assertEquals(Collections.singletonMap("ENV_VARIABLE", "Value1"), rawConfiguration.getFromCredHelper().getEnvironment());
    Assert.assertEquals("gcr", rawConfiguration.getToCredHelper().getHelperName().get());
    Assert.assertEquals(Collections.singletonMap("ENV_VARIABLE", "Value2"), rawConfiguration.getToCredHelper().getEnvironment());
    Assert.assertEquals("openjdk:15", rawConfiguration.getFromImage().get());
    Assert.assertEquals(Arrays.asList("-cp", "."), rawConfiguration.getJvmFlags());
    Assert.assertEquals(new HashMap<>(ImmutableMap.of("unit", "cm")), rawConfiguration.getLabels());
    Assert.assertEquals("com.example.Main", rawConfiguration.getMainClass().get());
    Assert.assertEquals(Arrays.asList("80/tcp", "0"), rawConfiguration.getPorts());
    Assert.assertEquals(Arrays.asList("--log", "info"), rawConfiguration.getProgramArguments().get());
    Assert.assertEquals(new HashSet<>(Arrays.asList("additional", "tags")), Sets.newHashSet(rawConfiguration.getToTags()));
    Assert.assertEquals("admin:wheel", rawConfiguration.getUser().get());
    Assert.assertEquals("2011-12-03T22:42:05Z", rawConfiguration.getFilesModificationTime());
    Assert.assertEquals(Paths.get("test"), rawConfiguration.getDockerExecutable().get());
    Assert.assertEquals(new HashMap<>(ImmutableMap.of("docker", "client")), rawConfiguration.getDockerEnvironment());
    Assert.assertEquals(Paths.get("digest/path"), jibPluginConfiguration.getDigestOutputPath());
    Assert.assertEquals(Paths.get("id/path"), jibPluginConfiguration.getImageIdOutputPath());
    Assert.assertEquals(Paths.get("json/path"), jibPluginConfiguration.getImageJsonOutputPath());
    Assert.assertEquals(Paths.get("tar/path"), jibPluginConfiguration.getTarOutputPath());
    Mockito.verifyNoMoreInteractions(eventHandlers);
}
Also used : AuthProperty(com.google.cloud.tools.jib.plugins.common.AuthProperty) MavenSession(org.apache.maven.execution.MavenSession) FromAuthConfiguration(com.google.cloud.tools.jib.maven.JibPluginConfiguration.FromAuthConfiguration) Server(org.apache.maven.settings.Server) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Settings(org.apache.maven.settings.Settings) CredHelperConfiguration(com.google.cloud.tools.jib.plugins.common.RawConfiguration.CredHelperConfiguration) Test(org.junit.Test)

Example 13 with EventHandlers

use of com.google.cloud.tools.jib.event.EventHandlers in project jib by GoogleContainerTools.

the class ProgressEventTest method testAccumulateProgress.

@Test
public void testAccumulateProgress() {
    Consumer<ProgressEvent> progressEventConsumer = progressEvent -> {
        double fractionOfRoot = progressEvent.getAllocation().getFractionOfRoot();
        long units = progressEvent.getUnits();
        progress += units * fractionOfRoot;
    };
    EventHandlers eventHandlers = makeEventHandlers(progressEventConsumer);
    eventHandlers.dispatch(new ProgressEvent(AllocationTree.child1Child, 50));
    Assert.assertEquals(1.0 / 2 / 100 * 50, progress, DOUBLE_ERROR_MARGIN);
    eventHandlers.dispatch(new ProgressEvent(AllocationTree.child1Child, 50));
    Assert.assertEquals(1.0 / 2, progress, DOUBLE_ERROR_MARGIN);
    eventHandlers.dispatch(new ProgressEvent(AllocationTree.child2, 10));
    Assert.assertEquals(1.0 / 2 + 1.0 / 2 / 200 * 10, progress, DOUBLE_ERROR_MARGIN);
    eventHandlers.dispatch(new ProgressEvent(AllocationTree.child2, 190));
    Assert.assertEquals(1.0, progress, DOUBLE_ERROR_MARGIN);
}
Also used : Consumer(java.util.function.Consumer) Allocation(com.google.cloud.tools.jib.event.progress.Allocation) Map(java.util.Map) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) HashMap(java.util.HashMap) Test(org.junit.Test) Assert(org.junit.Assert) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Test(org.junit.Test)

Example 14 with EventHandlers

use of com.google.cloud.tools.jib.event.EventHandlers in project jib by GoogleContainerTools.

the class ProgressEventTest method testType.

@Test
public void testType() {
    // Used to test whether or not progress event was consumed
    boolean[] called = new boolean[] { false };
    Consumer<ProgressEvent> buildImageConsumer = progressEvent -> {
        called[0] = true;
    };
    EventHandlers eventHandlers = makeEventHandlers(buildImageConsumer);
    eventHandlers.dispatch(new ProgressEvent(AllocationTree.child1, 50));
    Assert.assertTrue(called[0]);
}
Also used : Consumer(java.util.function.Consumer) Allocation(com.google.cloud.tools.jib.event.progress.Allocation) Map(java.util.Map) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) HashMap(java.util.HashMap) Test(org.junit.Test) Assert(org.junit.Assert) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Test(org.junit.Test)

Example 15 with EventHandlers

use of com.google.cloud.tools.jib.event.EventHandlers in project jib by google.

the class BuildAndCacheApplicationLayerStep method call.

@Override
public PreparedLayer call() throws IOException, CacheCorruptedException {
    String description = String.format(DESCRIPTION, layerName);
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    eventHandlers.dispatch(LogEvent.progress(description + "..."));
    try (ProgressEventDispatcher ignored = progressEventDispatcherFactory.create("building " + layerName + " layer", 1);
        TimerEventDispatcher ignored2 = new TimerEventDispatcher(eventHandlers, description)) {
        Cache cache = buildContext.getApplicationLayersCache();
        ImmutableList<FileEntry> layerEntries = ImmutableList.copyOf(layerConfiguration.getEntries());
        // Don't build the layer if it exists already.
        Optional<CachedLayer> optionalCachedLayer = cache.retrieve(layerEntries);
        if (optionalCachedLayer.isPresent()) {
            return new PreparedLayer.Builder(optionalCachedLayer.get()).setName(layerName).build();
        }
        Blob layerBlob = new ReproducibleLayerBuilder(layerEntries).build();
        CachedLayer cachedLayer = cache.writeUncompressedLayer(layerBlob, layerEntries);
        eventHandlers.dispatch(LogEvent.debug(description + " built " + cachedLayer.getDigest()));
        return new PreparedLayer.Builder(cachedLayer).setName(layerName).build();
    }
}
Also used : Blob(com.google.cloud.tools.jib.blob.Blob) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) ReproducibleLayerBuilder(com.google.cloud.tools.jib.image.ReproducibleLayerBuilder) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) ReproducibleLayerBuilder(com.google.cloud.tools.jib.image.ReproducibleLayerBuilder) Cache(com.google.cloud.tools.jib.cache.Cache)

Aggregations

EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)42 ProgressEventDispatcher (com.google.cloud.tools.jib.builder.ProgressEventDispatcher)24 TimerEventDispatcher (com.google.cloud.tools.jib.builder.TimerEventDispatcher)22 Test (org.junit.Test)14 DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)10 Image (com.google.cloud.tools.jib.image.Image)10 IOException (java.io.IOException)10 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)8 ImmutableList (com.google.common.collect.ImmutableList)8 Map (java.util.Map)8 RegistryException (com.google.cloud.tools.jib.api.RegistryException)6 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)6 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)6 Cache (com.google.cloud.tools.jib.cache.Cache)6 Allocation (com.google.cloud.tools.jib.event.progress.Allocation)6 BuildableManifestTemplate (com.google.cloud.tools.jib.image.json.BuildableManifestTemplate)6 ImageToJsonTranslator (com.google.cloud.tools.jib.image.json.ImageToJsonTranslator)6 ManifestTemplate (com.google.cloud.tools.jib.image.json.ManifestTemplate)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6