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