forked from lsjostro/prometheus-plugin
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding test for Runs * Removing unnecessary null check * Removing unused class + adding tests for CodeCoverageCollector * further tests
- Loading branch information
Waschndolos
authored
Jan 6, 2024
1 parent
0c12405
commit 366d672
Showing
6 changed files
with
283 additions
and
33 deletions.
There are no files selected for viewing
19 changes: 0 additions & 19 deletions
19
src/main/java/org/jenkinsci/plugins/prometheus/BaseCollector.java
This file was deleted.
Oops, something went wrong.
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
138 changes: 138 additions & 0 deletions
138
src/test/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollectorTest.java
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,138 @@ | ||
package org.jenkinsci.plugins.prometheus; | ||
|
||
import hudson.Plugin; | ||
import hudson.model.Item; | ||
import hudson.model.Job; | ||
import hudson.model.Run; | ||
import io.jenkins.plugins.coverage.metrics.steps.CoverageBuildAction; | ||
import io.prometheus.client.Collector; | ||
import jenkins.model.Jenkins; | ||
import org.jenkinsci.plugins.prometheus.config.PrometheusConfiguration; | ||
import org.jenkinsci.plugins.prometheus.util.ConfigurationUtils; | ||
import org.jenkinsci.plugins.prometheus.util.Jobs; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.*; | ||
|
||
|
||
@ExtendWith(MockitoExtension.class) | ||
class CodeCoverageCollectorTest { | ||
|
||
@Mock | ||
private Jenkins jenkins; | ||
|
||
@Mock | ||
private PrometheusConfiguration config; | ||
|
||
@Test | ||
void shouldNotProduceMetricsWhenNoCoveragePluginDetected() { | ||
try (MockedStatic<Jenkins> jenkinsStatic = mockStatic(Jenkins.class)) { | ||
jenkinsStatic.when(Jenkins::get).thenReturn(jenkins); | ||
when(jenkins.getPlugin("coverage")).thenReturn(null); | ||
CodeCoverageCollector sut = new CodeCoverageCollector(); | ||
|
||
List<Collector.MetricFamilySamples> collect = sut.collect(); | ||
assertEquals(0, collect.size()); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldNotProduceMetricsWhenItIsNotConfigured() { | ||
try ( | ||
MockedStatic<Jenkins> jenkinsStatic = mockStatic(Jenkins.class); | ||
MockedStatic<PrometheusConfiguration> configurationStatic = mockStatic(PrometheusConfiguration.class) | ||
) { | ||
jenkinsStatic.when(Jenkins::get).thenReturn(jenkins); | ||
configurationStatic.when(PrometheusConfiguration::get).thenReturn(config); | ||
when(jenkins.getPlugin("coverage")).thenReturn(new Plugin.DummyImpl()); | ||
when(config.isCollectCodeCoverage()).thenReturn(false); | ||
|
||
CodeCoverageCollector sut = new CodeCoverageCollector(); | ||
|
||
List<Collector.MetricFamilySamples> collect = sut.collect(); | ||
assertEquals(0, collect.size()); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldNotProduceMetricsWhenJobIsBuilding() { | ||
try ( | ||
MockedStatic<Jenkins> jenkinsStatic = mockStatic(Jenkins.class); | ||
MockedStatic<PrometheusConfiguration> configurationStatic = mockStatic(PrometheusConfiguration.class); | ||
) { | ||
jenkinsStatic.when(Jenkins::get).thenReturn(jenkins); | ||
configurationStatic.when(PrometheusConfiguration::get).thenReturn(config); | ||
Job jobUnderTest = mock(Job.class); | ||
Run lastBuild = mock(Run.class); | ||
when(lastBuild.isBuilding()).thenReturn(true); | ||
when(jobUnderTest.getLastBuild()).thenReturn(lastBuild); | ||
when(jenkins.getAllItems(Job.class)).thenReturn(List.of(jobUnderTest)); | ||
when(jenkins.getPlugin("coverage")).thenReturn(new Plugin.DummyImpl()); | ||
when(config.isCollectCodeCoverage()).thenReturn(true); | ||
|
||
CodeCoverageCollector sut = new CodeCoverageCollector(); | ||
|
||
List<Collector.MetricFamilySamples> collect = sut.collect(); | ||
assertEquals(0, collect.size()); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldNotProduceMetricsWhenJobHasNoCoverageBuildAction() { | ||
try ( | ||
MockedStatic<Jenkins> jenkinsStatic = mockStatic(Jenkins.class); | ||
MockedStatic<PrometheusConfiguration> configurationStatic = mockStatic(PrometheusConfiguration.class); | ||
) { | ||
jenkinsStatic.when(Jenkins::get).thenReturn(jenkins); | ||
configurationStatic.when(PrometheusConfiguration::get).thenReturn(config); | ||
Job jobUnderTest = mock(Job.class); | ||
Run lastBuild = mock(Run.class); | ||
when(lastBuild.isBuilding()).thenReturn(false); | ||
when(jobUnderTest.getLastBuild()).thenReturn(lastBuild); | ||
when(jenkins.getAllItems(Job.class)).thenReturn(List.of(jobUnderTest)); | ||
when(jenkins.getPlugin("coverage")).thenReturn(new Plugin.DummyImpl()); | ||
when(config.isCollectCodeCoverage()).thenReturn(true); | ||
|
||
CodeCoverageCollector sut = new CodeCoverageCollector(); | ||
|
||
List<Collector.MetricFamilySamples> collect = sut.collect(); | ||
assertEquals(0, collect.size()); | ||
} | ||
} | ||
|
||
@Test | ||
void shouldProduceMetricsWhenJobHasCoverageBuildAction() { | ||
try ( | ||
MockedStatic<Jenkins> jenkinsStatic = mockStatic(Jenkins.class); | ||
MockedStatic<PrometheusConfiguration> configurationStatic = mockStatic(PrometheusConfiguration.class); | ||
MockedStatic<ConfigurationUtils> configurationUtils = mockStatic(ConfigurationUtils.class); | ||
) { | ||
configurationUtils.when(ConfigurationUtils::getNamespace).thenReturn("foo"); | ||
configurationUtils.when(ConfigurationUtils::getSubSystem).thenReturn("bar"); | ||
jenkinsStatic.when(Jenkins::get).thenReturn(jenkins); | ||
configurationStatic.when(PrometheusConfiguration::get).thenReturn(config); | ||
Job jobUnderTest = mock(Job.class); | ||
Run lastBuild = mock(Run.class); | ||
when(lastBuild.isBuilding()).thenReturn(false); | ||
when(jobUnderTest.getLastBuild()).thenReturn(lastBuild); | ||
CoverageBuildAction action = mock(CoverageBuildAction.class); | ||
when(lastBuild.getAction(CoverageBuildAction.class)).thenReturn(action); | ||
when(jenkins.getAllItems(Job.class)).thenReturn(List.of(jobUnderTest)); | ||
when(jenkins.getPlugin("coverage")).thenReturn(new Plugin.DummyImpl()); | ||
when(config.getJobAttributeName()).thenReturn("jenkins_job"); | ||
when(config.isCollectCodeCoverage()).thenReturn(true); | ||
|
||
CodeCoverageCollector sut = new CodeCoverageCollector(); | ||
|
||
List<Collector.MetricFamilySamples> collect = sut.collect(); | ||
assertEquals(12, collect.size(), "12 metrics should have been collected"); | ||
} | ||
} | ||
} |
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
114 changes: 114 additions & 0 deletions
114
src/test/java/org/jenkinsci/plugins/prometheus/util/RunsTest.java
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,114 @@ | ||
package org.jenkinsci.plugins.prometheus.util; | ||
|
||
import hudson.model.ParameterValue; | ||
import hudson.model.ParametersAction; | ||
import hudson.model.Result; | ||
import hudson.model.Run; | ||
import org.jenkinsci.plugins.prometheus.config.PrometheusConfiguration; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.mockito.Mock; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class RunsTest { | ||
|
||
@Mock | ||
Run mockedRun; | ||
|
||
@Test | ||
void testIncludeBuildMetricsReturnsFalseIfRunIsBuilding() { | ||
when(mockedRun.isBuilding()).thenReturn(true); | ||
|
||
boolean include = Runs.includeBuildInMetrics(mockedRun); | ||
assertFalse(include); | ||
} | ||
|
||
@Test | ||
void testGetBuildParametersWontFailIfNoActionsAvailable() { | ||
when(mockedRun.getActions(ParametersAction.class)).thenReturn(List.of()); | ||
|
||
Map<String, Object> parameters = Runs.getBuildParameters(mockedRun); | ||
assertEquals(0, parameters.size()); | ||
} | ||
|
||
@Test | ||
void testGetBuildParametersWontFailIfParameterValueIsNull() { | ||
ParameterValue parameterValue = mock(ParameterValue.class); | ||
when(parameterValue.getName()).thenReturn("failBuildOnError"); | ||
when(parameterValue.getValue()).thenReturn(true); | ||
ParametersAction action = new ParametersAction(parameterValue); | ||
when(mockedRun.getActions(ParametersAction.class)).thenReturn(List.of(action)); | ||
|
||
Map<String, Object> parameters = Runs.getBuildParameters(mockedRun); | ||
assertEquals(1, parameters.size()); | ||
|
||
|
||
assertEquals(true, parameters.get("failBuildOnError")); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provideBuildResults") | ||
void testIncludeBuildMetrics(Result result) { | ||
when(mockedRun.isBuilding()).thenReturn(false); | ||
when(mockedRun.getResult()).thenReturn(result); | ||
try (MockedStatic<PrometheusConfiguration> prometheusConfigurationStatic = mockStatic(PrometheusConfiguration.class)) { | ||
|
||
|
||
PrometheusConfiguration configuration = getPrometheusConfigurationForTest(result, true); | ||
prometheusConfigurationStatic.when(PrometheusConfiguration::get).thenReturn(configuration); | ||
|
||
boolean include = Runs.includeBuildInMetrics(mockedRun); | ||
assertTrue(include, "Run is aborted and Prometheus is configured to return results for these builds"); | ||
|
||
configuration = getPrometheusConfigurationForTest(result, false); | ||
prometheusConfigurationStatic.when(PrometheusConfiguration::get).thenReturn(configuration); | ||
|
||
include = Runs.includeBuildInMetrics(mockedRun); | ||
assertFalse(include, "Run is aborted and Prometheus is not configured to return results for these builds"); | ||
} | ||
} | ||
|
||
|
||
|
||
private static Stream<Arguments> provideBuildResults() { | ||
return Stream.of( | ||
Arguments.of(Result.ABORTED), | ||
Arguments.of(Result.FAILURE), | ||
Arguments.of(Result.NOT_BUILT), | ||
Arguments.of(Result.SUCCESS), | ||
Arguments.of(Result.UNSTABLE) | ||
); | ||
} | ||
|
||
private PrometheusConfiguration getPrometheusConfigurationForTest(Result result, boolean prometheusPluginConfiguredToReturn) { | ||
PrometheusConfiguration mockedPrometheusConfiguration = mock(PrometheusConfiguration.class); | ||
if (Result.ABORTED.equals(result)) { | ||
when(mockedPrometheusConfiguration.isCountAbortedBuilds()).thenReturn(prometheusPluginConfiguredToReturn); | ||
} | ||
if (Result.FAILURE.equals(result)) { | ||
when(mockedPrometheusConfiguration.isCountFailedBuilds()).thenReturn(prometheusPluginConfiguredToReturn); | ||
} | ||
if (Result.NOT_BUILT.equals(result)) { | ||
when(mockedPrometheusConfiguration.isCountNotBuiltBuilds()).thenReturn(prometheusPluginConfiguredToReturn); | ||
} | ||
if (Result.SUCCESS.equals(result)) { | ||
when(mockedPrometheusConfiguration.isCountSuccessfulBuilds()).thenReturn(prometheusPluginConfiguredToReturn); | ||
} | ||
if (Result.UNSTABLE.equals(result)) { | ||
when(mockedPrometheusConfiguration.isCountUnstableBuilds()).thenReturn(prometheusPluginConfiguredToReturn); | ||
} | ||
return mockedPrometheusConfiguration; | ||
} | ||
} |