Subversion Repositories zfs_utils

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#! /usr/bin/env perl

use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/..";
use File::Temp qw(tempfile tempdir);
use File::Copy;
use File::Basename;

# Unit test for sneakernet script
# This test runs sneakernet in dry-run mode with maximum verbosity
# to validate configuration and command generation without making changes.

print "=" x 60 . "\n";
print "Sneakernet Unit Test\n";
print "=" x 60 . "\n\n";

my $sneakernet_script = "$FindBin::Bin/sneakernet";
my $config_file = "$sneakernet_script.conf.yaml";

# Verify files exist
unless (-f $sneakernet_script) {
    die "ERROR: sneakernet script not found at: $sneakernet_script\n";
}

unless (-f $config_file) {
    die "ERROR: Configuration file not found at: $config_file\n";
}

print "Test Configuration:\n";
print "  Script: $sneakernet_script\n";
print "  Config: $config_file\n";
print "  Mode: dry-run (--dryrun)\n";
print "  Verbosity: 3 (-vvv)\n";
print "\n";

# Test 1: Version check
print "-" x 60 . "\n";
print "Test 1: Version Check\n";
print "-" x 60 . "\n";
my $version_output = `perl $sneakernet_script --version 2>&1`;
my $version_exit = $? >> 8;
print "Output: $version_output";
print "Exit code: $version_exit\n";
if ($version_exit == 0 && $version_output =~ /sneakernet v\d+\.\d+/) {
    print "✓ PASS: Version check successful\n\n";
} else {
    print "✗ FAIL: Version check failed\n\n";
}

# Test 2: Help output
print "-" x 60 . "\n";
print "Test 2: Help Output\n";
print "-" x 60 . "\n";
my $help_output = `perl $sneakernet_script --help 2>&1`;
my $help_exit = $? >> 8;
print "Output:\n$help_output";
print "Exit code: $help_exit\n";
if ($help_exit == 0 && $help_output =~ /Usage:/) {
    print "✓ PASS: Help output successful\n\n";
} else {
    print "✗ FAIL: Help output failed\n\n";
}

# Test 3: Dry-run with maximum verbosity
print "-" x 60 . "\n";
print "Test 3: Dry-run Execution (High Verbosity)\n";
print "-" x 60 . "\n";
print "Running: perl $sneakernet_script --dryrun -vvv\n";
print "-" x 60 . "\n\n";

my $dryrun_output = `perl $sneakernet_script --dryrun -vvv 2>&1`;
my $dryrun_exit = $? >> 8;

print "Output:\n";
print $dryrun_output;
print "\n";
print "-" x 60 . "\n";
print "Exit code: $dryrun_exit\n";

# Analyze the output
my $has_config = $dryrun_output =~ /config|configuration/i;
my $has_dryrun = $dryrun_output =~ /dry[-\s]?run/i;
my $has_commands = $dryrun_output =~ /zfs send|command/i;

print "\nAnalysis:\n";
print "  Contains config info: " . ($has_config ? "Yes" : "No") . "\n";
print "  Dry-run mode active: " . ($has_dryrun ? "Yes" : "No") . "\n";
print "  Generated commands: " . ($has_commands ? "Yes" : "No") . "\n";

if ($dryrun_exit == 0) {
    print "\n✓ PASS: Dry-run completed successfully\n";
} else {
    print "\n✗ FAIL: Dry-run exited with error code $dryrun_exit\n";
}

# Test 4: Check log file creation
print "\n";
print "-" x 60 . "\n";
print "Test 4: Log File Check\n";
print "-" x 60 . "\n";

my $log_file = "$sneakernet_script.log";
if (-f $log_file) {
    print "Log file found: $log_file\n";
    my $log_size = -s $log_file;
    print "Log file size: $log_size bytes\n";
    
    if ($log_size > 0) {
        print "\nLast 20 lines of log file:\n";
        print "- " x 30 . "\n";
        my @log_lines = `tail -20 $log_file 2>/dev/null`;
        print @log_lines;
        print "- " x 30 . "\n";
        print "✓ PASS: Log file created and populated\n";
    } else {
        print "✗ FAIL: Log file is empty\n";
    }
} else {
    print "✗ FAIL: Log file not found at $log_file\n";
}

# Test 5: Status file check
print "\n";
print "-" x 60 . "\n";
print "Test 5: Status File Check\n";
print "-" x 60 . "\n";

my $status_file = "$sneakernet_script.status";
if (-f $status_file) {
    print "Status file found: $status_file\n";
    my $status_size = -s $status_file;
    print "Status file size: $status_size bytes\n";
    
    if ($status_size > 0) {
        print "\nStatus file contents:\n";
        print "- " x 30 . "\n";
        open my $fh, '<', $status_file or die "Can't open status file: $!\n";
        print while <$fh>;
        close $fh;
        print "- " x 30 . "\n";
        print "✓ INFO: Status file exists (may be from previous run)\n";
    } else {
        print "✓ INFO: Status file is empty (expected in dry-run)\n";
    }
} else {
    print "✓ INFO: Status file not found (expected in dry-run)\n";
}

# Summary
print "\n";
print "=" x 60 . "\n";
print "Test Summary\n";
print "=" x 60 . "\n";
print "All tests completed. Review output above for details.\n";
print "Note: Dry-run mode ensures no actual writes were performed.\n";
print "\n";

1;