Search in sources :

Example 26 with FileEntry

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

the class LayerEntriesSelectorTest method testGenerateSelector_sourceModificationTimeChanged.

@Test
public void testGenerateSelector_sourceModificationTimeChanged() throws IOException {
    Path layerFile = temporaryFolder.newFile().toPath();
    Files.setLastModifiedTime(layerFile, FileTime.from(Instant.EPOCH));
    FileEntry layerEntry = defaultLayerEntry(layerFile, AbsoluteUnixPath.get("/extraction/path"));
    DescriptorDigest expectedSelector = LayerEntriesSelector.generateSelector(ImmutableList.of(layerEntry));
    // Verify that changing source modification time generates a different selector
    Files.setLastModifiedTime(layerFile, FileTime.from(Instant.ofEpochSecond(1)));
    Assert.assertNotEquals(expectedSelector, LayerEntriesSelector.generateSelector(ImmutableList.of(layerEntry)));
    // Verify that changing source modification time back generates same selector
    Files.setLastModifiedTime(layerFile, FileTime.from(Instant.EPOCH));
    Assert.assertEquals(expectedSelector, LayerEntriesSelector.generateSelector(ImmutableList.of(layerEntry)));
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Path(java.nio.file.Path) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Test(org.junit.Test)

Example 27 with FileEntry

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

the class LayerEntriesSelectorTest method testGenerateSelector_targetModificationTimeChanged.

@Test
public void testGenerateSelector_targetModificationTimeChanged() throws IOException {
    Path layerFile = temporaryFolder.newFile().toPath();
    AbsoluteUnixPath pathInContainer = AbsoluteUnixPath.get("/bar");
    FilePermissions permissions = FilePermissions.fromOctalString("111");
    FileEntry layerEntry1 = new FileEntry(layerFile, pathInContainer, permissions, Instant.now());
    FileEntry layerEntry2 = new FileEntry(layerFile, pathInContainer, permissions, Instant.EPOCH);
    // Verify that different target modification times generate different selectors
    Assert.assertNotEquals(LayerEntriesSelector.generateSelector(ImmutableList.of(layerEntry1)), LayerEntriesSelector.generateSelector(ImmutableList.of(layerEntry2)));
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Test(org.junit.Test)

Example 28 with FileEntry

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

the class BuildAndCacheApplicationLayerStepTest method testRun_emptyLayersIgnored.

@Test
public void testRun_emptyLayersIgnored() throws IOException, CacheCorruptedException {
    ImmutableList<FileEntriesLayer> fakeLayerConfigurations = ImmutableList.of(fakeDependenciesLayerConfiguration, emptyLayerConfiguration, fakeResourcesLayerConfiguration, fakeClassesLayerConfiguration, emptyLayerConfiguration);
    Mockito.when(mockBuildContext.getLayerConfigurations()).thenReturn(fakeLayerConfigurations);
    // Populates the cache.
    List<Layer> applicationLayers = buildFakeLayersToCache();
    Assert.assertEquals(3, applicationLayers.size());
    ImmutableList<FileEntry> dependenciesLayerEntries = ImmutableList.copyOf(fakeLayerConfigurations.get(0).getEntries());
    ImmutableList<FileEntry> resourcesLayerEntries = ImmutableList.copyOf(fakeLayerConfigurations.get(2).getEntries());
    ImmutableList<FileEntry> classesLayerEntries = ImmutableList.copyOf(fakeLayerConfigurations.get(3).getEntries());
    CachedLayer dependenciesCachedLayer = cache.retrieve(dependenciesLayerEntries).orElseThrow(AssertionError::new);
    CachedLayer resourcesCachedLayer = cache.retrieve(resourcesLayerEntries).orElseThrow(AssertionError::new);
    CachedLayer classesCachedLayer = cache.retrieve(classesLayerEntries).orElseThrow(AssertionError::new);
    // Verifies that the cached layers are up-to-date.
    Assert.assertEquals(applicationLayers.get(0).getBlobDescriptor().getDigest(), dependenciesCachedLayer.getDigest());
    Assert.assertEquals(applicationLayers.get(1).getBlobDescriptor().getDigest(), resourcesCachedLayer.getDigest());
    Assert.assertEquals(applicationLayers.get(2).getBlobDescriptor().getDigest(), classesCachedLayer.getDigest());
    // Verifies that the cache reader gets the same layers as the newest application layers.
    assertBlobsEqual(applicationLayers.get(0).getBlob(), dependenciesCachedLayer.getBlob());
    assertBlobsEqual(applicationLayers.get(1).getBlob(), resourcesCachedLayer.getBlob());
    assertBlobsEqual(applicationLayers.get(2).getBlob(), classesCachedLayer.getBlob());
}
Also used : FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Layer(com.google.cloud.tools.jib.image.Layer) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) Test(org.junit.Test)

Example 29 with FileEntry

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

the class JibContainerBuilderTest method testToContainerBuildPlan.

@Test
public void testToContainerBuildPlan() throws InvalidImageReferenceException, IOException {
    ImageConfiguration imageConfiguration = ImageConfiguration.builder(ImageReference.parse("base/image")).build();
    JibContainerBuilder containerBuilder = new JibContainerBuilder(imageConfiguration, spyBuildContextBuilder).setPlatforms(ImmutableSet.of(new Platform("testArchitecture", "testOS"))).setCreationTime(Instant.ofEpochMilli(1000)).setFormat(ImageFormat.OCI).setEnvironment(ImmutableMap.of("env", "var")).setLabels(ImmutableMap.of("com.example.label", "value")).setVolumes(AbsoluteUnixPath.get("/mnt/vol"), AbsoluteUnixPath.get("/media/data")).setExposedPorts(ImmutableSet.of(Port.tcp(1234), Port.udp(5678))).setUser("user").setWorkingDirectory(AbsoluteUnixPath.get("/working/directory")).setEntrypoint(Arrays.asList("entry", "point")).setProgramArguments(Arrays.asList("program", "arguments")).addLayer(Arrays.asList(Paths.get("/non/existing/foo")), "/into/this");
    ContainerBuildPlan buildPlan = containerBuilder.toContainerBuildPlan();
    Assert.assertEquals("base/image", buildPlan.getBaseImage());
    Assert.assertEquals(ImmutableSet.of(new Platform("testArchitecture", "testOS")), buildPlan.getPlatforms());
    Assert.assertEquals(Instant.ofEpochMilli(1000), buildPlan.getCreationTime());
    Assert.assertEquals(ImageFormat.OCI, buildPlan.getFormat());
    Assert.assertEquals(ImmutableMap.of("env", "var"), buildPlan.getEnvironment());
    Assert.assertEquals(ImmutableMap.of("com.example.label", "value"), buildPlan.getLabels());
    Assert.assertEquals(ImmutableSet.of(AbsoluteUnixPath.get("/mnt/vol"), AbsoluteUnixPath.get("/media/data")), buildPlan.getVolumes());
    Assert.assertEquals(ImmutableSet.of(Port.tcp(1234), Port.udp(5678)), buildPlan.getExposedPorts());
    Assert.assertEquals("user", buildPlan.getUser());
    Assert.assertEquals(AbsoluteUnixPath.get("/working/directory"), buildPlan.getWorkingDirectory());
    Assert.assertEquals(Arrays.asList("entry", "point"), buildPlan.getEntrypoint());
    Assert.assertEquals(Arrays.asList("program", "arguments"), buildPlan.getCmd());
    Assert.assertEquals(1, buildPlan.getLayers().size());
    MatcherAssert.assertThat(buildPlan.getLayers().get(0), CoreMatchers.instanceOf(FileEntriesLayer.class));
    Assert.assertEquals(Arrays.asList(new FileEntry(Paths.get("/non/existing/foo"), AbsoluteUnixPath.get("/into/this/foo"), FilePermissions.fromOctalString("644"), Instant.ofEpochSecond(1))), ((FileEntriesLayer) buildPlan.getLayers().get(0)).getEntries());
}
Also used : Platform(com.google.cloud.tools.jib.api.buildplan.Platform) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 30 with FileEntry

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

the class LayerEntriesSelector method toSortedJsonTemplates.

/**
 * Converts a list of {@link FileEntry}s into a list of {@link LayerEntryTemplate}. The list is
 * sorted by source file first, then extraction path (see {@link LayerEntryTemplate#compareTo}).
 *
 * @param layerEntries the list of {@link FileEntry} to convert
 * @return list of {@link LayerEntryTemplate} after sorting
 * @throws IOException if checking the file creation time of a layer entry fails
 */
@VisibleForTesting
static List<LayerEntryTemplate> toSortedJsonTemplates(List<FileEntry> layerEntries) throws IOException {
    List<LayerEntryTemplate> jsonTemplates = new ArrayList<>();
    for (FileEntry entry : layerEntries) {
        jsonTemplates.add(new LayerEntryTemplate(entry));
    }
    Collections.sort(jsonTemplates);
    return jsonTemplates;
}
Also used : ArrayList(java.util.ArrayList) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)64 Test (org.junit.Test)45 Path (java.nio.file.Path)39 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)34 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)24 ContainerBuildPlan (com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)16 ArrayList (java.util.ArrayList)13 FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)10 Blob (com.google.cloud.tools.jib.blob.Blob)10 VisibleForTesting (com.google.common.annotations.VisibleForTesting)9 List (java.util.List)9 BufferedOutputStream (java.io.BufferedOutputStream)8 OutputStream (java.io.OutputStream)8 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)8 PathMatcher (java.nio.file.PathMatcher)7 Instant (java.time.Instant)7 Optional (java.util.Optional)7 Collectors (java.util.stream.Collectors)7 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)6 CachedLayer (com.google.cloud.tools.jib.cache.CachedLayer)6