From d64bcde9f6bd20209f22cde139121b920b9048e3 Mon Sep 17 00:00:00 2001 From: Waschndolos Date: Tue, 9 Jan 2024 16:52:57 +0100 Subject: [PATCH] Code coverage will not show job full name. Provide a default value of -1 if a specific coverage is not available in the coverage report (#612) * Returning job full name * Coverage will return a -1 if a specific coverage is not available now to make it clear that nothing has been found. * Adding line coverage metrics and a percentage gauge for all current coverage metrics --- .../prometheus/CodeCoverageCollector.java | 11 ++- .../prometheus/collectors/CollectorType.java | 9 +++ .../coverage/CoverageBranchCoveredGauge.java | 1 + .../coverage/CoverageBranchMissedGauge.java | 1 + .../coverage/CoverageBranchPercentGauge.java | 47 +++++++++++++ .../coverage/CoverageBranchTotalGauge.java | 1 + .../coverage/CoverageClassCoveredGauge.java | 1 + .../coverage/CoverageClassMissedGauge.java | 1 + .../coverage/CoverageClassPercentGauge.java | 47 +++++++++++++ .../coverage/CoverageClassTotalGauge.java | 1 + .../coverage/CoverageCollectorFactory.java | 21 ++++++ .../coverage/CoverageFileCoveredGauge.java | 1 + .../coverage/CoverageFileMissedGauge.java | 1 + .../coverage/CoverageFilePercentGauge.java | 47 +++++++++++++ .../coverage/CoverageFileTotalGauge.java | 1 + .../CoverageInstructionCoveredGauge.java | 1 + .../CoverageInstructionMissedGauge.java | 1 + .../CoverageInstructionPercentGauge.java | 47 +++++++++++++ .../CoverageInstructionTotalGauge.java | 1 + .../coverage/CoverageLineCoveredGauge.java | 46 +++++++++++++ .../coverage/CoverageLineMissedGauge.java | 46 +++++++++++++ .../coverage/CoverageLinePercentGauge.java | 47 +++++++++++++ .../coverage/CoverageLineTotalGauge.java | 46 +++++++++++++ .../coverage/CoverageMetricsCollector.java | 19 +++++ .../CoverageBranchCoveredGaugeTest.java | 7 +- .../CoverageBranchMissedGaugeTest.java | 8 ++- .../CoverageBranchPercentGaugeTest.java | 67 ++++++++++++++++++ .../CoverageBranchTotalGaugeTest.java | 8 ++- .../CoverageClassCoveredGaugeTest.java | 8 ++- .../CoverageClassMissedGaugeTest.java | 8 ++- .../CoverageClassPercentGaugeTest.java | 68 ++++++++++++++++++ .../coverage/CoverageClassTotalGaugeTest.java | 8 ++- .../CoverageFileCoveredGaugeTest.java | 7 +- .../coverage/CoverageFileMissedGaugeTest.java | 8 ++- .../CoverageFilePercentGaugeTest.java | 68 ++++++++++++++++++ .../coverage/CoverageFileTotalGaugeTest.java | 8 ++- .../CoverageInstructionCoveredGaugeTest.java | 7 +- .../CoverageInstructionMissedGaugeTest.java | 8 ++- .../CoverageInstructionPercentGaugeTest.java | 68 ++++++++++++++++++ .../CoverageInstructionTotalGaugeTest.java | 8 ++- .../CoverageLineCoveredGaugeTest.java | 68 ++++++++++++++++++ .../coverage/CoverageLineMissedGaugeTest.java | 67 ++++++++++++++++++ .../CoverageLinePercentGaugeTest.java | 68 ++++++++++++++++++ .../coverage/CoverageLineTotalGaugeTest.java | 69 +++++++++++++++++++ .../collectors/coverage/CoverageTest.java | 13 ++++ 45 files changed, 1081 insertions(+), 13 deletions(-) create mode 100644 src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchPercentGauge.java create mode 100644 src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassPercentGauge.java create mode 100644 src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFilePercentGauge.java create mode 100644 src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionPercentGauge.java create mode 100644 src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineCoveredGauge.java create mode 100644 src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineMissedGauge.java create mode 100644 src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLinePercentGauge.java create mode 100644 src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineTotalGauge.java create mode 100644 src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchPercentGaugeTest.java create mode 100644 src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassPercentGaugeTest.java create mode 100644 src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFilePercentGaugeTest.java create mode 100644 src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionPercentGaugeTest.java create mode 100644 src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineCoveredGaugeTest.java create mode 100644 src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineMissedGaugeTest.java create mode 100644 src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLinePercentGaugeTest.java create mode 100644 src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineTotalGaugeTest.java diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollector.java b/src/main/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollector.java index cb606276f..b3a22a695 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollector.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/CodeCoverageCollector.java @@ -63,20 +63,29 @@ private List collectCoverageMetricForJob(Job job) { collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_CLASS_COVERED, new String[]{jobAttributeName})); collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_CLASS_MISSED, new String[]{jobAttributeName})); collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_CLASS_TOTAL, new String[]{jobAttributeName})); + collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_CLASS_PERCENT, new String[]{jobAttributeName})); collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_BRANCH_COVERED, new String[]{jobAttributeName})); collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_BRANCH_MISSED, new String[]{jobAttributeName})); collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_BRANCH_TOTAL, new String[]{jobAttributeName})); + collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_BRANCH_PERCENT, new String[]{jobAttributeName})); collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_INSTRUCTION_COVERED, new String[]{jobAttributeName})); collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_INSTRUCTION_MISSED, new String[]{jobAttributeName})); collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_INSTRUCTION_TOTAL, new String[]{jobAttributeName})); + collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_INSTRUCTION_PERCENT, new String[]{jobAttributeName})); collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_FILE_COVERED, new String[]{jobAttributeName})); collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_FILE_MISSED, new String[]{jobAttributeName})); collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_FILE_TOTAL, new String[]{jobAttributeName})); + collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_FILE_PERCENT, new String[]{jobAttributeName})); - collectors.forEach(c -> c.calculateMetric(lastBuild, new String[]{job.getName()})); + collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_LINE_COVERED, new String[]{jobAttributeName})); + collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_LINE_MISSED, new String[]{jobAttributeName})); + collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_LINE_TOTAL, new String[]{jobAttributeName})); + collectors.add(factory.createCoverageRunCollector(CollectorType.COVERAGE_LINE_PERCENT, new String[]{jobAttributeName})); + + collectors.forEach(c -> c.calculateMetric(lastBuild, new String[]{job.getFullName()})); return collectors.stream() .map(MetricCollector::collect) diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/CollectorType.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/CollectorType.java index 2faffb040..11ef275b6 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/CollectorType.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/CollectorType.java @@ -45,18 +45,27 @@ public enum CollectorType { COVERAGE_CLASS_COVERED("coverage_class_covered"), COVERAGE_CLASS_MISSED("coverage_class_missed"), COVERAGE_CLASS_TOTAL("coverage_class_total"), + COVERAGE_CLASS_PERCENT("coverage_class_percent"), COVERAGE_BRANCH_COVERED("coverage_branch_covered"), COVERAGE_BRANCH_MISSED("coverage_branch_missed"), COVERAGE_BRANCH_TOTAL("coverage_branch_total"), + COVERAGE_BRANCH_PERCENT("coverage_branch_percent"), COVERAGE_INSTRUCTION_COVERED("coverage_instruction_covered"), COVERAGE_INSTRUCTION_MISSED("coverage_instruction_missed"), COVERAGE_INSTRUCTION_TOTAL("coverage_instruction_total"), + COVERAGE_INSTRUCTION_PERCENT("coverage_instruction_percent"), COVERAGE_FILE_COVERED("coverage_file_covered"), COVERAGE_FILE_MISSED("coverage_file_missed"), COVERAGE_FILE_TOTAL("coverage_file_total"), + COVERAGE_FILE_PERCENT("coverage_file_percent"), + + COVERAGE_LINE_COVERED("coverage_line_covered"), + COVERAGE_LINE_MISSED("coverage_line_missed"), + COVERAGE_LINE_TOTAL("coverage_line_total"), + COVERAGE_LINE_PERCENT("coverage_line_percent"), JOB_LOG_UPDATED_GAUGE("job_log_updated"); diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchCoveredGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchCoveredGauge.java index 2b51d24a8..d99997fca 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchCoveredGauge.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchCoveredGauge.java @@ -36,6 +36,7 @@ public void calculateMetric(Run jenkinsObject, String[] labelValues) { Optional optional = getCoverage(jenkinsObject, Metric.BRANCH, Baseline.PROJECT); if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); return; } diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchMissedGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchMissedGauge.java index 647304f9c..d299a0a47 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchMissedGauge.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchMissedGauge.java @@ -36,6 +36,7 @@ public void calculateMetric(Run jenkinsObject, String[] labelValues) { Optional optional = getCoverage(jenkinsObject, Metric.BRANCH, Baseline.PROJECT); if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); return; } diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchPercentGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchPercentGauge.java new file mode 100644 index 000000000..4c07cf162 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchPercentGauge.java @@ -0,0 +1,47 @@ +package org.jenkinsci.plugins.prometheus.collectors.coverage; + +import edu.hm.hafner.coverage.Coverage; +import edu.hm.hafner.coverage.Metric; +import hudson.model.Run; +import io.jenkins.plugins.coverage.metrics.model.Baseline; +import io.prometheus.client.Gauge; +import io.prometheus.client.SimpleCollector; +import org.jenkinsci.plugins.prometheus.collectors.CollectorType; + +import java.util.Optional; + +public class CoverageBranchPercentGauge extends CoverageMetricsCollector, Gauge> { + + protected CoverageBranchPercentGauge(String[] labelNames, String namespace, String subsystem) { + super(labelNames, namespace, subsystem); + } + + @Override + protected CollectorType getCollectorType() { + return CollectorType.COVERAGE_BRANCH_PERCENT; + } + + @Override + protected String getHelpText() { + return "Returns the coverage of branches in percent"; + } + + @Override + protected SimpleCollector.Builder getCollectorBuilder() { + return Gauge.build(); + } + + @Override + public void calculateMetric(Run jenkinsObject, String[] labelValues) { + + Optional optional = getCoverage(jenkinsObject, Metric.BRANCH, Baseline.PROJECT); + if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); + return; + } + + Coverage coverage = optional.get(); + collector.labels(labelValues).set(calculatePercentage(coverage)); + } + +} diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchTotalGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchTotalGauge.java index 22dea4ab5..0af9a8d37 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchTotalGauge.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchTotalGauge.java @@ -36,6 +36,7 @@ public void calculateMetric(Run jenkinsObject, String[] labelValues) { Optional optional = getCoverage(jenkinsObject, Metric.BRANCH, Baseline.PROJECT); if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); return; } diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassCoveredGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassCoveredGauge.java index d211f3a05..b7addbae2 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassCoveredGauge.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassCoveredGauge.java @@ -36,6 +36,7 @@ public void calculateMetric(Run jenkinsObject, String[] labelValues) { Optional optional = getCoverage(jenkinsObject, Metric.CLASS, Baseline.PROJECT); if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); return; } diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassMissedGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassMissedGauge.java index 3e665eedf..30cab4fa6 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassMissedGauge.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassMissedGauge.java @@ -36,6 +36,7 @@ public void calculateMetric(Run jenkinsObject, String[] labelValues) { Optional optional = getCoverage(jenkinsObject, Metric.CLASS, Baseline.PROJECT); if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); return; } diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassPercentGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassPercentGauge.java new file mode 100644 index 000000000..61ff0a8af --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassPercentGauge.java @@ -0,0 +1,47 @@ +package org.jenkinsci.plugins.prometheus.collectors.coverage; + +import edu.hm.hafner.coverage.Coverage; +import edu.hm.hafner.coverage.Metric; +import hudson.model.Run; +import io.jenkins.plugins.coverage.metrics.model.Baseline; +import io.prometheus.client.Gauge; +import io.prometheus.client.SimpleCollector; +import org.jenkinsci.plugins.prometheus.collectors.CollectorType; + +import java.util.Optional; + +public class CoverageClassPercentGauge extends CoverageMetricsCollector, Gauge> { + + protected CoverageClassPercentGauge(String[] labelNames, String namespace, String subsystem) { + super(labelNames, namespace, subsystem); + } + + @Override + protected CollectorType getCollectorType() { + return CollectorType.COVERAGE_CLASS_PERCENT; + } + + @Override + protected String getHelpText() { + return "Returns the coverage of classes in percent"; + } + + @Override + protected SimpleCollector.Builder getCollectorBuilder() { + return Gauge.build(); + } + + @Override + public void calculateMetric(Run jenkinsObject, String[] labelValues) { + + Optional optional = getCoverage(jenkinsObject, Metric.CLASS, Baseline.PROJECT); + if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); + return; + } + + Coverage coverage = optional.get(); + collector.labels(labelValues).set(calculatePercentage(coverage)); + } + +} diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassTotalGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassTotalGauge.java index b395cdd53..d36b39d58 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassTotalGauge.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassTotalGauge.java @@ -36,6 +36,7 @@ public void calculateMetric(Run jenkinsObject, String[] labelValues) { Optional optional = getCoverage(jenkinsObject, Metric.CLASS, Baseline.PROJECT); if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); return; } diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageCollectorFactory.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageCollectorFactory.java index 10c999caf..c6bf04941 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageCollectorFactory.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageCollectorFactory.java @@ -17,24 +17,45 @@ public class CoverageCollectorFactory extends BaseCollectorFactory { return saveBuildCollector(new CoverageClassMissedGauge(labelNames, namespace, subsystem)); case COVERAGE_CLASS_TOTAL: return saveBuildCollector(new CoverageClassTotalGauge(labelNames, namespace, subsystem)); + case COVERAGE_CLASS_PERCENT: + return saveBuildCollector(new CoverageClassPercentGauge(labelNames, namespace, subsystem)); + case COVERAGE_BRANCH_COVERED: return saveBuildCollector(new CoverageBranchCoveredGauge(labelNames, namespace, subsystem)); case COVERAGE_BRANCH_MISSED: return saveBuildCollector(new CoverageBranchMissedGauge(labelNames, namespace, subsystem)); case COVERAGE_BRANCH_TOTAL: return saveBuildCollector(new CoverageBranchTotalGauge(labelNames, namespace, subsystem)); + case COVERAGE_BRANCH_PERCENT: + return saveBuildCollector(new CoverageBranchPercentGauge(labelNames, namespace, subsystem)); + case COVERAGE_INSTRUCTION_COVERED: return saveBuildCollector(new CoverageInstructionCoveredGauge(labelNames, namespace, subsystem)); case COVERAGE_INSTRUCTION_MISSED: return saveBuildCollector(new CoverageInstructionMissedGauge(labelNames, namespace, subsystem)); case COVERAGE_INSTRUCTION_TOTAL: return saveBuildCollector(new CoverageInstructionTotalGauge(labelNames, namespace, subsystem)); + case COVERAGE_INSTRUCTION_PERCENT: + return saveBuildCollector(new CoverageInstructionPercentGauge(labelNames, namespace, subsystem)); + case COVERAGE_FILE_COVERED: return saveBuildCollector(new CoverageFileCoveredGauge(labelNames, namespace, subsystem)); case COVERAGE_FILE_MISSED: return saveBuildCollector(new CoverageFileMissedGauge(labelNames, namespace, subsystem)); case COVERAGE_FILE_TOTAL: return saveBuildCollector(new CoverageFileTotalGauge(labelNames, namespace, subsystem)); + case COVERAGE_FILE_PERCENT: + return saveBuildCollector(new CoverageFilePercentGauge(labelNames, namespace, subsystem)); + + case COVERAGE_LINE_COVERED: + return saveBuildCollector(new CoverageLineCoveredGauge(labelNames, namespace, subsystem)); + case COVERAGE_LINE_MISSED: + return saveBuildCollector(new CoverageLineMissedGauge(labelNames, namespace, subsystem)); + case COVERAGE_LINE_TOTAL: + return saveBuildCollector(new CoverageLineTotalGauge(labelNames, namespace, subsystem)); + case COVERAGE_LINE_PERCENT: + return saveBuildCollector(new CoverageLinePercentGauge(labelNames, namespace, subsystem)); + default: return new NoOpMetricCollector<>(); } diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileCoveredGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileCoveredGauge.java index 1750e5843..ee5032b59 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileCoveredGauge.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileCoveredGauge.java @@ -36,6 +36,7 @@ public void calculateMetric(Run jenkinsObject, String[] labelValues) { Optional optional = getCoverage(jenkinsObject, Metric.FILE, Baseline.PROJECT); if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); return; } diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileMissedGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileMissedGauge.java index d34655ee6..b07355738 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileMissedGauge.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileMissedGauge.java @@ -36,6 +36,7 @@ public void calculateMetric(Run jenkinsObject, String[] labelValues) { Optional optional = getCoverage(jenkinsObject, Metric.FILE, Baseline.PROJECT); if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); return; } diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFilePercentGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFilePercentGauge.java new file mode 100644 index 000000000..335e7e60d --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFilePercentGauge.java @@ -0,0 +1,47 @@ +package org.jenkinsci.plugins.prometheus.collectors.coverage; + +import edu.hm.hafner.coverage.Coverage; +import edu.hm.hafner.coverage.Metric; +import hudson.model.Run; +import io.jenkins.plugins.coverage.metrics.model.Baseline; +import io.prometheus.client.Gauge; +import io.prometheus.client.SimpleCollector; +import org.jenkinsci.plugins.prometheus.collectors.CollectorType; + +import java.util.Optional; + +public class CoverageFilePercentGauge extends CoverageMetricsCollector, Gauge> { + + protected CoverageFilePercentGauge(String[] labelNames, String namespace, String subsystem) { + super(labelNames, namespace, subsystem); + } + + @Override + protected CollectorType getCollectorType() { + return CollectorType.COVERAGE_FILE_PERCENT; + } + + @Override + protected String getHelpText() { + return "Returns the coverage of files in percent"; + } + + @Override + protected SimpleCollector.Builder getCollectorBuilder() { + return Gauge.build(); + } + + @Override + public void calculateMetric(Run jenkinsObject, String[] labelValues) { + + Optional optional = getCoverage(jenkinsObject, Metric.FILE, Baseline.PROJECT); + if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); + return; + } + + Coverage coverage = optional.get(); + collector.labels(labelValues).set(calculatePercentage(coverage)); + } + +} diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileTotalGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileTotalGauge.java index bee019f00..4b877babd 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileTotalGauge.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileTotalGauge.java @@ -36,6 +36,7 @@ public void calculateMetric(Run jenkinsObject, String[] labelValues) { Optional optional = getCoverage(jenkinsObject, Metric.FILE, Baseline.PROJECT); if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); return; } diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionCoveredGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionCoveredGauge.java index 7bd77dace..75f2d4570 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionCoveredGauge.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionCoveredGauge.java @@ -36,6 +36,7 @@ public void calculateMetric(Run jenkinsObject, String[] labelValues) { Optional optional = getCoverage(jenkinsObject, Metric.INSTRUCTION, Baseline.PROJECT); if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); return; } diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionMissedGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionMissedGauge.java index 4fc941123..8fb9f4fb8 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionMissedGauge.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionMissedGauge.java @@ -36,6 +36,7 @@ public void calculateMetric(Run jenkinsObject, String[] labelValues) { Optional optional = getCoverage(jenkinsObject, Metric.INSTRUCTION, Baseline.PROJECT); if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); return; } diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionPercentGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionPercentGauge.java new file mode 100644 index 000000000..fafef3d85 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionPercentGauge.java @@ -0,0 +1,47 @@ +package org.jenkinsci.plugins.prometheus.collectors.coverage; + +import edu.hm.hafner.coverage.Coverage; +import edu.hm.hafner.coverage.Metric; +import hudson.model.Run; +import io.jenkins.plugins.coverage.metrics.model.Baseline; +import io.prometheus.client.Gauge; +import io.prometheus.client.SimpleCollector; +import org.jenkinsci.plugins.prometheus.collectors.CollectorType; + +import java.util.Optional; + +public class CoverageInstructionPercentGauge extends CoverageMetricsCollector, Gauge> { + + protected CoverageInstructionPercentGauge(String[] labelNames, String namespace, String subsystem) { + super(labelNames, namespace, subsystem); + } + + @Override + protected CollectorType getCollectorType() { + return CollectorType.COVERAGE_INSTRUCTION_PERCENT; + } + + @Override + protected String getHelpText() { + return "Returns the coverage of instructions in percent"; + } + + @Override + protected SimpleCollector.Builder getCollectorBuilder() { + return Gauge.build(); + } + + @Override + public void calculateMetric(Run jenkinsObject, String[] labelValues) { + + Optional optional = getCoverage(jenkinsObject, Metric.INSTRUCTION, Baseline.PROJECT); + if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); + return; + } + + Coverage coverage = optional.get(); + collector.labels(labelValues).set(calculatePercentage(coverage)); + } + +} diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionTotalGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionTotalGauge.java index 75e4af0dd..ee482a467 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionTotalGauge.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionTotalGauge.java @@ -36,6 +36,7 @@ public void calculateMetric(Run jenkinsObject, String[] labelValues) { Optional optional = getCoverage(jenkinsObject, Metric.INSTRUCTION, Baseline.PROJECT); if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); return; } diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineCoveredGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineCoveredGauge.java new file mode 100644 index 000000000..efcef5a08 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineCoveredGauge.java @@ -0,0 +1,46 @@ +package org.jenkinsci.plugins.prometheus.collectors.coverage; + +import edu.hm.hafner.coverage.Coverage; +import edu.hm.hafner.coverage.Metric; +import hudson.model.Run; +import io.jenkins.plugins.coverage.metrics.model.Baseline; +import io.prometheus.client.Gauge; +import io.prometheus.client.SimpleCollector; +import org.jenkinsci.plugins.prometheus.collectors.CollectorType; + +import java.util.Optional; + +public class CoverageLineCoveredGauge extends CoverageMetricsCollector, Gauge> { + + protected CoverageLineCoveredGauge(String[] labelNames, String namespace, String subsystem) { + super(labelNames, namespace, subsystem); + } + + @Override + protected CollectorType getCollectorType() { + return CollectorType.COVERAGE_LINE_COVERED; + } + + @Override + protected String getHelpText() { + return "Returns the number of lines covered"; + } + + @Override + protected SimpleCollector.Builder getCollectorBuilder() { + return Gauge.build(); + } + + @Override + public void calculateMetric(Run jenkinsObject, String[] labelValues) { + + Optional optional = getCoverage(jenkinsObject, Metric.LINE, Baseline.PROJECT); + if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); + return; + } + + Coverage coverage = optional.get(); + collector.labels(labelValues).set(coverage.getCovered()); + } +} diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineMissedGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineMissedGauge.java new file mode 100644 index 000000000..35934d279 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineMissedGauge.java @@ -0,0 +1,46 @@ +package org.jenkinsci.plugins.prometheus.collectors.coverage; + +import edu.hm.hafner.coverage.Coverage; +import edu.hm.hafner.coverage.Metric; +import hudson.model.Run; +import io.jenkins.plugins.coverage.metrics.model.Baseline; +import io.prometheus.client.Gauge; +import io.prometheus.client.SimpleCollector; +import org.jenkinsci.plugins.prometheus.collectors.CollectorType; + +import java.util.Optional; + +public class CoverageLineMissedGauge extends CoverageMetricsCollector, Gauge> { + + protected CoverageLineMissedGauge(String[] labelNames, String namespace, String subsystem) { + super(labelNames, namespace, subsystem); + } + + @Override + protected CollectorType getCollectorType() { + return CollectorType.COVERAGE_LINE_MISSED; + } + + @Override + protected String getHelpText() { + return "Returns the number of lines missed"; + } + + @Override + protected SimpleCollector.Builder getCollectorBuilder() { + return Gauge.build(); + } + + @Override + public void calculateMetric(Run jenkinsObject, String[] labelValues) { + + Optional optional = getCoverage(jenkinsObject, Metric.LINE, Baseline.PROJECT); + if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); + return; + } + + Coverage coverage = optional.get(); + collector.labels(labelValues).set(coverage.getMissed()); + } +} diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLinePercentGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLinePercentGauge.java new file mode 100644 index 000000000..66f1187db --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLinePercentGauge.java @@ -0,0 +1,47 @@ +package org.jenkinsci.plugins.prometheus.collectors.coverage; + +import edu.hm.hafner.coverage.Coverage; +import edu.hm.hafner.coverage.Metric; +import hudson.model.Run; +import io.jenkins.plugins.coverage.metrics.model.Baseline; +import io.prometheus.client.Gauge; +import io.prometheus.client.SimpleCollector; +import org.jenkinsci.plugins.prometheus.collectors.CollectorType; + +import java.util.Optional; + +public class CoverageLinePercentGauge extends CoverageMetricsCollector, Gauge> { + + protected CoverageLinePercentGauge(String[] labelNames, String namespace, String subsystem) { + super(labelNames, namespace, subsystem); + } + + @Override + protected CollectorType getCollectorType() { + return CollectorType.COVERAGE_LINE_PERCENT; + } + + @Override + protected String getHelpText() { + return "Returns the coverage of lines in percent"; + } + + @Override + protected SimpleCollector.Builder getCollectorBuilder() { + return Gauge.build(); + } + + @Override + public void calculateMetric(Run jenkinsObject, String[] labelValues) { + + Optional optional = getCoverage(jenkinsObject, Metric.LINE, Baseline.PROJECT); + if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); + return; + } + + Coverage coverage = optional.get(); + collector.labels(labelValues).set(calculatePercentage(coverage)); + } + +} diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineTotalGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineTotalGauge.java new file mode 100644 index 000000000..14d71d388 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineTotalGauge.java @@ -0,0 +1,46 @@ +package org.jenkinsci.plugins.prometheus.collectors.coverage; + +import edu.hm.hafner.coverage.Coverage; +import edu.hm.hafner.coverage.Metric; +import hudson.model.Run; +import io.jenkins.plugins.coverage.metrics.model.Baseline; +import io.prometheus.client.Gauge; +import io.prometheus.client.SimpleCollector; +import org.jenkinsci.plugins.prometheus.collectors.CollectorType; + +import java.util.Optional; + +public class CoverageLineTotalGauge extends CoverageMetricsCollector, Gauge> { + + protected CoverageLineTotalGauge(String[] labelNames, String namespace, String subsystem) { + super(labelNames, namespace, subsystem); + } + + @Override + protected CollectorType getCollectorType() { + return CollectorType.COVERAGE_LINE_TOTAL; + } + + @Override + protected String getHelpText() { + return "Returns the number of lines total"; + } + + @Override + protected SimpleCollector.Builder getCollectorBuilder() { + return Gauge.build(); + } + + @Override + public void calculateMetric(Run jenkinsObject, String[] labelValues) { + + Optional optional = getCoverage(jenkinsObject, Metric.LINE, Baseline.PROJECT); + if (optional.isEmpty()) { + collector.labels(labelValues).set(-1); + return; + } + + Coverage coverage = optional.get(); + collector.labels(labelValues).set(coverage.getTotal()); + } +} diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageMetricsCollector.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageMetricsCollector.java index 05062666c..094d427c7 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageMetricsCollector.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageMetricsCollector.java @@ -27,4 +27,23 @@ protected Optional getCoverage(Run jenkinsRun, Metric metric, Ba .map(x -> (Coverage)x) .findFirst(); } + + protected double calculatePercentage(Coverage coverage) { + if (coverage == null) { + return -1.0; + } + + long covered = coverage.getCovered(); + long total = coverage.getTotal(); + + if (covered >= 0 && total >= 0) { + if (total != 0) { + return (double) (covered * 100) / total; + } else { + return -1.0; + } + } else { + return -1.0; + } + } } diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchCoveredGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchCoveredGaugeTest.java index aa6d09629..0915718b3 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchCoveredGaugeTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchCoveredGaugeTest.java @@ -56,6 +56,11 @@ public void testNothingFailsIfNoCoverageFound() { Assertions.assertEquals("Returns the number of branches covered", familySamples.help); Assertions.assertEquals("default_jenkins_builds_coverage_branch_covered", familySamples.name); - Assertions.assertEquals(0, familySamples.samples.size()); + List samples = familySamples.samples; + Assertions.assertEquals(1, familySamples.samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); } } \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchMissedGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchMissedGaugeTest.java index 508e0b905..491d057c5 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchMissedGaugeTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchMissedGaugeTest.java @@ -57,7 +57,13 @@ public void testNothingFailsIfNoCoverageFound() { Assertions.assertEquals("Returns the number of branches missed", familySamples.help); Assertions.assertEquals("default_jenkins_builds_coverage_branch_missed", familySamples.name); - Assertions.assertEquals(0, familySamples.samples.size()); + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1.0, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); } } \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchPercentGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchPercentGaugeTest.java new file mode 100644 index 000000000..29985d736 --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchPercentGaugeTest.java @@ -0,0 +1,67 @@ +package org.jenkinsci.plugins.prometheus.collectors.coverage; + +import edu.hm.hafner.coverage.Metric; +import io.jenkins.plugins.coverage.metrics.model.Baseline; +import io.prometheus.client.Collector; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.List; + +public class CoverageBranchPercentGaugeTest extends CoverageTest { + + + public CoverageBranchPercentGaugeTest() { + super(Baseline.PROJECT, Metric.BRANCH); + } + + @Test + public void testPercentage() { + setUpSuccessfulMocksForPercent(); + CoverageBranchPercentGauge sut = new CoverageBranchPercentGauge(new String[]{"job"}, getNamespace(), getSubSystem()); + + sut.calculateMetric(mock, new String[]{"myJob"}); + + List metricFamilySamples = sut.collect(); + Assertions.assertEquals(1, metricFamilySamples.size()); + + Collector.MetricFamilySamples familySamples = metricFamilySamples.get(0); + + Assertions.assertEquals("Returns the coverage of branches in percent", familySamples.help); + Assertions.assertEquals("default_jenkins_builds_coverage_branch_percent", familySamples.name); + + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(25.0, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); + + } + + @Test + public void testNothingFailsIfNoCoverageFound() { + setUpUnsuccessfulMocks(); + + CoverageBranchPercentGauge sut = new CoverageBranchPercentGauge(new String[]{"job"}, getNamespace(), getSubSystem()); + + sut.calculateMetric(mock, new String[]{"myJob"}); + + List metricFamilySamples = sut.collect(); + Assertions.assertEquals(1, metricFamilySamples.size()); + + Collector.MetricFamilySamples familySamples = metricFamilySamples.get(0); + + Assertions.assertEquals("Returns the coverage of branches in percent", familySamples.help); + Assertions.assertEquals("default_jenkins_builds_coverage_branch_percent", familySamples.name); + + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); + } +} \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchTotalGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchTotalGaugeTest.java index c69601648..e42382592 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchTotalGaugeTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageBranchTotalGaugeTest.java @@ -56,6 +56,12 @@ public void testNothingFailsIfNoCoverageFound() { Assertions.assertEquals("Returns the number of branches total", familySamples.help); Assertions.assertEquals("default_jenkins_builds_coverage_branch_total", familySamples.name); - Assertions.assertEquals(0, familySamples.samples.size()); + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); } } \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassCoveredGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassCoveredGaugeTest.java index 634fa91b1..43ac2767d 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassCoveredGaugeTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassCoveredGaugeTest.java @@ -56,7 +56,13 @@ public void testNothingFailsIfNoCoverageFound() { Assertions.assertEquals("Returns the number of classes covered", familySamples.help); Assertions.assertEquals("default_jenkins_builds_coverage_class_covered", familySamples.name); - Assertions.assertEquals(0, familySamples.samples.size()); + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); } } \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassMissedGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassMissedGaugeTest.java index e0ce4b063..031ce9226 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassMissedGaugeTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassMissedGaugeTest.java @@ -56,7 +56,13 @@ public void testNothingFailsIfNoCoverageFound() { Assertions.assertEquals("Returns the number of classes missed", familySamples.help); Assertions.assertEquals("default_jenkins_builds_coverage_class_missed", familySamples.name); - Assertions.assertEquals(0, familySamples.samples.size()); + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); } } \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassPercentGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassPercentGaugeTest.java new file mode 100644 index 000000000..eb5d60073 --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassPercentGaugeTest.java @@ -0,0 +1,68 @@ +package org.jenkinsci.plugins.prometheus.collectors.coverage; + +import edu.hm.hafner.coverage.Metric; +import io.jenkins.plugins.coverage.metrics.model.Baseline; +import io.prometheus.client.Collector; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.List; + +public class CoverageClassPercentGaugeTest extends CoverageTest { + + + public CoverageClassPercentGaugeTest() { + super(Baseline.PROJECT, Metric.CLASS); + } + + @Test + public void testPercentage() { + setUpSuccessfulMocksForPercent(); + CoverageClassPercentGauge sut = new CoverageClassPercentGauge(new String[]{"job"}, getNamespace(), getSubSystem()); + + sut.calculateMetric(mock, new String[]{"myJob"}); + + List metricFamilySamples = sut.collect(); + Assertions.assertEquals(1, metricFamilySamples.size()); + + Collector.MetricFamilySamples familySamples = metricFamilySamples.get(0); + + Assertions.assertEquals("Returns the coverage of classes in percent", familySamples.help); + Assertions.assertEquals("default_jenkins_builds_coverage_class_percent", familySamples.name); + + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(25.0, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); + + } + + @Test + public void testNothingFailsIfNoCoverageFound() { + setUpUnsuccessfulMocks(); + + CoverageClassPercentGauge sut = new CoverageClassPercentGauge(new String[]{"job"}, getNamespace(), getSubSystem()); + + sut.calculateMetric(mock, new String[]{"myJob"}); + + List metricFamilySamples = sut.collect(); + Assertions.assertEquals(1, metricFamilySamples.size()); + + Collector.MetricFamilySamples familySamples = metricFamilySamples.get(0); + + Assertions.assertEquals("Returns the coverage of classes in percent", familySamples.help); + Assertions.assertEquals("default_jenkins_builds_coverage_class_percent", familySamples.name); + + + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); + } +} \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassTotalGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassTotalGaugeTest.java index f59dcd969..ce8172b97 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassTotalGaugeTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageClassTotalGaugeTest.java @@ -56,7 +56,13 @@ public void testNothingFailsIfNoCoverageFound() { Assertions.assertEquals("Returns the number of classes total", familySamples.help); Assertions.assertEquals("default_jenkins_builds_coverage_class_total", familySamples.name); - Assertions.assertEquals(0, familySamples.samples.size()); + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); } } \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileCoveredGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileCoveredGaugeTest.java index b2bde4e07..3b9d2f6a4 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileCoveredGaugeTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileCoveredGaugeTest.java @@ -56,7 +56,12 @@ public void testNothingFailsIfNoCoverageFound() { Assertions.assertEquals("Returns the number of files covered", familySamples.help); Assertions.assertEquals("default_jenkins_builds_coverage_file_covered", familySamples.name); - Assertions.assertEquals(0, familySamples.samples.size()); + List samples = familySamples.samples; + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); } } \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileMissedGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileMissedGaugeTest.java index ad8230ce9..af624418d 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileMissedGaugeTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileMissedGaugeTest.java @@ -56,7 +56,13 @@ public void testNothingFailsIfNoCoverageFound() { Assertions.assertEquals("Returns the number of files missed", familySamples.help); Assertions.assertEquals("default_jenkins_builds_coverage_file_missed", familySamples.name); - Assertions.assertEquals(0, familySamples.samples.size()); + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); } } \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFilePercentGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFilePercentGaugeTest.java new file mode 100644 index 000000000..0badb44b7 --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFilePercentGaugeTest.java @@ -0,0 +1,68 @@ +package org.jenkinsci.plugins.prometheus.collectors.coverage; + +import edu.hm.hafner.coverage.Metric; +import io.jenkins.plugins.coverage.metrics.model.Baseline; +import io.prometheus.client.Collector; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.List; + +public class CoverageFilePercentGaugeTest extends CoverageTest { + + + public CoverageFilePercentGaugeTest() { + super(Baseline.PROJECT, Metric.FILE); + } + + @Test + public void testPercentage() { + setUpSuccessfulMocksForPercent(); + CoverageFilePercentGauge sut = new CoverageFilePercentGauge(new String[]{"job"}, getNamespace(), getSubSystem()); + + sut.calculateMetric(mock, new String[]{"myJob"}); + + List metricFamilySamples = sut.collect(); + Assertions.assertEquals(1, metricFamilySamples.size()); + + Collector.MetricFamilySamples familySamples = metricFamilySamples.get(0); + + Assertions.assertEquals("Returns the coverage of files in percent", familySamples.help); + Assertions.assertEquals("default_jenkins_builds_coverage_file_percent", familySamples.name); + + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(25.0, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); + + } + + @Test + public void testNothingFailsIfNoCoverageFound() { + setUpUnsuccessfulMocks(); + + CoverageFilePercentGauge sut = new CoverageFilePercentGauge(new String[]{"job"}, getNamespace(), getSubSystem()); + + sut.calculateMetric(mock, new String[]{"myJob"}); + + List metricFamilySamples = sut.collect(); + Assertions.assertEquals(1, metricFamilySamples.size()); + + Collector.MetricFamilySamples familySamples = metricFamilySamples.get(0); + + Assertions.assertEquals("Returns the coverage of files in percent", familySamples.help); + Assertions.assertEquals("default_jenkins_builds_coverage_file_percent", familySamples.name); + + + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); + } +} \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileTotalGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileTotalGaugeTest.java index 49d751bb1..3692ed7a0 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileTotalGaugeTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageFileTotalGaugeTest.java @@ -56,7 +56,13 @@ public void testNothingFailsIfNoCoverageFound() { Assertions.assertEquals("Returns the number of files total", familySamples.help); Assertions.assertEquals("default_jenkins_builds_coverage_file_total", familySamples.name); - Assertions.assertEquals(0, familySamples.samples.size()); + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); } } \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionCoveredGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionCoveredGaugeTest.java index 13386ccc5..7f191b65e 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionCoveredGaugeTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionCoveredGaugeTest.java @@ -56,7 +56,12 @@ public void testNothingFailsIfNoCoverageFound() { Assertions.assertEquals("Returns the number of instructions covered", familySamples.help); Assertions.assertEquals("default_jenkins_builds_coverage_instruction_covered", familySamples.name); - Assertions.assertEquals(0, familySamples.samples.size()); + List samples = familySamples.samples; + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); } } \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionMissedGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionMissedGaugeTest.java index f78cbaa95..03082d700 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionMissedGaugeTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionMissedGaugeTest.java @@ -56,7 +56,13 @@ public void testNothingFailsIfNoCoverageFound() { Assertions.assertEquals("Returns the number of instructions missed", familySamples.help); Assertions.assertEquals("default_jenkins_builds_coverage_instruction_missed", familySamples.name); - Assertions.assertEquals(0, familySamples.samples.size()); + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); } } \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionPercentGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionPercentGaugeTest.java new file mode 100644 index 000000000..1e3746a2c --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionPercentGaugeTest.java @@ -0,0 +1,68 @@ +package org.jenkinsci.plugins.prometheus.collectors.coverage; + +import edu.hm.hafner.coverage.Metric; +import io.jenkins.plugins.coverage.metrics.model.Baseline; +import io.prometheus.client.Collector; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.List; + +public class CoverageInstructionPercentGaugeTest extends CoverageTest { + + + public CoverageInstructionPercentGaugeTest() { + super(Baseline.PROJECT, Metric.INSTRUCTION); + } + + @Test + public void testPercentage() { + setUpSuccessfulMocksForPercent(); + CoverageInstructionPercentGauge sut = new CoverageInstructionPercentGauge(new String[]{"job"}, getNamespace(), getSubSystem()); + + sut.calculateMetric(mock, new String[]{"myJob"}); + + List metricFamilySamples = sut.collect(); + Assertions.assertEquals(1, metricFamilySamples.size()); + + Collector.MetricFamilySamples familySamples = metricFamilySamples.get(0); + + Assertions.assertEquals("Returns the coverage of instructions in percent", familySamples.help); + Assertions.assertEquals("default_jenkins_builds_coverage_instruction_percent", familySamples.name); + + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(25.0, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); + + } + + @Test + public void testNothingFailsIfNoCoverageFound() { + setUpUnsuccessfulMocks(); + + CoverageInstructionPercentGauge sut = new CoverageInstructionPercentGauge(new String[]{"job"}, getNamespace(), getSubSystem()); + + sut.calculateMetric(mock, new String[]{"myJob"}); + + List metricFamilySamples = sut.collect(); + Assertions.assertEquals(1, metricFamilySamples.size()); + + Collector.MetricFamilySamples familySamples = metricFamilySamples.get(0); + + Assertions.assertEquals("Returns the coverage of instructions in percent", familySamples.help); + Assertions.assertEquals("default_jenkins_builds_coverage_instruction_percent", familySamples.name); + + + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); + } +} \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionTotalGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionTotalGaugeTest.java index f3ccbe981..4fafb3d89 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionTotalGaugeTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageInstructionTotalGaugeTest.java @@ -56,7 +56,13 @@ public void testNothingFailsIfNoCoverageFound() { Assertions.assertEquals("Returns the number of instructions total", familySamples.help); Assertions.assertEquals("default_jenkins_builds_coverage_instruction_total", familySamples.name); - Assertions.assertEquals(0, familySamples.samples.size()); + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); } } \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineCoveredGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineCoveredGaugeTest.java new file mode 100644 index 000000000..9a917bc8e --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineCoveredGaugeTest.java @@ -0,0 +1,68 @@ +package org.jenkinsci.plugins.prometheus.collectors.coverage; + +import edu.hm.hafner.coverage.Metric; +import io.jenkins.plugins.coverage.metrics.model.Baseline; +import io.prometheus.client.Collector; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.List; + +public class CoverageLineCoveredGaugeTest extends CoverageTest { + + + public CoverageLineCoveredGaugeTest() { + super(Baseline.PROJECT, Metric.LINE); + } + + @Test + public void testCovered() { + setUpSuccessfulMocksForCovered(); + CoverageLineCoveredGauge sut = new CoverageLineCoveredGauge(new String[]{"job"}, getNamespace(), getSubSystem()); + + sut.calculateMetric(mock, new String[]{"myJob"}); + + List metricFamilySamples = sut.collect(); + Assertions.assertEquals(1, metricFamilySamples.size()); + + Collector.MetricFamilySamples familySamples = metricFamilySamples.get(0); + + Assertions.assertEquals("Returns the number of lines covered", familySamples.help); + Assertions.assertEquals("default_jenkins_builds_coverage_line_covered", familySamples.name); + + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(10.0, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); + + } + + @Test + public void testNothingFailsIfNoCoverageFound() { + setUpUnsuccessfulMocks(); + + CoverageLineCoveredGauge sut = new CoverageLineCoveredGauge(new String[]{"job"}, getNamespace(), getSubSystem()); + + sut.calculateMetric(mock, new String[]{"myJob"}); + + List metricFamilySamples = sut.collect(); + Assertions.assertEquals(1, metricFamilySamples.size()); + + Collector.MetricFamilySamples familySamples = metricFamilySamples.get(0); + + Assertions.assertEquals("Returns the number of lines covered", familySamples.help); + Assertions.assertEquals("default_jenkins_builds_coverage_line_covered", familySamples.name); + + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); + } + +} \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineMissedGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineMissedGaugeTest.java new file mode 100644 index 000000000..870018850 --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineMissedGaugeTest.java @@ -0,0 +1,67 @@ +package org.jenkinsci.plugins.prometheus.collectors.coverage; + +import edu.hm.hafner.coverage.Metric; +import io.jenkins.plugins.coverage.metrics.model.Baseline; +import io.prometheus.client.Collector; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.List; + +class CoverageLineMissedGaugeTest extends CoverageTest { + + + public CoverageLineMissedGaugeTest() { + super(Baseline.PROJECT, Metric.LINE); + } + + @Test + public void testMissed() { + setUpSuccessfulMocksForMissed(); + CoverageLineMissedGauge sut = new CoverageLineMissedGauge(new String[]{"job"}, getNamespace(), getSubSystem()); + + sut.calculateMetric(mock, new String[]{"myJob"}); + + List metricFamilySamples = sut.collect(); + Assertions.assertEquals(1, metricFamilySamples.size()); + + Collector.MetricFamilySamples familySamples = metricFamilySamples.get(0); + + Assertions.assertEquals("Returns the number of lines missed", familySamples.help); + Assertions.assertEquals("default_jenkins_builds_coverage_line_missed", familySamples.name); + + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(5.0, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); + + } + + @Test + public void testNothingFailsIfNoCoverageFound() { + setUpUnsuccessfulMocks(); + + CoverageLineMissedGauge sut = new CoverageLineMissedGauge(new String[]{"job"}, getNamespace(), getSubSystem()); + + sut.calculateMetric(mock, new String[]{"myJob"}); + + List metricFamilySamples = sut.collect(); + Assertions.assertEquals(1, metricFamilySamples.size()); + + Collector.MetricFamilySamples familySamples = metricFamilySamples.get(0); + + Assertions.assertEquals("Returns the number of lines missed", familySamples.help); + Assertions.assertEquals("default_jenkins_builds_coverage_line_missed", familySamples.name); + + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); + } +} \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLinePercentGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLinePercentGaugeTest.java new file mode 100644 index 000000000..0c4052a72 --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLinePercentGaugeTest.java @@ -0,0 +1,68 @@ +package org.jenkinsci.plugins.prometheus.collectors.coverage; + +import edu.hm.hafner.coverage.Metric; +import io.jenkins.plugins.coverage.metrics.model.Baseline; +import io.prometheus.client.Collector; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.List; + +public class CoverageLinePercentGaugeTest extends CoverageTest { + + + public CoverageLinePercentGaugeTest() { + super(Baseline.PROJECT, Metric.LINE); + } + + @Test + public void testPercentage() { + setUpSuccessfulMocksForPercent(); + CoverageLinePercentGauge sut = new CoverageLinePercentGauge(new String[]{"job"}, getNamespace(), getSubSystem()); + + sut.calculateMetric(mock, new String[]{"myJob"}); + + List metricFamilySamples = sut.collect(); + Assertions.assertEquals(1, metricFamilySamples.size()); + + Collector.MetricFamilySamples familySamples = metricFamilySamples.get(0); + + Assertions.assertEquals("Returns the coverage of lines in percent", familySamples.help); + Assertions.assertEquals("default_jenkins_builds_coverage_line_percent", familySamples.name); + + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(25.0, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); + + } + + @Test + public void testNothingFailsIfNoCoverageFound() { + setUpUnsuccessfulMocks(); + + CoverageLinePercentGauge sut = new CoverageLinePercentGauge(new String[]{"job"}, getNamespace(), getSubSystem()); + + sut.calculateMetric(mock, new String[]{"myJob"}); + + List metricFamilySamples = sut.collect(); + Assertions.assertEquals(1, metricFamilySamples.size()); + + Collector.MetricFamilySamples familySamples = metricFamilySamples.get(0); + + Assertions.assertEquals("Returns the coverage of lines in percent", familySamples.help); + Assertions.assertEquals("default_jenkins_builds_coverage_line_percent", familySamples.name); + + + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); + } +} \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineTotalGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineTotalGaugeTest.java new file mode 100644 index 000000000..52fc9ab80 --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageLineTotalGaugeTest.java @@ -0,0 +1,69 @@ +package org.jenkinsci.plugins.prometheus.collectors.coverage; + +import edu.hm.hafner.coverage.Metric; +import io.jenkins.plugins.coverage.metrics.model.Baseline; +import io.prometheus.client.Collector; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class CoverageLineTotalGaugeTest extends CoverageTest { + + + public CoverageLineTotalGaugeTest() { + super(Baseline.PROJECT, Metric.LINE); + } + + @Test + public void testMissed() { + setUpSuccessfulMocksForTotal(); + CoverageLineTotalGauge sut = new CoverageLineTotalGauge(new String[]{"job"}, getNamespace(), getSubSystem()); + + sut.calculateMetric(mock, new String[]{"myJob"}); + + List metricFamilySamples = sut.collect(); + Assertions.assertEquals(1, metricFamilySamples.size()); + + Collector.MetricFamilySamples familySamples = metricFamilySamples.get(0); + + Assertions.assertEquals("Returns the number of lines total", familySamples.help); + Assertions.assertEquals("default_jenkins_builds_coverage_line_total", familySamples.name); + + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(15.0, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); + + } + + @Test + public void testNothingFailsIfNoCoverageFound() { + setUpUnsuccessfulMocks(); + + CoverageLineTotalGauge sut = new CoverageLineTotalGauge(new String[]{"job"}, getNamespace(), getSubSystem()); + + sut.calculateMetric(mock, new String[]{"myJob"}); + + List metricFamilySamples = sut.collect(); + Assertions.assertEquals(1, metricFamilySamples.size()); + + Collector.MetricFamilySamples familySamples = metricFamilySamples.get(0); + + Assertions.assertEquals("Returns the number of lines total", familySamples.help); + Assertions.assertEquals("default_jenkins_builds_coverage_line_total", familySamples.name); + + List samples = familySamples.samples; + + Assertions.assertEquals(1, samples.size()); + + Collector.MetricFamilySamples.Sample sample = samples.get(0); + Assertions.assertEquals(-1, sample.value); + Assertions.assertEquals("myJob", sample.labelValues.get(0)); + } +} \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageTest.java index 4409f48a9..6167d4016 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/coverage/CoverageTest.java @@ -49,6 +49,19 @@ protected void setUpSuccessfulMocksForTotal() { when(mock.getAction(CoverageBuildAction.class)).thenReturn(mockedCoverageBuildAction); } + protected void setUpSuccessfulMocksForPercent() { + CoverageBuildAction mockedCoverageBuildAction = Mockito.mock(CoverageBuildAction.class); + Coverage mockedCoverage = Mockito.mock(Coverage.class); + when(mockedCoverage.getMetric()).thenReturn(metric); + when(mockedCoverage.getTotal()).thenReturn(100); + when(mockedCoverage.getCovered()).thenReturn(25); + when(mockedCoverageBuildAction.getAllValues(baseline)).thenReturn(List.of(mockedCoverage)); + when(mock.getAction(CoverageBuildAction.class)).thenReturn(mockedCoverageBuildAction); + } + + + + protected void setUpUnsuccessfulMocks() {