Skip to content

Commit

Permalink
Adding Null Checks and further tests (#673)
Browse files Browse the repository at this point in the history
  • Loading branch information
Waschndolos authored Jun 5, 2024
1 parent a3d8f9b commit 3b62d81
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Collector.MetricFamilySamples> collect = sut.collect();

validateMetricFamilySampleListSize(collect, 1);
validateMetricFamilySampleSize(collect.get(0), 0);
}
}
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);
}
}

0 comments on commit 3b62d81

Please sign in to comment.