[#4] 增加端到端测试

This commit is contained in:
root 2025-09-16 08:04:13 +00:00
parent 1fbf725d50
commit a9281639bf
10 changed files with 608 additions and 0 deletions

View File

@ -0,0 +1,16 @@
services:
bind9:
image: argus-bind9:latest
container_name: argus-bind9-test
ports:
- "53:53/tcp"
- "53:53/udp"
volumes:
- ./private:/private
restart: unless-stopped
networks:
- bind-test-network
networks:
bind-test-network:
driver: bridge

View File

@ -0,0 +1,16 @@
$TTL 604800
@ IN SOA ns1.argus.com. admin.argus.com. (
3 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
; 定义 DNS 服务器
@ IN NS ns1.argus.com.
; 定义 ns1 主机
ns1 IN A 127.0.0.1
; 定义 web 指向 192.168.1.100
web IN A 192.168.1.100

View File

@ -0,0 +1,16 @@
$TTL 604800
@ IN SOA ns1.argus.com. admin.argus.com. (
2 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
; 定义 DNS 服务器
@ IN NS ns1.argus.com.
; 定义 ns1 主机
ns1 IN A 127.0.0.1
; 定义 web 指向 12.4.5.6
web IN A 12.4.5.6

View File

@ -0,0 +1,4 @@
zone "argus.com" {
type master;
file "/etc/bind/db.argus.com";
};

View File

@ -0,0 +1,112 @@
#!/bin/bash
# End-to-end test for BIND9 DNS server
# This script runs all tests in sequence to validate the complete functionality
# Usage: ./00_e2e_test.sh
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo "=========================================="
echo "BIND9 DNS Server End-to-End Test Suite"
echo "=========================================="
# Track test results
total_tests=0
passed_tests=0
failed_tests=0
# Function to run a test step
run_test_step() {
local step_name="$1"
local script_name="$2"
local description="$3"
echo ""
echo "[$step_name] $description"
echo "$(printf '=%.0s' {1..50})"
((total_tests++))
if [ ! -f "$SCRIPT_DIR/$script_name" ]; then
echo "✗ Test script not found: $script_name"
((failed_tests++))
return 1
fi
# Make sure script is executable
chmod +x "$SCRIPT_DIR/$script_name"
# Run the test
echo "Executing: $SCRIPT_DIR/$script_name"
if "$SCRIPT_DIR/$script_name"; then
echo "$step_name completed successfully"
((passed_tests++))
return 0
else
echo "$step_name failed"
((failed_tests++))
return 1
fi
}
# Cleanup any previous test environment (but preserve the Docker image)
echo ""
echo "[SETUP] Cleaning up any previous test environment..."
if [ -f "$SCRIPT_DIR/05_cleanup.sh" ]; then
chmod +x "$SCRIPT_DIR/05_cleanup.sh"
"$SCRIPT_DIR/05_cleanup.sh" || true
fi
echo ""
echo "Starting BIND9 DNS server end-to-end test sequence..."
# Test sequence
run_test_step "TEST-01" "01_start_container.sh" "Start BIND9 container" || true
run_test_step "TEST-02" "02_dig_test.sh" "Initial DNS resolution test" || true
run_test_step "TEST-03" "03_reload_test.sh" "Configuration reload with IP modification" || true
run_test_step "TEST-04" "04_persistence_test.sh" "Configuration persistence after restart" || true
# Final cleanup (but preserve logs for review)
echo ""
echo "[CLEANUP] Cleaning up test environment..."
run_test_step "CLEANUP" "05_cleanup.sh" "Clean up containers and networks" || true
# Test summary
echo ""
echo "=========================================="
echo "TEST SUMMARY"
echo "=========================================="
echo "Total tests: $total_tests"
echo "Passed: $passed_tests"
echo "Failed: $failed_tests"
if [ $failed_tests -eq 0 ]; then
echo ""
echo "✅ ALL TESTS PASSED!"
echo ""
echo "BIND9 DNS server functionality validated:"
echo " ✓ Container startup and basic functionality"
echo " ✓ DNS resolution for configured domains"
echo " ✓ Configuration modification and reload"
echo " ✓ Configuration persistence across restarts"
echo " ✓ Cleanup and resource management"
echo ""
echo "The BIND9 DNS server is ready for production use."
exit 0
else
echo ""
echo "❌ SOME TESTS FAILED!"
echo ""
echo "Please review the test output above to identify and fix issues."
echo "You may need to:"
echo " - Check Docker installation and permissions"
echo " - Verify network connectivity"
echo " - Review BIND9 configuration files"
echo " - Check system resources and port availability"
exit 1
fi

View File

@ -0,0 +1,38 @@
#!/bin/bash
# Start BIND9 test container
# Usage: ./01_start_container.sh
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TEST_DIR="$(dirname "$SCRIPT_DIR")"
cd "$TEST_DIR"
echo "Starting BIND9 test container..."
# Ensure private directory exists with proper permissions
mkdir -p private
chmod 777 private
# Start the container
docker compose up -d
echo "Waiting for container to be ready..."
sleep 5
# Check if container is running
if docker compose ps | grep -q "Up"; then
echo "✓ Container started successfully"
echo "Container status:"
docker compose ps
else
echo "✗ Failed to start container"
docker compose logs
exit 1
fi
echo ""
echo "BIND9 test environment is ready!"
echo "DNS server listening on localhost:53"

View File

@ -0,0 +1,72 @@
#!/bin/bash
# Test DNS resolution using dig
# Usage: ./02_dig_test.sh
set -e
echo "Testing DNS resolution with dig..."
# Function to test DNS query
test_dns_query() {
local hostname="$1"
local expected_ip="$2"
local description="$3"
echo ""
echo "Testing: $description"
echo "Query: $hostname.argus.com"
echo "Expected IP: $expected_ip"
# Perform dig query
result=$(dig @localhost $hostname.argus.com A +short 2>/dev/null || echo "QUERY_FAILED")
if [ "$result" = "QUERY_FAILED" ]; then
echo "✗ DNS query failed"
return 1
elif [ "$result" = "$expected_ip" ]; then
echo "✓ DNS query successful: $result"
return 0
else
echo "✗ DNS query returned unexpected result: $result"
return 1
fi
}
# Check if dig is available
if ! command -v dig &> /dev/null; then
echo "Installing dig (dnsutils)..."
apt-get update && apt-get install -y dnsutils
fi
# Check if container is running
if ! docker compose ps | grep -q "Up"; then
echo "Error: BIND9 container is not running"
echo "Please start the container first with: ./01_start_container.sh"
exit 1
fi
echo "=== DNS Resolution Tests ==="
# Test cases based on current configuration
failed_tests=0
# Test ns1.argus.com -> 127.0.0.1
if ! test_dns_query "ns1" "127.0.0.1" "Name server resolution"; then
((failed_tests++))
fi
# Test web.argus.com -> 12.4.5.6
if ! test_dns_query "web" "12.4.5.6" "Web server resolution"; then
((failed_tests++))
fi
echo ""
echo "=== Test Summary ==="
if [ $failed_tests -eq 0 ]; then
echo "✓ All DNS tests passed!"
exit 0
else
echo "$failed_tests test(s) failed"
exit 1
fi

View File

@ -0,0 +1,112 @@
#!/bin/bash
# Test DNS configuration reload with IP modification
# Usage: ./03_reload_test.sh
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TEST_DIR="$(dirname "$SCRIPT_DIR")"
echo "=== DNS Configuration Reload Test ==="
# Check if container is running
if ! docker compose ps | grep -q "Up"; then
echo "Error: BIND9 container is not running"
echo "Please start the container first with: ./01_start_container.sh"
exit 1
fi
# Check if dig is available
if ! command -v dig &> /dev/null; then
echo "Installing dig (dnsutils)..."
apt-get update && apt-get install -y dnsutils
fi
# Function to test DNS query
test_dns_query() {
local hostname="$1"
local expected_ip="$2"
local description="$3"
echo "Testing: $description"
echo "Query: $hostname.argus.com -> Expected: $expected_ip"
result=$(dig @localhost $hostname.argus.com A +short 2>/dev/null || echo "QUERY_FAILED")
if [ "$result" = "$expected_ip" ]; then
echo "$result"
return 0
else
echo "✗ Got: $result, Expected: $expected_ip"
return 1
fi
}
echo ""
echo "Step 1: Testing initial DNS configuration..."
# Test initial configuration
if ! test_dns_query "web" "12.4.5.6" "Initial web.argus.com resolution"; then
echo "Initial DNS test failed"
exit 1
fi
echo ""
echo "Step 2: Modifying DNS configuration..."
# Backup original configuration
cp "$TEST_DIR/private/argus/bind/db.argus.com" "$TEST_DIR/private/argus/bind/db.argus.com.backup" 2>/dev/null || true
# Create new configuration with modified IP
DB_FILE="$TEST_DIR/private/argus/bind/db.argus.com"
# Check if persistent config exists, if not use from container
if [ ! -f "$DB_FILE" ]; then
echo "Persistent config not found, copying from container..."
docker compose exec bind9 cp /etc/bind/db.argus.com /private/argus/bind/db.argus.com
docker compose exec bind9 chown bind:bind /private/argus/bind/db.argus.com
fi
# Modify the IP address (12.4.5.6 -> 192.168.1.100)
sed -i 's/12\.4\.5\.6/192.168.1.100/g' "$DB_FILE"
# Increment serial number for DNS cache invalidation
current_serial=$(grep -o "2[[:space:]]*;" "$DB_FILE" | grep -o "2")
new_serial=$((current_serial + 1))
sed -i "s/2[[:space:]]*;/${new_serial} ;/" "$DB_FILE"
echo "Modified configuration:"
echo "- Changed web.argus.com IP: 12.4.5.6 -> 192.168.1.100"
echo "- Updated serial number: $current_serial -> $new_serial"
echo ""
echo "Step 3: Reloading BIND9 configuration..."
# Reload BIND9 configuration
docker compose exec bind9 /usr/local/bin/reload-bind9.sh
echo "Configuration reloaded"
# Wait a moment for changes to take effect
sleep 3
echo ""
echo "Step 4: Testing modified DNS configuration..."
# Test modified configuration
if ! test_dns_query "web" "192.168.1.100" "Modified web.argus.com resolution"; then
echo "Modified DNS test failed"
exit 1
fi
# Also verify ns1 still works
if ! test_dns_query "ns1" "127.0.0.1" "ns1.argus.com still working"; then
echo "ns1 DNS test failed after reload"
exit 1
fi
echo ""
echo "✓ DNS configuration reload test completed successfully!"
echo "✓ IP address changed from 12.4.5.6 to 192.168.1.100"
echo "✓ Configuration persisted and reloaded correctly"

View File

@ -0,0 +1,115 @@
#!/bin/bash
# Test configuration persistence after container restart
# Usage: ./04_persistence_test.sh
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TEST_DIR="$(dirname "$SCRIPT_DIR")"
echo "=== Configuration Persistence Test ==="
# Check if dig is available
if ! command -v dig &> /dev/null; then
echo "Installing dig (dnsutils)..."
apt-get update && apt-get install -y dnsutils
fi
# Function to test DNS query
test_dns_query() {
local hostname="$1"
local expected_ip="$2"
local description="$3"
echo "Testing: $description"
echo "Query: $hostname.argus.com -> Expected: $expected_ip"
result=$(dig @localhost $hostname.argus.com A +short 2>/dev/null || echo "QUERY_FAILED")
if [ "$result" = "$expected_ip" ]; then
echo "$result"
return 0
else
echo "✗ Got: $result, Expected: $expected_ip"
return 1
fi
}
echo ""
echo "Step 1: Stopping current container..."
# Stop the container
docker compose down
echo "Container stopped"
echo ""
echo "Step 2: Verifying persistent configuration exists..."
# Check if modified configuration exists
DB_FILE="$TEST_DIR/private/argus/bind/db.argus.com"
if [ ! -f "$DB_FILE" ]; then
echo "✗ Persistent configuration file not found: $DB_FILE"
exit 1
fi
# Check if the modified IP is in the configuration
if grep -q "192.168.1.100" "$DB_FILE"; then
echo "✓ Modified IP (192.168.1.100) found in persistent configuration"
else
echo "✗ Modified IP not found in persistent configuration"
echo "Configuration content:"
cat "$DB_FILE"
exit 1
fi
echo ""
echo "Step 3: Restarting container with persistent configuration..."
# Start the container again
docker compose up -d
echo "Waiting for container to be ready..."
sleep 5
# Check if container is running
if ! docker compose ps | grep -q "Up"; then
echo "✗ Failed to restart container"
docker compose logs
exit 1
fi
echo "✓ Container restarted successfully"
echo ""
echo "Step 4: Testing DNS resolution after restart..."
# Wait a bit more for DNS to be fully ready
sleep 5
# Test that the modified configuration is still active
if ! test_dns_query "web" "192.168.1.100" "Persistent web.argus.com resolution"; then
echo "✗ Persistent configuration test failed"
exit 1
fi
# Also verify ns1 still works
if ! test_dns_query "ns1" "127.0.0.1" "ns1.argus.com still working"; then
echo "✗ ns1 DNS test failed after restart"
exit 1
fi
echo ""
echo "Step 5: Verifying configuration files are linked correctly..."
# Check that the persistent files are properly linked
echo "Checking file links in container:"
docker compose exec bind9 ls -la /etc/bind/named.conf.local /etc/bind/db.argus.com
echo ""
echo "✓ Configuration persistence test completed successfully!"
echo "✓ Modified IP (192.168.1.100) persisted after container restart"
echo "✓ Configuration files properly linked to persistent storage"
echo "✓ DNS resolution working correctly with persisted configuration"

View File

@ -0,0 +1,107 @@
#!/bin/bash
# Clean up test environment and containers
# Usage: ./05_cleanup.sh [--full]
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TEST_DIR="$(dirname "$SCRIPT_DIR")"
# Parse command line arguments
FULL_CLEANUP=false
while [[ $# -gt 0 ]]; do
case $1 in
--full)
FULL_CLEANUP=true
shift
;;
*)
echo "Unknown option: $1"
echo "Usage: $0 [--full]"
echo " --full: Also remove persistent data and Docker image"
exit 1
;;
esac
done
cd "$TEST_DIR"
echo "=== Cleaning up BIND9 test environment ==="
echo ""
echo "Step 1: Stopping and removing containers..."
# Stop and remove containers
docker compose down -v
echo "✓ Containers stopped and removed"
echo ""
echo "Step 2: Removing Docker networks..."
# Clean up networks
docker network prune -f > /dev/null 2>&1 || true
echo "✓ Docker networks cleaned"
if [ "$FULL_CLEANUP" = true ]; then
echo ""
echo "Step 3: Removing persistent data..."
# Remove persistent data directory
if [ -d "private" ]; then
rm -rf private
echo "✓ Persistent data directory removed"
else
echo "✓ No persistent data directory found"
fi
echo ""
echo "Step 4: Removing Docker image..."
# Remove the test image if it exists
if docker images --format "{{.Repository}}:{{.Tag}}" | grep -q "^argus-bind9:latest$"; then
docker rmi argus-bind9:latest
echo "✓ Docker image 'argus-bind9:latest' removed"
else
echo "✓ Docker image 'argus-bind9:latest' not found"
fi
echo ""
echo "Step 5: Cleaning up unused Docker resources..."
# Clean up any dangling images and unused volumes
docker system prune -f > /dev/null 2>&1 || true
echo "✓ Docker system cleaned"
else
echo ""
echo "Step 3: Preserving persistent data and Docker image..."
echo "✓ Persistent data preserved in: private/"
echo "✓ Docker image 'argus-bind9:latest' preserved"
echo ""
echo "To perform full cleanup including persistent data and image, run:"
echo " $0 --full"
fi
echo ""
echo "=== Cleanup Summary ==="
echo "✓ Containers stopped and removed"
echo "✓ Docker networks cleaned"
if [ "$FULL_CLEANUP" = true ]; then
echo "✓ Persistent data removed"
echo "✓ Docker image removed"
echo "✓ System resources cleaned"
echo ""
echo "Full cleanup completed! Test environment completely removed."
else
echo "✓ Persistent data preserved"
echo "✓ Docker image preserved"
echo ""
echo "Basic cleanup completed! Run './01_start_container.sh' to restart testing."
fi
echo ""
echo "Test environment cleanup finished."