Search in sources :

Example 1 with DetectableExecutableRunner

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

the class ClangPackageManagerRunnerTest method runTest.

private PackageDetailsResult runTest(ClangPackageManagerInfo packageManagerInfo, ClangPackageManagerResolver packageResolver, String pkgName, String pkgArch, boolean archBuried, String pkgMgrOwnerQueryResultPattern, String pkgMgrDetailsQueryResultPattern, File dependencyFile) throws ExecutableRunnerException {
    ClangPackageManager currentPackageManager = new ClangPackageManager(packageManagerInfo, packageResolver);
    File workingDirectory = new File("test");
    DetectableExecutableRunner executableRunner = Mockito.mock(DetectableExecutableRunner.class);
    List<String> fileSpecificGetOwnerArgs = new ArrayList<>(packageManagerInfo.getPkgMgrGetOwnerCmdArgs());
    fileSpecificGetOwnerArgs.add(dependencyFile.getAbsolutePath());
    if (packageManagerInfo.getPkgInfoArgs().isPresent() && (pkgMgrDetailsQueryResultPattern != null)) {
        List<String> fileSpecificGetDetailsArgs = new ArrayList<>(packageManagerInfo.getPkgInfoArgs().get());
        // If absent or buried, the detail query should omit arch; otherwise: the detail query should include arch
        if (archBuried || pkgArch == null) {
            fileSpecificGetDetailsArgs.add(pkgName);
        } else {
            fileSpecificGetDetailsArgs.add(pkgName + ":" + pkgArch);
        }
        String pkgMgrGetDetailsQueryFileOutput = String.format(pkgMgrDetailsQueryResultPattern, dependencyFile);
        ExecutableOutput pkgMgrGetDetailsQueryFileResult = new ExecutableOutput(0, pkgMgrGetDetailsQueryFileOutput, "");
        Mockito.when(executableRunner.execute(workingDirectory, packageManagerInfo.getPkgMgrCmdString(), fileSpecificGetDetailsArgs)).thenReturn(pkgMgrGetDetailsQueryFileResult);
    }
    String pkgMgrGetOwnerQueryFileOutput = String.format(pkgMgrOwnerQueryResultPattern, dependencyFile.getAbsolutePath());
    ExecutableOutput pkgMgrGetOwnerQueryFileResult = new ExecutableOutput(0, pkgMgrGetOwnerQueryFileOutput, "");
    Mockito.when(executableRunner.execute(workingDirectory, packageManagerInfo.getPkgMgrCmdString(), fileSpecificGetOwnerArgs)).thenReturn(pkgMgrGetOwnerQueryFileResult);
    ClangPackageManagerRunner runner = new ClangPackageManagerRunner();
    // Test
    return runner.getPackages(currentPackageManager, workingDirectory, executableRunner, dependencyFile);
}
Also used : ClangPackageManager(com.synopsys.integration.detectable.detectables.clang.packagemanager.ClangPackageManager) DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) ClangPackageManagerRunner(com.synopsys.integration.detectable.detectables.clang.packagemanager.ClangPackageManagerRunner) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) ArrayList(java.util.ArrayList) File(java.io.File)

Example 2 with DetectableExecutableRunner

use of com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner 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 3 with DetectableExecutableRunner

use of com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner 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 4 with DetectableExecutableRunner

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

the class RpmPackageManagerTest method testResolve.

@Test
public void testResolve() throws ExecutableRunnerException, NotOwnedByAnyPkgException {
    RpmPackageManagerResolver resolver = new RpmPackageManagerResolver(new Gson());
    ClangPackageManagerInfo currentPackageManager = null;
    DetectableExecutableRunner executableRunner = null;
    File workingDirectory = null;
    final String queryPackageOutput = "{ epoch: \"(none)\", name: \"glibc-headers\", version: \"2.17-222.el7\", arch: \"x86_64\" }\n" + "{ epoch: \"3\", name: \"test-package\", version: \"test-version\", arch: \"test_arch\" }\n";
    List<PackageDetails> pkgs = resolver.resolvePackages(currentPackageManager, executableRunner, workingDirectory, queryPackageOutput);
    assertEquals(2, pkgs.size());
    boolean foundGLibcHeaders = false;
    boolean foundTestPkg = false;
    for (PackageDetails pkg : pkgs) {
        if (pkg.getPackageName().equals("glibc-headers")) {
            foundGLibcHeaders = true;
            assertEquals("2.17-222.el7", pkg.getPackageVersion());
            assertEquals("x86_64", pkg.getPackageArch());
        }
        if (pkg.getPackageName().equals("test-package")) {
            foundTestPkg = true;
            assertEquals("3:test-version", pkg.getPackageVersion());
            assertEquals("test_arch", pkg.getPackageArch());
        }
    }
    assertTrue(foundGLibcHeaders);
    assertTrue(foundTestPkg);
}
Also used : ClangPackageManagerInfo(com.synopsys.integration.detectable.detectables.clang.packagemanager.ClangPackageManagerInfo) DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) RpmPackageManagerResolver(com.synopsys.integration.detectable.detectables.clang.packagemanager.resolver.RpmPackageManagerResolver) Gson(com.google.gson.Gson) File(java.io.File) PackageDetails(com.synopsys.integration.detectable.detectables.clang.packagemanager.PackageDetails) Test(org.junit.jupiter.api.Test)

Example 5 with DetectableExecutableRunner

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

the class DockerExtractorTest method testExtractTarReturningContainerFileSystem.

@Test
@DisabledOnOs(WINDOWS)
public void testExtractTarReturningContainerFileSystem() throws ExecutableRunnerException, IOException {
    String image = null;
    String imageId = null;
    String tar = fakeDockerTarFile.getAbsolutePath();
    DetectableExecutableRunner executableRunner = getDetectableExecutableRunner();
    Extraction extraction = extract(image, imageId, tar, fakeContainerFileSystemFile, null, fakeResultsFile, executableRunner);
    assertTrue(extraction.getMetaData(DockerExtractor.DOCKER_IMAGE_NAME_META_DATA).get().endsWith("testDockerTarfile.tar"));
    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"));
    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)

Aggregations

DetectableExecutableRunner (com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner)17 Test (org.junit.jupiter.api.Test)14 Executable (com.synopsys.integration.executable.Executable)9 ExecutableOutput (com.synopsys.integration.executable.ExecutableOutput)9 Extraction (com.synopsys.integration.detectable.extraction.Extraction)7 File (java.io.File)7 DisabledOnOs (org.junit.jupiter.api.condition.DisabledOnOs)5 ExecutableTarget (com.synopsys.integration.detectable.ExecutableTarget)4 ClangPackageManagerInfoFactory (com.synopsys.integration.detectable.detectables.clang.packagemanager.ClangPackageManagerInfoFactory)3 PackageDetails (com.synopsys.integration.detectable.detectables.clang.packagemanager.PackageDetails)3 ArrayList (java.util.ArrayList)3 Gson (com.google.gson.Gson)2 FileFinder (com.synopsys.integration.common.util.finder.FileFinder)2 ClangPackageManager (com.synopsys.integration.detectable.detectables.clang.packagemanager.ClangPackageManager)2 ClangPackageManagerRunner (com.synopsys.integration.detectable.detectables.clang.packagemanager.ClangPackageManagerRunner)2 DpkgPackageManagerResolver (com.synopsys.integration.detectable.detectables.clang.packagemanager.resolver.DpkgPackageManagerResolver)2 DpkgPkgDetailsResolver (com.synopsys.integration.detectable.detectables.clang.packagemanager.resolver.DpkgPkgDetailsResolver)2 GoModCliExtractor (com.synopsys.integration.detectable.detectables.go.gomod.GoModCliExtractor)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2