| 64 |
rodolico |
1 |
#!/usr/bin/env perl
|
|
|
2 |
|
|
|
3 |
use strict;
|
|
|
4 |
use warnings;
|
|
|
5 |
use FindBin;
|
|
|
6 |
use lib "$FindBin::Bin/..";
|
|
|
7 |
|
|
|
8 |
# Test the parseSnapshotDateTime helper function directly
|
|
|
9 |
# This demonstrates the improved maintainability from the refactoring
|
|
|
10 |
|
|
|
11 |
print "=" x 60 . "\n";
|
|
|
12 |
print "Testing parseSnapshotDateTime helper function\n";
|
|
|
13 |
print "=" x 60 . "\n\n";
|
|
|
14 |
|
|
|
15 |
# Access the helper function (not exported, so use full package name)
|
|
|
16 |
require ZFS_Utils;
|
|
|
17 |
|
|
|
18 |
my @test_cases = (
|
|
|
19 |
# [snapshot_name, expected_result_description]
|
|
|
20 |
['2025-12-18', 'PASS - Simple date'],
|
|
|
21 |
['2025-12-18_10:30:00', 'PASS - Date with time (colon separator)'],
|
|
|
22 |
['2025-12-18_10.30.00', 'PASS - Date with time (dot separator)'],
|
|
|
23 |
['2025-12-18T10:30:00', 'PASS - ISO 8601 format'],
|
|
|
24 |
['prefix_2025-12-18_suffix', 'PASS - Date with prefix and suffix'],
|
|
|
25 |
['daily_2025-12-18_10:30:00_backup', 'PASS - Complex name with time'],
|
|
|
26 |
['no_date_here', 'SKIP - No date in name'],
|
|
|
27 |
['2025-13-45', 'SKIP - Invalid date'],
|
|
|
28 |
['', 'SKIP - Empty string'],
|
|
|
29 |
);
|
|
|
30 |
|
|
|
31 |
print "Test Cases:\n";
|
|
|
32 |
print "-" x 60 . "\n";
|
|
|
33 |
|
|
|
34 |
my $pass_count = 0;
|
|
|
35 |
my $skip_count = 0;
|
|
|
36 |
my $fail_count = 0;
|
|
|
37 |
|
|
|
38 |
foreach my $test (@test_cases) {
|
|
|
39 |
my ($snapname, $expected) = @$test;
|
|
|
40 |
my $epoch = ZFS_Utils::parseSnapshotDateTime($snapname);
|
|
|
41 |
|
|
|
42 |
printf "%-40s => ", $snapname || '(empty)';
|
|
|
43 |
|
|
|
44 |
if (defined $epoch) {
|
|
|
45 |
# Convert epoch back to readable date for verification
|
|
|
46 |
my $date = scalar localtime($epoch);
|
|
|
47 |
print "epoch $epoch ($date)\n";
|
|
|
48 |
if ($expected =~ /^PASS/) {
|
|
|
49 |
print " ✓ $expected\n";
|
|
|
50 |
$pass_count++;
|
|
|
51 |
} else {
|
|
|
52 |
print " ✗ UNEXPECTED: got epoch when expecting skip\n";
|
|
|
53 |
$fail_count++;
|
|
|
54 |
}
|
|
|
55 |
} else {
|
|
|
56 |
print "undef (no date parsed)\n";
|
|
|
57 |
if ($expected =~ /^SKIP/) {
|
|
|
58 |
print " ✓ $expected\n";
|
|
|
59 |
$skip_count++;
|
|
|
60 |
} else {
|
|
|
61 |
print " ✗ UNEXPECTED: got undef when expecting parse\n";
|
|
|
62 |
$fail_count++;
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
print "-" x 60 . "\n";
|
|
|
68 |
print "\nSummary:\n";
|
|
|
69 |
print " Passed: $pass_count\n";
|
|
|
70 |
print " Skipped (as expected): $skip_count\n";
|
|
|
71 |
print " Failed: $fail_count\n";
|
|
|
72 |
|
|
|
73 |
print "\n";
|
|
|
74 |
print "=" x 60 . "\n";
|
|
|
75 |
if ($fail_count == 0) {
|
|
|
76 |
print "ALL TESTS PASSED\n";
|
|
|
77 |
print "Helper function is working correctly and can be reused!\n";
|
|
|
78 |
exit 0;
|
|
|
79 |
} else {
|
|
|
80 |
print "SOME TESTS FAILED\n";
|
|
|
81 |
exit 1;
|
|
|
82 |
}
|
|
|
83 |
print "=" x 60 . "\n";
|