| 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";
|
| 63 |
rodolico |
156 |
print "All basic tests completed. Review output above for details.\n";
|
| 62 |
rodolico |
157 |
print "Note: Dry-run mode ensures no actual writes were performed.\n";
|
|
|
158 |
print "\n";
|
|
|
159 |
|
| 63 |
rodolico |
160 |
# Test 6: Target mode simulation
|
|
|
161 |
print "=" x 60 . "\n";
|
|
|
162 |
print "Test 6: Target Mode Simulation\n";
|
|
|
163 |
print "=" x 60 . "\n";
|
|
|
164 |
print "Note: This test simulates running on the target server\n";
|
|
|
165 |
print " by temporarily overriding the hostname.\n\n";
|
|
|
166 |
|
|
|
167 |
# Read the config to get the target hostname
|
|
|
168 |
my $target_hostname;
|
|
|
169 |
if (open my $cfg_fh, '<', $config_file) {
|
|
|
170 |
while (my $line = <$cfg_fh>) {
|
|
|
171 |
if ($line =~ /^\s*hostname:\s*['"]?([^'"]+)['"]?\s*$/) {
|
|
|
172 |
# This is a bit crude, but we need to find target.hostname
|
|
|
173 |
# We'll look for it after we see 'target:' section
|
|
|
174 |
my $in_target = 0;
|
|
|
175 |
seek($cfg_fh, 0, 0); # Reset to beginning
|
|
|
176 |
while (my $l2 = <$cfg_fh>) {
|
|
|
177 |
if ($l2 =~ /^target:/) {
|
|
|
178 |
$in_target = 1;
|
|
|
179 |
next;
|
|
|
180 |
}
|
|
|
181 |
if ($in_target && $l2 =~ /^\s*hostname:\s*['"]?([^'"]+)['"]?\s*$/) {
|
|
|
182 |
$target_hostname = $1;
|
|
|
183 |
last;
|
|
|
184 |
}
|
|
|
185 |
if ($in_target && $l2 =~ /^[a-z]+:/ && $l2 !~ /^\s/) {
|
|
|
186 |
$in_target = 0; # Exited target section
|
|
|
187 |
}
|
|
|
188 |
}
|
|
|
189 |
last;
|
|
|
190 |
}
|
|
|
191 |
}
|
|
|
192 |
close $cfg_fh;
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
if ($target_hostname) {
|
|
|
196 |
print "Target hostname from config: $target_hostname\n";
|
|
|
197 |
print "Running sneakernet with HOSTNAME=$target_hostname\n";
|
|
|
198 |
print "-" x 60 . "\n\n";
|
|
|
199 |
|
|
|
200 |
# Set environment variable to simulate target hostname
|
|
|
201 |
local $ENV{HOSTNAME} = $target_hostname;
|
|
|
202 |
|
|
|
203 |
my $target_output = `HOSTNAME=$target_hostname perl $sneakernet_script --dryrun -vvv 2>&1`;
|
|
|
204 |
my $target_exit = $? >> 8;
|
|
|
205 |
|
|
|
206 |
print "Output:\n";
|
|
|
207 |
print $target_output;
|
|
|
208 |
print "\n";
|
|
|
209 |
print "-" x 60 . "\n";
|
|
|
210 |
print "Exit code: $target_exit\n";
|
|
|
211 |
|
|
|
212 |
# Analyze the output for target-specific behavior
|
|
|
213 |
my $detected_target = $target_output =~ /target|receiving|import/i;
|
|
|
214 |
my $has_receive = $target_output =~ /zfs receive/i;
|
|
|
215 |
my $has_geli = $target_output =~ /geli|decrypt|mount/i;
|
|
|
216 |
|
|
|
217 |
print "\nTarget Mode Analysis:\n";
|
|
|
218 |
print " Running as target: " . ($detected_target ? "Yes" : "No") . "\n";
|
|
|
219 |
print " Contains zfs receive: " . ($has_receive ? "Yes" : "No") . "\n";
|
|
|
220 |
print " Contains GELI operations: " . ($has_geli ? "Yes" : "No") . "\n";
|
|
|
221 |
|
|
|
222 |
if ($target_exit == 0) {
|
|
|
223 |
print "\n✓ PASS: Target mode dry-run completed successfully\n";
|
|
|
224 |
} else {
|
|
|
225 |
print "\n✗ FAIL: Target mode dry-run exited with error code $target_exit\n";
|
|
|
226 |
}
|
|
|
227 |
} else {
|
|
|
228 |
print "⚠ SKIP: Could not determine target hostname from config\n";
|
|
|
229 |
print " Target mode test skipped.\n";
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
print "\n";
|
|
|
233 |
print "=" x 60 . "\n";
|
|
|
234 |
print "All Tests Complete\n";
|
|
|
235 |
print "=" x 60 . "\n";
|
|
|
236 |
print "\n";
|
|
|
237 |
|
| 62 |
rodolico |
238 |
1;
|