Search in sources :

Example 11 with Executable

use of com.synopsys.integration.executable.Executable in project synopsys-detect by blackducksoftware.

the class DockerExtractorTest method testExtractTarReturningOriginalTar.

@Test
@DisabledOnOs(WINDOWS)
public void testExtractTarReturningOriginalTar() throws ExecutableRunnerException, IOException {
    String image = null;
    String imageId = null;
    String tar = fakeDockerTarFile.getAbsolutePath();
    DetectableExecutableRunner executableRunner = getDetectableExecutableRunner();
    Extraction extraction = extract(image, imageId, tar, null, null, fakeResultsFile, executableRunner);
    // No returned .tar.gz: scan given docker tar instead
    assertTrue(extraction.getMetaData(DockerExtractor.DOCKER_IMAGE_NAME_META_DATA).get().endsWith("testDockerTarfile.tar"));
    assertTrue(extraction.getMetaData(DockerExtractor.DOCKER_TAR_META_DATA).get().getName().endsWith("testDockerTarfile.tar"));
    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"));
    assertTrue(command.get(4).startsWith("--docker.tar="));
    assertTrue(command.get(4).endsWith("testDockerTarfile.tar"));
}
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 12 with Executable

use of com.synopsys.integration.executable.Executable in project synopsys-detect by blackducksoftware.

the class BatteryDetectRunner method executeDetectScript.

private DetectOutput executeDetectScript(List<String> detectArguments) throws ExecutableRunnerException {
    List<String> shellArguments = new ArrayList<>();
    String target = "";
    if (SystemUtils.IS_OS_WINDOWS) {
        target = "powershell";
        shellArguments.add("\"[Net.ServicePointManager]::SecurityProtocol = 'tls12'; irm https://detect.synopsys.com/detect.ps1?$(Get-Random) | iex; detect\"");
    } else {
        File scriptTarget = new File(scriptDirectory, "detect.sh");
        if (scriptTarget.exists()) {
            Assertions.assertTrue(scriptTarget.delete(), "Failed to cleanup an existing detect shell script. This file is cleaned up to ensure latest script is always used.");
        }
        ExecutableOutput downloadOutput = downloadDetectBash(scriptTarget);
        Assertions.assertTrue(downloadOutput.getReturnCode() == 0 && scriptTarget.exists(), "Something went wrong downloading the detect script.");
        Assertions.assertTrue(scriptTarget.setExecutable(true), "Failed to change script permissions to execute. The downloaded detect script must be executable.");
        target = scriptTarget.toString();
    }
    shellArguments.addAll(detectArguments);
    Map<String, String> environmentVariables = new HashMap<>();
    if (StringUtils.isNotBlank(detectVersion)) {
        environmentVariables.put("DETECT_LATEST_RELEASE_VERSION", detectVersion);
    }
    Executable executable = Executable.create(outputDirectory, environmentVariables, target, shellArguments);
    ProcessBuilderRunner executableRunner = new ProcessBuilderRunner(new Slf4jIntLogger(logger));
    ExecutableOutput result = executableRunner.execute(executable);
    Assertions.assertEquals(0, result.getReturnCode(), "Detect returned a non-zero exit code:" + result.getReturnCode());
    List<String> lines = result.getStandardOutputAsList();
    Assertions.assertTrue(lines.size() > 0, "Detect wrote nothing to standard out.");
    return new DetectOutput(result.getStandardOutputAsList());
}
Also used : ProcessBuilderRunner(com.synopsys.integration.executable.ProcessBuilderRunner) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) Slf4jIntLogger(com.synopsys.integration.log.Slf4jIntLogger) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Executable(com.synopsys.integration.executable.Executable) File(java.io.File)

Example 13 with Executable

use of com.synopsys.integration.executable.Executable in project synopsys-detect by blackducksoftware.

the class BatteryDetectRunner method downloadDetectBash.

private ExecutableOutput downloadDetectBash(File target) throws ExecutableRunnerException {
    List<String> shellArguments = new ArrayList<>();
    shellArguments.add("-s");
    shellArguments.add("-L");
    shellArguments.add("https://detect.synopsys.com/detect.sh");
    shellArguments.add("-o");
    shellArguments.add(target.toString());
    Executable executable = Executable.create(outputDirectory, new HashMap<>(), "curl", shellArguments);
    ProcessBuilderRunner executableRunner = new ProcessBuilderRunner(new Slf4jIntLogger(logger));
    return executableRunner.execute(executable);
}
Also used : ProcessBuilderRunner(com.synopsys.integration.executable.ProcessBuilderRunner) Slf4jIntLogger(com.synopsys.integration.log.Slf4jIntLogger) ArrayList(java.util.ArrayList) Executable(com.synopsys.integration.executable.Executable)

Example 14 with Executable

use of com.synopsys.integration.executable.Executable in project synopsys-detect by blackducksoftware.

the class RebarExtractor method extract.

public Extraction extract(File directory, ExecutableTarget rebarExe) {
    try {
        toolVersionLogger.log(directory, rebarExe);
        List<CodeLocation> codeLocations = new ArrayList<>();
        Map<String, String> envVars = new HashMap<>();
        envVars.put("REBAR_COLOR", "none");
        List<String> arguments = new ArrayList<>();
        arguments.add("tree");
        Executable rebar3TreeExe = ExecutableUtils.createFromTarget(directory, envVars, rebarExe, arguments);
        List<String> output = executableRunner.execute(rebar3TreeExe).getStandardOutputAsList();
        RebarParseResult parseResult = rebarTreeParser.parseRebarTreeOutput(output);
        codeLocations.add(parseResult.getCodeLocation());
        Extraction.Builder builder = new Extraction.Builder().success(codeLocations);
        parseResult.getProjectNameVersion().ifPresent(projectNameVersion -> builder.projectName(projectNameVersion.getName()).projectVersion(projectNameVersion.getVersion()));
        return builder.build();
    } catch (Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) HashMap(java.util.HashMap) RebarParseResult(com.synopsys.integration.detectable.detectables.rebar.model.RebarParseResult) ArrayList(java.util.ArrayList) Extraction(com.synopsys.integration.detectable.extraction.Extraction) Executable(com.synopsys.integration.executable.Executable)

Example 15 with Executable

use of com.synopsys.integration.executable.Executable in project synopsys-detect by blackducksoftware.

the class DockerExtractorTest method testExtractImageReturningContainerFileSystem.

@Test
@DisabledOnOs(WINDOWS)
public void testExtractImageReturningContainerFileSystem() throws ExecutableRunnerException, IOException {
    final String image = "ubuntu:latest";
    String imageId = null;
    String tar = null;
    DetectableExecutableRunner executableRunner = getDetectableExecutableRunner();
    Extraction extraction = extract(image, imageId, tar, fakeContainerFileSystemFile, null, fakeResultsFile, executableRunner);
    assertEquals("ubuntu:latest", extraction.getMetaData(DockerExtractor.DOCKER_IMAGE_NAME_META_DATA).get());
    assertTrue(extraction.getMetaData(DockerExtractor.CONTAINER_FILESYSTEM_META_DATA).get().getName().endsWith("_containerfilesystem.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)

Aggregations

Executable (com.synopsys.integration.executable.Executable)18 Extraction (com.synopsys.integration.detectable.extraction.Extraction)11 File (java.io.File)9 ArrayList (java.util.ArrayList)8 DetectableExecutableRunner (com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner)7 HashMap (java.util.HashMap)7 ExecutableOutput (com.synopsys.integration.executable.ExecutableOutput)6 Test (org.junit.jupiter.api.Test)6 DisabledOnOs (org.junit.jupiter.api.condition.DisabledOnOs)4 ExecutableTarget (com.synopsys.integration.detectable.ExecutableTarget)3 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)3 DetectableException (com.synopsys.integration.detectable.detectable.exception.DetectableException)3 ExecutableRunnerException (com.synopsys.integration.executable.ExecutableRunnerException)3 ProcessBuilderRunner (com.synopsys.integration.executable.ProcessBuilderRunner)3 GoModCliExtractor (com.synopsys.integration.detectable.detectables.go.gomod.GoModCliExtractor)2 Slf4jIntLogger (com.synopsys.integration.log.Slf4jIntLogger)2 IOException (java.io.IOException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 GraphParser (com.paypal.digraph.parser.GraphParser)1