github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/health/disk_pressure_test.go (about)

     1  package health
     2  
     3  import (
     4  	. "github.com/onsi/ginkgo/v2"
     5  	. "github.com/onsi/gomega"
     6  	"github.com/pyroscope-io/pyroscope/pkg/util/bytesize"
     7  	"github.com/pyroscope-io/pyroscope/pkg/util/disk"
     8  )
     9  
    10  var _ = Describe("DiskPressure", func() {
    11  	It("does not fire if threshold is zero", func() {
    12  		var d DiskPressure
    13  		m, err := d.Probe()
    14  		Expect(err).ToNot(HaveOccurred())
    15  		Expect(m.Status).To(Equal(Healthy))
    16  		Expect(m.Message).To(BeEmpty())
    17  	})
    18  
    19  	It("does not fire if available space is greater than the configured threshold", func() {
    20  		d := DiskPressure{
    21  			Threshold: 5,
    22  		}
    23  		m, err := d.makeProbe(disk.UsageStats{
    24  			Total:     10 * bytesize.GB,
    25  			Available: 1 * bytesize.GB,
    26  		})
    27  		Expect(err).ToNot(HaveOccurred())
    28  		Expect(m.Status).To(Equal(Healthy))
    29  		Expect(m.Message).To(BeEmpty())
    30  	})
    31  
    32  	It("fires if less than the configured threshold is available", func() {
    33  		d := DiskPressure{
    34  			Threshold: 5,
    35  		}
    36  		m, err := d.makeProbe(disk.UsageStats{
    37  			Total:     100 * bytesize.GB,
    38  			Available: 4 * bytesize.GB,
    39  		})
    40  		Expect(err).ToNot(HaveOccurred())
    41  		Expect(m.Status).To(Equal(Critical))
    42  		Expect(m.Message).To(Equal("Disk space is running low: 4.00 GB available (4.0%)"))
    43  	})
    44  
    45  	It("fires if less than 1GB is available", func() {
    46  		d := DiskPressure{
    47  			Threshold: 5,
    48  		}
    49  		m, err := d.makeProbe(disk.UsageStats{
    50  			Total:     5 * bytesize.GB,
    51  			Available: bytesize.GB - 1,
    52  		})
    53  		Expect(err).ToNot(HaveOccurred())
    54  		Expect(m.Status).To(Equal(Critical))
    55  		Expect(m.Message).To(Equal("Disk space is running low: 1024.00 MB available (20.0%)"))
    56  	})
    57  
    58  	It("fires if available is less than the configured threshold", func() {
    59  		d := DiskPressure{
    60  			Threshold: 5,
    61  		}
    62  		m, err := d.makeProbe(disk.UsageStats{
    63  			Total:     1 * bytesize.GB,
    64  			Available: 1 * bytesize.MB,
    65  		})
    66  		Expect(err).ToNot(HaveOccurred())
    67  		Expect(m.Status).To(Equal(Critical))
    68  		Expect(m.Message).To(Equal("Disk space is running low: 1.00 MB available (0.1%)"))
    69  	})
    70  
    71  	It("fires if no space available", func() {
    72  		d := DiskPressure{
    73  			Threshold: 5,
    74  		}
    75  		m, err := d.makeProbe(disk.UsageStats{
    76  			Total:     100 * bytesize.MB,
    77  			Available: 0,
    78  		})
    79  		Expect(err).ToNot(HaveOccurred())
    80  		Expect(m.Status).To(Equal(Critical))
    81  		Expect(m.Message).To(Equal("Disk space is running low: 0 bytes available (0.0%)"))
    82  	})
    83  
    84  	It("fails if Available > Total", func() {
    85  		var d DiskPressure
    86  		_, err := d.makeProbe(disk.UsageStats{
    87  			Total:     1 * bytesize.GB,
    88  			Available: 2 * bytesize.GB,
    89  		})
    90  		Expect(err).To(MatchError(errTotalLessThanAvailable))
    91  	})
    92  
    93  	It("fails if Total is zero", func() {
    94  		var d DiskPressure
    95  		_, err := d.makeProbe(disk.UsageStats{
    96  			Total:     0,
    97  			Available: 2 * bytesize.GB,
    98  		})
    99  		Expect(err).To(MatchError(errZeroTotalSize))
   100  	})
   101  })