Search in sources :

Example 61 with Extraction

use of com.synopsys.integration.detectable.extraction.Extraction in project synopsys-detect by blackducksoftware.

the class DockerExtractorTest method testExtractImageReturningSquashedImage.

@Test
@DisabledOnOs(WINDOWS)
public void testExtractImageReturningSquashedImage() throws ExecutableRunnerException, IOException {
    final String image = "ubuntu:latest";
    String imageId = null;
    String tar = null;
    DetectableExecutableRunner executableRunner = getDetectableExecutableRunner();
    Extraction extraction = extract(image, imageId, tar, fakeContainerFileSystemFile, fakeSquashedImageFile, fakeResultsFile, executableRunner);
    assertEquals("ubuntu:latest", extraction.getMetaData(DockerExtractor.DOCKER_IMAGE_NAME_META_DATA).get());
    assertTrue(extraction.getMetaData(DockerExtractor.SQUASHED_IMAGE_META_DATA).get().getName().endsWith("_squashedimage.tar.gz"));
    ArgumentCaptor<Executable> executableArgumentCaptor = ArgumentCaptor.forClass(Executable.class);
    Mockito.verify(executableRunner).execute(executableArgumentCaptor.capture());
    Executable executableToVerify = executableArgumentCaptor.getValue();
    List<String> command = executableToVerify.getCommandWithArguments();
    assertTrue(command.get(0).endsWith("/fake/test/java"));
    assertEquals("-jar", command.get(1));
    assertTrue(command.get(2).endsWith("/fake/test/dockerinspector.jar"));
    assertTrue(command.get(3).startsWith("--spring.config.location="));
    assertTrue(command.get(3).endsWith("/application.properties"));
    assertEquals("--docker.image=ubuntu:latest", command.get(4));
}
Also used : DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) Extraction(com.synopsys.integration.detectable.extraction.Extraction) Executable(com.synopsys.integration.executable.Executable) DisabledOnOs(org.junit.jupiter.api.condition.DisabledOnOs) Test(org.junit.jupiter.api.Test)

Example 62 with Extraction

use of com.synopsys.integration.detectable.extraction.Extraction in project synopsys-detect by blackducksoftware.

the class DockerExtractor method executeDocker.

private Extraction executeDocker(File outputDirectory, String imageArgument, String suppliedImagePiece, String dockerTarFilePath, File directory, ExecutableTarget javaExe, ExecutableTarget dockerExe, DockerInspectorInfo dockerInspectorInfo, DockerProperties dockerProperties) throws IOException, ExecutableRunnerException {
    File dockerPropertiesFile = new File(outputDirectory, "application.properties");
    dockerProperties.populatePropertiesFile(dockerPropertiesFile, outputDirectory);
    Map<String, String> environmentVariables = new HashMap<>(0);
    List<String> dockerArguments = new ArrayList<>();
    dockerArguments.add("-jar");
    dockerArguments.add(dockerInspectorInfo.getDockerInspectorJar().getAbsolutePath());
    dockerArguments.add("--spring.config.location=file:" + dockerPropertiesFile.getCanonicalPath());
    dockerArguments.add(imageArgument);
    if (dockerInspectorInfo.hasAirGapImageFiles()) {
        importTars(dockerInspectorInfo.getAirGapInspectorImageTarFiles(), outputDirectory, environmentVariables, dockerExe);
    }
    Executable dockerExecutable = ExecutableUtils.createFromTarget(outputDirectory, environmentVariables, javaExe, dockerArguments);
    executableRunner.execute(dockerExecutable);
    File producedSquashedImageFile = fileFinder.findFile(outputDirectory, SQUASHED_IMAGE_FILENAME_PATTERN);
    if (producedSquashedImageFile != null) {
        logger.debug("Returning squashed image: {}", producedSquashedImageFile.getAbsolutePath());
    }
    File producedContainerFileSystemFile = fileFinder.findFile(outputDirectory, CONTAINER_FILESYSTEM_FILENAME_PATTERN);
    if (producedContainerFileSystemFile != null) {
        logger.debug("Returning container filesystem: {}", producedContainerFileSystemFile.getAbsolutePath());
    }
    Extraction.Builder extractionBuilder = findCodeLocations(outputDirectory, directory);
    // The value of DOCKER_IMAGE_NAME_META_DATA is built into the codelocation name, so changing how its value is derived is likely to
    // change how codelocation names are generated. Currently either an image repo, repo:tag, or tarfile path gets written there.
    // It's tempting to always store the image repo:tag in that field, but that would change code location naming with consequences for users.
    String imageIdentifier = getImageIdentifierFromOutputDirectoryIfImageIdPresent(outputDirectory, suppliedImagePiece, imageIdentifierType);
    extractionBuilder.metaData(SQUASHED_IMAGE_META_DATA, producedSquashedImageFile).metaData(CONTAINER_FILESYSTEM_META_DATA, producedContainerFileSystemFile).metaData(DOCKER_IMAGE_NAME_META_DATA, imageIdentifier);
    if (StringUtils.isNotBlank(dockerTarFilePath)) {
        File givenDockerTarfile = new File(dockerTarFilePath);
        logger.debug("Returning given docker tarfile: {}", givenDockerTarfile.getAbsolutePath());
        extractionBuilder.metaData(DOCKER_TAR_META_DATA, givenDockerTarfile);
    }
    return extractionBuilder.build();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Extraction(com.synopsys.integration.detectable.extraction.Extraction) Executable(com.synopsys.integration.executable.Executable) File(java.io.File)

Example 63 with Extraction

use of com.synopsys.integration.detectable.extraction.Extraction in project synopsys-detect by blackducksoftware.

the class NugetInspectorExtractor method extract.

public Extraction extract(List<File> targets, File outputDirectory, NugetInspector inspector, NugetInspectorOptions nugetInspectorOptions) {
    try {
        List<NugetTargetResult> results = new ArrayList<>();
        for (int i = 0; i < targets.size(); i++) {
            File targetDirectory = new File(outputDirectory, "inspection-" + i);
            results.add(executeTarget(inspector, targets.get(i), targetDirectory, nugetInspectorOptions));
        }
        List<CodeLocation> codeLocations = results.stream().flatMap(it -> it.codeLocations.stream()).collect(Collectors.toList());
        Map<File, CodeLocation> codeLocationsBySource = new HashMap<>();
        DependencyGraphCombiner combiner = new DependencyGraphCombiner();
        codeLocations.forEach(codeLocation -> {
            File sourcePathFile = codeLocation.getSourcePath().orElse(null);
            if (codeLocationsBySource.containsKey(sourcePathFile)) {
                logger.debug("Combined code location for: " + sourcePathFile);
                CodeLocation destination = codeLocationsBySource.get(sourcePathFile);
                combiner.addGraphAsChildrenToRoot((MutableDependencyGraph) destination.getDependencyGraph(), codeLocation.getDependencyGraph());
            } else {
                codeLocationsBySource.put(sourcePathFile, codeLocation);
            }
        });
        Optional<NameVersion> nameVersion = results.stream().filter(it -> it.nameVersion != null).map(it -> it.nameVersion).findFirst();
        List<CodeLocation> uniqueCodeLocations = new ArrayList<>(codeLocationsBySource.values());
        return new Extraction.Builder().success(uniqueCodeLocations).nameVersionIfPresent(nameVersion).build();
    } catch (Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : Extraction(com.synopsys.integration.detectable.extraction.Extraction) MutableDependencyGraph(com.synopsys.integration.bdio.graph.MutableDependencyGraph) NugetParseResult(com.synopsys.integration.detectable.detectables.nuget.parse.NugetParseResult) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) FileFinder(com.synopsys.integration.common.util.finder.FileFinder) ArrayList(java.util.ArrayList) NameVersion(com.synopsys.integration.util.NameVersion) NugetInspectorOptions(com.synopsys.integration.detectable.detectable.inspector.nuget.NugetInspectorOptions) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) Map(java.util.Map) NugetInspector(com.synopsys.integration.detectable.detectable.inspector.nuget.NugetInspector) Logger(org.slf4j.Logger) NugetInspectorParser(com.synopsys.integration.detectable.detectables.nuget.parse.NugetInspectorParser) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) DependencyGraphCombiner(com.synopsys.integration.bdio.graph.DependencyGraphCombiner) List(java.util.List) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) Optional(java.util.Optional) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) NameVersion(com.synopsys.integration.util.NameVersion) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) IOException(java.io.IOException) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) Extraction(com.synopsys.integration.detectable.extraction.Extraction) File(java.io.File) DependencyGraphCombiner(com.synopsys.integration.bdio.graph.DependencyGraphCombiner)

Aggregations

Extraction (com.synopsys.integration.detectable.extraction.Extraction)63 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)38 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)23 IOException (java.io.IOException)19 Test (org.junit.jupiter.api.Test)18 File (java.io.File)17 ArrayList (java.util.ArrayList)14 ExecutableOutput (com.synopsys.integration.executable.ExecutableOutput)13 DetectableExecutableRunner (com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner)12 NameVersion (com.synopsys.integration.util.NameVersion)12 Executable (com.synopsys.integration.executable.Executable)11 ExecutableRunnerException (com.synopsys.integration.executable.ExecutableRunnerException)9 List (java.util.List)9 ExecutableTarget (com.synopsys.integration.detectable.ExecutableTarget)8 HashMap (java.util.HashMap)8 Optional (java.util.Optional)8 DetectableException (com.synopsys.integration.detectable.detectable.exception.DetectableException)7 Collectors (java.util.stream.Collectors)7 ExecutableFailedException (com.synopsys.integration.detectable.detectable.executable.ExecutableFailedException)6 DetectableResult (com.synopsys.integration.detectable.detectable.result.DetectableResult)6