new: parse disk info via lsblk output Fixes #709 (#760)

This commit is contained in:
lollipopkit🏳️‍⚧️
2025-05-17 00:45:38 +08:00
committed by GitHub
parent d88e97e699
commit 7e16d2f159
6 changed files with 685 additions and 184 deletions

92
test/btrfs_test.dart Normal file
View File

@@ -0,0 +1,92 @@
// ignore_for_file: avoid_print
import 'package:flutter_test/flutter_test.dart';
import 'package:server_box/data/model/server/disk.dart';
void main() {
group('BTRFS RAID1 disk parsing', () {
test('correctly handles BTRFS RAID1 with same UUID', () {
final disks = Disk.parse(_btrfsRaidJsonOutput);
expect(disks, isNotEmpty);
expect(disks.length, 4); // Should have 2 parent disks + 2 BTRFS partitions
// We should get two distinct disks with the same UUID but different paths
final nvme1Disk = disks.firstWhere((disk) => disk.path == '/dev/nvme1n1p1');
final nvme2Disk = disks.firstWhere((disk) => disk.path == '/dev/nvme2n1p1');
// Both should exist
expect(nvme1Disk, isNotNull);
expect(nvme2Disk, isNotNull);
// They should have the same UUID (since they're part of the same BTRFS volume)
expect(nvme1Disk.uuid, nvme2Disk.uuid);
// But they should be treated as distinct disks
expect(identical(nvme1Disk, nvme2Disk), isFalse);
// Verify DiskUsage counts physical disks correctly
final usage = DiskUsage.parse(disks);
// With our unique path+kname identifier, both disks should be counted
expect(usage.size, nvme1Disk.size + nvme2Disk.size);
expect(usage.used, nvme1Disk.used + nvme2Disk.used);
});
});
}
// Simulated BTRFS RAID1 lsblk JSON output
const _btrfsRaidJsonOutput = '''
{
"blockdevices": [
{
"name": "nvme1n1",
"kname": "nvme1n1",
"path": "/dev/nvme1n1",
"fstype": null,
"mountpoint": null,
"fssize": null,
"fsused": null,
"fsavail": null,
"fsuse%": null,
"children": [
{
"name": "nvme1n1p1",
"kname": "nvme1n1p1",
"path": "/dev/nvme1n1p1",
"fstype": "btrfs",
"mountpoint": "/mnt/raid",
"fssize": "500000000000",
"fsused": "100000000000",
"fsavail": "400000000000",
"fsuse%": "20%",
"uuid": "btrfs-raid-uuid-1234-5678"
}
]
},
{
"name": "nvme2n1",
"kname": "nvme2n1",
"path": "/dev/nvme2n1",
"fstype": null,
"mountpoint": null,
"fssize": null,
"fsused": null,
"fsavail": null,
"fsuse%": null,
"children": [
{
"name": "nvme2n1p1",
"kname": "nvme2n1p1",
"path": "/dev/nvme2n1p1",
"fstype": "btrfs",
"mountpoint": "/mnt/raid",
"fssize": "500000000000",
"fsused": "100000000000",
"fsavail": "400000000000",
"fsuse%": "20%",
"uuid": "btrfs-raid-uuid-1234-5678"
}
]
}
]
}
''';

View File

@@ -4,16 +4,199 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:server_box/data/model/server/disk.dart';
void main() {
test('parse disk', () {
for (final raw in _raws) {
print('---' * 10);
final disks = Disk.parse(raw);
print(disks.join('\n'));
print('\n');
}
group('Disk parsing', () {
test('parse traditional df output', () {
for (final raw in _raws) {
final disks = Disk.parse(raw);
expect(disks, isNotEmpty);
}
});
test('parse lsblk JSON output', () {
final disks = Disk.parse(_jsonLsblkOutput);
expect(disks, isNotEmpty);
expect(disks.length, 6); // Should find ext4 root, vfat efi, and ext2 boot
// Verify root filesystem
final rootFs = disks.firstWhere((disk) => disk.mount == '/');
expect(rootFs.fsTyp, 'ext4');
expect(rootFs.size, BigInt.parse('982141468672') ~/ BigInt.from(1024));
expect(rootFs.used, BigInt.parse('552718364672') ~/ BigInt.from(1024));
expect(rootFs.avail, BigInt.parse('379457622016') ~/ BigInt.from(1024));
expect(rootFs.usedPercent, 56);
// Verify boot/efi filesystem
final efiFs = disks.firstWhere((disk) => disk.mount == '/boot/efi');
expect(efiFs.fsTyp, 'vfat');
expect(efiFs.size, BigInt.parse('535805952') ~/ BigInt.from(1024));
expect(efiFs.usedPercent, 1);
// Verify boot filesystem
final bootFs = disks.firstWhere((disk) => disk.mount == '/boot');
expect(bootFs.fsTyp, 'ext2');
expect(bootFs.usedPercent, 34);
});
test('parse nested lsblk JSON output with parent/child relationships', () {
final disks = Disk.parse(_nestedJsonLsblkOutput);
expect(disks, isNotEmpty);
// Check parent device with children
final parentDisk = disks.firstWhere((disk) => disk.path == '/dev/nvme0n1');
expect(parentDisk.children, isNotEmpty);
expect(parentDisk.children.length, 3);
// Check one of the children
final rootPartition = parentDisk.children.firstWhere((disk) => disk.mount == '/');
expect(rootPartition.fsTyp, 'ext4');
expect(rootPartition.path, '/dev/nvme0n1p2');
expect(rootPartition.usedPercent, 45);
// Verify we have a child partition with UUID
final bootPartition = parentDisk.children.firstWhere((disk) => disk.mount == '/boot');
expect(bootPartition.uuid, '12345678-abcd-1234-abcd-1234567890ab');
});
test('DiskUsage handles zero size correctly', () {
final usage = DiskUsage(used: BigInt.from(1000), size: BigInt.zero);
expect(usage.usedPercent, 0); // Should return 0 instead of throwing
});
test('DiskUsage handles null kname', () {
final disks = [
Disk(
path: '/dev/sda1',
mount: '/mnt',
usedPercent: 50,
used: BigInt.from(5000),
size: BigInt.from(10000),
avail: BigInt.from(5000),
kname: null, // Explicitly null kname
),
];
final usage = DiskUsage.parse(disks);
expect(usage.used, BigInt.from(5000));
expect(usage.size, BigInt.from(10000));
expect(usage.usedPercent, 50);
// This would use the "unknown" fallback for kname
});
});
}
const _jsonLsblkOutput = '''
{
"blockdevices": [
{
"fstype": "LVM2_member",
"mountpoint": null,
"fssize": null,
"fsused": null,
"fsavail": null,
"fsuse%": null
},{
"fstype": "ext4",
"mountpoint": "/",
"fssize": 982141468672,
"fsused": 552718364672,
"fsavail": 379457622016,
"fsuse%": "56%"
},{
"fstype": "swap",
"mountpoint": "[SWAP]",
"fssize": null,
"fsused": null,
"fsavail": null,
"fsuse%": null
},{
"fstype": null,
"mountpoint": null,
"fssize": null,
"fsused": null,
"fsavail": null,
"fsuse%": null
},{
"fstype": "vfat",
"mountpoint": "/boot/efi",
"fssize": 535805952,
"fsused": 6127616,
"fsavail": 529678336,
"fsuse%": "1%"
},{
"fstype": "ext2",
"mountpoint": "/boot",
"fssize": 477210624,
"fsused": 161541120,
"fsavail": 290084864,
"fsuse%": "34%"
},{
"fstype": "crypto_LUKS",
"mountpoint": null,
"fssize": null,
"fsused": null,
"fsavail": null,
"fsuse%": null
}
]
}
''';
const _nestedJsonLsblkOutput = '''
{
"blockdevices": [
{
"name": "nvme0n1",
"kname": "nvme0n1",
"path": "/dev/nvme0n1",
"fstype": null,
"mountpoint": null,
"fssize": null,
"fsused": null,
"fsavail": null,
"fsuse%": null,
"children": [
{
"name": "nvme0n1p1",
"kname": "nvme0n1p1",
"path": "/dev/nvme0n1p1",
"fstype": "vfat",
"mountpoint": "/boot/efi",
"fssize": "512000000",
"fsused": "25600000",
"fsavail": "486400000",
"fsuse%": "5%",
"uuid": "98765432-dcba-4321-dcba-0987654321fe"
},
{
"name": "nvme0n1p2",
"kname": "nvme0n1p2",
"path": "/dev/nvme0n1p2",
"fstype": "ext4",
"mountpoint": "/",
"fssize": "500000000000",
"fsused": "225000000000",
"fsavail": "275000000000",
"fsuse%": "45%",
"uuid": "abcdef12-3456-7890-abcd-ef1234567890"
},
{
"name": "nvme0n1p3",
"kname": "nvme0n1p3",
"path": "/dev/nvme0n1p3",
"fstype": "ext4",
"mountpoint": "/boot",
"fssize": "1000000000",
"fsused": "500000000",
"fsavail": "500000000",
"fsuse%": "50%",
"uuid": "12345678-abcd-1234-abcd-1234567890ab"
}
]
}
]
}
''';
const _raws = [
// '''
// Filesystem 1K-blocks Used Available Use% Mounted on