Skip to content

Commit

Permalink
fix: Fix logs and exceptions handling for single scan mode (#519)
Browse files Browse the repository at this point in the history
* fix: Fix logs and exceptions handling for single scan mode

Signed-off-by: Oleg Kopysov <o.kopysov@samsung.com>

* Update variable name

Co-authored-by: Taewan Kim <t25.kim@samsung.com>
Signed-off-by: Oleg Kopysov <o.kopysov@samsung.com>

---------

Signed-off-by: Oleg Kopysov <o.kopysov@samsung.com>
Co-authored-by: Taewan Kim <t25.kim@samsung.com>
  • Loading branch information
o-kopysov and tiokim authored May 20, 2024
1 parent e08fe1b commit 777dfc8
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/main/java/com/lpvs/LicensePreValidationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package com.lpvs;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
Expand All @@ -25,6 +26,7 @@
@SpringBootApplication(scanBasePackages = {"com.lpvs"})
@EnableAutoConfiguration
@EnableAsync
@Slf4j
public class LicensePreValidationService {

/**
Expand Down Expand Up @@ -57,8 +59,10 @@ public static void main(String[] args) {
SpringApplication.run(LicensePreValidationService.class, args);
exitHandler = applicationContext.getBean(LPVSExitHandler.class);
} catch (IllegalArgumentException e) {
System.err.println("An IllegalArgumentException occurred: " + e.getMessage());
log.error("An IllegalArgumentException occurred: " + e.getMessage());
System.exit(1);
} catch (Exception e) {
log.info("LPVS application is being closed.");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/lpvs/service/LPVSGitHubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public void commentResults(
GHPullRequest pullRequest = getPullRequest(webhookConfig, repository);

if (pullRequest == null) {
log.error("Can't find pull request " + webhookConfig.getPullRequestUrl());
log.error("Pull request is not found " + webhookConfig.getPullRequestUrl());
lpvsPullRequest.setStatus(LPVSPullRequestStatus.NO_ACCESS.toString());
pullRequestRepository.saveAndFlush(lpvsPullRequest);
return;
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/lpvs/service/scan/LPVSDetectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ public void runOneScan() {
String report =
LPVSCommentUtil.reportCommentBuilder(
webhookConfig, scanResult, detectedConflicts);
log.info(report);
if (report != null && !report.isEmpty()) {
log.info(report);
}
}
log.info("Single scan completed.");
} catch (Exception ex) {
log.error("\n\n\n Single scan finished with errors \n\n\n");
log.error("Single scan finished with errors.");
log.error("Can't trigger single scan: " + ex.getMessage());
}
SpringApplication.exit(ctx, () -> 0);
Expand Down Expand Up @@ -174,7 +176,7 @@ public List<LPVSFile> runScan(LPVSQueue webhookConfig, String path) throws Excep
}
return files;
} catch (IllegalArgumentException | NullPointerException ex) {
log.error(ex.getMessage());
log.error("Exception occurred during running the scan.");
return new ArrayList<>();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ public void runScan(LPVSQueue webhookConfig, String path) throws Exception {
LPVSPayloadUtil.createBufferReader(
LPVSPayloadUtil.createInputStreamReader(
process.getErrorStream()));
log.error(output.readLine());
String line = output.readLine();
if (line != null) {
log.error(line);
}
throw new Exception(
"Scanoss scanner terminated with non-zero code. Terminating.");
} finally {
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/com/lpvs/service/scan/LPVSDetectServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,20 @@ void testCommentBuilder_ConflictFilePresent() throws Exception {
assertNotNull(commentHTML);
}

@Test
void testCommentBuilder_NoConflictNoLicense() {
List<LPVSLicenseService.Conflict<String, String>> expected = new ArrayList<>();
LPVSQueue webhookConfig = new LPVSQueue();
List<LPVSFile> scanResults = new ArrayList<>();
String commentGitHub =
LPVSCommentUtil.reportCommentBuilder(webhookConfig, scanResults, expected);
String commentHTML =
LPVSCommentUtil.buildHTMLComment(webhookConfig, scanResults, expected);

assertEquals(commentGitHub, "");
assertEquals(commentHTML, "<html><body></body></html>");
}

@Test
public void testGetPathByPullRequest() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,33 @@ public void testRunScan_StatusEqualsOne() throws Exception {
}
}

@Test
public void testRunScan_StatusEqualsOneNoErrorStream() throws Exception {
Mockito.when(lpvsQueue.getRepositoryUrl()).thenReturn("https://github.com/Samsung/LPVS");
Mockito.when(lpvsQueue.getPullRequestUrl())
.thenReturn("https://github.com/Samsung/LPVS/pull/1");
Process process = Mockito.mock(Process.class);
try (MockedConstruction<ProcessBuilder> mockedPb =
Mockito.mockConstruction(
ProcessBuilder.class,
(mock, context) -> {
when(mock.inheritIO()).thenReturn(mock);
when(mock.start()).thenReturn(process);
when(process.getErrorStream()).thenReturn(null);
when(process.waitFor()).thenReturn(1);
})) {
Exception exception =
assertThrows(
Exception.class, () -> scanossDetectService.runScan(lpvsQueue, "path"));

// Verify that the method throws an exception when the status is 1
assertEquals(null, exception.getMessage());

verify(mockedPb.constructed().get(0)).start();
verify(process, times(1)).waitFor();
}
}

@Test
public void testRunScan_StatusEqualsZero() throws Exception {
Process process = Mockito.mock(Process.class);
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/com/lpvs/util/LPVSCommentUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,22 @@ void testReportCommentBuilder() {
assertNotNull(comment);
}

@Test
void testReportCommentBuilder_HubLink() {
LPVSQueue webhookConfig = new LPVSQueue();
List<LPVSFile> scanResults = new ArrayList<>();
scanResults.add(createSampleFile("testPath1", "test1"));
LPVSLicenseService.Conflict<String, String> conflict_1 =
new LPVSLicenseService.Conflict<>("MIT", "Apache-2.0");
List<LPVSLicenseService.Conflict<String, String>> conflicts =
List.of(conflict_1, conflict_1);
webhookConfig.setHubLink("any_link");
String comment =
LPVSCommentUtil.reportCommentBuilder(webhookConfig, scanResults, conflicts);

assertNotNull(comment);
}

@Test
void testBuildHTMLComment() {
LPVSQueue webhookConfig = new LPVSQueue();
Expand All @@ -150,6 +166,22 @@ void testBuildHTMLComment() {
assertNotNull(htmlComment);
}

@Test
void testBuildHTMLComment_HubLink() {
LPVSQueue webhookConfig = new LPVSQueue();
List<LPVSFile> scanResults = new ArrayList<>();
scanResults.add(createSampleFile("testPath1", "test1"));
LPVSLicenseService.Conflict<String, String> conflict_1 =
new LPVSLicenseService.Conflict<>("MIT", "Apache-2.0");
List<LPVSLicenseService.Conflict<String, String>> conflicts =
List.of(conflict_1, conflict_1);
webhookConfig.setHubLink("some_link");
String htmlComment =
LPVSCommentUtil.buildHTMLComment(webhookConfig, scanResults, conflicts);

assertNotNull(htmlComment);
}

@Test
void testSaveHTMLToFile() throws IOException {
String htmlContent = "<html><body><p>Test HTML</p></body></html>";
Expand Down

0 comments on commit 777dfc8

Please sign in to comment.