From 3b62d8178eecdade870fd751635ab14d29dbac85 Mon Sep 17 00:00:00 2001 From: Waschndolos Date: Wed, 5 Jun 2024 10:37:57 +0200 Subject: [PATCH] Adding Null Checks and further tests (#673) --- .../collectors/disk/DiskUsageBytesGauge.java | 8 ++- .../disk/DiskUsageFileCountGauge.java | 6 +- .../collectors/disk/JobUsageBytesGauge.java | 7 +- .../disk/DiskUsageBytesGaugeTest.java | 64 ++++++++++++++++++ .../disk/DiskUsageFileCountGaugeTest.java | 12 ++++ .../disk/JobUsageBytesGaugeTest.java | 66 +++++++++++++++++++ 6 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageBytesGaugeTest.java create mode 100644 src/test/java/org/jenkinsci/plugins/prometheus/collectors/disk/JobUsageBytesGaugeTest.java diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageBytesGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageBytesGauge.java index 941668620..81a743b79 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageBytesGauge.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageBytesGauge.java @@ -32,6 +32,12 @@ public void calculateMetric(DiskItem jenkinsObject, String[] labelValues) { if (jenkinsObject == null) { return; } - this.collector.labels(labelValues).set(jenkinsObject.getUsage() * 1024); + + Long usage = jenkinsObject.getUsage(); + if (usage == null) { + return; + } + + this.collector.labels(labelValues).set(usage * 1024); } } diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageFileCountGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageFileCountGauge.java index 35d66c875..db2688abd 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageFileCountGauge.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageFileCountGauge.java @@ -32,6 +32,10 @@ public void calculateMetric(DiskItem jenkinsObject, String[] labelValues) { if (jenkinsObject == null) { return; } - this.collector.labels(labelValues).set(jenkinsObject.getCount()); + Long count = jenkinsObject.getCount(); + if (count == null) { + return; + } + this.collector.labels(labelValues).set(count); } } diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/JobUsageBytesGauge.java b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/JobUsageBytesGauge.java index d7619d517..a9188d940 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/JobUsageBytesGauge.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/collectors/disk/JobUsageBytesGauge.java @@ -32,6 +32,11 @@ public void calculateMetric(JobDiskItem jenkinsObject, String[] labelValues) { if (jenkinsObject == null) { return; } - this.collector.labels(labelValues).set(jenkinsObject.getUsage() * 1024); + Long usage = jenkinsObject.getUsage(); + if (usage == null) { + return; + } + + this.collector.labels(labelValues).set(usage * 1024); } } diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageBytesGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageBytesGaugeTest.java new file mode 100644 index 000000000..b2362433d --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageBytesGaugeTest.java @@ -0,0 +1,64 @@ +package org.jenkinsci.plugins.prometheus.collectors.disk; + +import com.cloudbees.simplediskusage.DiskItem; +import io.prometheus.client.Collector; +import org.jenkinsci.plugins.prometheus.collectors.testutils.CollectorTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class DiskUsageBytesGaugeTest extends CollectorTest { + + @Mock + DiskItem mock; + + @Test + public void testCollectResult() { + + when(mock.getUsage()).thenReturn(10L); + + DiskUsageBytesGauge sut = new DiskUsageBytesGauge(getLabelNames(), getNamespace(), getSubSystem()); + sut.calculateMetric(mock, getLabelValues()); + + List collect = sut.collect(); + + validateMetricFamilySampleListSize(collect, 1); + + Collector.MetricFamilySamples samples = collect.get(0); + + validateNames(samples, new String[]{"default_jenkins_disk_usage_bytes"}); + validateMetricFamilySampleSize(samples, 1); + validateHelp(samples, "Disk usage of first level folder in JENKINS_HOME in bytes"); + validateValue(samples, 0, 10240.0); + } + + @Test + public void testDiskItemIsNull() { + DiskUsageBytesGauge sut = new DiskUsageBytesGauge(getLabelNames(), getNamespace(), getSubSystem()); + sut.calculateMetric(null, getLabelValues()); + + List collect = sut.collect(); + + validateMetricFamilySampleListSize(collect, 1); + validateMetricFamilySampleSize(collect.get(0), 0); + } + + @Test + public void testDiskItemUsageIsNull() { + when(mock.getUsage()).thenReturn(null); + DiskUsageBytesGauge sut = new DiskUsageBytesGauge(getLabelNames(), getNamespace(), getSubSystem()); + sut.calculateMetric(mock, getLabelValues()); + + List collect = sut.collect(); + + validateMetricFamilySampleListSize(collect, 1); + validateMetricFamilySampleSize(collect.get(0), 0); + } +} \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageFileCountGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageFileCountGaugeTest.java index d395c5fa7..4c055d65b 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageFileCountGaugeTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageFileCountGaugeTest.java @@ -48,4 +48,16 @@ public void testDiskItemIsNull() { validateMetricFamilySampleListSize(collect, 1); validateMetricFamilySampleSize(collect.get(0), 0); } + + @Test + public void testDiskItemCountIsNull() { + when(mock.getCount()).thenReturn(null); + DiskUsageFileCountGauge sut = new DiskUsageFileCountGauge(getLabelNames(), getNamespace(), getSubSystem()); + sut.calculateMetric(mock, getLabelValues()); + + List collect = sut.collect(); + + validateMetricFamilySampleListSize(collect, 1); + validateMetricFamilySampleSize(collect.get(0), 0); + } } \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/collectors/disk/JobUsageBytesGaugeTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/disk/JobUsageBytesGaugeTest.java new file mode 100644 index 000000000..08b994e43 --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/prometheus/collectors/disk/JobUsageBytesGaugeTest.java @@ -0,0 +1,66 @@ +package org.jenkinsci.plugins.prometheus.collectors.disk; + +import com.cloudbees.simplediskusage.DiskItem; +import com.cloudbees.simplediskusage.JobDiskItem; +import io.prometheus.client.Collector; +import org.jenkinsci.plugins.prometheus.collectors.testutils.CollectorTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; + +import static org.mockito.Mockito.when; + + + +@ExtendWith(MockitoExtension.class) +class JobUsageBytesGaugeTest extends CollectorTest { + + @Mock + JobDiskItem mock; + + @Test + public void testCollectResult() { + + when(mock.getUsage()).thenReturn(10L); + + JobUsageBytesGauge sut = new JobUsageBytesGauge(getLabelNames(), getNamespace(), getSubSystem()); + sut.calculateMetric(mock, getLabelValues()); + + List collect = sut.collect(); + + validateMetricFamilySampleListSize(collect, 1); + + Collector.MetricFamilySamples samples = collect.get(0); + + validateNames(samples, new String[]{"default_jenkins_job_usage_bytes"}); + validateMetricFamilySampleSize(samples, 1); + validateHelp(samples, "Amount of disk usage (bytes) for each job in Jenkins"); + validateValue(samples, 0, 10240.0); + } + + @Test + public void testJobDiskItemIsNull() { + JobUsageBytesGauge sut = new JobUsageBytesGauge(getLabelNames(), getNamespace(), getSubSystem()); + sut.calculateMetric(null, getLabelValues()); + + List collect = sut.collect(); + + validateMetricFamilySampleListSize(collect, 1); + validateMetricFamilySampleSize(collect.get(0), 0); + } + + @Test + public void testJobDiskItemUsageIsNull() { + when(mock.getUsage()).thenReturn(null); + JobUsageBytesGauge sut = new JobUsageBytesGauge(getLabelNames(), getNamespace(), getSubSystem()); + sut.calculateMetric(mock, getLabelValues()); + + List collect = sut.collect(); + + validateMetricFamilySampleListSize(collect, 1); + validateMetricFamilySampleSize(collect.get(0), 0); + } +} \ No newline at end of file