Subversion Repositories zfs_utils

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
62 rodolico 1
#! /usr/bin/env perl
2
 
3
use strict;
4
use warnings;
5
use FindBin;
6
use lib "$FindBin::Bin/..";
7
use File::Temp qw(tempfile tempdir);
8
use File::Copy;
9
use File::Basename;
10
 
11
# Unit test for sneakernet script
12
# This test runs sneakernet in dry-run mode with maximum verbosity
13
# to validate configuration and command generation without making changes.
14
 
15
print "=" x 60 . "\n";
16
print "Sneakernet Unit Test\n";
17
print "=" x 60 . "\n\n";
18
 
19
my $sneakernet_script = "$FindBin::Bin/sneakernet";
20
my $config_file = "$sneakernet_script.conf.yaml";
21
 
22
# Verify files exist
23
unless (-f $sneakernet_script) {
24
    die "ERROR: sneakernet script not found at: $sneakernet_script\n";
25
}
26
 
27
unless (-f $config_file) {
28
    die "ERROR: Configuration file not found at: $config_file\n";
29
}
30
 
31
print "Test Configuration:\n";
32
print "  Script: $sneakernet_script\n";
33
print "  Config: $config_file\n";
34
print "  Mode: dry-run (--dryrun)\n";
35
print "  Verbosity: 3 (-vvv)\n";
36
print "\n";
37
 
38
# Test 1: Version check
39
print "-" x 60 . "\n";
40
print "Test 1: Version Check\n";
41
print "-" x 60 . "\n";
42
my $version_output = `perl $sneakernet_script --version 2>&1`;
43
my $version_exit = $? >> 8;
44
print "Output: $version_output";
45
print "Exit code: $version_exit\n";
46
if ($version_exit == 0 && $version_output =~ /sneakernet v\d+\.\d+/) {
47
    print "✓ PASS: Version check successful\n\n";
48
} else {
49
    print "✗ FAIL: Version check failed\n\n";
50
}
51
 
52
# Test 2: Help output
53
print "-" x 60 . "\n";
54
print "Test 2: Help Output\n";
55
print "-" x 60 . "\n";
56
my $help_output = `perl $sneakernet_script --help 2>&1`;
57
my $help_exit = $? >> 8;
58
print "Output:\n$help_output";
59
print "Exit code: $help_exit\n";
60
if ($help_exit == 0 && $help_output =~ /Usage:/) {
61
    print "✓ PASS: Help output successful\n\n";
62
} else {
63
    print "✗ FAIL: Help output failed\n\n";
64
}
65
 
66
# Test 3: Dry-run with maximum verbosity
67
print "-" x 60 . "\n";
68
print "Test 3: Dry-run Execution (High Verbosity)\n";
69
print "-" x 60 . "\n";
70
print "Running: perl $sneakernet_script --dryrun -vvv\n";
71
print "-" x 60 . "\n\n";
72
 
73
my $dryrun_output = `perl $sneakernet_script --dryrun -vvv 2>&1`;
74
my $dryrun_exit = $? >> 8;
75
 
76
print "Output:\n";
77
print $dryrun_output;
78
print "\n";
79
print "-" x 60 . "\n";
80
print "Exit code: $dryrun_exit\n";
81
 
82
# Analyze the output
83
my $has_config = $dryrun_output =~ /config|configuration/i;
84
my $has_dryrun = $dryrun_output =~ /dry[-\s]?run/i;
85
my $has_commands = $dryrun_output =~ /zfs send|command/i;
86
 
87
print "\nAnalysis:\n";
88
print "  Contains config info: " . ($has_config ? "Yes" : "No") . "\n";
89
print "  Dry-run mode active: " . ($has_dryrun ? "Yes" : "No") . "\n";
90
print "  Generated commands: " . ($has_commands ? "Yes" : "No") . "\n";
91
 
92
if ($dryrun_exit == 0) {
93
    print "\n✓ PASS: Dry-run completed successfully\n";
94
} else {
95
    print "\n✗ FAIL: Dry-run exited with error code $dryrun_exit\n";
96
}
97
 
98
# Test 4: Check log file creation
99
print "\n";
100
print "-" x 60 . "\n";
101
print "Test 4: Log File Check\n";
102
print "-" x 60 . "\n";
103
 
104
my $log_file = "$sneakernet_script.log";
105
if (-f $log_file) {
106
    print "Log file found: $log_file\n";
107
    my $log_size = -s $log_file;
108
    print "Log file size: $log_size bytes\n";
109
 
110
    if ($log_size > 0) {
111
        print "\nLast 20 lines of log file:\n";
112
        print "- " x 30 . "\n";
113
        my @log_lines = `tail -20 $log_file 2>/dev/null`;
114
        print @log_lines;
115
        print "- " x 30 . "\n";
116
        print "✓ PASS: Log file created and populated\n";
117
    } else {
118
        print "✗ FAIL: Log file is empty\n";
119
    }
120
} else {
121
    print "✗ FAIL: Log file not found at $log_file\n";
122
}
123
 
124
# Test 5: Status file check
125
print "\n";
126
print "-" x 60 . "\n";
127
print "Test 5: Status File Check\n";
128
print "-" x 60 . "\n";
129
 
130
my $status_file = "$sneakernet_script.status";
131
if (-f $status_file) {
132
    print "Status file found: $status_file\n";
133
    my $status_size = -s $status_file;
134
    print "Status file size: $status_size bytes\n";
135
 
136
    if ($status_size > 0) {
137
        print "\nStatus file contents:\n";
138
        print "- " x 30 . "\n";
139
        open my $fh, '<', $status_file or die "Can't open status file: $!\n";
140
        print while <$fh>;
141
        close $fh;
142
        print "- " x 30 . "\n";
143
        print "✓ INFO: Status file exists (may be from previous run)\n";
144
    } else {
145
        print "✓ INFO: Status file is empty (expected in dry-run)\n";
146
    }
147
} else {
148
    print "✓ INFO: Status file not found (expected in dry-run)\n";
149
}
150
 
151
# Summary
152
print "\n";
153
print "=" x 60 . "\n";
154
print "Test Summary\n";
155
print "=" x 60 . "\n";
156
print "All tests completed. Review output above for details.\n";
157
print "Note: Dry-run mode ensures no actual writes were performed.\n";
158
print "\n";
159
 
160
1;