Skip to content

Commit

Permalink
Merge pull request #5 from abaluha-hubs/dbpath-bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
gschneider-r7 authored Sep 22, 2020
2 parents 8a82d45 + e4e0481 commit ed05937
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void handle(String name, TarArchiveEntry entry, InputStream contents, Ima
synchronized (this) {
// the --root flag sets the path which all rpm commands use as base for relative paths.
// the --dbpath flag uses a path relative to the root path. we use "." (current directory) since the root path is the full rpmdb path.
Process process = new ProcessBuilder("/usr/bin/env", "rpm", "--root=" + directory.getAbsolutePath(),"--dbpath=.", "-qa", "--queryformat=" + RPM_QUERY_FORMAT).start();
Process process = new ProcessBuilder("/usr/bin/env", "rpm", "--root=" + directory.getAbsolutePath(),"--dbpath=/", "-qa", "--queryformat=" + RPM_QUERY_FORMAT).start();
LOGGER.info(format("[Image: {}] Parsing RPM output.", image.getId()).getMessage());
layerPath.getLayer().addPackages(rpmPackageParser.parse(process.getInputStream(), image.getOperatingSystem() == null ? layerPath.getLayer().getOperatingSystem() : image.getOperatingSystem()));
if (!process.waitFor(5, TimeUnit.SECONDS))
Expand All @@ -64,7 +64,7 @@ public void handle(String name, TarArchiveEntry entry, InputStream contents, Ima
String rpmImage = (rpmDockerImage == null || rpmDockerImage.isEmpty()) ? DEFAULT_DOCKER_IMAGE : rpmDockerImage;
LOGGER.info("Using docker image '{}' to execute RPM command.", rpmImage);
Process process = new ProcessBuilder("/usr/bin/env", "docker", "run", "--rm","--mount", "type=bind,source=" + directory.getAbsolutePath() + ",target=/rpm",
rpmImage, "rpm", "--root=/rpm", "--dbpath=.", "-qa", "--queryformat=" + RPM_QUERY_FORMAT).start();
rpmImage, "rpm", "--root=/rpm", "--dbpath=/", "-qa", "--queryformat=" + RPM_QUERY_FORMAT).start();
process.waitFor(300, TimeUnit.SECONDS);
if (process.exitValue() != 0) {
LOGGER.error("Docker exection failed with exit code {}.", process.exitValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

class DockerImageAnalyzerServiceTest {

@Test
public void test() throws IOException {
public void testAlpine() throws IOException {
// Given
File tarFile = new File(getClass().getClassLoader().getResource("containers/fakealpine.tar").getFile());
ImageId expectedId = new ImageId("sha256:7be494284b1dea6cb2012a5ef99676b4ec22868d9ee235c60e48181542d70fd5");
Expand All @@ -36,4 +35,27 @@ public void test() throws IOException {
assertEquals(expectedLayers, image.getLayers().size());
assertEquals(expectedPackages, image.getPackages().size());
}

@Test
public void testCentos() throws IOException {
// Given
File tarFile = new File(getClass().getClassLoader().getResource("containers/centos/test_image_centos.tar").getFile());
ImageId expectedId = new ImageId("sha256:bcbdfe9aa33a028bedc671ecfe56ce628ff64a8ad1b2fa46855c03955fcdc5bf");
long expectedSize = 72704;
// omit OS check because this image tar was built from scratch
long expectedLayers = 3;
long expectedPackages = 1;

// When
DockerImageAnalyzerService analyzer = new DockerImageAnalyzerService(null);
Path tmpdir = Files.createTempDirectory("r7dia");
Image image = analyzer.analyze(tarFile, tmpdir.toString());

// Then
assertEquals(expectedId, image.getId());
assertEquals(expectedSize, image.getSize());
assertEquals(expectedLayers, image.getLayers().size());
assertEquals(expectedPackages, image.getPackages().size());
assertEquals("A set of system configuration and setup files", image.getPackages().stream().findFirst().get().getDescription());
}
}
13 changes: 13 additions & 0 deletions src/test/resources/containers/centos/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM docker.io/library/centos:centos6.10
ARG TMP_RPM="/tmp/rpm"
RUN mkdir ${TMP_RPM} && \
rpm --initdb --dbpath ${TMP_RPM} && \
yum remove setup -y -q && \
yum install --downloadonly --downloaddir=/tmp setup -y -q && \
cd ${TMP_RPM} && \
rpm --dbpath ${TMP_RPM} -i ../setup-*.noarch.rpm

FROM scratch
COPY --from=0 /tmp/rpm/Packages /var/lib/rpm/Packages
COPY --from=0 /etc/centos-release /etc/os-release
COPY --from=0 /etc/centos-release /etc/centos-release
Binary file not shown.

0 comments on commit ed05937

Please sign in to comment.