Search in sources :

Example 36 with FileEntry

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

the class ReproducibleLayerBuilderTest method testBuild_ownership.

@Test
public void testBuild_ownership() throws IOException {
    Path testRoot = temporaryFolder.getRoot().toPath();
    Path someFile = createFile(testRoot, "someFile", "content", 54321);
    Blob blob = new ReproducibleLayerBuilder(ImmutableList.of(defaultLayerEntry(someFile, AbsoluteUnixPath.get("/file1")), new FileEntry(someFile, AbsoluteUnixPath.get("/file2"), FilePermissions.fromOctalString("123"), Instant.EPOCH, ""), new FileEntry(someFile, AbsoluteUnixPath.get("/file3"), FilePermissions.fromOctalString("123"), Instant.EPOCH, ":"), new FileEntry(someFile, AbsoluteUnixPath.get("/file4"), FilePermissions.fromOctalString("123"), Instant.EPOCH, "333:"), new FileEntry(someFile, AbsoluteUnixPath.get("/file5"), FilePermissions.fromOctalString("123"), Instant.EPOCH, ":555"), new FileEntry(someFile, AbsoluteUnixPath.get("/file6"), FilePermissions.fromOctalString("123"), Instant.EPOCH, "333:555"), new FileEntry(someFile, AbsoluteUnixPath.get("/file7"), FilePermissions.fromOctalString("123"), Instant.EPOCH, "user:"), new FileEntry(someFile, AbsoluteUnixPath.get("/file8"), FilePermissions.fromOctalString("123"), Instant.EPOCH, ":group"), new FileEntry(someFile, AbsoluteUnixPath.get("/file9"), FilePermissions.fromOctalString("123"), Instant.EPOCH, "user:group"))).build();
    Path tarFile = temporaryFolder.newFile().toPath();
    try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(tarFile))) {
        blob.writeTo(out);
    }
    try (TarArchiveInputStream in = new TarArchiveInputStream(Files.newInputStream(tarFile))) {
        TarArchiveEntry entry1 = in.getNextTarEntry();
        assertThat(entry1.getLongUserId()).isEqualTo(0);
        assertThat(entry1.getLongGroupId()).isEqualTo(0);
        assertThat(entry1.getUserName()).isEmpty();
        assertThat(entry1.getGroupName()).isEmpty();
        TarArchiveEntry entry2 = in.getNextTarEntry();
        assertThat(entry2.getLongUserId()).isEqualTo(0);
        assertThat(entry2.getLongGroupId()).isEqualTo(0);
        assertThat(entry2.getUserName()).isEmpty();
        assertThat(entry2.getGroupName()).isEmpty();
        TarArchiveEntry entry3 = in.getNextTarEntry();
        assertThat(entry3.getLongUserId()).isEqualTo(0);
        assertThat(entry3.getLongGroupId()).isEqualTo(0);
        assertThat(entry3.getUserName()).isEmpty();
        assertThat(entry3.getGroupName()).isEmpty();
        TarArchiveEntry entry4 = in.getNextTarEntry();
        assertThat(entry4.getLongUserId()).isEqualTo(333);
        assertThat(entry4.getLongGroupId()).isEqualTo(0);
        assertThat(entry4.getUserName()).isEmpty();
        assertThat(entry4.getGroupName()).isEmpty();
        TarArchiveEntry entry5 = in.getNextTarEntry();
        assertThat(entry5.getLongUserId()).isEqualTo(0);
        assertThat(entry5.getLongGroupId()).isEqualTo(555);
        assertThat(entry5.getUserName()).isEmpty();
        assertThat(entry5.getGroupName()).isEmpty();
        TarArchiveEntry entry6 = in.getNextTarEntry();
        assertThat(entry6.getLongUserId()).isEqualTo(333);
        assertThat(entry6.getLongGroupId()).isEqualTo(555);
        assertThat(entry6.getUserName()).isEmpty();
        assertThat(entry6.getGroupName()).isEmpty();
        TarArchiveEntry entry7 = in.getNextTarEntry();
        assertThat(entry7.getLongUserId()).isEqualTo(0);
        assertThat(entry7.getLongGroupId()).isEqualTo(0);
        assertThat(entry7.getUserName()).isEqualTo("user");
        assertThat(entry7.getGroupName()).isEmpty();
        TarArchiveEntry entry8 = in.getNextTarEntry();
        assertThat(entry8.getLongUserId()).isEqualTo(0);
        assertThat(entry8.getLongGroupId()).isEqualTo(0);
        assertThat(entry8.getUserName()).isEmpty();
        assertThat(entry8.getGroupName()).isEqualTo("group");
        TarArchiveEntry entry9 = in.getNextTarEntry();
        assertThat(entry9.getLongUserId()).isEqualTo(0);
        assertThat(entry9.getLongGroupId()).isEqualTo(0);
        assertThat(entry9.getUserName()).isEqualTo("user");
        assertThat(entry9.getGroupName()).isEqualTo("group");
    }
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) Blob(com.google.cloud.tools.jib.blob.Blob) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Test(org.junit.Test)

Example 37 with FileEntry

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

the class ReproducibleLayerBuilderTest method testBuild_permissions.

@Test
public void testBuild_permissions() throws IOException {
    Path testRoot = temporaryFolder.getRoot().toPath();
    Path folder = Files.createDirectories(testRoot.resolve("files1"));
    Path fileA = createFile(testRoot, "fileA", "abc", 54321);
    Path fileB = createFile(testRoot, "fileB", "def", 54321);
    Blob blob = new ReproducibleLayerBuilder(ImmutableList.of(defaultLayerEntry(fileA, AbsoluteUnixPath.get("/somewhere/fileA")), new FileEntry(fileB, AbsoluteUnixPath.get("/somewhere/fileB"), FilePermissions.fromOctalString("123"), FileEntriesLayer.DEFAULT_MODIFICATION_TIME), new FileEntry(folder, AbsoluteUnixPath.get("/somewhere/folder"), FilePermissions.fromOctalString("456"), FileEntriesLayer.DEFAULT_MODIFICATION_TIME))).build();
    Path tarFile = temporaryFolder.newFile().toPath();
    try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(tarFile))) {
        blob.writeTo(out);
    }
    try (TarArchiveInputStream in = new TarArchiveInputStream(Files.newInputStream(tarFile))) {
        // Root folder (default folder permissions)
        assertThat(in.getNextTarEntry().getMode()).isEqualTo(040755);
        // fileA (default file permissions)
        assertThat(in.getNextTarEntry().getMode()).isEqualTo(0100644);
        // fileB (custom file permissions)
        assertThat(in.getNextTarEntry().getMode()).isEqualTo(0100123);
        // folder (custom folder permissions)
        assertThat(in.getNextTarEntry().getMode()).isEqualTo(040456);
    }
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) Blob(com.google.cloud.tools.jib.blob.Blob) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) BufferedOutputStream(java.io.BufferedOutputStream) Test(org.junit.Test)

Example 38 with FileEntry

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

the class ReproducibleLayerBuilderTest method testBuild_parentDirBehavior.

@Test
public void testBuild_parentDirBehavior() throws IOException {
    Path testRoot = temporaryFolder.getRoot().toPath();
    // the path doesn't really matter on source files, but these are structured
    Path parent = Files.createDirectories(testRoot.resolve("dirA"));
    Path fileA = Files.createFile(parent.resolve("fileA"));
    Path ignoredParent = Files.createDirectories(testRoot.resolve("dirB-ignored"));
    Path fileB = Files.createFile(ignoredParent.resolve("fileB"));
    Path fileC = Files.createFile(Files.createDirectories(testRoot.resolve("dirC-absent")).resolve("fileC"));
    Blob layer = new ReproducibleLayerBuilder(ImmutableList.of(new FileEntry(parent, AbsoluteUnixPath.get("/root/dirA"), FilePermissions.fromOctalString("111"), Instant.ofEpochSecond(10)), new FileEntry(fileA, AbsoluteUnixPath.get("/root/dirA/fileA"), FilePermissions.fromOctalString("222"), Instant.ofEpochSecond(20)), new FileEntry(fileB, AbsoluteUnixPath.get("/root/dirB-ignored/fileB"), FilePermissions.fromOctalString("333"), Instant.ofEpochSecond(30)), new FileEntry(ignoredParent, AbsoluteUnixPath.get("/root/dirB-ignored"), FilePermissions.fromOctalString("444"), Instant.ofEpochSecond(40)), new FileEntry(fileC, AbsoluteUnixPath.get("/root/dirC-absent/file3"), FilePermissions.fromOctalString("555"), Instant.ofEpochSecond(50)))).build();
    Path tarFile = temporaryFolder.newFile().toPath();
    try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(tarFile))) {
        layer.writeTo(out);
    }
    try (TarArchiveInputStream in = new TarArchiveInputStream(Files.newInputStream(tarFile))) {
        // root (default folder permissions)
        TarArchiveEntry root = in.getNextTarEntry();
        assertThat(root.getMode()).isEqualTo(040755);
        assertThat(root.getModTime().toInstant()).isEqualTo(Instant.ofEpochSecond(1));
        assertThat(root.getLongUserId()).isEqualTo(0);
        assertThat(root.getLongGroupId()).isEqualTo(0);
        assertThat(root.getUserName()).isEmpty();
        assertThat(root.getGroupName()).isEmpty();
        // parentAAA (custom permissions, custom timestamp)
        TarArchiveEntry rootParentA = in.getNextTarEntry();
        assertThat(rootParentA.getMode()).isEqualTo(040111);
        assertThat(rootParentA.getModTime().toInstant()).isEqualTo(Instant.ofEpochSecond(10));
        assertThat(rootParentA.getLongUserId()).isEqualTo(0);
        assertThat(rootParentA.getLongGroupId()).isEqualTo(0);
        assertThat(rootParentA.getUserName()).isEmpty();
        assertThat(rootParentA.getGroupName()).isEmpty();
        // skip over fileA
        in.getNextTarEntry();
        // parentBBB (default permissions - ignored custom permissions, since fileB added first)
        TarArchiveEntry rootParentB = in.getNextTarEntry();
        // TODO (#1650): we want 040444 here.
        assertThat(rootParentB.getMode()).isEqualTo(040755);
        // TODO (#1650): we want Instant.ofEpochSecond(40) here.
        assertThat(rootParentB.getModTime().toInstant()).isEqualTo(Instant.ofEpochSecond(1));
        assertThat(rootParentB.getLongUserId()).isEqualTo(0);
        assertThat(rootParentB.getLongGroupId()).isEqualTo(0);
        assertThat(rootParentB.getUserName()).isEmpty();
        assertThat(rootParentB.getGroupName()).isEmpty();
        // skip over fileB
        in.getNextTarEntry();
        // parentCCC (default permissions - no entry provided)
        TarArchiveEntry rootParentC = in.getNextTarEntry();
        assertThat(rootParentC.getMode()).isEqualTo(040755);
        assertThat(rootParentC.getModTime().toInstant()).isEqualTo(Instant.ofEpochSecond(1));
        assertThat(rootParentC.getLongUserId()).isEqualTo(0);
        assertThat(rootParentC.getLongGroupId()).isEqualTo(0);
        assertThat(rootParentC.getUserName()).isEmpty();
        assertThat(rootParentC.getGroupName()).isEmpty();
    // we don't care about fileC
    }
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) Blob(com.google.cloud.tools.jib.blob.Blob) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Test(org.junit.Test)

Example 39 with FileEntry

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

the class ReproducibleLayerBuilderTest method testBuild_timestampNonDefault.

@Test
public void testBuild_timestampNonDefault() throws IOException {
    Path file = createFile(temporaryFolder.getRoot().toPath(), "fileA", "some content", 54321);
    Blob blob = new ReproducibleLayerBuilder(ImmutableList.of(new FileEntry(file, AbsoluteUnixPath.get("/fileA"), FilePermissions.DEFAULT_FILE_PERMISSIONS, Instant.ofEpochSecond(123)))).build();
    Path tarFile = temporaryFolder.newFile().toPath();
    try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(tarFile))) {
        blob.writeTo(out);
    }
    // Reads the file back.
    try (TarArchiveInputStream in = new TarArchiveInputStream(Files.newInputStream(tarFile))) {
        assertThat(in.getNextEntry().getLastModifiedDate().toInstant()).isEqualTo(Instant.EPOCH.plusSeconds(123));
    }
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) Blob(com.google.cloud.tools.jib.blob.Blob) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) BufferedOutputStream(java.io.BufferedOutputStream) Test(org.junit.Test)

Example 40 with FileEntry

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

the class LayerEntriesSelectorTest method testGenerateSelector_permissionsModified.

@Test
public void testGenerateSelector_permissionsModified() throws IOException {
    Path layerFile = temporaryFolder.newFolder("testFolder").toPath().resolve("file");
    Files.write(layerFile, "hello".getBytes(StandardCharsets.UTF_8));
    FileEntry layerEntry111 = new FileEntry(layerFile, AbsoluteUnixPath.get("/extraction/path"), FilePermissions.fromOctalString("111"), FileEntriesLayer.DEFAULT_MODIFICATION_TIME);
    FileEntry layerEntry222 = new FileEntry(layerFile, AbsoluteUnixPath.get("/extraction/path"), FilePermissions.fromOctalString("222"), FileEntriesLayer.DEFAULT_MODIFICATION_TIME);
    // Verify that changing permissions generates a different selector
    Assert.assertNotEquals(LayerEntriesSelector.generateSelector(ImmutableList.of(layerEntry111)), LayerEntriesSelector.generateSelector(ImmutableList.of(layerEntry222)));
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Path(java.nio.file.Path) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Test(org.junit.Test)

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