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());
}
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);
}
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;
}
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");
}
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());
}
Aggregations