use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib by google.
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");
}
use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib by google.
the class SkaffoldSyncMapTemplateTest method testGetJsonString_emptyGenerated.
@Test
public void testGetJsonString_emptyGenerated() throws IOException {
SkaffoldSyncMapTemplate ssmt = new SkaffoldSyncMapTemplate();
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_EMPTY_GENERATED, ssmt.getJsonString());
}
use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib by google.
the class JavaContainerBuilderHelperTest method testExtraDirectoryLayerConfiguration_globPermissions.
@Test
public void testExtraDirectoryLayerConfiguration_globPermissions() throws URISyntaxException, IOException {
Path extraFilesDirectory = Paths.get(Resources.getResource("core/layer").toURI());
Map<String, FilePermissions> permissionsMap = ImmutableMap.of("/a", FilePermissions.fromOctalString("123"), "/a/*", FilePermissions.fromOctalString("456"), "**/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("456")).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()));
}
}
use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib by google.
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;
}
use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib by google.
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);
}
Aggregations