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 Null Checks and further tests (#673)
- Loading branch information
Waschndolos
authored
Jun 5, 2024
1 parent
a3d8f9b
commit 3b62d81
Showing
6 changed files
with
160 additions
and
3 deletions.
There are no files selected for viewing
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
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
64 changes: 64 additions & 0 deletions
64
src/test/java/org/jenkinsci/plugins/prometheus/collectors/disk/DiskUsageBytesGaugeTest.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,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<Collector.MetricFamilySamples> 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<Collector.MetricFamilySamples> 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<Collector.MetricFamilySamples> collect = sut.collect(); | ||
|
||
validateMetricFamilySampleListSize(collect, 1); | ||
validateMetricFamilySampleSize(collect.get(0), 0); | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
src/test/java/org/jenkinsci/plugins/prometheus/collectors/disk/JobUsageBytesGaugeTest.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,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<Collector.MetricFamilySamples> 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<Collector.MetricFamilySamples> 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<Collector.MetricFamilySamples> collect = sut.collect(); | ||
|
||
validateMetricFamilySampleListSize(collect, 1); | ||
validateMetricFamilySampleSize(collect.get(0), 0); | ||
} | ||
} |