From c7712f4e2f7c418ffaf617a668a0d23c92a04b55 Mon Sep 17 00:00:00 2001 From: Stefan Schantl Date: Sun, 5 May 2024 17:02:58 +0200 Subject: [PATCH] filesystem-functions.pl: Add testsuite Signed-off-by: Stefan Schantl --- config/cfgroot/filesystem-functions.pl | 100 +++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/config/cfgroot/filesystem-functions.pl b/config/cfgroot/filesystem-functions.pl index 10c24df86..256a514b0 100644 --- a/config/cfgroot/filesystem-functions.pl +++ b/config/cfgroot/filesystem-functions.pl @@ -743,3 +743,103 @@ sub _filter_subvolume_list (%) { } 1; + +# Remove or uncomment the next line to run the testsuite. +__END__ + +sub testsuite() { + # Test the volume status function. + my %test = &volumes_status( + "devices" => "all", + "uuids" => "True", + "df" => "True" + ); + + # Check the hash contains anything. + if (%test) { + print "volumes_status() - PASSED!\n"; + } + + # Drop the test hash + undef(%test); + + # BTRFS RELATED TESTS! + if (&is_btrfs("/")) { + print "&is_btrfs() - Performing BTRFS tests!\n"; + + &btrfs_tests(); + } else { + print "&is_btrfs() - Skipped BTRFS tests!\n"; + } +} + +sub btrfs_tests() { + # Name of the snapshot. + my $test_snapshot_name = "xxxTESTSUITE_SNAPSHOTxxx"; + + # Try to get a list of known subvolumes. + my %subvolumes = &btrfs_get_subvolumes(); + + # Check if at least one subvolume could be obtained. + if (%subvolumes) { + print "&btrfs_get_subvolumes() - PASSED!\n"; + } + + # Drop the subvolumes hash. + undef(%subvolumes); + + # Create a snapshot. + my $ret = &btrfs_create_snapshot($test_snapshot_name); + + # Check the function returned a return code. + if ($ret) { + print "&btrfs_create_snapshot() - FAILED! (Ret: $ret)\n"; + } else { + print "&btrfs_create_snapshot() - PASSED!\n"; + } + + # Try to get a list of all known snapshots. + my %snapshots = &btrfs_get_snapshots(); + + # Check if at least one snapshot could be obtained. + if (%snapshots) { + print "&btrfs_get_snapshots() - PASSED!\n"; + } + + # Value to store the ID of the test snapshot. + my $test_snapshot_id; + + # Loop through the hash of snapshots. + foreach my $id (sort keys %snapshots) { + # Grab the snapshot name. + my $snapshot = $snapshots{$id}; + + # Skip found snapshot until we found our test snapshot. + next unless $snapshot eq $test_snapshot_name; + + # Store the ID of the test snapshot. + $test_snapshot_id = $id; + + print "SUCESSFULLY GOT THE ID ($id) OF $test_snapshot_name\n"; + + # Break the loop. + last; + } + + # Check if an ID for the test snapshot could be obtained. + if ($test_snapshot_id) { + # Try to delete the snapshot. + $ret = &btrfs_remove_snapshot($test_snapshot_id); + + # Check if the function returned a code. + if ($ret) { + print "&btrfs_remove_snapshot() - FAILED (Ret: $ret)\n"; + } else { + print "&btrfs_remove_snapshot() - PASSED!\n"; + } + } else { + print "&btrfs_remove_snapshot() - SKIPPED NO ID!\n"; + } +} + +&testsuite(); -- 2.39.5