Search in sources :

Example 51 with FileEntriesLayer

use of com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer in project docker-maven-plugin by fabric8io.

the class JibServiceUtilTest method testCopyToContainer.

@Test
@Category(UnixOnlyTests.class)
public void testCopyToContainer(@Mocked JibContainerBuilder containerBuilder) throws IOException {
    // Given
    File temporaryDirectory = Files.createTempDirectory("jib-test").toFile();
    File temporaryFile = new File(temporaryDirectory, "foo.txt");
    boolean wasNewFileCreated = temporaryFile.createNewFile();
    String tmpRoot = temporaryDirectory.getParent();
    // When
    JibServiceUtil.copyToContainer(containerBuilder, temporaryDirectory, tmpRoot, Collections.emptyMap());
    // Then
    assertTrue(wasNewFileCreated);
    new Verifications() {

        {
            FileEntriesLayer fileEntriesLayer;
            containerBuilder.addFileEntriesLayer(fileEntriesLayer = withCapture());
            assertNotNull(fileEntriesLayer);
            assertEquals(1, fileEntriesLayer.getEntries().size());
            assertEquals(temporaryFile.toPath(), fileEntriesLayer.getEntries().get(0).getSourceFile());
            assertEquals(AbsoluteUnixPath.fromPath(Paths.get(temporaryFile.getAbsolutePath().substring(tmpRoot.length()))), fileEntriesLayer.getEntries().get(0).getExtractionPath());
        }
    };
}
Also used : FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Verifications(mockit.Verifications) File(java.io.File) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 52 with FileEntriesLayer

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

the class JavaContainerBuilderHelperTest method testExtraDirectoryLayerConfiguration_overlappingPermissions.

@Test
public void testExtraDirectoryLayerConfiguration_overlappingPermissions() throws URISyntaxException, IOException {
    Path extraFilesDirectory = Paths.get(Resources.getResource("core/layer").toURI());
    Map<String, FilePermissions> permissionsMap = ImmutableMap.of("/a**", FilePermissions.fromOctalString("123"), // Should be ignored, since first match takes priority
    "/a/b**", FilePermissions.fromOctalString("000"), // Should override first match since explicit path is used instead of glob
    "/a/b/bar", FilePermissions.fromOctalString("765"));
    FileEntriesLayer fileEntriesLayer = JavaContainerBuilderHelper.extraDirectoryLayerConfiguration(extraFilesDirectory, AbsoluteUnixPath.get("/"), Collections.emptyList(), Collections.emptyList(), permissionsMap, (ignored1, ignored2) -> Instant.EPOCH);
    assertThat(fileEntriesLayer.getEntries()).comparingElementsUsing(EXTRACTION_PATH_OF).containsExactly("/a", "/a/b", "/a/b/bar", "/c", "/c/cat", "/foo");
    Map<AbsoluteUnixPath, FilePermissions> expectedPermissions = ImmutableMap.<AbsoluteUnixPath, FilePermissions>builder().put(AbsoluteUnixPath.get("/a"), FilePermissions.fromOctalString("123")).put(AbsoluteUnixPath.get("/a/b"), FilePermissions.fromOctalString("123")).put(AbsoluteUnixPath.get("/a/b/bar"), FilePermissions.fromOctalString("765")).put(AbsoluteUnixPath.get("/c"), FilePermissions.DEFAULT_FOLDER_PERMISSIONS).put(AbsoluteUnixPath.get("/c/cat"), FilePermissions.DEFAULT_FILE_PERMISSIONS).put(AbsoluteUnixPath.get("/foo"), FilePermissions.DEFAULT_FILE_PERMISSIONS).build();
    for (FileEntry entry : fileEntriesLayer.getEntries()) {
        assertThat(entry.getPermissions()).isEqualTo(expectedPermissions.get(entry.getExtractionPath()));
    }
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Test(org.junit.Test)

Example 53 with FileEntriesLayer

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

the class JavaContainerBuilderHelperTest method testExtraDirectoryLayerConfiguration_includesAndExcludesEverything.

@Test
public void testExtraDirectoryLayerConfiguration_includesAndExcludesEverything() throws URISyntaxException, IOException {
    Path extraFilesDirectory = Paths.get(Resources.getResource("core/layer").toURI());
    FileEntriesLayer layerConfiguration = JavaContainerBuilderHelper.extraDirectoryLayerConfiguration(extraFilesDirectory, AbsoluteUnixPath.get("/"), Arrays.asList("**/*"), Arrays.asList("**/*"), Collections.emptyMap(), (ignored1, ignored2) -> Instant.EPOCH);
    assertThat(layerConfiguration.getEntries()).isEmpty();
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Test(org.junit.Test)

Example 54 with FileEntriesLayer

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

the class BuildFilesTest method testToJibContainerBuilder_allProperties.

@Test
public void testToJibContainerBuilder_allProperties() throws URISyntaxException, IOException, InvalidImageReferenceException {
    Path buildfile = Paths.get(Resources.getResource("buildfiles/projects/allProperties/jib.yaml").toURI());
    Path projectRoot = buildfile.getParent();
    JibContainerBuilder jibContainerBuilder = BuildFiles.toJibContainerBuilder(projectRoot, buildfile, buildCli, commonCliOptions, consoleLogger);
    ContainerBuildPlan resolved = jibContainerBuilder.toContainerBuildPlan();
    Assert.assertEquals("ubuntu", resolved.getBaseImage());
    Assert.assertEquals(Instant.ofEpochMilli(2000), resolved.getCreationTime());
    Assert.assertEquals(ImageFormat.OCI, resolved.getFormat());
    Assert.assertEquals(ImmutableSet.of(new Platform("arm", "linux"), new Platform("amd64", "darwin")), resolved.getPlatforms());
    Assert.assertEquals(ImmutableMap.of("KEY1", "v1", "KEY2", "v2"), resolved.getEnvironment());
    Assert.assertEquals(ImmutableSet.of(AbsoluteUnixPath.get("/volume1"), AbsoluteUnixPath.get("/volume2")), resolved.getVolumes());
    Assert.assertEquals(ImmutableMap.of("label1", "l1", "label2", "l2"), resolved.getLabels());
    Assert.assertEquals(ImmutableSet.of(Port.udp(123), Port.tcp(456), Port.tcp(789)), resolved.getExposedPorts());
    Assert.assertEquals("customUser", resolved.getUser());
    Assert.assertEquals(AbsoluteUnixPath.get("/home"), resolved.getWorkingDirectory());
    Assert.assertEquals(ImmutableList.of("sh", "script.sh"), resolved.getEntrypoint());
    Assert.assertEquals(ImmutableList.of("--param", "param"), resolved.getCmd());
    Assert.assertEquals(1, resolved.getLayers().size());
    FileEntriesLayer resolvedLayer = (FileEntriesLayer) resolved.getLayers().get(0);
    Assert.assertEquals("scripts", resolvedLayer.getName());
    Assert.assertEquals(FileEntriesLayer.builder().addEntry(projectRoot.resolve("project/script.sh"), AbsoluteUnixPath.get("/home/script.sh")).build().getEntries(), resolvedLayer.getEntries());
    Assert.assertEquals(LayerObject.Type.FILE_ENTRIES, resolvedLayer.getType());
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 55 with FileEntriesLayer

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

the class ArtifactLayers method getDirectoryContentsAsLayer.

/**
 * Creates a layer containing contents of a directory. Only paths that match the given predicate
 * will be added.
 *
 * @param layerName name of the layer
 * @param sourceRoot path to source directory
 * @param pathFilter predicate to determine whether to add the path or not
 * @param basePathInContainer path to destination on container
 * @return {@link FileEntriesLayer} representing the layer
 * @throws IOException if io exception occurs when reading from the source directory
 */
public static FileEntriesLayer getDirectoryContentsAsLayer(String layerName, Path sourceRoot, Predicate<Path> pathFilter, AbsoluteUnixPath basePathInContainer) throws IOException {
    FileEntriesLayer.Builder builder = FileEntriesLayer.builder().setName(layerName);
    new DirectoryWalker(sourceRoot).filterRoot().filter(path -> pathFilter.test(path)).walk(path -> {
        AbsoluteUnixPath pathOnContainer = basePathInContainer.resolve(sourceRoot.relativize(path));
        builder.addEntry(path, pathOnContainer);
    });
    return builder.build();
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Predicate(java.util.function.Predicate) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) IOException(java.io.IOException) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker)

Aggregations

FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)154 Test (org.junit.Test)111 Path (java.nio.file.Path)92 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)85 ContainerBuildPlan (com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)55 List (java.util.List)29 IOException (java.io.IOException)26 ArrayList (java.util.ArrayList)26 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)24 FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)23 Map (java.util.Map)23 Files (java.nio.file.Files)21 Predicate (java.util.function.Predicate)20 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)16 Collectors (java.util.stream.Collectors)16 FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)13 Paths (java.nio.file.Paths)13 Optional (java.util.Optional)13 JavaContainerBuilder (com.google.cloud.tools.jib.api.JavaContainerBuilder)12 JibPluginExtensionException (com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException)12