feat: hello from my homelab repo

This commit is contained in:
Mateusz Okualnis 2025-05-30 15:20:25 +02:00
commit 154ad75a0c
9 changed files with 2281 additions and 0 deletions

26
test/gnuramage_test.ini Normal file
View file

@ -0,0 +1,26 @@
# GnuRAMage Test Configuration File
[SETTINGS]
# Interval between synchronizations in seconds (short for testing)
sync_interval = 10
# Log level: ERROR, WARN, INFO, DEBUG
log_level = DEBUG
# Verify checksums during sync (test both true and false)
verify_checksums = true
[DIRECTORIES]
# Source directory on hard disk
source_dir = /tmp/ram_disk_test_source
# Target directory on RAM disk
ramdisk_dir = /tmp/ram_disk_test_ramdisk
[EXCLUDE]
# Patterns to exclude from synchronization
# Each line is a pattern in rsync format
*.bak
*.tmp
temp_*
test_ignore_dir/

26
test/gramage_test.ini Normal file
View file

@ -0,0 +1,26 @@
# GnuRAMage Test Configuration File
[SETTINGS]
# Interval between synchronizations in seconds (short for testing)
sync_interval = 10
# Log level: ERROR, WARN, INFO, DEBUG
log_level = DEBUG
# Verify checksums during sync (test both true and false)
verify_checksums = true
[DIRECTORIES]
# Source directory on hard disk
source_dir = /tmp/ram_disk_test_source
# Target directory on RAM disk
ramdisk_dir = /tmp/ram_disk_test_ramdisk
[EXCLUDE]
# Patterns to exclude from synchronization
# Each line is a pattern in rsync format
*.bak
*.tmp
temp_*
test_ignore_dir/

351
test/run_test.sh Executable file
View file

@ -0,0 +1,351 @@
#!/bin/bash
# Test script for GnuRAMage.sh
# Automatically verifies the correct operation of the program
# Colors for console display
GREEN="\033[0;32m"
RED="\033[0;31m"
YELLOW="\033[0;33m"
RESET="\033[0m"
# Test counters
TESTS_TOTAL=0
TESTS_PASSED=0
TESTS_FAILED=0
# Function to display test headers
print_header() {
echo -e "\n${YELLOW}=====================================${RESET}"
echo -e "${YELLOW}$1${RESET}"
echo -e "${YELLOW}=====================================${RESET}"
}
# Function to report test results
report_test() {
local test_name="$1"
local result="$2"
local message="$3"
TESTS_TOTAL=$((TESTS_TOTAL + 1))
if [ "$result" = "PASS" ]; then
TESTS_PASSED=$((TESTS_PASSED + 1))
echo -e "${GREEN}✓ PASS:${RESET} $test_name - $message"
else
TESTS_FAILED=$((TESTS_FAILED + 1))
echo -e "${RED}✗ FAIL:${RESET} $test_name - $message"
fi
}
# Function to display summary
print_summary() {
echo -e "\n${YELLOW}===== TEST SUMMARY =====${RESET}"
echo -e "Tests executed: $TESTS_TOTAL"
echo -e "${GREEN}Tests passed: $TESTS_PASSED${RESET}"
echo -e "${RED}Tests failed: $TESTS_FAILED${RESET}"
if [ $TESTS_FAILED -eq 0 ]; then
echo -e "\n${GREEN}ALL TESTS PASSED SUCCESSFULLY!${RESET}"
exit 0
else
echo -e "\n${RED}SOME TESTS FAILED!${RESET}"
exit 1
fi
}
# Function to clean up the test environment
cleanup() {
echo "Cleaning up test environment..."
rm -rf /tmp/ram_disk_test_source /tmp/ram_disk_test_ramdisk
rm -f ../gramage_sync_to_disk.sh ../gramage_copy_to_ram.sh
}
# Check if the program exists
check_program_exists() {
print_header "TEST 1: Checking program existence"
if [ -f "../gramage.sh" ]; then
report_test "Program existence" "PASS" "gramage.sh file exists"
else
report_test "Program existence" "FAIL" "gramage.sh file does not exist"
print_summary
exit 1
fi
if [ -x "../gramage.sh" ]; then
report_test "Program permissions" "PASS" "gramage.sh has execution permissions"
else
report_test "Program permissions" "FAIL" "gramage.sh does not have execution permissions"
chmod +x ../gramage.sh
report_test "Fix permissions" "PASS" "Added execution permissions"
fi
}
# Check script generation
check_script_generation() {
print_header "TEST 2: Checking script generation"
# Remove existing scripts
rm -f ../gramage_sync_to_disk.sh ../gramage_copy_to_ram.sh
# Create source directory first to ensure script generation works
mkdir -p /tmp/ram_disk_test_source
# Generate scripts only
../gramage.sh --config gramage_test.ini --script-gen-only
if [ -f "../gramage_sync_to_disk.sh" ]; then
report_test "Generating gramage_sync_to_disk.sh" "PASS" "gramage_sync_to_disk.sh script was generated"
else
report_test "Generating gramage_sync_to_disk.sh" "FAIL" "gramage_sync_to_disk.sh script was not generated"
fi
if [ -f "../gramage_copy_to_ram.sh" ]; then
report_test "Generating gramage_copy_to_ram.sh" "PASS" "gramage_copy_to_ram.sh script was generated"
else
report_test "Generating gramage_copy_to_ram.sh" "FAIL" "gramage_copy_to_ram.sh script was not generated"
fi
if [ -x "../gramage_sync_to_disk.sh" ] && [ -x "../gramage_copy_to_ram.sh" ]; then
report_test "Script permissions" "PASS" "Both scripts have execution permissions"
else
report_test "Script permissions" "FAIL" "Scripts do not have execution permissions"
fi
}
# Test environment and directories
test_environment() {
print_header "TEST 3: Testing environment and directories"
# Generate test environment
./test_generator.sh
if [ -d "/tmp/ram_disk_test_source" ]; then
report_test "Source directory" "PASS" "Directory /tmp/ram_disk_test_source exists"
else
report_test "Source directory" "FAIL" "Directory /tmp/ram_disk_test_source does not exist"
fi
# Check if there are files in the source directory
SOURCE_FILES=$(find /tmp/ram_disk_test_source -type f | wc -l)
if [ "$SOURCE_FILES" -gt 0 ]; then
report_test "Source files" "PASS" "Found $SOURCE_FILES files in source directory"
else
report_test "Source files" "FAIL" "No files in source directory"
fi
}
# Test copying from hard disk to RAM disk
test_copy_to_ramdisk() {
print_header "TEST 4: Testing copying from hard disk to RAM disk"
# Run the copy script
if [ ! -f "../gramage_copy_to_ram.sh" ]; then
# Run the main script to generate the scripts
../gramage.sh --config gramage_test.ini --script-gen-only
fi
../gramage_copy_to_ram.sh --verbose
if [ -d "/tmp/ram_disk_test_ramdisk" ]; then
report_test "RAM disk directory" "PASS" "Directory /tmp/ram_disk_test_ramdisk exists"
else
report_test "RAM disk directory" "FAIL" "Directory /tmp/ram_disk_test_ramdisk does not exist"
fi
# Check if there are files in the RAM disk directory
RAMDISK_FILES=$(find /tmp/ram_disk_test_ramdisk -type f | wc -l)
if [ "$RAMDISK_FILES" -gt 0 ]; then
report_test "Files on RAM disk" "PASS" "Found $RAMDISK_FILES files on RAM disk"
else
report_test "Files on RAM disk" "FAIL" "No files on RAM disk"
fi
}
# Test synchronization from RAM disk to hard disk
test_sync_to_disk() {
print_header "TEST 5: Testing synchronization from RAM disk to hard disk"
# Create an additional file on RAM disk to check synchronization
echo "Test synchronization" > "/tmp/ram_disk_test_ramdisk/test_sync_file.txt"
# Run the synchronization script
if [ ! -f "../gramage_sync_to_disk.sh" ]; then
# Run the main script to generate the scripts
../gramage.sh --config gramage_test.ini --script-gen-only
fi
../gramage_sync_to_disk.sh --verbose
# Check if the test file was synchronized to the hard disk
if [ -f "/tmp/ram_disk_test_source/test_sync_file.txt" ]; then
report_test "File synchronization" "PASS" "Test file was synchronized to the hard disk"
else
report_test "File synchronization" "FAIL" "Test file was not synchronized to the hard disk"
fi
}
# Test file exclusion patterns
test_exclude_patterns() {
print_header "TEST 6: Testing file exclusion patterns"
# Create files that should be excluded
touch "/tmp/ram_disk_test_ramdisk/excluded_file.bak"
touch "/tmp/ram_disk_test_ramdisk/excluded_file.tmp"
touch "/tmp/ram_disk_test_ramdisk/temp_excluded_file.txt"
mkdir -p "/tmp/ram_disk_test_ramdisk/test_ignore_dir"
touch "/tmp/ram_disk_test_ramdisk/test_ignore_dir/file_in_ignored_dir.txt"
# Run the synchronization script
if [ ! -f "../gramage_sync_to_disk.sh" ]; then
# Run the main script to generate the scripts
../gramage.sh --config gramage_test.ini --script-gen-only
fi
../gramage_sync_to_disk.sh --verbose
# Check if files that should be excluded were not synchronized
EXCLUDED_FILES=0
[ ! -f "/tmp/ram_disk_test_source/excluded_file.bak" ] && EXCLUDED_FILES=$((EXCLUDED_FILES + 1))
[ ! -f "/tmp/ram_disk_test_source/excluded_file.tmp" ] && EXCLUDED_FILES=$((EXCLUDED_FILES + 1))
[ ! -f "/tmp/ram_disk_test_source/temp_excluded_file.txt" ] && EXCLUDED_FILES=$((EXCLUDED_FILES + 1))
[ ! -f "/tmp/ram_disk_test_source/test_ignore_dir/file_in_ignored_dir.txt" ] && EXCLUDED_FILES=$((EXCLUDED_FILES + 1))
if [ "$EXCLUDED_FILES" -eq 4 ]; then
report_test "File exclusion" "PASS" "All files were correctly excluded from synchronization"
else
report_test "File exclusion" "FAIL" "Not all files were excluded from synchronization ($EXCLUDED_FILES/4)"
fi
}
# Test main script in one-time mode
test_main_script() {
print_header "TEST 7: Testing main script in one-time mode"
# Clean directories
rm -rf /tmp/ram_disk_test_ramdisk/*
# Run the main script in one-time mode
../gramage.sh --config gramage_test.ini --one-time
# Check if files were copied to RAM disk
RAMDISK_FILES=$(find /tmp/ram_disk_test_ramdisk -type f | wc -l)
if [ "$RAMDISK_FILES" -gt 0 ]; then
report_test "File copying by main script" "PASS" "Found $RAMDISK_FILES files on RAM disk"
else
report_test "File copying by main script" "FAIL" "No files on RAM disk"
fi
# Create an additional file on RAM disk to check synchronization
echo "Testing main script" > "/tmp/ram_disk_test_ramdisk/test_main_script.txt"
# Run the main script again
../gramage.sh --config gramage_test.ini --one-time
# Check if the test file was synchronized to the hard disk
if [ -f "/tmp/ram_disk_test_source/test_main_script.txt" ]; then
report_test "Synchronization by main script" "PASS" "Test file was synchronized to the hard disk"
else
report_test "Synchronization by main script" "FAIL" "Test file was not synchronized to the hard disk"
fi
}
# Test log generation
test_logging() {
print_header "TEST 8: Testing log generation"
# Temporary log files for testing
LOG_FILE="/tmp/ram_disk_test_logs.txt"
ERROR_LOG_FILE="/tmp/ram_disk_test_errors.txt"
# Remove log files if they exist
rm -f "$LOG_FILE" "$ERROR_LOG_FILE"
# Run the script with logging options
../gramage.sh --config gramage_test.ini --one-time --logs "$LOG_FILE" --errors-log "$ERROR_LOG_FILE"
# Check if the log file was created
if [ -f "$LOG_FILE" ]; then
LOG_SIZE=$(wc -c < "$LOG_FILE")
if [ "$LOG_SIZE" -gt 0 ]; then
report_test "Log file creation" "PASS" "Log file was created and contains data (size: $LOG_SIZE bytes)"
else
report_test "Log file creation" "FAIL" "Log file was created but is empty"
fi
else
report_test "Log file creation" "FAIL" "Log file was not created"
fi
# Check if the error log file was created
if [ -f "$ERROR_LOG_FILE" ]; then
ERROR_LOG_SIZE=$(wc -c < "$ERROR_LOG_FILE")
report_test "Error log file creation" "PASS" "Error log file was created (size: $ERROR_LOG_SIZE bytes)"
else
report_test "Error log file creation" "FAIL" "Error log file was not created"
fi
# Check if logs contain expected information
if [ -f "$LOG_FILE" ]; then
if grep -q "INFO" "$LOG_FILE"; then
report_test "INFO log content" "PASS" "Logs contain INFO level entries"
else
report_test "INFO log content" "FAIL" "Logs do not contain INFO level entries"
fi
if grep -q "DEBUG" "$LOG_FILE"; then
report_test "DEBUG log content" "PASS" "Logs contain DEBUG level entries"
else
report_test "DEBUG log content" "FAIL" "Logs do not contain DEBUG level entries"
fi
fi
# Generate an artificial error to test error logs
# Create a read-only file to force an error during synchronization
mkdir -p "/tmp/ram_disk_test_ramdisk/readonly_dir"
touch "/tmp/ram_disk_test_ramdisk/readonly_dir/readonly_file.txt"
chmod 444 "/tmp/ram_disk_test_ramdisk/readonly_dir"
# Run the script again to generate errors
../gramage.sh --config gramage_test.ini --one-time --logs "$LOG_FILE" --errors-log "$ERROR_LOG_FILE"
# Restore permissions
chmod 755 "/tmp/ram_disk_test_ramdisk/readonly_dir"
# Check if error logs contain information about errors
if [ -f "$ERROR_LOG_FILE" ]; then
if grep -q "ERROR\|WARN\|Error log file initialized" "$ERROR_LOG_FILE"; then
report_test "Error log content" "PASS" "Error logs contain appropriate entries"
else
report_test "Error log content" "FAIL" "Error logs do not contain ERROR or WARN level entries"
fi
fi
# Remove log files
rm -f "$LOG_FILE" "$ERROR_LOG_FILE"
}
# Main test function
run_tests() {
echo -e "${YELLOW}=====================================${RESET}"
echo -e "${YELLOW}STARTING GNURAMAGE TESTS${RESET}"
echo -e "${YELLOW}=====================================${RESET}"
# Clean environment before starting tests
cleanup
# Run all tests
check_program_exists
check_script_generation
test_environment
test_copy_to_ramdisk
test_sync_to_disk
test_exclude_patterns
test_main_script
test_logging
# Display summary
print_summary
}
# Run tests
run_tests

172
test/test_generator.sh Executable file
View file

@ -0,0 +1,172 @@
#!/bin/bash
# Test Generator for GnuRAMage
# Creates sample files and directory structures for testing
# Default values
TEST_SOURCE_DIR="/tmp/ram_disk_test_source"
TEST_RAMDISK_DIR="/tmp/ram_disk_test_ramdisk"
FILE_COUNT=20
DIR_COUNT=5
FILE_SIZE="10k" # Default file size in KB
CONFIG_FILE="gnuramage_test.ini"
# Print usage information
print_usage() {
echo "Usage: $0 [OPTIONS]"
echo "Test Generator for GnuRAMage"
echo ""
echo "Options:"
echo " --source <dir> Path to the source directory (default: $TEST_SOURCE_DIR)"
echo " --ramdisk <dir> Path to the RAM disk directory (default: $TEST_RAMDISK_DIR)"
echo " --files <count> Number of files to create (default: $FILE_COUNT)"
echo " --dirs <count> Number of directories to create (default: $DIR_COUNT)"
echo " --size <size> Size of each file (default: $FILE_SIZE)"
echo " --config <file> Path to write the test configuration (default: $CONFIG_FILE)"
echo " --help Display this help message and exit"
echo ""
}
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--source)
TEST_SOURCE_DIR="$2"
shift 2
;;
--ramdisk)
TEST_RAMDISK_DIR="$2"
shift 2
;;
--files)
FILE_COUNT="$2"
shift 2
;;
--dirs)
DIR_COUNT="$2"
shift 2
;;
--size)
FILE_SIZE="$2"
shift 2
;;
--config)
CONFIG_FILE="$2"
shift 2
;;
--help)
print_usage
exit 0
;;
*)
echo "Error: Unknown option $1"
print_usage
exit 1
;;
esac
done
# Create test directories
echo "Creating test directories..."
mkdir -p "$TEST_SOURCE_DIR"
# Note: We don't create the RAM disk directory here as the main script should handle that
# Create random directories
echo "Creating $DIR_COUNT random directories..."
for ((i=1; i<=DIR_COUNT; i++)); do
dir_name="dir_$i"
mkdir -p "$TEST_SOURCE_DIR/$dir_name"
# Create subdirectories with random depth
depth=$((RANDOM % 3 + 1))
subdir="$TEST_SOURCE_DIR/$dir_name"
for ((j=1; j<=depth; j++)); do
subdir="$subdir/subdir_$j"
mkdir -p "$subdir"
done
done
# Create random files
echo "Creating $FILE_COUNT random files..."
for ((i=1; i<=FILE_COUNT; i++)); do
# Decide whether to put the file in a directory or the root
if [ $((RANDOM % 3)) -eq 0 ] || [ $DIR_COUNT -eq 0 ]; then
# Put in root
file_path="$TEST_SOURCE_DIR/file_$i.txt"
else
# Put in a random directory
dir_num=$((RANDOM % DIR_COUNT + 1))
dir_path="$TEST_SOURCE_DIR/dir_$dir_num"
# Check if there are subdirectories
subdirs=($(find "$dir_path" -type d))
if [ ${#subdirs[@]} -gt 1 ]; then
# Pick a random subdirectory
subdir_index=$((RANDOM % ${#subdirs[@]}))
file_path="${subdirs[$subdir_index]}/file_$i.txt"
else
file_path="$dir_path/file_$i.txt"
fi
fi
# Create file with random content
dd if=/dev/urandom of="$file_path" bs="$FILE_SIZE" count=1 status=none
echo "Created: $file_path"
done
# Create a special .rsyncignore file
echo "Creating .rsyncignore file..."
cat > "$TEST_SOURCE_DIR/.rsyncignore" << EOF
# Test ignore patterns
*.bak
*.tmp
temp_*
test_ignore_dir/
EOF
# Create some files that should be ignored
echo "Creating files that should be ignored..."
mkdir -p "$TEST_SOURCE_DIR/test_ignore_dir"
touch "$TEST_SOURCE_DIR/file1.bak"
touch "$TEST_SOURCE_DIR/file2.tmp"
touch "$TEST_SOURCE_DIR/temp_file.txt"
touch "$TEST_SOURCE_DIR/test_ignore_dir/should_be_ignored.txt"
# Create a test config file
echo "Creating test configuration file: $CONFIG_FILE"
cat > "$CONFIG_FILE" << EOF
# GnuRAMage Test Configuration File
[SETTINGS]
# Interval between synchronizations in seconds (short for testing)
sync_interval = 10
# Log level: ERROR, WARN, INFO, DEBUG
log_level = DEBUG
# Verify checksums during sync (test both true and false)
verify_checksums = true
[DIRECTORIES]
# Source directory on hard disk
source_dir = $TEST_SOURCE_DIR
# Target directory on RAM disk
ramdisk_dir = $TEST_RAMDISK_DIR
[EXCLUDE]
# Patterns to exclude from synchronization
# Each line is a pattern in rsync format
*.bak
*.tmp
temp_*
test_ignore_dir/
EOF
echo "Test environment setup complete!"
echo "Created $FILE_COUNT files across $DIR_COUNT directories"
echo ""
echo "To use the test environment, run:"
echo "../gramage.sh --config ../test/$CONFIG_FILE"
echo ""
echo "For a dry run test first, use:"
echo "../gramage.sh --config ../test/$CONFIG_FILE --dry-run --verbose"