Search in sources :

Example 6 with ExecutableTarget

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

the class GoModCliExtractorTest method handleMultipleReplacementsForOneComponentTest.

// These tests weren't updated to use the new json format for go the first call of go list, yet somehow still passing?
// With the removal of -u, Mockito kept returning null for that first go list call instead of ExecutableOutput.
// I think this test is redundant with the existence of GoModDetectableMinusWhyTest
// Leaving it here for now to review one day. JM-01/2022
@Test
public void handleMultipleReplacementsForOneComponentTest() throws ExecutableRunnerException, ExecutableFailedException, DetectableException {
    DetectableExecutableRunner executableRunner = Mockito.mock(DetectableExecutableRunner.class);
    File directory = new File("");
    ExecutableTarget goExe = ExecutableTarget.forFile(new File(""));
    Answer<ExecutableOutput> executableAnswer = new Answer<ExecutableOutput>() {

        String[] goListArgs = { "list", "-m", "-json" };

        String[] goListJsonArgs = { "list", "-m", "-json", "all" };

        String[] goModGraphArgs = { "mod", "graph" };

        @Override
        public ExecutableOutput answer(InvocationOnMock invocation) {
            Executable executable = invocation.getArgument(0, Executable.class);
            List<String> commandLine = executable.getCommandWithArguments();
            ExecutableOutput result = null;
            if (commandLine.containsAll(Arrays.asList(goListJsonArgs))) {
                result = goListJsonOutput();
            } else if (commandLine.containsAll(Arrays.asList(goListArgs))) {
                result = goListOutput();
            } else if (commandLine.containsAll(Arrays.asList(goModGraphArgs))) {
                result = goModGraphOutput();
            } else {
                result = new ExecutableOutput(0, "", "");
            }
            return result;
        }
    };
    GoModCliExtractor goModCliExtractor = buildGoModCliExtractor(executableRunner, executableAnswer);
    boolean wasSuccessful = true;
    Extraction extraction = goModCliExtractor.extract(directory, goExe);
    if (extraction.getError() instanceof ArrayIndexOutOfBoundsException) {
        wasSuccessful = false;
    }
    Assertions.assertTrue(wasSuccessful);
}
Also used : Answer(org.mockito.stubbing.Answer) DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) GoModCliExtractor(com.synopsys.integration.detectable.detectables.go.gomod.GoModCliExtractor) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Extraction(com.synopsys.integration.detectable.extraction.Extraction) Executable(com.synopsys.integration.executable.Executable) File(java.io.File) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) Test(org.junit.jupiter.api.Test)

Example 7 with ExecutableTarget

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

the class GoModCliExtractorTest method handleGoModWhyExceptionTest.

@Test
public void handleGoModWhyExceptionTest() throws ExecutableRunnerException, ExecutableFailedException, DetectableException {
    DetectableExecutableRunner executableRunner = Mockito.mock(DetectableExecutableRunner.class);
    File directory = new File("");
    ExecutableTarget goExe = ExecutableTarget.forFile(new File(""));
    Answer<ExecutableOutput> executableAnswer = new Answer<ExecutableOutput>() {

        String[] goListArgs = { "list", "-m", "-json" };

        String[] goListJsonArgs = { "list", "-m", "-json", "all" };

        String[] goModGraphArgs = { "mod", "graph" };

        String[] goModWhyArgs = { "mod", "why", "-m", "all" };

        @Override
        public ExecutableOutput answer(InvocationOnMock invocation) throws Throwable {
            Executable executable = invocation.getArgument(0, Executable.class);
            List<String> commandLine = executable.getCommandWithArguments();
            ExecutableOutput result = null;
            if (commandLine.containsAll(Arrays.asList(goListJsonArgs))) {
                result = goListJsonOutput();
            } else if (commandLine.containsAll(Arrays.asList(goListArgs))) {
                result = goListOutput();
            } else if (commandLine.containsAll(Arrays.asList(goModGraphArgs))) {
                result = goModGraphOutput();
            } else if (commandLine.containsAll(Arrays.asList(goModWhyArgs))) {
                throw new ExecutableRunnerException(new DetectableException("Unit Test Go Mod Why error"));
            } else {
                result = new ExecutableOutput(0, "", "");
            }
            return result;
        }
    };
    GoModCliExtractor goModCliExtractor = buildGoModCliExtractor(executableRunner, executableAnswer);
    boolean wasSuccessful = true;
    Extraction extraction = goModCliExtractor.extract(directory, goExe);
    if (extraction.getError() instanceof ArrayIndexOutOfBoundsException) {
        wasSuccessful = false;
    }
    Assertions.assertTrue(wasSuccessful);
}
Also used : ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) Answer(org.mockito.stubbing.Answer) DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) GoModCliExtractor(com.synopsys.integration.detectable.detectables.go.gomod.GoModCliExtractor) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Extraction(com.synopsys.integration.detectable.extraction.Extraction) Executable(com.synopsys.integration.executable.Executable) File(java.io.File) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) Test(org.junit.jupiter.api.Test)

Example 8 with ExecutableTarget

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

the class DockerExtractorTest method extract.

private Extraction extract(String image, String imageId, String tar, File returnedContainerFileSystemFile, File returnedSquashedImageFile, File resultsFile, DetectableExecutableRunner executableRunner) throws IOException, ExecutableRunnerException {
    FileFinder fileFinder = Mockito.mock(FileFinder.class);
    DockerExtractor dockerExtractor = getMockDockerExtractor(executableRunner, fileFinder);
    File directory = new File(".");
    File outputDirectory = new File("build/tmp/test/DockerExtractorTest");
    ExecutableTarget bashExe = null;
    ExecutableTarget javaExe = ExecutableTarget.forFile(new File("fake/test/java"));
    DockerInspectorInfo dockerInspectorInfo = Mockito.mock(DockerInspectorInfo.class);
    Mockito.when(fileFinder.findFile(outputDirectory, DockerExtractor.CONTAINER_FILESYSTEM_FILENAME_PATTERN)).thenReturn(returnedContainerFileSystemFile);
    Mockito.when(fileFinder.findFile(outputDirectory, DockerExtractor.SQUASHED_IMAGE_FILENAME_PATTERN)).thenReturn(returnedSquashedImageFile);
    Mockito.when(fileFinder.findFile(outputDirectory, DockerExtractor.RESULTS_FILENAME_PATTERN)).thenReturn(resultsFile);
    Mockito.when(dockerInspectorInfo.getDockerInspectorJar()).thenReturn(new File("fake/test/dockerinspector.jar"));
    return dockerExtractor.extract(directory, outputDirectory, bashExe, javaExe, image, imageId, tar, dockerInspectorInfo, Mockito.mock(DockerProperties.class));
}
Also used : DockerProperties(com.synopsys.integration.detectable.detectables.docker.DockerProperties) DockerExtractor(com.synopsys.integration.detectable.detectables.docker.DockerExtractor) FileFinder(com.synopsys.integration.common.util.finder.FileFinder) File(java.io.File) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) DockerInspectorInfo(com.synopsys.integration.detectable.detectables.docker.DockerInspectorInfo)

Example 9 with ExecutableTarget

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

the class GradleAirGapCreator method installGradleDependencies.

public void installGradleDependencies(File gradleTemp, File gradleTarget, String inspectorVersion) throws DetectUserFriendlyException {
    logger.info("Checking for gradle on the path.");
    ExecutableTarget gradle;
    try {
        gradle = gradleResolver.resolveGradle(new DetectableEnvironment(gradleTemp));
        if (gradle == null) {
            throw new DetectUserFriendlyException("Gradle must be on the path to make an Air Gap zip.", ExitCodeType.FAILURE_CONFIGURATION);
        }
    } catch (DetectableException e) {
        throw new DetectUserFriendlyException("An error occurred while finding Gradle which is needed to make an Air Gap zip.", e, ExitCodeType.FAILURE_CONFIGURATION);
    }
    File gradleOutput = new File(gradleTemp, "dependencies");
    logger.info("Using temporary gradle dependency directory: " + gradleOutput);
    File buildGradle = new File(gradleTemp, "build.gradle");
    File settingsGradle = new File(gradleTemp, "settings.gradle");
    logger.info("Using temporary gradle build file: " + buildGradle);
    logger.info("Using temporary gradle settings file: " + settingsGradle);
    FileUtil.createMissingParentDirectories(buildGradle);
    FileUtil.createMissingParentDirectories(settingsGradle);
    logger.info("Writing to temporary gradle build file.");
    try {
        Map<String, String> gradleScriptData = new HashMap<>();
        gradleScriptData.put("gradleOutput", StringEscapeUtils.escapeJava(gradleOutput.getCanonicalPath()));
        Template gradleScriptTemplate = configuration.getTemplate("create-gradle-airgap-script.ftl");
        try (Writer fileWriter = new FileWriter(buildGradle)) {
            gradleScriptTemplate.process(gradleScriptData, fileWriter);
        }
        FileUtils.writeStringToFile(settingsGradle, "", StandardCharsets.UTF_8);
    } catch (IOException | TemplateException e) {
        throw new DetectUserFriendlyException("An error occurred creating the temporary build.gradle while creating the Air Gap zip.", e, ExitCodeType.FAILURE_CONFIGURATION);
    }
    logger.info("Invoking gradle install on temporary directory.");
    try {
        ExecutableOutput executableOutput = executableRunner.execute(ExecutableUtils.createFromTarget(gradleTemp, gradle, "installDependencies"));
        if (executableOutput.getReturnCode() != 0) {
            throw new DetectUserFriendlyException("Gradle returned a non-zero exit code while installing Air Gap dependencies.", ExitCodeType.FAILURE_CONFIGURATION);
        }
    } catch (ExecutableRunnerException e) {
        throw new DetectUserFriendlyException("An error occurred using Gradle to make an Air Gap zip.", e, ExitCodeType.FAILURE_CONFIGURATION);
    }
    try {
        logger.info("Moving generated dependencies to final gradle folder: " + gradleTarget.getCanonicalPath());
        FileUtils.moveDirectory(gradleOutput, gradleTarget);
        FileUtils.deleteDirectory(gradleTemp);
    } catch (IOException e) {
        throw new DetectUserFriendlyException("An error occurred moving gradle dependencies to Air Gap folder.", ExitCodeType.FAILURE_CONFIGURATION);
    }
}
Also used : HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) FileWriter(java.io.FileWriter) IOException(java.io.IOException) DetectableEnvironment(com.synopsys.integration.detectable.DetectableEnvironment) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) Template(freemarker.template.Template) DetectUserFriendlyException(com.synopsys.integration.detect.configuration.DetectUserFriendlyException) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) File(java.io.File) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Example 10 with ExecutableTarget

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

the class NpmCliExtractor method extract.

public Extraction extract(File directory, ExecutableTarget npmExe, @Nullable String npmArguments, File packageJsonFile) {
    toolVersionLogger.log(directory, npmExe);
    PackageJson packageJson;
    try {
        packageJson = parsePackageJson(packageJsonFile);
    } catch (IOException e) {
        return new Extraction.Builder().exception(e).build();
    }
    List<String> exeArgs = new ArrayList<>();
    exeArgs.add("ls");
    exeArgs.add("-json");
    Optional.ofNullable(npmArguments).map(arg -> arg.split(" ")).ifPresent(additionalArguments -> exeArgs.addAll(Arrays.asList(additionalArguments)));
    ExecutableOutput npmLsOutput;
    try {
        npmLsOutput = executableRunner.execute(ExecutableUtils.createFromTarget(directory, npmExe, exeArgs));
    } catch (Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
    String standardOutput = npmLsOutput.getStandardOutput();
    String errorOutput = npmLsOutput.getErrorOutput();
    if (StringUtils.isNotBlank(errorOutput)) {
        logger.error("Error when running npm ls -json command");
        logger.error(errorOutput);
        return new Extraction.Builder().failure("Npm wrote to stderr while running npm ls.").build();
    } else if (StringUtils.isNotBlank(standardOutput)) {
        logger.debug("Parsing npm ls file.");
        logger.debug(standardOutput);
        NpmPackagerResult result = npmCliParser.generateCodeLocation(standardOutput, packageJson);
        String projectName = result.getProjectName() != null ? result.getProjectName() : packageJson.name;
        String projectVersion = result.getProjectVersion() != null ? result.getProjectVersion() : packageJson.version;
        return new Extraction.Builder().success(result.getCodeLocation()).projectName(projectName).projectVersion(projectVersion).build();
    } else {
        logger.error("Nothing returned from npm ls -json command");
        return new Extraction.Builder().failure("Npm returned error after running npm ls.").build();
    }
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Extraction(com.synopsys.integration.detectable.extraction.Extraction) LoggerFactory(org.slf4j.LoggerFactory) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) StringUtils(org.apache.commons.lang3.StringUtils) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) ArrayList(java.util.ArrayList) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) NpmCliParser(com.synopsys.integration.detectable.detectables.npm.cli.parse.NpmCliParser) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ToolVersionLogger(com.synopsys.integration.detectable.util.ToolVersionLogger) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) Gson(com.google.gson.Gson) DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult) Optional(java.util.Optional) ExecutableUtils(com.synopsys.integration.detectable.ExecutableUtils) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) ArrayList(java.util.ArrayList) Extraction(com.synopsys.integration.detectable.extraction.Extraction) IOException(java.io.IOException) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) NpmPackagerResult(com.synopsys.integration.detectable.detectables.npm.lockfile.result.NpmPackagerResult) IOException(java.io.IOException)

Aggregations

ExecutableTarget (com.synopsys.integration.detectable.ExecutableTarget)15 File (java.io.File)15 DetectableExecutableRunner (com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner)10 ExecutableOutput (com.synopsys.integration.executable.ExecutableOutput)10 Extraction (com.synopsys.integration.detectable.extraction.Extraction)9 List (java.util.List)8 ExecutableUtils (com.synopsys.integration.detectable.ExecutableUtils)6 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)6 DetectableException (com.synopsys.integration.detectable.detectable.exception.DetectableException)6 Executable (com.synopsys.integration.executable.Executable)6 IOException (java.io.IOException)6 Optional (java.util.Optional)6 Collectors (java.util.stream.Collectors)6 StandardCharsets (java.nio.charset.StandardCharsets)5 ArrayList (java.util.ArrayList)5 FileUtils (org.apache.commons.io.FileUtils)5 ExecutableFailedException (com.synopsys.integration.detectable.detectable.executable.ExecutableFailedException)4 ExecutableRunnerException (com.synopsys.integration.executable.ExecutableRunnerException)4 HashMap (java.util.HashMap)4 Test (org.junit.jupiter.api.Test)4