homelab infrastructure services
at fix-docker-install 140 lines 4.2 kB view raw
1#!/bin/bash 2 3# Test edge cases for port allocation strategy 4 5set -euo pipefail 6 7SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 8 9# Source the main functions 10source "$SCRIPT_DIR/validate_functions.sh" 11 12log() { 13 echo "[Port Edge Case Tests] $*" 14} 15 16test_future_service_ports() { 17 log "Testing future service scalability..." 18 19 # Test adding a hypothetical third service (service=2) 20 # UID would be 11200 (prod), 11210 (test) 21 22 local future_service_prod_uid="11200" 23 local future_service_test_uid="11210" 24 25 local prod_ports 26 prod_ports=$(calculate_service_ports "$future_service_prod_uid" 5 | tr '\n' ',' | sed 's/,$//') 27 28 local test_ports 29 test_ports=$(calculate_service_ports "$future_service_test_uid" 5 | tr '\n' ',' | sed 's/,$//') 30 31 echo "Future service ports:" 32 echo " Prod: $prod_ports" 33 echo " Test: $test_ports" 34 35 # Verify no conflicts with existing services 36 local existing_ports=("11000" "11001" "11002" "11010" "11011" "11012" "11100" "11101" "11102" "11110" "11111" "11112") 37 local new_ports=("11200" "11201" "11202" "11203" "11204" "11210" "11211" "11212" "11213" "11214") 38 39 local conflicts=0 40 for new_port in "${new_ports[@]}"; do 41 for existing_port in "${existing_ports[@]}"; do 42 if [[ "$new_port" == "$existing_port" ]]; then 43 echo "✗ Conflict: Port $new_port already in use" 44 ((conflicts++)) 45 fi 46 done 47 done 48 49 if [[ $conflicts -eq 0 ]]; then 50 echo "✓ No conflicts with future service ports" 51 else 52 echo "✗ Found $conflicts port conflicts" 53 fi 54} 55 56test_many_ports_per_service() { 57 log "Testing services with many ports..." 58 59 # Test a service that needs 10 ports 60 local service_uid="11010" # station-test 61 local port_count=10 62 63 local ports 64 ports=$(calculate_service_ports "$service_uid" "$port_count") 65 66 echo "Station-test with $port_count ports:" 67 echo "$ports" | tr '\n' ',' | sed 's/,$//' | fold -w 50 68 69 # Verify they're sequential 70 local expected_start=$service_uid 71 local actual_ports 72 actual_ports=($ports) 73 74 local sequential=true 75 for ((i=0; i<port_count; i++)); do 76 local expected=$((expected_start + i)) 77 if [[ "${actual_ports[$i]}" != "$expected" ]]; then 78 echo "✗ Port sequence broken at index $i: expected $expected, got ${actual_ports[$i]}" 79 sequential=false 80 fi 81 done 82 83 if [[ "$sequential" == "true" ]]; then 84 echo "✓ All $port_count ports are sequential" 85 fi 86} 87 88test_port_range_boundaries() { 89 log "Testing port range boundaries..." 90 91 # Check that our UIDs don't conflict with well-known ports 92 local services=("11000" "11010" "11100" "11110") 93 94 for service_uid in "${services[@]}"; do 95 if [[ $service_uid -lt 1024 ]]; then 96 echo "✗ UID $service_uid conflicts with privileged port range (<1024)" 97 elif [[ $service_uid -lt 49152 ]]; then 98 echo "✓ UID $service_uid in safe user port range (1024-49151)" 99 else 100 echo "⚠ UID $service_uid in dynamic/ephemeral range (49152-65535)" 101 fi 102 done 103 104 # Check we're not in common service port ranges 105 local common_ports=(22 23 25 53 80 110 143 443 993 995 1433 3306 5432 6379 11211) 106 local our_ports=(11000 11001 11002 11010 11011 11012 11100 11101 11102 11110 11111 11112) 107 108 local conflicts=0 109 for our_port in "${our_ports[@]}"; do 110 for common_port in "${common_ports[@]}"; do 111 if [[ $our_port -eq $common_port ]]; then 112 echo "✗ Our port $our_port conflicts with common service port" 113 ((conflicts++)) 114 fi 115 done 116 done 117 118 if [[ $conflicts -eq 0 ]]; then 119 echo "✓ No conflicts with common service ports" 120 fi 121} 122 123main() { 124 echo "Port Allocation Edge Case Tests" 125 echo "===============================" 126 echo 127 128 test_future_service_ports 129 echo 130 131 test_many_ports_per_service 132 echo 133 134 test_port_range_boundaries 135 echo 136 137 log "All edge case tests completed!" 138} 139 140main "$@"