-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Zvi Grinberg <zgrinber@redhat.com>
- Loading branch information
1 parent
b17c33a
commit 3542331
Showing
10 changed files
with
256 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.6/apache-maven-3.9.6-bin.zip | ||
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
226 changes: 226 additions & 0 deletions
226
src/test/java/com/redhat/exhort/impl/ExhortApiIT.java.orig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
/* | ||
* Copyright © 2023 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.redhat.exhort.impl; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.redhat.exhort.Api; | ||
import com.redhat.exhort.ExhortTest; | ||
import com.redhat.exhort.api.AnalysisReport; | ||
import com.redhat.exhort.api.ProviderReport; | ||
import com.redhat.exhort.providers.HelperExtension; | ||
import com.redhat.exhort.tools.Ecosystem; | ||
import com.redhat.exhort.tools.Operations; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.EnumSource; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.invocation.InvocationOnMock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mockStatic; | ||
|
||
@Tag("IntegrationTest") | ||
@ExtendWith(HelperExtension.class) | ||
@ExtendWith(MockitoExtension.class) | ||
class ExhortApiIT extends ExhortTest { | ||
|
||
private static Api api; | ||
private static Map<String,String> ecoSystemsManifestNames; | ||
|
||
private MockedStatic<Operations> mockedOperations; | ||
@BeforeAll | ||
static void beforeAll() { | ||
api = new ExhortApi(); | ||
<<<<<<< HEAD | ||
System.setProperty("EXHORT_DEV_MODE","true"); | ||
======= | ||
System.setProperty("RHDA_SOURCE","exhort-java-api-it"); | ||
System.setProperty("EXHORT_DEV_MODE","false"); | ||
>>>>>>> java-enhanced-it-working | ||
ecoSystemsManifestNames = Map.of("golang", "go.mod","maven","pom.xml","npm","package.json","pypi","requirements.txt"); | ||
|
||
} | ||
|
||
@Tag("IntegrationTest") | ||
@AfterAll | ||
static void afterAll() { | ||
<<<<<<< HEAD | ||
======= | ||
System.clearProperty("RHDA_SOURCE"); | ||
>>>>>>> java-enhanced-it-working | ||
System.clearProperty("EXHORT_DEV_MODE"); | ||
api = null; | ||
} | ||
@Tag("IntegrationTest") | ||
@ParameterizedTest | ||
@EnumSource(value = Ecosystem.Type.class, names = { "GOLANG", "MAVEN", "NPM", "PYTHON" }) | ||
void Integration_Test_End_To_End_Stack_Analysis(Ecosystem.Type packageManager) throws IOException, ExecutionException, InterruptedException { | ||
String manifestFileName = ecoSystemsManifestNames.get(packageManager.getType()); | ||
String pathToManifest = getFileFromResource(manifestFileName, "tst_manifests", "it", packageManager.getType(), manifestFileName); | ||
preparePythonEnvironment(packageManager); | ||
// Github action runner with all maven and java versions seems to enter infinite loop in integration tests of MAVEN when runnig dependency maven plugin to produce verbose text dependenct tree format. | ||
// locally it's not recreated with same versions | ||
mockMavenDependencyTree(packageManager); | ||
AnalysisReport analysisReportResult = api.stackAnalysis(pathToManifest).get(); | ||
handleJsonResponse(analysisReportResult,true); | ||
releaseStaticMock(packageManager); | ||
} | ||
|
||
private void releaseStaticMock(Ecosystem.Type packageManager) { | ||
if(packageManager.equals(Ecosystem.Type.MAVEN)) { | ||
this.mockedOperations.close(); | ||
} | ||
} | ||
|
||
|
||
@Tag("IntegrationTest") | ||
@ParameterizedTest | ||
@EnumSource(value = Ecosystem.Type.class, names = { "GOLANG", "MAVEN", "NPM", "PYTHON" }) | ||
void Integration_Test_End_To_End_Stack_Analysis_Mixed(Ecosystem.Type packageManager) throws IOException, ExecutionException, InterruptedException { | ||
String manifestFileName = ecoSystemsManifestNames.get(packageManager.getType()); | ||
String pathToManifest = getFileFromResource(manifestFileName, "tst_manifests", "it", packageManager.getType(), manifestFileName); | ||
preparePythonEnvironment(packageManager); | ||
// Github action runner with all maven and java versions seems to enter infinite loop in integration tests of MAVEN when runnig dependency maven plugin to produce verbose text dependenct tree format. | ||
// locally it's not recreated with same versions | ||
mockMavenDependencyTree(packageManager); | ||
AnalysisReport analysisReportJson = api.stackAnalysisMixed(pathToManifest).get().json; | ||
String analysisReportHtml = new String(api.stackAnalysisMixed(pathToManifest).get().html); | ||
handleJsonResponse(analysisReportJson,true); | ||
handleHtmlResponse(analysisReportHtml); | ||
releaseStaticMock(packageManager); | ||
} | ||
|
||
@Tag("IntegrationTest") | ||
@ParameterizedTest | ||
@EnumSource(value = Ecosystem.Type.class, names = { "GOLANG", "MAVEN", "NPM", "PYTHON" }) | ||
void Integration_Test_End_To_End_Stack_Analysis_Html(Ecosystem.Type packageManager) throws IOException, ExecutionException, InterruptedException { | ||
String manifestFileName = ecoSystemsManifestNames.get(packageManager.getType()); | ||
String pathToManifest = getFileFromResource(manifestFileName, "tst_manifests", "it", packageManager.getType(), manifestFileName); | ||
preparePythonEnvironment(packageManager); | ||
// Github action runner with all maven and java versions seems to enter infinite loop in integration tests of MAVEN when runnig dependency maven plugin to produce verbose text dependenct tree format. | ||
// locally it's not recreated with same versions | ||
mockMavenDependencyTree(packageManager); | ||
String analysisReportHtml = new String(api.stackAnalysisHtml(pathToManifest).get()); | ||
releaseStaticMock(packageManager); | ||
handleHtmlResponse(analysisReportHtml); | ||
} | ||
|
||
|
||
@Tag("IntegrationTest") | ||
@ParameterizedTest | ||
@EnumSource(value = Ecosystem.Type.class, names = { "GOLANG", "MAVEN", "NPM", "PYTHON" }) | ||
void Integration_Test_End_To_End_Component_Analysis(Ecosystem.Type packageManager) throws IOException, ExecutionException, InterruptedException { | ||
String manifestFileName = ecoSystemsManifestNames.get(packageManager.getType()); | ||
byte[] manifestContent = getStringFromFile("tst_manifests", "it", packageManager.getType(), manifestFileName).getBytes(); | ||
preparePythonEnvironment(packageManager); | ||
AnalysisReport analysisReportResult = api.componentAnalysis(manifestFileName,manifestContent).get(); | ||
handleJsonResponse(analysisReportResult,false); | ||
} | ||
|
||
|
||
private static void preparePythonEnvironment(Ecosystem.Type packageManager) { | ||
if(packageManager.equals(Ecosystem.Type.PYTHON)) { | ||
System.setProperty("EXHORT_PYTHON_VIRTUAL_ENV","true"); | ||
System.setProperty("EXHORT_PYTHON_INSTALL_BEST_EFFORTS","true"); | ||
System.setProperty("MATCH_MANIFEST_VERSIONS","false"); | ||
} | ||
else { | ||
System.clearProperty("EXHORT_PYTHON_VIRTUAL_ENV"); | ||
System.clearProperty("EXHORT_PYTHON_INSTALL_BEST_EFFORTS"); | ||
System.clearProperty("MATCH_MANIFEST_VERSIONS"); | ||
} | ||
} | ||
|
||
private static void handleJsonResponse(AnalysisReport analysisReportResult, boolean positiveNumberOfTransitives) { | ||
analysisReportResult.getProviders().entrySet().stream().forEach(provider -> { assertTrue(provider.getValue().getStatus().getOk()); | ||
assertTrue(provider.getValue().getStatus().getCode() == HttpURLConnection.HTTP_OK); | ||
}); | ||
analysisReportResult.getProviders().entrySet().stream() | ||
.map(Map.Entry::getValue) | ||
.map(ProviderReport::getSources) | ||
.map(Map::entrySet) | ||
.flatMap(Collection::stream) | ||
.map(Map.Entry::getValue) | ||
.forEach( source -> assertTrue(source.getSummary().getTotal() > 0 )); | ||
|
||
if(positiveNumberOfTransitives) { | ||
assertTrue(analysisReportResult.getScanned().getTransitive() > 0); | ||
} | ||
else { | ||
assertEquals(0,analysisReportResult.getScanned().getTransitive()); | ||
} | ||
} | ||
|
||
private void handleHtmlResponse(String analysisReportHtml) throws JsonProcessingException { | ||
ObjectMapper om = new ObjectMapper(); | ||
assertTrue(analysisReportHtml.contains("svg") && analysisReportHtml.contains("html")); | ||
int jsonStart = analysisReportHtml.indexOf("\"report\":"); | ||
int jsonEnd = analysisReportHtml.indexOf("}}}}}"); | ||
String embeddedJson = analysisReportHtml.substring(jsonStart + 9 ,jsonEnd + 5); | ||
JsonNode jsonInHtml = om.readTree(embeddedJson); | ||
JsonNode scannedNode = jsonInHtml.get("scanned"); | ||
assertTrue(scannedNode.get("total").asInt(0) > 0); | ||
assertTrue(scannedNode.get("transitive").asInt(0) > 0); | ||
JsonNode status = jsonInHtml.get("providers").get("snyk").get("status"); | ||
assertTrue(status.get("code").asInt(0) == 200); | ||
assertTrue(status.get("ok").asBoolean(false)); | ||
|
||
} | ||
private void mockMavenDependencyTree(Ecosystem.Type packageManager) throws IOException { | ||
if(packageManager.equals(Ecosystem.Type.MAVEN)) { | ||
mockedOperations = mockStatic(Operations.class); | ||
String depTree; | ||
try (var is = getResourceAsStreamDecision(getClass(), new String [] { "tst_manifests", "it","maven", "depTree.txt"})) { | ||
depTree = new String(is.readAllBytes()); | ||
} | ||
mockedOperations.when(() -> Operations.runProcess(any(),any())).thenAnswer(invocationOnMock -> { return getOutputFileAndOverwriteItWithMock(depTree, invocationOnMock, "-DoutputFile");}); | ||
} | ||
} | ||
|
||
public static String getOutputFileAndOverwriteItWithMock(String outputFileContent, InvocationOnMock invocationOnMock, String parameterPrefix) throws IOException { | ||
String[] rawArguments = (String[]) invocationOnMock.getRawArguments()[0]; | ||
Optional<String> outputFileArg = Arrays.stream(rawArguments).filter(arg -> arg!= null && arg.startsWith(parameterPrefix)).findFirst(); | ||
String outputFilePath=null; | ||
if(outputFileArg.isPresent()) | ||
{ | ||
String outputFile = outputFileArg.get(); | ||
outputFilePath = outputFile.substring(outputFile.indexOf("=") + 1); | ||
Files.writeString(Path.of(outputFilePath), outputFileContent); | ||
} | ||
return outputFilePath; | ||
} | ||
|
||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters