Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

15.1 KB
Raw Blame
package fsfilters

import "testing"

func TestReadOnlyFilesystemReason(t *testing.T) {
	tests := []struct {
		name       string
		fsType     string
		totalBytes uint64
		usedBytes  uint64
		reason     string
		skip       bool
	}{
		{
			name:       "erofs filesystem",
			fsType:     "erofs",
			totalBytes: 10,
			usedBytes:  5,
			reason:     "erofs",
			skip:       true,
		},
		{
			name:       "fuse.erofs filesystem",
			fsType:     "fuse.erofs",
			totalBytes: 10,
			usedBytes:  8,
			reason:     "erofs",
			skip:       true,
		},
		{
			name:       "overlay at capacity",
			fsType:     "overlay",
			totalBytes: 100,
			usedBytes:  100,
			reason:     "overlay",
			skip:       true,
		},
		{
			name:       "squashfs filesystem",
			fsType:     "squashfs",
			totalBytes: 4096,
			usedBytes:  4096,
			reason:     "squashfs",
			skip:       true,
		},
		{
			name:       "squash-fs alias",
			fsType:     "Squash-FS",
			totalBytes: 2048,
			usedBytes:  2048,
			reason:     "squashfs",
			skip:       true,
		},
		{
			name:       "iso9660 filesystem",
			fsType:     "iso9660",
			totalBytes: 700 * 1024 * 1024,
			usedBytes:  700 * 1024 * 1024,
			reason:     "iso9660",
			skip:       true,
		},
		{
			name:       "cdfs filesystem",
			fsType:     "CDFS",
			totalBytes: 600 * 1024 * 1024,
			usedBytes:  600 * 1024 * 1024,
			reason:     "cdfs",
			skip:       true,
		},
		{
			name:       "udf optical media",
			fsType:     "udf",
			totalBytes: 4 * 1024 * 1024 * 1024,
			usedBytes:  4 * 1024 * 1024 * 1024,
			reason:     "udf",
			skip:       true,
		},
		{
			name:       "cramfs image",
			fsType:     "cramfs",
			totalBytes: 128 * 1024 * 1024,
			usedBytes:  128 * 1024 * 1024,
			reason:     "cramfs",
			skip:       true,
		},
		{
			name:       "romfs firmware partition",
			fsType:     "romfs",
			totalBytes: 16 * 1024 * 1024,
			usedBytes:  16 * 1024 * 1024,
			reason:     "romfs",
			skip:       true,
		},
		{
			name:       "fuse iso image",
			fsType:     "fuse.cdfs",
			totalBytes: 700 * 1024 * 1024,
			usedBytes:  700 * 1024 * 1024,
			reason:     "cdfs",
			skip:       true,
		},
		{
			name:       "overlay below capacity",
			fsType:     "overlay",
			totalBytes: 100,
			usedBytes:  50,
			reason:     "",
			skip:       false,
		},
		{
			name:       "regular ext4",
			fsType:     "ext4",
			totalBytes: 100,
			usedBytes:  50,
			reason:     "",
			skip:       false,
		},
	}

	for _, tc := range tests {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			reason, skip := ReadOnlyFilesystemReason(tc.fsType, tc.totalBytes, tc.usedBytes)
			if reason != tc.reason || skip != tc.skip {
				t.Errorf("expected (%q, %t), got (%q, %t)", tc.reason, tc.skip, reason, skip)
			}
		})
	}
}

func TestShouldIgnoreReadOnlyFilesystem(t *testing.T) {
	if !ShouldIgnoreReadOnlyFilesystem("erofs", 100, 10) {
		t.Fatalf("expected erofs to be ignored")
	}

	if !ShouldIgnoreReadOnlyFilesystem("squashfs", 100, 100) {
		t.Fatalf("expected squashfs to be ignored")
	}

	if ShouldIgnoreReadOnlyFilesystem("ext4", 100, 10) {
		t.Fatalf("expected ext4 to be included")
	}
}

func TestShouldSkipFilesystem(t *testing.T) {
	tests := []struct {
		name       string
		fsType     string
		mountpoint string
		totalBytes uint64
		usedBytes  uint64
		expectSkip bool
	}{
		// Virtual filesystem types
		{"tmpfs", "tmpfs", "/tmp", 1024, 512, true},
		{"devtmpfs", "devtmpfs", "/dev", 1024, 100, true},
		{"cgroup2", "cgroup2", "/sys/fs/cgroup", 0, 0, true},
		{"sysfs", "sysfs", "/sys", 0, 0, true},
		{"proc", "proc", "/proc", 0, 0, true},
		{"virtual fs case insensitive", "TMPFS", "/tmp", 1024, 512, true},
		{"autofs automount", "autofs", "/mnt/samba/share", 0, 0, true},

		// Network filesystem types
		{"nfs mount", "nfs4", "/mnt/nas", 1000000, 500000, true},
		{"cifs mount", "cifs", "/mnt/share", 1000000, 500000, true},
		{"fuse.sshfs", "fuse.sshfs", "/mnt/remote", 1000000, 500000, true},
		{"9p VM shared folder", "9p", "/mnt/host", 1000000, 500000, true},
		{"fuse.mergerfs local pool", "fuse.mergerfs", "/mnt/storage", 1000000, 500000, false},

		// Special mountpoint prefixes
		{"/dev prefix", "ext4", "/dev/shm", 1024, 100, true},
		{"/proc prefix", "ext4", "/proc/sys", 1024, 100, true},
		{"/sys prefix", "ext4", "/sys/kernel", 1024, 100, true},
		{"/run prefix", "ext4", "/run/user/1000", 1024, 100, true},
		{"/var/lib/docker", "ext4", "/var/lib/docker/overlay2", 1000000, 500000, true},
		{"/snap prefix", "ext4", "/snap/core/12345", 1000000, 500000, true},
		{"/boot/efi exact", "vfat", "/boot/efi", 512 * 1024 * 1024, 50 * 1024 * 1024, true},
		{"/boot/firmware exact", "vfat", "/boot/firmware", 512 * 1024 * 1024, 50 * 1024 * 1024, true},
		{"/boot regular filesystem - should NOT skip", "ext4", "/boot", 2 * 1024 * 1024 * 1024, 256 * 1024 * 1024, false},
		{"Proxmox cluster config filesystem", "fuse", "/etc/pve", 128 * 1024 * 1024, 256 * 1024, true},
		{"/var/lib/containers podman", "ext4", "/var/lib/containers/storage/overlay/abc123/merged", 1000000, 500000, true},

		// Container overlay paths in non-standard locations (issue #790)
		{"enhance containers overlay", "ext4", "/var/local/enhance/containers/abc123/overlay/merged", 1000000, 500000, true},
		{"custom containers diff", "ext4", "/opt/containers/myapp/diff/layer", 1000000, 500000, true},
		{"custom containers overlay2", "ext4", "/data/containers/xyz/overlay2/layer1", 1000000, 500000, true},
		// EnhanceCP container_tmp paths (issue #790)
		{"enhancecp container_tmp merged", "overlay", "/var/container_tmp/0d6eaae4-1aa0-4aad-a9d9-d430329c2e38/merged", 1000000, 500000, true},
		{"enhancecp container_tmp overlay", "overlay", "/var/container_tmp/abc123/overlay/layer", 1000000, 500000, true},

		// TrueNAS SCALE Docker overlay paths (issue #718)
		{"truenas ix-apps docker overlay2", "zfs", "/mnt/.ix-apps/docker/overlay2/6c9aad59ec9b0bc33d5e374/merged", 173 * 1024 * 1024 * 1024 * 1024, 399 * 1024 * 1024, true},
		{"truenas ix-apps docker overlay2 diff", "zfs", "/mnt/.ix-apps/docker/overlay2/c5e317c7340f9273d394dc0/diff", 173 * 1024 * 1024 * 1024 * 1024, 399 * 1024 * 1024, true},

		// Windows paths
		{"Windows System Reserved", "NTFS", "System Reserved", 500 * 1024 * 1024, 100 * 1024 * 1024, true},
		{"Windows C drive - should NOT skip", "NTFS", "C:\\", 500 * 1024 * 1024 * 1024, 200 * 1024 * 1024 * 1024, false},
		{"Windows D drive - should NOT skip", "NTFS", "D:\\", 1000 * 1024 * 1024 * 1024, 500 * 1024 * 1024 * 1024, false},

		// macOS APFS companion volumes and simulator runtime images should not
		// look like separate machine disks.
		{"macos root APFS volume - should NOT skip", "apfs", "/", 245107195904, 213573853184, false},
		{"macos external APFS volume - should NOT skip", "apfs", "/Volumes/Development", 999995129856, 204885471232, false},
		{"macos data companion volume", "apfs", "/System/Volumes/Data", 245107195904, 213573853184, true},
		{"macos preboot companion volume", "apfs", "/System/Volumes/Preboot", 245107195904, 213573853184, true},
		{"macos simulator runtime volume", "apfs", "/Library/Developer/CoreSimulator/Volumes/iOS_23E254a", 18058575872, 17593331712, true},
		{"macos app translocation volume", "apfs", "/private/var/folders/xy/random/T/AppTranslocation/2A9C/d/Pulse.app", 245107195904, 221809991680, true},
		{"macos time machine backup snapshot", "apfs", "/Volumes/.timemachine/5D0A488B-87DC-4532-96F4-FA1E0BFDD0F4/2026-10-25-122503.backup", 16090000000000, 352000000000, true},
		{"macos time machine network destination", "apfs", "/Volumes/.timemachine/tower/6DBDE5B4-8924-4042-B6EE-4A5C68074C46/MacTimeMachine", 1649000000000, 403000000000, true},

		// Regular filesystems that should NOT be skipped
		{"ext4 root", "ext4", "/", 100 * 1024 * 1024 * 1024, 50 * 1024 * 1024 * 1024, false},
		{"xfs data", "xfs", "/data", 500 * 1024 * 1024 * 1024, 200 * 1024 * 1024 * 1024, false},
		{"btrfs home", "btrfs", "/home", 200 * 1024 * 1024 * 1024, 100 * 1024 * 1024 * 1024, false},
		{"zfs pool", "zfs", "/tank", 10 * 1024 * 1024 * 1024 * 1024, 5 * 1024 * 1024 * 1024 * 1024, false},

		// Edge cases
		{"empty fsType", "", "/mnt/data", 1000000, 500000, false},
		{"empty mountpoint", "ext4", "", 1000000, 500000, false},
		{"whitespace fsType", "  tmpfs  ", "/tmp", 1024, 512, true},

		// Read-only filesystems (should still work through new function)
		{"squashfs via ShouldSkipFilesystem", "squashfs", "/snap/firefox/123", 100 * 1024 * 1024, 100 * 1024 * 1024, true},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			skip, reasons := ShouldSkipFilesystem(tc.fsType, tc.mountpoint, tc.totalBytes, tc.usedBytes)
			if skip != tc.expectSkip {
				t.Errorf("expected skip=%t, got skip=%t (reasons: %v)", tc.expectSkip, skip, reasons)
			}
		})
	}
}

func TestMatchesUserExclude(t *testing.T) {
	tests := []struct {
		name       string
		mountpoint string
		patterns   []string
		expected   bool
	}{
		// Exact match
		{"exact match", "/mnt/backup", []string{"/mnt/backup"}, true},
		{"exact no match", "/mnt/backup", []string{"/mnt/data"}, false},
		{"exact case sensitive", "/mnt/Backup", []string{"/mnt/backup"}, false},

		// Prefix patterns (ending with *)
		{"prefix match", "/mnt/external", []string{"/mnt/ext*"}, true},
		{"prefix match long", "/mnt/external-drive-2", []string{"/mnt/ext*"}, true},
		{"prefix no match", "/home/user", []string{"/mnt/ext*"}, false},

		// Contains patterns (*substring*)
		{"contains match", "/mnt/pbs-data", []string{"*pbs*"}, true},
		{"contains match middle", "/data/pbs/backup", []string{"*pbs*"}, true},
		{"contains no match", "/mnt/data", []string{"*pbs*"}, false},

		// Multiple patterns
		{"multiple patterns first match", "/mnt/backup", []string{"/mnt/backup", "/data/*"}, true},
		{"multiple patterns second match", "/data/files", []string{"/mnt/backup", "/data/*"}, true},
		{"multiple patterns no match", "/home/user", []string{"/mnt/backup", "/data/*"}, false},

		// Edge cases
		{"empty patterns", "/mnt/backup", []string{}, false},
		{"nil patterns", "/mnt/backup", nil, false},
		{"empty pattern in list", "/mnt/backup", []string{"", "/mnt/backup"}, true},
		{"whitespace pattern", "/mnt/backup", []string{"  /mnt/backup  "}, true},

		// Real-world use cases (Issue #896)
		{"exclude pbs mount", "/mnt/pbs-datastore", []string{"*pbs*"}, true},
		{"exclude specific mount", "/media/external-hdd", []string{"/media/external-hdd"}, true},
		{"exclude all media mounts", "/media/usb-drive", []string{"/media/*"}, true},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			result := MatchesUserExclude(tc.mountpoint, tc.patterns)
			if result != tc.expected {
				t.Errorf("MatchesUserExclude(%q, %v) = %t, want %t", tc.mountpoint, tc.patterns, result, tc.expected)
			}
		})
	}
}

func TestMatchesDiskExclude(t *testing.T) {
	tests := []struct {
		name       string
		device     string
		mountpoint string
		patterns   []string
		expected   bool
	}{
		{"empty patterns", "/dev/sda", "/mnt/data", nil, false},
		{"mountpoint exact match", "/dev/sdb", "/mnt/backup", []string{"/mnt/backup"}, true},
		{"mountpoint prefix match", "/dev/sdb", "/mnt/external-drive", []string{"/mnt/ext*"}, true},
		{"device exact match", "/dev/sda", "/mnt/data", []string{"/dev/sda"}, true},
		{"device name match", "/dev/nvme0n1", "/mnt/fast", []string{"nvme0n1"}, true},
		{"device contains match", "/dev/nvme1n1", "/mnt/fast", []string{"*nvme*"}, true},
		{"no match", "/dev/sdc", "/mnt/data", []string{"/mnt/backup", "/dev/sda"}, false},
		{"device without prefix match", "sdd", "/mnt/data", []string{"sdd"}, true},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			result := MatchesDiskExclude(tc.device, tc.mountpoint, tc.patterns)
			if result != tc.expected {
				t.Errorf("MatchesDiskExclude(%q, %q, %v) = %t, want %t", tc.device, tc.mountpoint, tc.patterns, result, tc.expected)
			}
		})
	}
}

func TestMatchesDeviceExclude(t *testing.T) {
	tests := []struct {
		name     string
		device   string
		patterns []string
		expected bool
	}{
		{"empty patterns", "/dev/sda", nil, false},
		{"exact path match", "/dev/sda", []string{"/dev/sda"}, true},
		{"exact name match", "/dev/sda", []string{"sda"}, true},
		{"prefix added match", "sdb", []string{"/dev/sdb"}, true},
		{"contains match", "/dev/nvme0n1", []string{"*nvme*"}, true},
		{"whitespace pattern", "/dev/sdc", []string{"  /dev/sdc  "}, true},
		{"no match", "/dev/sdd", []string{"/dev/sde"}, false},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			result := MatchesDeviceExclude(tc.device, tc.patterns)
			if result != tc.expected {
				t.Errorf("MatchesDeviceExclude(%q, %v) = %t, want %t", tc.device, tc.patterns, result, tc.expected)
			}
		})
	}
}

func TestIsVirtualBlockDevice(t *testing.T) {
	tests := []struct {
		name     string
		device   string
		expected bool
	}{
		{"zfs zvol", "zd0", true},
		{"zfs zvol numeric tail", "zd48", true},
		{"zram device", "zram0", true},
		{"loopback", "loop3", true},
		{"device mapper", "dm-7", true},
		{"raid md", "md0", true},
		{"virtio disk", "vda", true},
		{"xen disk", "xvda", true},
		{"optical drive", "sr0", true},
		{"optical drive with dev prefix", "/dev/sr1", true},
		{"with dev prefix", "/dev/zram0", true},
		{"uppercase prefix", "/DEV/ZD0", true},
		{"sata disk", "sda", false},
		{"sata partition-ish", "sdb3", false},
		{"nvme", "nvme0n1", false},
		{"empty", "", false},
		{"whitespace", "   ", false},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			if got := IsVirtualBlockDevice(tc.device); got != tc.expected {
				t.Errorf("IsVirtualBlockDevice(%q) = %t, want %t", tc.device, got, tc.expected)
			}
		})
	}
}

// Regression guard: IsNonPhysicalDiskIODevice answers a different question
// from IsVirtualBlockDevice. Conflating them once blanked host disk I/O for
// every agent running inside a VM, because virtio and Xen disks cannot
// report SMART but their I/O counters are real host disk activity.
func TestIsNonPhysicalDiskIODevice(t *testing.T) {
	excluded := []string{"loop0", "ram0", "zram0", "dm-0", "zd0", "zd16", "zd160", "/dev/zd0"}
	for _, device := range excluded {
		if !IsNonPhysicalDiskIODevice(device) {
			t.Errorf("IsNonPhysicalDiskIODevice(%q) = false, want true", device)
		}
	}

	kept := []string{"sda", "nvme0n1", "vda", "vdb", "xvda", "md0", "pmem0", "nbd0", "rbd0", "/dev/vda"}
	for _, device := range kept {
		if IsNonPhysicalDiskIODevice(device) {
			t.Errorf("IsNonPhysicalDiskIODevice(%q) = true, want false", device)
		}
	}

	// vd*/xvd* must stay excluded for SMART while staying included for I/O.
	for _, device := range []string{"vda", "xvda"} {
		if !IsVirtualBlockDevice(device) {
			t.Errorf("IsVirtualBlockDevice(%q) = false, want true", device)
		}
		if IsNonPhysicalDiskIODevice(device) {
			t.Errorf("IsNonPhysicalDiskIODevice(%q) = true, want false", device)
		}
	}
}

func TestIsHiddenSystemMount(t *testing.T) {
	hidden := [][]string{
		{"local", "nodev", "nosuid", "journaled", "nobrowse"},
		{"nobrowse"},
	}
	for _, opts := range hidden {
		if !IsHiddenSystemMount(opts) {
			t.Errorf("IsHiddenSystemMount(%v) = false, want true", opts)
		}
	}

	visible := [][]string{
		nil,
		{"rw", "relatime"},
		{"local", "nodev", "nosuid", "journaled", "noowners"},
		{"nobrowsex"},
	}
	for _, opts := range visible {
		if IsHiddenSystemMount(opts) {
			t.Errorf("IsHiddenSystemMount(%v) = true, want false", opts)
		}
	}
}