Files
tyto/backend/internal/collectors/disk_test.go
vikingowl 1e83819318 feat: add unit tests for backend collectors and frontend
Backend tests:
- CPU, memory, disk, network collector tests (existing)
- Added temperature, processes, system, AMD GPU collector tests
- All tests use mock filesystem data

Frontend tests:
- Added Vitest with jsdom environment
- Tests for formatters (formatBytes, formatUptime, etc.)
- Tests for theme store

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 05:49:26 +01:00

72 lines
2.0 KiB
Go

package collectors
import (
"os"
"path/filepath"
"testing"
)
func TestDiskCollector_IOStats(t *testing.T) {
tmpDir := t.TempDir()
procPath := filepath.Join(tmpDir, "proc")
if err := os.MkdirAll(procPath, 0755); err != nil {
t.Fatal(err)
}
// Create mock /proc/diskstats
diskstatsContent := ` 8 0 sda 12345 6789 1234567 12345 54321 9876 7654321 54321 0 12345 66666 0 0 0 0 0 0
8 1 sda1 12340 6780 1234000 12340 54320 9870 7654000 54320 0 12340 66660 0 0 0 0 0 0
259 0 nvme0n1 98765 4321 9876543 98765 87654 3210 8765432 87654 0 98765 186419 0 0 0 0 0 0
259 1 nvme0n1p1 98760 4320 9876000 98760 87650 3200 8765000 87650 0 98760 186410 0 0 0 0 0 0
`
if err := os.WriteFile(filepath.Join(procPath, "diskstats"), []byte(diskstatsContent), 0644); err != nil {
t.Fatal(err)
}
// Create mock /proc/mounts (will fail statfs but shouldn't crash)
mountsContent := `/dev/sda1 /boot ext4 rw,relatime 0 0
/dev/nvme0n1p2 / ext4 rw,relatime 0 0
tmpfs /tmp tmpfs rw 0 0
`
if err := os.WriteFile(filepath.Join(procPath, "mounts"), []byte(mountsContent), 0644); err != nil {
t.Fatal(err)
}
collector := NewDiskCollector(procPath, filepath.Join(procPath, "mounts"))
stats, err := collector.Collect()
if err != nil {
t.Fatalf("Collect failed: %v", err)
}
// Should have IO stats for sda and nvme0n1 (not partitions)
if len(stats.IO) < 2 {
t.Errorf("Expected at least 2 IO entries, got %d", len(stats.IO))
}
// Verify we're not including partitions
for _, io := range stats.IO {
if io.Device == "sda1" || io.Device == "nvme0n1p1" {
t.Errorf("Should not include partition %s", io.Device)
}
}
}
func TestDiskCollector_MissingFiles(t *testing.T) {
tmpDir := t.TempDir()
collector := NewDiskCollector(tmpDir, filepath.Join(tmpDir, "nonexistent"))
stats, err := collector.Collect()
// Should not error, just return empty stats
if err != nil {
t.Logf("Error (acceptable): %v", err)
}
if stats.Mounts == nil {
t.Error("Mounts should not be nil")
}
if stats.IO == nil {
t.Error("IO should not be nil")
}
}