Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/metric/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func collectPartitionMetrics(partition disk.PartitionStat) (*DiskData, CustomErr
// Combine all metrics into a DiskData structure
return &DiskData{
Device: partition.Device,
Mountpoint: partition.Mountpoint,
TotalBytes: &usageStats.Total,
UsedBytes: &usageStats.Used,
FreeBytes: &usageStats.Free,
Expand Down Expand Up @@ -217,6 +218,7 @@ func CollectDiskMetrics() (MetricsSlice, []CustomErr) {
defaultDiskData := []*DiskData{
{
Device: "unknown",
Mountpoint: "unknown",
TotalBytes: nil,
FreeBytes: nil,
UsedBytes: nil,
Expand Down
1 change: 1 addition & 0 deletions internal/metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (m MemoryData) isMetric() {}

type DiskData struct {
Device string `json:"device"` // Device
Mountpoint string `json:"mountpoint"` // Mountpoint
TotalBytes *uint64 `json:"total_bytes"` // Total space of device in bytes
FreeBytes *uint64 `json:"free_bytes"` // Free space of device in bytes
UsedBytes *uint64 `json:"used_bytes"` // Used space of device in bytes
Expand Down
3 changes: 3 additions & 0 deletions openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ components:
device:
type: string
example: "/dev/sda1"
mountpoint:
type: string
example: "/home"
total_bytes:
type: integer
format: uint64
Expand Down
60 changes: 60 additions & 0 deletions test/integration/disk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package integration

import (
"testing"

"github.com/bluewave-labs/capture/internal/metric"
)

// TestDiskMetricsContent verifies that disk metrics can be collected and contain valid data.
// It specifically checks that the Mountpoint field is correctly populated for detected disks.
func TestDiskMetricsContent(t *testing.T) {
// Attempt to collect disk metrics using the internal metric package
metricsSlice, errs := metric.CollectDiskMetrics()

// Log any partial errors encountered during collection (e.g. permission denied on specific partitions)
// We do not fail the test here as partial failures are expected in some environments.
if len(errs) > 0 {
t.Logf("Encountered partial errors collecting disk metrics: %v", errs)
}

// If there are truly no metrics, or only the default "unknown" entry, skip in this environment.
if len(metricsSlice) == 0 {
t.Skip("No disk metrics found. This may indicate restricted disk access or a collection failure.")
}

if len(metricsSlice) == 1 {
if dd, ok := metricsSlice[0].(*metric.DiskData); ok &&
dd.Device == "unknown" && dd.Mountpoint == "unknown" {
t.Skip("Only default 'unknown' disk metric returned; disk metrics not available in this environment.")
}
}

foundValidDisk := false

for _, m := range metricsSlice {
// Assert that the metric is of type *DiskData
diskData, ok := m.(*metric.DiskData)
if !ok {
t.Errorf("Expected *metric.DiskData, got %T", m)
continue
}

// Log the discovered device and mountpoint for debugging context
t.Logf("Found Disk: Device=%s, Mountpoint=%s", diskData.Device, diskData.Mountpoint)

// Validation: The Mountpoint field must not be empty
if diskData.Mountpoint == "" {
t.Errorf("Disk device %s has an empty Mountpoint field", diskData.Device)
} else if diskData.Mountpoint == "unknown" {
t.Logf("Disk device %s has default 'unknown' mountpoint (metric collection may have failed)", diskData.Device)
} else {
foundValidDisk = true
}
}

// If we iterated through metrics but didn't find any valid disk with a mountpoint, log a warning.
if !foundValidDisk {
t.Error("No valid disks with mountpoints were found. The mountpoint feature may not be working correctly.")
}
}