Skip to content

Commit d6bd919

Browse files
committed
Add unit test to check if results are found from build log
1 parent bca2458 commit d6bd919

File tree

3 files changed

+182
-8
lines changed

3 files changed

+182
-8
lines changed

src/main/java/jenkinsci/plugins/influxdb/generators/SonarQubePointGenerator.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,15 +326,14 @@ private String[] getSonarProjectFromBuildLog(Run<?, ?> build) throws IOException
326326
}
327327
match = p_qg_url_v4_8.matcher(line);
328328
if (match.matches()) {
329-
url = match.group(1);
330-
//Task already executed. No need to search for other lines
331-
break;
329+
url = match.group(1); // https://<url>/dashboard?id=<id>
330+
url = url.substring(0,url.lastIndexOf(('/'))); // strip '/dashboard?id=<id>'
331+
continue;
332332
}
333333
match = p_qg_url_timeout.matcher(line);
334334
if (match.matches()) {
335335
url = match.group(1);
336-
//Task already executed. No need to search for other lines
337-
break;
336+
continue;
338337
}
339338
match = p_analysis_url.matcher(line);
340339
if (match.matches()) {

src/test/java/jenkinsci/plugins/influxdb/generators/SonarQubePointGeneratorTest.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
import static org.junit.Assert.*;
1717
import static org.mockito.ArgumentMatchers.any;
1818

19+
import java.io.BufferedReader;
1920
import java.io.File;
21+
import java.io.FileReader;
22+
import java.io.IOException;
2023
import java.util.List;
2124
import java.nio.file.Path;
2225
import java.nio.file.Paths;
@@ -45,6 +48,8 @@ public class SonarQubePointGeneratorTest {
4548
private static final String[] customReportPath = {"custom", "report", "path"};
4649
private static final String customReportName = "custom-report.txt";
4750

51+
private File resourceDirectory;
52+
4853
@BeforeClass
4954
public static void beforeClass() {
5055

@@ -87,6 +92,12 @@ public void before() {
8792
listener = Mockito.mock(TaskListener.class);
8893
measurementRenderer = new ProjectNameRenderer(CUSTOM_PREFIX, null);
8994

95+
try {
96+
resourceDirectory = new File(SonarQubePointGeneratorTest.class.getResource(".").toURI());
97+
} catch (Exception ignored) {
98+
resourceDirectory = null;
99+
}
100+
90101
Mockito.when(build.getNumber()).thenReturn(BUILD_NUMBER);
91102
Mockito.when(build.getParent()).thenReturn(job);
92103
Mockito.when(job.getName()).thenReturn(JOB_NAME);
@@ -219,11 +230,10 @@ public void getSonarCustomReportFilePath() {
219230
}
220231

221232
@Test
222-
public void hasReportFindsCorrectInformationFromReportFile() throws Exception {
233+
public void hasReportFindsCorrectInformationFromReportFile() {
223234
EnvVars envVars = new EnvVars();
224235
envVars.put("SONARQUBE_BUILD_REPORT_NAME", "report-task.txt");
225-
File directory = new File(SonarQubePointGeneratorTest.class.getResource(".").toURI());
226-
envVars.put("WORKSPACE", directory.getAbsolutePath());
236+
envVars.put("WORKSPACE", resourceDirectory.getAbsolutePath());
227237

228238
SonarQubePointGenerator generator = new SonarQubePointGenerator(build, listener, measurementRenderer, currTime, StringUtils.EMPTY, StringUtils.EMPTY, envVars);
229239
boolean hasReport = generator.hasReport();
@@ -236,4 +246,25 @@ public void hasReportFindsCorrectInformationFromReportFile() throws Exception {
236246
assertEquals(id,generator.getSonarBuildTaskId());
237247
assertEquals(url + "/api/ce/task?id=" + id,generator.getSonarBuildTaskIdUrl());
238248
}
249+
250+
@Test
251+
public void hasReportFindsCorrectInformationFromBuildLogs() throws Exception {
252+
File directory = new File(resourceDirectory, "sonarqube");
253+
File file = new File(directory, "build-log.txt");
254+
BufferedReader reader = new BufferedReader(new FileReader(file));
255+
Mockito.when(build.getLogReader()).thenReturn(reader);
256+
EnvVars envVars = new EnvVars();
257+
envVars.put("WORKSPACE", "mikki hiiri");
258+
259+
SonarQubePointGenerator generator = new SonarQubePointGenerator(build, listener, measurementRenderer, currTime, StringUtils.EMPTY, StringUtils.EMPTY, envVars);
260+
boolean hasReport = generator.hasReport();
261+
262+
String id = "321EXAMPLE";
263+
String url = "http://sonarqube:9001";
264+
assertTrue(hasReport);
265+
assertEquals("InfluxDBPlugin-log",generator.getProjectKey());
266+
assertEquals(url,generator.getSonarBuildURL());
267+
assertEquals(id,generator.getSonarBuildTaskId());
268+
assertEquals(url + "/api/ce/task?id=" + id,generator.getSonarBuildTaskIdUrl());
269+
}
239270
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
INFO: Scanner configuration file: /var/jenkins_home/tools/hudson.plugins.sonar.SonarRunnerInstallation/my-sonar-scanner/conf/sonar-scanner.properties
2+
INFO: Project root configuration file: /var/jenkins_home/workspace/influxdb-plugin/build-influxdb-plugin/sonar-project.properties
3+
INFO: SonarScanner 4.8.0.2856
4+
INFO: Java 11.0.19 Eclipse Adoptium (64-bit)
5+
INFO: Linux 6.6.12-linuxkit amd64
6+
INFO: User cache: /var/jenkins_home/.sonar/cache
7+
INFO: Analyzing on SonarQube server 9.9.1.69595
8+
INFO: Default locale: "en", source code encoding: "UTF-8" (analysis is platform dependent)
9+
INFO: Load global settings
10+
INFO: Load global settings (done) | time=116ms
11+
INFO: Server id: 147B411E-AY1qOJnrLVom97iUZedE
12+
INFO: User cache: /var/jenkins_home/.sonar/cache
13+
INFO: Load/download plugins
14+
INFO: Load plugins index
15+
INFO: Load plugins index (done) | time=42ms
16+
INFO: Load/download plugins (done) | time=166ms
17+
INFO: Process project properties
18+
INFO: Process project properties (done) | time=27ms
19+
INFO: Execute project builders
20+
INFO: Execute project builders (done) | time=44ms
21+
INFO: Project key: InfluxDBPlugin-log
22+
INFO: Base dir: /var/jenkins_home/workspace/influxdb-plugin/build-influxdb-plugin
23+
INFO: Working dir: /var/jenkins_home/workspace/influxdb-plugin/build-influxdb-plugin/.scannerwork
24+
INFO: Load project settings for component key: 'InfluxDBPlugin-log'
25+
INFO: Load project settings for component key: 'InfluxDBPlugin-log' (done) | time=29ms
26+
INFO: Auto-configuring with CI 'Jenkins'
27+
INFO: Load quality profiles
28+
INFO: Load quality profiles (done) | time=137ms
29+
INFO: Load active rules
30+
INFO: Load active rules (done) | time=2180ms
31+
INFO: Load analysis cache
32+
INFO: Load analysis cache (830 bytes) | time=27ms
33+
INFO: Load project repositories
34+
INFO: Load project repositories (done) | time=23ms
35+
INFO: Indexing files...
36+
INFO: Project configuration:
37+
INFO: 73 files indexed
38+
INFO: 1 file ignored because of scm ignore settings
39+
INFO: Quality profile for java: Sonar way
40+
INFO: Quality profile for json: Sonar way
41+
INFO: Quality profile for web: Sonar way
42+
INFO: Quality profile for yaml: Sonar way
43+
INFO: ------------- Run sensors on module InfluxDBPlugin-log
44+
INFO: Load metrics repository
45+
INFO: Load metrics repository (done) | time=164ms
46+
INFO: Sensor JavaSensor [java]
47+
INFO: Configured Java source version (sonar.java.source): none
48+
INFO: JavaClasspath initialization
49+
INFO: JavaClasspath initialization (done) | time=5ms
50+
INFO: JavaTestClasspath initialization
51+
INFO: JavaTestClasspath initialization (done) | time=1ms
52+
INFO: Server-side caching is enabled. The Java analyzer will not try to leverage data from a previous analysis.
53+
INFO: Using ECJ batch to parse 30 Main java source files with batch size 117 KB.
54+
INFO: Starting batch processing.
55+
INFO: The Java analyzer cannot skip unchanged files in this context. A full analysis is performed for all files.
56+
INFO: 100% analyzed
57+
INFO: Batch processing: Done.
58+
INFO: Did not optimize analysis for any files, performed a full analysis for all 30 files.
59+
WARN: Dependencies/libraries were not provided for analysis of SOURCE files. The 'sonar.java.libraries' property is empty. Verify your configuration, as you might end up with less precise results.
60+
WARN: Dependencies/libraries were not provided for analysis of TEST files. The 'sonar.java.test.libraries' property is empty. Verify your configuration, as you might end up with less precise results.
61+
WARN: Unresolved imports/types have been detected during analysis. Enable DEBUG mode to see them.
62+
WARN: Use of preview features have been detected during analysis. Enable DEBUG mode to see them.
63+
INFO: Using ECJ batch to parse 15 Test java source files with batch size 117 KB.
64+
INFO: Starting batch processing.
65+
INFO: 100% analyzed
66+
INFO: Batch processing: Done.
67+
INFO: Did not optimize analysis for any files, performed a full analysis for all 15 files.
68+
WARN: Unresolved imports/types have been detected during analysis. Enable DEBUG mode to see them.
69+
WARN: Use of preview features have been detected during analysis. Enable DEBUG mode to see them.
70+
INFO: No "Generated" source files to scan.
71+
INFO: Sensor JavaSensor [java] (done) | time=11072ms
72+
INFO: Sensor JaCoCo XML Report Importer [jacoco]
73+
INFO: 'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
74+
INFO: Importing 1 report(s). Turn your logs in debug mode in order to see the exhaustive list.
75+
INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=147ms
76+
INFO: Sensor IaC CloudFormation Sensor [iac]
77+
INFO: 0 source files to be analyzed
78+
INFO: 0/0 source files have been analyzed
79+
INFO: Sensor IaC CloudFormation Sensor [iac] (done) | time=25ms
80+
INFO: Sensor IaC Kubernetes Sensor [iac]
81+
INFO: 0 source files to be analyzed
82+
INFO: 0/0 source files have been analyzed
83+
INFO: Sensor IaC Kubernetes Sensor [iac] (done) | time=8ms
84+
INFO: Sensor JavaScript inside YAML analysis [javascript]
85+
INFO: No input files found for analysis
86+
INFO: Hit the cache for 0 out of 0
87+
INFO: Miss the cache for 0 out of 0
88+
INFO: Sensor JavaScript inside YAML analysis [javascript] (done) | time=15ms
89+
INFO: Sensor CSS Rules [javascript]
90+
WARN: Error when running: 'node -v'. Is Node.js available during analysis?
91+
INFO: Hit the cache for 0 out of 0
92+
INFO: Miss the cache for 0 out of 0
93+
INFO: Sensor CSS Rules [javascript] (done) | time=10760ms
94+
INFO: Sensor C# Project Type Information [csharp]
95+
INFO: Sensor C# Project Type Information [csharp] (done) | time=1ms
96+
INFO: Sensor C# Analysis Log [csharp]
97+
INFO: Sensor C# Analysis Log [csharp] (done) | time=13ms
98+
INFO: Sensor C# Properties [csharp]
99+
INFO: Sensor C# Properties [csharp] (done) | time=0ms
100+
INFO: Sensor SurefireSensor [java]
101+
INFO: parsing [/var/jenkins_home/workspace/influxdb-plugin/build-influxdb-plugin/target/surefire-reports]
102+
INFO: Sensor SurefireSensor [java] (done) | time=146ms
103+
INFO: Sensor HTML [web]
104+
INFO: Sensor HTML [web] (done) | time=175ms
105+
INFO: Sensor TextAndSecretsSensor [text]
106+
INFO: 67 source files to be analyzed
107+
INFO: 67/67 source files have been analyzed
108+
INFO: Sensor TextAndSecretsSensor [text] (done) | time=173ms
109+
INFO: Sensor VB.NET Project Type Information [vbnet]
110+
INFO: Sensor VB.NET Project Type Information [vbnet] (done) | time=1ms
111+
INFO: Sensor VB.NET Analysis Log [vbnet]
112+
INFO: Sensor VB.NET Analysis Log [vbnet] (done) | time=17ms
113+
INFO: Sensor VB.NET Properties [vbnet]
114+
INFO: Sensor VB.NET Properties [vbnet] (done) | time=0ms
115+
INFO: Sensor IaC Docker Sensor [iac]
116+
INFO: 0 source files to be analyzed
117+
INFO: 0/0 source files have been analyzed
118+
INFO: Sensor IaC Docker Sensor [iac] (done) | time=61ms
119+
INFO: ------------- Run sensors on project
120+
INFO: Sensor Analysis Warnings import [csharp]
121+
INFO: Sensor Analysis Warnings import [csharp] (done) | time=3ms
122+
INFO: Sensor Zero Coverage Sensor
123+
INFO: Sensor Zero Coverage Sensor (done) | time=5ms
124+
INFO: Sensor Java CPD Block Indexer
125+
INFO: Sensor Java CPD Block Indexer (done) | time=92ms
126+
INFO: SCM Publisher SCM provider for this project is: git
127+
INFO: SCM Publisher 2 source files to be analyzed
128+
INFO: SCM Publisher 2/2 source files have been analyzed (done) | time=262ms
129+
INFO: CPD Executor 25 files had no CPD blocks
130+
INFO: CPD Executor Calculating CPD for 25 files
131+
INFO: CPD Executor CPD calculation finished (done) | time=34ms
132+
INFO: Analysis report generated in 161ms, dir size=507.9 kB
133+
INFO: Analysis report compressed in 4801ms, zip size=207.0 kB
134+
INFO: Analysis report uploaded in 342ms
135+
INFO: ANALYSIS SUCCESSFUL, you can find the results at: http://sonarqube:9001/dashboard?id=InfluxDBPlugin-log
136+
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
137+
INFO: More about the report processing at http://sonarqube:9001/api/ce/task?id=321EXAMPLE
138+
INFO: Analysis total time: 52.300 s
139+
INFO: ------------------------------------------------------------------------
140+
INFO: EXECUTION SUCCESS
141+
INFO: ------------------------------------------------------------------------
142+
INFO: Total time: 54.302s
143+
INFO: Final Memory: 24M/84M
144+
INFO: ------------------------------------------------------------------------

0 commit comments

Comments
 (0)