tarballer/test/test.sh

222 lines
7.5 KiB
Bash
Executable File

#!/bin/sh
# Determine which test to run
TEST_TYPE=${1:-"all"}
run_basic_test() {
echo "=== RUNNING BASIC TEST ==="
# Clean up existing test directories
rm -rf /test/complex /test/complex-extracted
# Create complex directory structure
mkdir -p /test/complex/dir1/subdir1/subsubdir1
mkdir -p /test/complex/dir1/subdir2
mkdir -p /test/complex/dir2/subdir1
# Create files at different levels
echo 'root level file' > /test/complex/rootfile.txt
echo 'level 1 file in dir1' > /test/complex/dir1/file1.txt
echo 'level 1 file in dir2' > /test/complex/dir2/file2.txt
echo 'level 2 file in subdir1' > /test/complex/dir1/subdir1/file3.txt
echo 'level 2 file in subdir2' > /test/complex/dir1/subdir2/file4.txt
echo 'level 3 file in subsubdir1' > /test/complex/dir1/subdir1/subsubdir1/file5.txt
# Create a symbolic link with a relative path instead of absolute
cd /test/complex/dir2 && ln -s ../rootfile.txt symlink.txt && cd /workdir
# Calculate MD5 hashes of original files for verification
find /test/complex -type f | sort | xargs md5sum > /test/complex-original-md5.txt
# Print the original structure for reference
echo '=== ORIGINAL DIRECTORY STRUCTURE ==='
find /test/complex -type f -o -type l | sort
# Create the tarball
/bin/tarballer -source /test/complex -output /workdir/complex.tar.gz -prefix complex-app
# Extract the tarball
mkdir -p /test/complex-extracted
tar -xzf /workdir/complex.tar.gz -C /test/complex-extracted
# Verify the extracted structure
echo '=== EXTRACTED DIRECTORY STRUCTURE ==='
find /test/complex-extracted -type f -o -type l | sort
# Calculate MD5 hashes of extracted files
find /test/complex-extracted -type f | sort | xargs md5sum > /test/complex-extracted-md5.txt
# Compare file content
echo '=== VERIFYING FILE CONTENTS ==='
cat /test/complex/rootfile.txt
echo ' <-- Original: rootfile.txt'
cat /test/complex-extracted/complex-app/rootfile.txt
echo ' <-- Extracted: rootfile.txt'
cat /test/complex/dir1/subdir1/subsubdir1/file5.txt
echo ' <-- Original: deep nested file5.txt'
cat /test/complex-extracted/complex-app/dir1/subdir1/subsubdir1/file5.txt
echo ' <-- Extracted: deep nested file5.txt'
# Test symlink
echo '=== TESTING SYMLINK ==='
ls -la /test/complex/dir2/symlink.txt
ls -la /test/complex-extracted/complex-app/dir2/symlink.txt
# Verify MD5 hashes
echo '=== MD5 HASH VERIFICATION ==='
echo 'Original file hashes:'
cat /test/complex-original-md5.txt
echo 'Extracted file hashes:'
cat /test/complex-extracted-md5.txt
# Verify hash comparison
echo '=== COMPARING FILE HASHES ==='
ORIG_HASHES=$(cat /test/complex-original-md5.txt | awk '{print $1}' | sort)
EXTR_HASHES=$(cat /test/complex-extracted-md5.txt | grep -v 'complex-app$' | awk '{print $1}' | sort)
if [ "$ORIG_HASHES" = "$EXTR_HASHES" ]; then
echo 'SUCCESS: All file hashes match between original and extracted files!'
else
echo 'ERROR: Hash mismatch detected!'
return 1
fi
echo 'Basic test completed successfully!'
return 0
}
run_tar_comparison_test() {
echo "=== RUNNING TAR COMPARISON TEST ==="
# Clean up test directories
rm -rf /test/standard-test /test/standard-extracted /test/reference-extracted
# Create a diverse test directory structure
mkdir -p /test/standard-test/config/settings
mkdir -p /test/standard-test/data/user/documents
mkdir -p /test/standard-test/data/user/pictures
mkdir -p /test/standard-test/logs
# Create various file types
echo '{"app": "tarballer", "version": "1.0"}' > /test/standard-test/config/settings/app.json
echo 'debug=true' > /test/standard-test/config/settings/debug.conf
dd if=/dev/urandom bs=1K count=10 of=/test/standard-test/data/user/documents/binary.dat 2>/dev/null
echo 'Test log entry 1' > /test/standard-test/logs/app.log
echo 'Test log entry 2' >> /test/standard-test/logs/app.log
# Create symlinks
ln -s ../config/settings/app.json /test/standard-test/data/config-link.json
ln -s ../../logs/app.log /test/standard-test/data/user/log-link.txt
# Store MD5 hashes of original files for comparison
find /test/standard-test -type f | sort | xargs md5sum > /test/original-checksums.txt
# Create tarball using our utility
/bin/tarballer -source /test/standard-test -output /workdir/standard.tar.gz -prefix app
# Create a reference tarball using standard tar for comparison
tar -czf /workdir/reference.tar.gz -C /test/standard-test --transform 's,^./,app/,' .
echo '=== TARBALLER OUTPUT ==='
mkdir -p /test/standard-extracted
tar -xzf /workdir/standard.tar.gz -C /test/standard-extracted
find /test/standard-extracted -type f -o -type l | sort
# Extract reference tarball
echo '=== REFERENCE TAR OUTPUT ==='
mkdir -p /test/reference-extracted
tar -xzf /workdir/reference.tar.gz -C /test/reference-extracted
find /test/reference-extracted -type f -o -type l | sort
# Verify checksums match for all extracted files
echo '=== CHECKSUMS OF EXTRACTED FILES ==='
find /test/standard-extracted -type f | sort | xargs md5sum > /test/standard-checksums.txt
find /test/reference-extracted -type f | sort | xargs md5sum > /test/reference-checksums.txt
echo 'ORIGINAL FILE CHECKSUMS:'
cat /test/original-checksums.txt
echo 'TARBALLER EXTRACTED CHECKSUMS:'
cat /test/standard-checksums.txt
echo 'REFERENCE TAR EXTRACTED CHECKSUMS:'
cat /test/reference-checksums.txt
# Compare MD5 checksums systematically
echo '=== SYSTEMATIC MD5 COMPARISON ==='
# Extract just the hash part from each file
ORIG_HASHES=$(cat /test/original-checksums.txt | awk '{print $1}' | sort)
TARB_HASHES=$(cat /test/standard-checksums.txt | awk '{print $1}' | sort)
REF_HASHES=$(cat /test/reference-checksums.txt | awk '{print $1}' | sort)
# Compare original to tarballer extraction
if [ "$ORIG_HASHES" = "$TARB_HASHES" ]; then
echo 'SUCCESS: Tarballer extraction hashes match original files!'
else
echo 'ERROR: Hash mismatch detected between original and tarballer extraction!'
return 1
fi
# Compare original to reference tar extraction
if [ "$ORIG_HASHES" = "$REF_HASHES" ]; then
echo 'SUCCESS: Reference tar extraction hashes match original files!'
else
echo 'ERROR: Hash mismatch detected between original and reference tar extraction!'
return 1
fi
echo '=== VERIFYING SYMLINKS ==='
echo 'ORIGINAL SYMLINKS:'
find /test/standard-test -type l -exec ls -la {} \;
echo 'EXTRACTED SYMLINKS:'
find /test/standard-extracted -type l -exec ls -la {} \;
# Compare file counts to ensure all files were extracted
echo '=== FILE COUNT COMPARISON ==='
echo -n 'Original files: ' && find /test/standard-test -type f | wc -l
echo -n 'Extracted files: ' && find /test/standard-extracted -type f | wc -l
# Test symlink functionality
echo '=== TESTING SYMLINK CONTENT ==='
echo 'Original linked content:'
cat /test/standard-test/data/config-link.json
echo 'Extracted linked content:'
cat /test/standard-extracted/app/data/config-link.json
echo 'Tar comparison test completed successfully!'
return 0
}
# Main script execution
case "$TEST_TYPE" in
"basic")
run_basic_test
exit $?
;;
"tar")
run_tar_comparison_test
exit $?
;;
"all")
echo "=== RUNNING ALL TESTS ==="
run_basic_test
BASIC_RESULT=$?
run_tar_comparison_test
TAR_RESULT=$?
if [ $BASIC_RESULT -eq 0 ] && [ $TAR_RESULT -eq 0 ]; then
echo "✅ ALL TESTS PASSED SUCCESSFULLY!"
exit 0
else
echo "❌ SOME TESTS FAILED!"
exit 1
fi
;;
*)
echo "Unknown test type: $TEST_TYPE"
echo "Usage: $0 [basic|tar|all]"
exit 1
;;
esac