Search in sources :

Example 1 with Result

use of com.synopsys.integration.blackduck.dockerinspector.output.Result in project blackduck-docker-inspector by blackducksoftware.

the class IntegrationTestRunner method testTar.

public void testTar(TestConfig testConfig) throws IOException, InterruptedException, IntegrationException {
    File targetTarFile;
    if (testConfig.getTargetTarInSharedDir() != null) {
        targetTarFile = testConfig.getTargetTarInSharedDir();
    } else {
        targetTarFile = new File(testConfig.getTarFilePath());
    }
    String inspectTargetArg = String.format("--docker.tar=%s", targetTarFile);
    TestUtils.ensureFileDoesNotExist(testConfig.getOutputContainerFileSystemFile());
    if (testConfig.getOutputSquashedImageFile() != null) {
        TestUtils.ensureFileDoesNotExist(testConfig.getOutputSquashedImageFile());
    }
    File actualBdio;
    if (testConfig.getMode() == TestConfig.Mode.DETECT) {
        actualBdio = new File(String.format("%s/blackduck/bdio/%s_bdio.jsonld", System.getProperty("user.home"), testConfig.getCodelocationName()));
    } else {
        actualBdio = new File(String.format("%s/output/%s_bdio.jsonld", TestUtils.TEST_DIR_REL_PATH, testConfig.getCodelocationName()));
    }
    TestUtils.ensureFileDoesNotExist(actualBdio);
    List<String> cmd = commandCreator.createCmd(testConfig.getMode(), inspectTargetArg, testConfig.getTargetRepo(), testConfig.getTargetTag(), testConfig.getCodelocationName(), testConfig.getAdditionalArgs());
    System.out.printf("Running end to end test on %s with command %s\n", targetTarFile, cmd.toString());
    TestUtils.execCmd(String.join(" ", cmd), 240000L, true, testConfig.getEnv());
    System.out.println("blackduck-docker-inspector done; verifying results...");
    System.out.printf("Expecting output BDIO file: %s\n", actualBdio.getAbsolutePath());
    assertTrue(String.format("%s does not exist", actualBdio.getAbsolutePath()), actualBdio.exists());
    if (testConfig.isRequireBdioMatch()) {
        File expectedBdio = new File(String.format("src/test/resources/bdio/%s_bdio.jsonld", testConfig.getCodelocationName()));
        List<String> exceptLinesContainingThese = new ArrayList<>();
        exceptLinesContainingThese.add("\"@id\":");
        exceptLinesContainingThese.add("spdx:name");
        exceptLinesContainingThese.add("spdx:created");
        exceptLinesContainingThese.add("Tool:");
        exceptLinesContainingThese.add("kbSeparator");
        boolean outputBdioMatches = TestUtils.contentEquals(expectedBdio, actualBdio, exceptLinesContainingThese);
        assertTrue("BDIO produced does not match expected BDIO", outputBdioMatches);
    }
    SimpleBdioDocument doc = createBdioDocumentFromFile(actualBdio);
    assertTrue(doc.getComponents().size() >= testConfig.getMinNumberOfComponentsExpected());
    if (StringUtils.isNotBlank(testConfig.getOutputBomMustContainComponentPrefix())) {
        System.out.printf("Looking for component name starting with: %s\n", testConfig.getOutputBomMustContainComponentPrefix());
        boolean componentFound = isComponentFound(doc, testConfig.getOutputBomMustContainComponentPrefix());
        assertTrue(componentFound);
        System.out.printf("Found it\n");
    }
    if (StringUtils.isNotBlank(testConfig.getOutputBomMustNotContainComponentPrefix())) {
        System.out.printf("Making sure there is no component name starting with: %s\n", testConfig.getOutputBomMustNotContainComponentPrefix());
        boolean componentFound = false;
        componentFound = isComponentFound(doc, testConfig.getOutputBomMustNotContainComponentPrefix());
        assertFalse(componentFound);
        System.out.printf("It's not there\n");
    }
    if (StringUtils.isNotBlank(testConfig.getOutputBomMustContainExternalSystemTypeId())) {
        System.out.printf("Looking for component with externalSystemTypeId: %s\n", testConfig.getOutputBomMustContainExternalSystemTypeId());
        boolean externalSystemTypeIdFound = false;
        for (int i = 0; i < doc.getComponents().size(); i++) {
            System.out.printf("\tComponent: %s / %s; externalSystemTypeId: %s\n", doc.getComponents().get(i).name, doc.getComponents().get(i).version, doc.getComponents().get(i).bdioExternalIdentifier.forge);
            if (doc.getComponents().get(i).bdioExternalIdentifier.forge.equals(testConfig.getOutputBomMustContainExternalSystemTypeId())) {
                externalSystemTypeIdFound = true;
                break;
            }
        }
        assertTrue(externalSystemTypeIdFound);
        System.out.printf("Found it\n");
    }
    if (testConfig.getMode() != TestConfig.Mode.DETECT) {
        assertTrue(String.format("%s does not exist", testConfig.getOutputContainerFileSystemFile().getAbsolutePath()), testConfig.getOutputContainerFileSystemFile().exists());
    }
    if (testConfig.getOutputSquashedImageFile() != null) {
        assertTrue(String.format("%s does not exist", testConfig.getOutputSquashedImageFile().getAbsolutePath()), testConfig.getOutputSquashedImageFile().exists());
    }
    if ((testConfig.getMinContainerFileSystemFileSize() > 0) || (testConfig.getMaxContainerFileSystemFileSize() > 0)) {
        long actualContainerFileSystemFileSize = testConfig.getOutputContainerFileSystemFile().length();
        assertTrue(actualContainerFileSystemFileSize >= testConfig.getMinContainerFileSystemFileSize());
        assertTrue(actualContainerFileSystemFileSize <= testConfig.getMaxContainerFileSystemFileSize());
    }
    File resultsFile = new File(String.format(String.format("%s/output/%s", TestUtils.TEST_DIR_REL_PATH, ProgramPaths.RESULTS_JSON_FILENAME)));
    String resultsJsonString = FileUtils.readFileToString(resultsFile, StandardCharsets.UTF_8);
    Gson gson = new Gson();
    Result result = gson.fromJson(resultsJsonString, Result.class);
    assertTrue(result.isSucceeded());
    System.out.printf("results: %s", result.toString());
    assertTrue(StringUtils.isNotBlank(result.getDockerTarfilename()));
    assertTrue(result.getDockerTarfilename().endsWith(".tar"));
    assertEquals(0, result.getReturnCode());
    assertTrue(result.getBdioFilename().endsWith(".jsonld"));
    assertTrue(testConfig.getTarFilePath().endsWith(result.getDockerTarfilename()));
    if (testConfig.getOutputContainerFileSystemFile() != null) {
        assertTrue(result.getContainerFilesystemFilename().endsWith(".tar.gz"));
    }
    if (testConfig.isTestSquashedImageGeneration()) {
        assertTrue(result.getSquashedImageFilename().endsWith(".tar.gz"));
    }
}
Also used : ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) File(java.io.File) SimpleBdioDocument(com.synopsys.integration.bdio.model.SimpleBdioDocument) Result(com.synopsys.integration.blackduck.dockerinspector.output.Result)

Example 2 with Result

use of com.synopsys.integration.blackduck.dockerinspector.output.Result in project blackduck-docker-inspector by blackducksoftware.

the class IntegrationTestRunner method testImage.

public void testImage(TestConfig testConfig) throws IOException, InterruptedException, IntegrationException {
    String inspectTargetArg = null;
    File outputContainerFileSystemFile = null;
    if (testConfig.getInspectTargetImageRepoTag() != null) {
        outputContainerFileSystemFile = getOutputContainerFileSystemFileFromImageSpec(testConfig.getInspectTargetImageRepoTag());
        inspectTargetArg = String.format("--docker.image=%s", testConfig.getInspectTargetImageRepoTag());
    } else if (testConfig.getInspectTargetImageId() != null) {
        inspectTargetArg = String.format("--docker.image.id=%s", testConfig.getInspectTargetImageId());
    }
    TestUtils.ensureFileDoesNotExist(outputContainerFileSystemFile);
    File actualBdio = null;
    if (testConfig.getCodelocationName() != null) {
        if (testConfig.getMode() == TestConfig.Mode.DETECT) {
            actualBdio = new File(String.format(String.format("%s/blackduck/bdio/%s_bdio.jsonld", System.getProperty("user.home"), testConfig.getCodelocationName())));
        } else {
            actualBdio = new File(String.format(String.format("%s/output/%s_bdio.jsonld", TestUtils.TEST_DIR_REL_PATH, testConfig.getCodelocationName())));
        }
        TestUtils.ensureFileDoesNotExist(actualBdio);
    }
    List<String> cmd = commandCreator.createCmd(testConfig.getMode(), inspectTargetArg, testConfig.getTargetRepo(), testConfig.getTargetTag(), testConfig.getCodelocationName(), testConfig.getAdditionalArgs());
    System.out.println(String.format("Running end to end test on %s with command %s", testConfig.getInspectTargetImageRepoTag(), cmd.toString()));
    TestUtils.execCmd(String.join(" ", cmd), 30000L, true, testConfig.getEnv());
    System.out.println("blackduck-docker-inspector done; verifying results...");
    if (actualBdio != null) {
        System.out.printf("Expecting output BDIO file: %s\n", actualBdio.getAbsolutePath());
        assertTrue(actualBdio.exists());
        // TODO should use a order-insensitive comparison like Detect does
        if (testConfig.isRequireBdioMatch()) {
            File expectedBdio = new File(String.format(String.format("src/test/resources/bdio/%s_bdio.jsonld", testConfig.getCodelocationName())));
            List<String> exceptLinesContainingThese = new ArrayList<>();
            exceptLinesContainingThese.add("\"@id\":");
            exceptLinesContainingThese.add("spdx:created");
            exceptLinesContainingThese.add("Tool:");
            exceptLinesContainingThese.add("kbSeparator");
            boolean outputBdioMatches = TestUtils.contentEquals(expectedBdio, actualBdio, exceptLinesContainingThese);
            assertTrue(outputBdioMatches);
        }
        SimpleBdioDocument doc = createBdioDocumentFromFile(actualBdio);
        assertTrue(doc.getComponents().size() >= testConfig.getMinNumberOfComponentsExpected());
        if (StringUtils.isNotBlank(testConfig.getOutputBomMustContainComponentPrefix())) {
            System.out.printf("Looking for component name starting with: %s\n", testConfig.getOutputBomMustContainComponentPrefix());
            boolean componentFound = false;
            componentFound = isComponentFound(doc, testConfig.getOutputBomMustContainComponentPrefix());
            assertTrue(componentFound);
            System.out.printf("Found it\n");
        }
        if (StringUtils.isNotBlank(testConfig.getOutputBomMustNotContainComponentPrefix())) {
            System.out.printf("Making sure there is no component name starting with: %s\n", testConfig.getOutputBomMustNotContainComponentPrefix());
            boolean componentFound = false;
            componentFound = isComponentFound(doc, testConfig.getOutputBomMustNotContainComponentPrefix());
            assertFalse(componentFound);
            System.out.printf("It's not there\n");
        }
        if (StringUtils.isNotBlank(testConfig.getOutputBomMustContainExternalSystemTypeId())) {
            System.out.printf("Looking for component with externalSystemTypeId: %s\n", testConfig.getOutputBomMustContainExternalSystemTypeId());
            boolean externalSystemTypeIdFound = false;
            for (int i = 0; i < doc.getComponents().size(); i++) {
                System.out.printf("\tComponent: %s / %s; externalSystemTypeId: %s\n", doc.getComponents().get(i).name, doc.getComponents().get(i).version, doc.getComponents().get(i).bdioExternalIdentifier.forge);
                if (doc.getComponents().get(i).bdioExternalIdentifier.forge.equals(testConfig.getOutputBomMustContainExternalSystemTypeId())) {
                    externalSystemTypeIdFound = true;
                    break;
                }
            }
            assertTrue(externalSystemTypeIdFound);
            System.out.printf("Found it\n");
        }
    }
    if ((testConfig.getMode() != TestConfig.Mode.DETECT) && (outputContainerFileSystemFile != null)) {
        assertTrue(outputContainerFileSystemFile.exists());
    }
    if ((outputContainerFileSystemFile != null) && ((testConfig.getMinContainerFileSystemFileSize() > 0) || (testConfig.getMaxContainerFileSystemFileSize() > 0))) {
        long actualContainerFileSystemFileSize = outputContainerFileSystemFile.length();
        assertTrue(actualContainerFileSystemFileSize >= testConfig.getMinContainerFileSystemFileSize());
        assertTrue(actualContainerFileSystemFileSize <= testConfig.getMaxContainerFileSystemFileSize());
    }
    File resultsFile = new File(String.format(String.format("%s/output/%s", TestUtils.TEST_DIR_REL_PATH, ProgramPaths.RESULTS_JSON_FILENAME)));
    String resultsJsonString = FileUtils.readFileToString(resultsFile, StandardCharsets.UTF_8);
    Gson gson = new Gson();
    Result result = gson.fromJson(resultsJsonString, Result.class);
    assertTrue(result.isSucceeded());
    System.out.printf("results: %s", result.toString());
    if (testConfig.getInspectTargetImageRepoTag() != null) {
        if (testConfig.getInspectTargetImageRepoTag().contains(":")) {
            assertEquals(testConfig.getInspectTargetImageRepoTag(), String.format("%s:%s", result.getImageRepo(), result.getImageTag()));
        } else {
            assertEquals(testConfig.getInspectTargetImageRepoTag(), result.getImageRepo());
            assertEquals("latest", result.getImageTag());
        }
    } else {
        assertTrue(StringUtils.isNotBlank(result.getImageRepo()));
        assertTrue(StringUtils.isNotBlank(result.getImageTag()));
    }
    assertTrue(StringUtils.isNotBlank(result.getDockerTarfilename()));
    assertTrue(result.getDockerTarfilename().endsWith(".tar"));
    assertEquals(0, result.getReturnCode());
    assertTrue(result.getBdioFilename().endsWith(".jsonld"));
    if (testConfig.getOutputContainerFileSystemFile() != null) {
        assertTrue(result.getContainerFilesystemFilename().endsWith(".tar.gz"));
    }
    if (testConfig.isTestSquashedImageGeneration()) {
        assertTrue(result.getSquashedImageFilename().endsWith(".tar.gz"));
    }
}
Also used : ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) File(java.io.File) SimpleBdioDocument(com.synopsys.integration.bdio.model.SimpleBdioDocument) Result(com.synopsys.integration.blackduck.dockerinspector.output.Result)

Example 3 with Result

use of com.synopsys.integration.blackduck.dockerinspector.output.Result in project blackduck-docker-inspector by blackducksoftware.

the class HttpClientInspector method getBdio.

public Result getBdio() throws IntegrationException, InterruptedException {
    ImageInspectorClient imageInspectorClient = chooseImageInspectorClient();
    try {
        output.ensureWorkingOutputDirIsWriteable();
        ImageTarWrapper finalDockerTarfile = prepareDockerTarfile(imageInspectorClient);
        String containerFileSystemFilename = containerFilesystemFilename.deriveContainerFilesystemFilename(finalDockerTarfile.getImageRepo(), finalDockerTarfile.getImageTag());
        String dockerTarFilePathInContainer = containerPaths.getContainerPathToTargetFile(finalDockerTarfile.getFile().getCanonicalPath());
        String containerFileSystemPathInContainer = null;
        if (config.isOutputIncludeContainerfilesystem() || config.isOutputIncludeSquashedImage()) {
            containerFileSystemPathInContainer = containerPaths.getContainerPathToOutputFile(containerFileSystemFilename);
        }
        String bdioString = imageInspectorClient.getBdio(finalDockerTarfile.getFile().getCanonicalPath(), dockerTarFilePathInContainer, config.getDockerImageRepo(), config.getDockerImageTag(), containerFileSystemPathInContainer, config.getContainerFileSystemExcludedPaths(), config.isOrganizeComponentsByLayer(), config.isIncludeRemovedComponents(), config.isCleanupWorkingDir(), config.getDockerPlatformTopLayerId(), config.getTargetImageLinuxDistroOverride());
        logger.trace(String.format("bdioString: %s", bdioString));
        SimpleBdioDocument bdioDocument = toBdioDocument(bdioString);
        adjustBdio(bdioDocument);
        OutputFiles outputFiles = output.addOutputToFinalOutputDir(bdioDocument, finalDockerTarfile.getImageRepo(), finalDockerTarfile.getImageTag());
        if (config.isUploadBdio()) {
            NameVersion projectAndVersion = new NameVersion(bdioDocument.getProject().name, bdioDocument.getProject().version);
            blackDuckClient.uploadBdio(outputFiles.getBdioFile(), bdioDocument.getBillOfMaterials().spdxName, projectAndVersion);
        }
        cleanup();
        Result result = Result.createResultSuccess(finalDockerTarfile.getImageRepo(), finalDockerTarfile.getImageTag(), finalDockerTarfile.getFile().getName(), outputFiles.getBdioFile(), outputFiles.getContainerFileSystemFile(), outputFiles.getSquashedImageFile());
        return result;
    } catch (IOException e) {
        throw new IntegrationException(e.getMessage(), e);
    }
}
Also used : ImageTarWrapper(com.synopsys.integration.blackduck.dockerinspector.output.ImageTarWrapper) NameVersion(com.synopsys.integration.util.NameVersion) IntegrationException(com.synopsys.integration.exception.IntegrationException) OutputFiles(com.synopsys.integration.blackduck.dockerinspector.output.OutputFiles) IOException(java.io.IOException) SimpleBdioDocument(com.synopsys.integration.bdio.model.SimpleBdioDocument) Result(com.synopsys.integration.blackduck.dockerinspector.output.Result)

Example 4 with Result

use of com.synopsys.integration.blackduck.dockerinspector.output.Result in project blackduck-docker-inspector by blackducksoftware.

the class DockerInspector method inspectImage.

@PostConstruct
public void inspectImage() {
    Result result = null;
    try {
        if (!initAndValidate(config)) {
            System.exit(0);
        }
        result = inspector.getBdio();
    } catch (HelpGenerationException helpGenerationException) {
        String msg = String.format("Error generating help: %s", helpGenerationException.getMessage());
        logger.error(msg);
        logStackTraceIfDebug(helpGenerationException);
        result = Result.createResultFailure(msg);
    } catch (Exception e) {
        String msg = String.format("Error inspecting image: %s", e.getMessage());
        logger.error(msg);
        logStackTraceIfDebug(e);
        result = Result.createResultFailure(msg);
    }
    File resultsFile = new File(output.getFinalOutputDir(), programPaths.getDockerInspectorResultsFilename());
    resultFile.write(new Gson(), resultsFile, result);
    int returnCode = result.getReturnCode();
    logger.info(String.format("Returning %d", returnCode));
    System.exit(returnCode);
}
Also used : Gson(com.google.gson.Gson) File(java.io.File) ResultFile(com.synopsys.integration.blackduck.dockerinspector.output.ResultFile) HelpGenerationException(com.synopsys.integration.blackduck.dockerinspector.exception.HelpGenerationException) IntegrationException(com.synopsys.integration.exception.IntegrationException) HelpGenerationException(com.synopsys.integration.blackduck.dockerinspector.exception.HelpGenerationException) FileNotFoundException(java.io.FileNotFoundException) Result(com.synopsys.integration.blackduck.dockerinspector.output.Result) PostConstruct(javax.annotation.PostConstruct)

Aggregations

Result (com.synopsys.integration.blackduck.dockerinspector.output.Result)4 Gson (com.google.gson.Gson)3 SimpleBdioDocument (com.synopsys.integration.bdio.model.SimpleBdioDocument)3 File (java.io.File)3 IntegrationException (com.synopsys.integration.exception.IntegrationException)2 ArrayList (java.util.ArrayList)2 HelpGenerationException (com.synopsys.integration.blackduck.dockerinspector.exception.HelpGenerationException)1 ImageTarWrapper (com.synopsys.integration.blackduck.dockerinspector.output.ImageTarWrapper)1 OutputFiles (com.synopsys.integration.blackduck.dockerinspector.output.OutputFiles)1 ResultFile (com.synopsys.integration.blackduck.dockerinspector.output.ResultFile)1 NameVersion (com.synopsys.integration.util.NameVersion)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 PostConstruct (javax.annotation.PostConstruct)1