]> git.ipfire.org Git - people/stevee/ipfire-2.x.git/commitdiff
filesystem-functions.pl: Add testsuite filesystem-functions
authorStefan Schantl <stefan.schantl@ipfire.org>
Sun, 5 May 2024 15:02:58 +0000 (17:02 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Sun, 5 May 2024 15:02:58 +0000 (17:02 +0200)
Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
config/cfgroot/filesystem-functions.pl

index 10c24df869affd63e1c24495f65e3b7d97e98e0f..256a514b03b37613bec27a487279053d9b2f20d2 100644 (file)
@@ -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();