Skip to content

Commit

Permalink
SONAR-17722 add post tag logs
Browse files Browse the repository at this point in the history
  • Loading branch information
sns-seb authored and SonarTech committed Jul 30, 2019
1 parent 08c0d3c commit ef1f71f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.sonar.ce.task.projectanalysis.qualitygate.QualityGateStatus;
import org.sonar.ce.task.projectanalysis.qualitygate.QualityGateStatusHolder;
import org.sonar.ce.task.step.ComputationStepExecutor;
import org.sonar.core.util.logs.Profiler;
import org.sonar.core.util.stream.MoreCollectors;

import static java.lang.String.format;
Expand Down Expand Up @@ -109,10 +110,17 @@ public void finished(boolean allStepsExecuted) {
}

private static void executeTask(ProjectAnalysisImpl projectAnalysis, PostProjectAnalysisTask postProjectAnalysisTask) {
String status = "FAILED";
Profiler stepProfiler = Profiler.create(LOG).logTimeLast(true);
try {
stepProfiler.start();
postProjectAnalysisTask.finished(projectAnalysis);
status = "SUCCESS";
} catch (Exception e) {
LOG.error("Execution of task " + postProjectAnalysisTask.getClass() + " failed", e);
} finally {
stepProfiler.addContext("status", status);
stepProfiler.stopInfo("{}", postProjectAnalysisTask.getDescription());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public WebhookPostTask(WebhookPayloadFactory payloadFactory, WebHooks webHooks)
this.webHooks = webHooks;
}

@Override
public String getDescription() {
return "Webhooks";
}

@Override
public void finished(ProjectAnalysis analysis) {
webHooks.sendProjectAnalysisUpdate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.sonar.api.ce.posttask.PostProjectAnalysisTask;
import org.sonar.api.ce.posttask.Project;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.ce.task.CeTask;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
import org.sonar.ce.task.projectanalysis.analysis.Branch;
Expand Down Expand Up @@ -80,6 +82,8 @@ public class PostProjectAnalysisTasksExecutorTest {
public MutableQualityGateStatusHolderRule qualityGateStatusHolder = new MutableQualityGateStatusHolderRule();
@Rule
public BatchReportReaderRule reportReader = new BatchReportReaderRule();
@Rule
public LogTester logTester = new LogTester();

private String organizationUuid = "org1";
private String organizationKey = organizationUuid + "_key";
Expand All @@ -94,7 +98,7 @@ public class PostProjectAnalysisTasksExecutorTest {
.setComponent(component)
.setMainComponent(component)
.build();
private PostProjectAnalysisTask postProjectAnalysisTask = mock(PostProjectAnalysisTask.class);
private PostProjectAnalysisTask postProjectAnalysisTask = newPostProjectAnalysisTask("PT1");
private PostProjectAnalysisTasksExecutor underTest = new PostProjectAnalysisTasksExecutor(
ceTask, analysisMetadataHolder, qualityGateHolder, qualityGateStatusHolder,
reportReader, system2,
Expand Down Expand Up @@ -127,8 +131,8 @@ public void does_not_fail_when_there_is_no_PostProjectAnalysisTasksExecutor(bool
@Test
@UseDataProvider("booleanValues")
public void finished_calls_all_PostProjectAnalysisTask_in_order_of_the_array_and_passes_the_same_object_to_all(boolean allStepsExecuted) {
PostProjectAnalysisTask postProjectAnalysisTask1 = mock(PostProjectAnalysisTask.class);
PostProjectAnalysisTask postProjectAnalysisTask2 = mock(PostProjectAnalysisTask.class);
PostProjectAnalysisTask postProjectAnalysisTask1 = newPostProjectAnalysisTask("PT1");
PostProjectAnalysisTask postProjectAnalysisTask2 = newPostProjectAnalysisTask("PT2");
InOrder inOrder = inOrder(postProjectAnalysisTask1, postProjectAnalysisTask2);

new PostProjectAnalysisTasksExecutor(
Expand All @@ -137,12 +141,20 @@ public void finished_calls_all_PostProjectAnalysisTask_in_order_of_the_array_and
.finished(allStepsExecuted);

inOrder.verify(postProjectAnalysisTask1).finished(projectAnalysisArgumentCaptor.capture());
inOrder.verify(postProjectAnalysisTask1).getDescription();
inOrder.verify(postProjectAnalysisTask2).finished(projectAnalysisArgumentCaptor.capture());
inOrder.verify(postProjectAnalysisTask2).getDescription();
inOrder.verifyNoMoreInteractions();

List<PostProjectAnalysisTask.ProjectAnalysis> allValues = projectAnalysisArgumentCaptor.getAllValues();
assertThat(allValues).hasSize(2);
assertThat(allValues.get(0)).isSameAs(allValues.get(1));

assertThat(logTester.logs()).hasSize(2);
List<String> logs = logTester.logs(LoggerLevel.INFO);
assertThat(logs).hasSize(2);
assertThat(logs.get(0)).matches("^PT1 \\| status=SUCCESS \\| time=\\d+ms$");
assertThat(logs.get(1)).matches("^PT2 \\| status=SUCCESS \\| time=\\d+ms$");
}

@Test
Expand Down Expand Up @@ -364,9 +376,9 @@ public void scannerContext_loads_properties_from_scanner_report() {
@Test
@UseDataProvider("booleanValues")
public void finished_does_not_fail_if_listener_throws_exception_and_execute_subsequent_listeners(boolean allStepsExecuted) {
PostProjectAnalysisTask postProjectAnalysisTask1 = mock(PostProjectAnalysisTask.class);
PostProjectAnalysisTask postProjectAnalysisTask2 = mock(PostProjectAnalysisTask.class);
PostProjectAnalysisTask postProjectAnalysisTask3 = mock(PostProjectAnalysisTask.class);
PostProjectAnalysisTask postProjectAnalysisTask1 = newPostProjectAnalysisTask("PT1");
PostProjectAnalysisTask postProjectAnalysisTask2 = newPostProjectAnalysisTask("PT2");
PostProjectAnalysisTask postProjectAnalysisTask3 = newPostProjectAnalysisTask("PT3");
InOrder inOrder = inOrder(postProjectAnalysisTask1, postProjectAnalysisTask2, postProjectAnalysisTask3);

doThrow(new RuntimeException("Faking a listener throws an exception"))
Expand All @@ -379,9 +391,19 @@ public void finished_does_not_fail_if_listener_throws_exception_and_execute_subs
.finished(allStepsExecuted);

inOrder.verify(postProjectAnalysisTask1).finished(projectAnalysisArgumentCaptor.capture());
inOrder.verify(postProjectAnalysisTask1).getDescription();
inOrder.verify(postProjectAnalysisTask2).finished(projectAnalysisArgumentCaptor.capture());
inOrder.verify(postProjectAnalysisTask2).getDescription();
inOrder.verify(postProjectAnalysisTask3).finished(projectAnalysisArgumentCaptor.capture());
inOrder.verify(postProjectAnalysisTask3).getDescription();
inOrder.verifyNoMoreInteractions();

assertThat(logTester.logs()).hasSize(4);
List<String> logs = logTester.logs(LoggerLevel.INFO);
assertThat(logs).hasSize(3);
assertThat(logs.get(0)).matches("^PT1 \\| status=SUCCESS \\| time=\\d+ms$");
assertThat(logs.get(1)).matches("^PT2 \\| status=FAILED \\| time=\\d+ms$");
assertThat(logs.get(2)).matches("^PT3 \\| status=SUCCESS \\| time=\\d+ms$");
}

@DataProvider
Expand All @@ -398,4 +420,10 @@ private static Condition createCondition(String metricKey) {
return new Condition(metric, Condition.Operator.LESS_THAN.getDbValue(), "error threshold");
}

private static PostProjectAnalysisTask newPostProjectAnalysisTask(String description) {
PostProjectAnalysisTask res = mock(PostProjectAnalysisTask.class);
when(res.getDescription()).thenReturn(description);
return res;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public void wireMocks() {
when(configurationRepository.getConfiguration()).thenReturn(configuration);
}

@Test
public void has_description() {
assertThat(underTest.getDescription()).isNotEmpty();
}

@Test
public void call_webhooks_when_no_analysis_not_qualitygate() {
callWebHooks(null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
@ExtensionPoint
@ComputeEngineSide
public interface PostProjectAnalysisTask {

/**
* A short description or name for the task.
* <p>
* This will be used (but not limited to) in logs reporting the execution of the task.
*/
String getDescription();

/**
* This method is called whenever the processing of a Project analysis has finished, whether successfully or not.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ public void execute_throws_NPE_if_date_is_null() {
private static class CaptorPostProjectAnalysisTask implements PostProjectAnalysisTask {
private ProjectAnalysis projectAnalysis;

@Override
public String getDescription() {
return "captor";
}

@Override
public void finished(ProjectAnalysis analysis) {
this.projectAnalysis = analysis;
Expand Down

0 comments on commit ef1f71f

Please sign in to comment.