Search in sources :

Example 51 with FileEntry

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

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 52 with FileEntry

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

the class GradleProjectPropertiesTest method testCreateContainerBuilder_correctSourceFiles.

@Test
public void testCreateContainerBuilder_correctSourceFiles() throws URISyntaxException, InvalidImageReferenceException, CacheDirectoryCreationException {
    ContainerBuilderLayers layers = new ContainerBuilderLayers(setupBuildContext());
    Path applicationDirectory = getResource("gradle/application");
    assertThat(layers.snapshotsLayer.getEntries()).comparingElementsUsing(SOURCE_FILE_OF).containsExactly(applicationDirectory.resolve("dependencies/dependencyX-1.0.0-SNAPSHOT.jar"));
    assertThat(layers.dependenciesLayer.getEntries()).comparingElementsUsing(SOURCE_FILE_OF).containsExactly(applicationDirectory.resolve("dependencies/dependency-1.0.0.jar"), applicationDirectory.resolve("dependencies/more/dependency-1.0.0.jar"), applicationDirectory.resolve("dependencies/another/one/dependency-1.0.0.jar"), applicationDirectory.resolve("dependencies/libraryA.jar"), applicationDirectory.resolve("dependencies/libraryB.jar"), applicationDirectory.resolve("dependencies/library.jarC.jar"));
    assertThat(layers.resourcesLayer.getEntries()).comparingElementsUsing(SOURCE_FILE_OF).containsExactly(applicationDirectory.resolve("build/resources/main/resourceA"), applicationDirectory.resolve("build/resources/main/resourceB"), applicationDirectory.resolve("build/resources/main/world"));
    assertThat(layers.classesLayer.getEntries()).comparingElementsUsing(SOURCE_FILE_OF).containsExactly(applicationDirectory.resolve("build/classes/java/main/HelloWorld.class"), applicationDirectory.resolve("build/classes/java/main/some.class"));
    List<FileEntry> allFileEntries = new ArrayList<>();
    allFileEntries.addAll(layers.snapshotsLayer.getEntries());
    allFileEntries.addAll(layers.dependenciesLayer.getEntries());
    allFileEntries.addAll(layers.classesLayer.getEntries());
    allFileEntries.addAll(layers.resourcesLayer.getEntries());
    Set<Instant> modificationTimes = allFileEntries.stream().map(FileEntry::getModificationTime).collect(Collectors.toSet());
    assertThat(modificationTimes).containsExactly(EPOCH_PLUS_32);
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Instant(java.time.Instant) ArrayList(java.util.ArrayList) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Test(org.junit.Test)

Example 53 with FileEntry

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

the class Layers method toLayers.

/**
 * Convert a layer spec to a list of layer objects.
 *
 * <p>Does not handle missing directories for files added via this method. We can either prefill
 * directories here, or allow passing of the file entry information directly to the reproducible
 * layer builder
 *
 * @param buildRoot the directory to resolve relative paths, usually the directory where the build
 *     config file is located
 * @param layersSpec a layersSpec containing configuration for all layers
 * @return a {@link List} of {@link FileEntriesLayer} to use as part of a jib container build
 * @throws IOException if traversing a directory fails
 */
static List<FileEntriesLayer> toLayers(Path buildRoot, LayersSpec layersSpec) throws IOException {
    List<FileEntriesLayer> layers = new ArrayList<>();
    FilePropertiesStack filePropertiesStack = new FilePropertiesStack();
    // base properties
    layersSpec.getProperties().ifPresent(filePropertiesStack::push);
    for (LayerSpec entry : layersSpec.getEntries()) {
        // each loop is a new layer
        if (entry instanceof FileLayerSpec) {
            FileEntriesLayer.Builder layerBuiler = FileEntriesLayer.builder();
            FileLayerSpec fileLayer = (FileLayerSpec) entry;
            layerBuiler.setName(fileLayer.getName());
            // layer properties
            fileLayer.getProperties().ifPresent(filePropertiesStack::push);
            for (CopySpec copySpec : ((FileLayerSpec) entry).getFiles()) {
                // copy spec properties
                copySpec.getProperties().ifPresent(filePropertiesStack::push);
                // relativize all paths to the buildRoot location
                Path rawSrc = copySpec.getSrc();
                Path src = rawSrc.isAbsolute() ? rawSrc : buildRoot.resolve(rawSrc);
                AbsoluteUnixPath dest = copySpec.getDest();
                if (!Files.isDirectory(src) && !Files.isRegularFile(src)) {
                    throw new UnsupportedOperationException("Cannot create FileLayers from non-file, non-directory: " + src.toString());
                }
                if (Files.isRegularFile(src)) {
                    // regular file
                    if (!copySpec.getExcludes().isEmpty() || !copySpec.getIncludes().isEmpty()) {
                        throw new UnsupportedOperationException("Cannot apply includes/excludes on single file copy directives.");
                    }
                    layerBuiler.addEntry(src, copySpec.isDestEndsWithSlash() ? dest.resolve(src.getFileName()) : dest, filePropertiesStack.getFilePermissions(), filePropertiesStack.getModificationTime(), filePropertiesStack.getOwnership());
                } else if (Files.isDirectory(src)) {
                    // directory
                    List<PathMatcher> excludes = copySpec.getExcludes().stream().map(Layers::toPathMatcher).collect(Collectors.toList());
                    List<PathMatcher> includes = copySpec.getIncludes().stream().map(Layers::toPathMatcher).collect(Collectors.toList());
                    try (Stream<Path> dirWalk = Files.walk(src)) {
                        List<Path> filtered = dirWalk.filter(path -> excludes.stream().noneMatch(exclude -> exclude.matches(path))).filter(path -> {
                            // if there are no includes directives, include everything
                            if (includes.isEmpty()) {
                                return true;
                            }
                            // if there are includes directives, only include those specified
                            for (PathMatcher matcher : includes) {
                                if (matcher.matches(path)) {
                                    return true;
                                }
                            }
                            return false;
                        }).collect(Collectors.toList());
                        BiFunction<Path, FilePermissions, FileEntry> newEntry = (file, permission) -> new FileEntry(file, dest.resolve(src.relativize(file)), permission, filePropertiesStack.getModificationTime(), filePropertiesStack.getOwnership());
                        Set<Path> addedDirectories = new HashSet<>();
                        for (Path path : filtered) {
                            if (!Files.isDirectory(path) && !Files.isRegularFile(path)) {
                                throw new UnsupportedOperationException("Cannot create FileLayers from non-file, non-directory: " + src.toString());
                            }
                            if (Files.isDirectory(path)) {
                                addedDirectories.add(path);
                                layerBuiler.addEntry(newEntry.apply(path, filePropertiesStack.getDirectoryPermissions()));
                            } else if (Files.isRegularFile(path)) {
                                if (!path.startsWith(src)) {
                                    // be from a link scenario that we do not understand.
                                    throw new IllegalStateException(src.toString() + " is not a parent of " + path.toString());
                                }
                                Path parent = Verify.verifyNotNull(path.getParent());
                                while (true) {
                                    if (addedDirectories.contains(parent)) {
                                        break;
                                    }
                                    layerBuiler.addEntry(newEntry.apply(parent, filePropertiesStack.getDirectoryPermissions()));
                                    addedDirectories.add(parent);
                                    if (parent.equals(src)) {
                                        break;
                                    }
                                    parent = Verify.verifyNotNull(parent.getParent());
                                }
                                layerBuiler.addEntry(newEntry.apply(path, filePropertiesStack.getFilePermissions()));
                            }
                        }
                    }
                }
                copySpec.getProperties().ifPresent(ignored -> filePropertiesStack.pop());
            }
            fileLayer.getProperties().ifPresent(ignored -> filePropertiesStack.pop());
            // TODO: add logging/handling for empty layers
            layers.add(layerBuiler.build());
        } else {
            throw new UnsupportedOperationException("Only FileLayers are supported at this time.");
        }
    }
    layersSpec.getProperties().ifPresent(ignored -> filePropertiesStack.pop());
    return layers;
}
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) Verify(com.google.common.base.Verify) Files(java.nio.file.Files) BiFunction(java.util.function.BiFunction) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) Set(java.util.Set) IOException(java.io.IOException) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) Stream(java.util.stream.Stream) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) PathMatcher(java.nio.file.PathMatcher) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Path(java.nio.file.Path) FileSystems(java.nio.file.FileSystems) Set(java.util.Set) HashSet(java.util.HashSet) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ArrayList(java.util.ArrayList) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) PathMatcher(java.nio.file.PathMatcher) BiFunction(java.util.function.BiFunction) ArrayList(java.util.ArrayList) List(java.util.List) Stream(java.util.stream.Stream) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry)

Example 54 with FileEntry

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

the class PluginConfigurationProcessorTest method testPluginConfigurationProcessor_extraDirectory.

@Test
public void testPluginConfigurationProcessor_extraDirectory() throws URISyntaxException, InvalidContainerVolumeException, MainClassInferenceException, InvalidAppRootException, IOException, IncompatibleBaseImageJavaVersionException, InvalidWorkingDirectoryException, InvalidPlatformException, InvalidImageReferenceException, NumberFormatException, InvalidContainerizingModeException, InvalidFilesModificationTimeException, InvalidCreationTimeException, ExtraDirectoryNotFoundException {
    Path extraDirectory = Paths.get(Resources.getResource("core/layer").toURI());
    Mockito.<List<?>>when(rawConfiguration.getExtraDirectories()).thenReturn(Arrays.asList(new TestExtraDirectoriesConfiguration(extraDirectory)));
    when(rawConfiguration.getExtraDirectoryPermissions()).thenReturn(ImmutableMap.of("/target/dir/foo", FilePermissions.fromOctalString("123")));
    ContainerBuildPlan buildPlan = processCommonConfiguration();
    List<FileEntry> extraFiles = getLayerEntries(buildPlan, "extra files");
    assertThat(extraFiles).comparingElementsUsing(SOURCE_FILE_OF).containsExactly(extraDirectory.resolve("a"), extraDirectory.resolve("a/b"), extraDirectory.resolve("a/b/bar"), extraDirectory.resolve("c"), extraDirectory.resolve("c/cat"), extraDirectory.resolve("foo"));
    assertThat(extraFiles).comparingElementsUsing(EXTRACTION_PATH_OF).containsExactly("/target/dir/a", "/target/dir/a/b", "/target/dir/a/b/bar", "/target/dir/c", "/target/dir/c/cat", "/target/dir/foo");
    Optional<FileEntry> fooEntry = extraFiles.stream().filter(layerEntry -> layerEntry.getExtractionPath().equals(AbsoluteUnixPath.get("/target/dir/foo"))).findFirst();
    assertThat(fooEntry).isPresent();
    assertThat(fooEntry.get().getPermissions().toOctalString()).isEqualTo("123");
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) CredHelperConfiguration(com.google.cloud.tools.jib.plugins.common.RawConfiguration.CredHelperConfiguration) Arrays(java.util.Arrays) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) URISyntaxException(java.net.URISyntaxException) FileTime(java.nio.file.attribute.FileTime) PlatformConfiguration(com.google.cloud.tools.jib.plugins.common.RawConfiguration.PlatformConfiguration) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) ExtraDirectoriesConfiguration(com.google.cloud.tools.jib.plugins.common.RawConfiguration.ExtraDirectoriesConfiguration) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) ArgumentMatcher(org.mockito.ArgumentMatcher) JUnitParamsRunner(junitparams.JUnitParamsRunner) Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) JibContainerBuilderTestHelper(com.google.cloud.tools.jib.api.JibContainerBuilderTestHelper) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) Instant(java.time.Instant) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Optional(java.util.Optional) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) Parameters(junitparams.Parameters) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) Mock(org.mockito.Mock) Assert.assertThrows(org.junit.Assert.assertThrows) RegistryImage(com.google.cloud.tools.jib.api.RegistryImage) RunWith(org.junit.runner.RunWith) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Jib(com.google.cloud.tools.jib.api.Jib) CacheDirectoryCreationException(com.google.cloud.tools.jib.api.CacheDirectoryCreationException) ImmutableList(com.google.common.collect.ImmutableList) MockitoJUnit(org.mockito.junit.MockitoJUnit) Truth8.assertThat(com.google.common.truth.Truth8.assertThat) Nullable(javax.annotation.Nullable) Before(org.junit.Before) Answers(org.mockito.Answers) Resources(com.google.common.io.Resources) Files(java.nio.file.Files) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) ModificationTimeProvider(com.google.cloud.tools.jib.api.buildplan.ModificationTimeProvider) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) Correspondence(com.google.common.truth.Correspondence) Consumer(java.util.function.Consumer) LogEvent(com.google.cloud.tools.jib.api.LogEvent) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) Rule(org.junit.Rule) Containerizer(com.google.cloud.tools.jib.api.Containerizer) Paths(java.nio.file.Paths) DateTimeFormatter(java.time.format.DateTimeFormatter) RestoreSystemProperties(org.junit.contrib.java.lang.system.RestoreSystemProperties) MockitoRule(org.mockito.junit.MockitoRule) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 55 with FileEntry

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

the class SkaffoldSyncMapTemplateTest method testGetJsonString.

@Test
public void testGetJsonString() throws IOException {
    SkaffoldSyncMapTemplate ssmt = new SkaffoldSyncMapTemplate();
    ssmt.addGenerated(new FileEntry(GEN_SRC, AbsoluteUnixPath.get("/genDest"), FilePermissions.DEFAULT_FILE_PERMISSIONS, FileEntriesLayer.DEFAULT_MODIFICATION_TIME));
    ssmt.addDirect(new FileEntry(DIR_SRC_1, AbsoluteUnixPath.get("/dirDest1"), FilePermissions.DEFAULT_FILE_PERMISSIONS, FileEntriesLayer.DEFAULT_MODIFICATION_TIME));
    ssmt.addDirect(new FileEntry(DIR_SRC_2, AbsoluteUnixPath.get("/dirDest2"), FilePermissions.DEFAULT_FILE_PERMISSIONS, FileEntriesLayer.DEFAULT_MODIFICATION_TIME));
    Assert.assertEquals(TEST_JSON, ssmt.getJsonString());
}
Also used : 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