homelab infrastructure services
at main 188 lines 5.2 kB view raw
1#!/bin/bash 2 3# Test functions for machine setup scripts 4# Safe testing without system modifications 5 6set -euo pipefail 7 8SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 9 10# Source shared library functions 11source "$SCRIPT_DIR/lib.sh" 12 13test_sheet_numbers() { 14 echo "Testing sheet number generation..." 15 16 # Test cases with expected sheet numbers 17 local tests=( 18 "topsheet:5" # Default tinsnip sheet 19 ) 20 21 for test_case in "${tests[@]}"; do 22 IFS=':' read -r sheet expected <<< "$test_case" 23 actual=$(get_sheet_number "$sheet") 24 25 if [[ "$actual" == "$expected" ]]; then 26 echo "$sheet = $actual" 27 else 28 echo "$sheet = $actual (expected $expected)" 29 fi 30 done 31} 32 33test_uid_calculation() { 34 echo "Testing UID calculation..." 35 36 # Test cases for default sheet (topsheet) - only station which doesn't require registry 37 local tests=( 38 "station:prod:50000" # topsheet sheet (S=5), S=00 39 "station:test:50010" # topsheet sheet (S=5), S=00 40 ) 41 42 for test_case in "${tests[@]}"; do 43 IFS=':' read -r service env expected <<< "$test_case" 44 actual=$(calculate_service_uid "$service" "$env") 45 46 if [[ "$actual" == "$expected" ]]; then 47 echo "$service:$env = $actual" 48 else 49 echo "$service:$env = $actual (expected $expected)" 50 fi 51 done 52 unset TIN_SHEET 53} 54 55test_nfs_path_generation() { 56 echo "Testing NFS path generation..." 57 58 local sheet="topsheet" # Default for testing 59 local tests=( 60 "station:prod:/volume1/tinsnip/${sheet}/station/prod" 61 "gazette:test:/volume1/tinsnip/${sheet}/gazette/test" 62 ) 63 64 for test_case in "${tests[@]}"; do 65 IFS=':' read -r service env expected <<< "$test_case" 66 actual="/volume1/tinsnip/${sheet}/${service}/${env}" 67 68 if [[ "$actual" == "$expected" ]]; then 69 echo "$service:$env = $actual" 70 else 71 echo "$service:$env = $actual (expected $expected)" 72 fi 73 done 74} 75 76test_xdg_paths() { 77 echo "Testing XDG path generation..." 78 79 export XDG_STATE_HOME="$HOME/.local/state" 80 export XDG_DATA_HOME="$HOME/.local/share" 81 export XDG_CONFIG_HOME="$HOME/.config" 82 83 local service="gazette" 84 local sheet="${TIN_SHEET:-topsheet}" # Use env var or default 85 86 echo "XDG paths for $service:" 87 echo " State: ${XDG_STATE_HOME}/${sheet}/@${service}" 88 echo " Data: ${XDG_DATA_HOME}/${sheet}/@${service}" 89 echo " Config: ${XDG_CONFIG_HOME}/${sheet}/@${service}" 90} 91 92calculate_service_ports() { 93 local service_uid="$1" 94 local port_count="${2:-3}" 95 96 for ((i=0; i<port_count; i++)); do 97 echo $((service_uid + i)) 98 done 99} 100 101test_port_allocation() { 102 echo "Testing port allocation..." 103 104 # Test cases: service:env:expected_ports (topsheet sheet = 5) 105 local tests=( 106 "station:prod:50000,50001,50002" 107 "station:test:50010,50011,50012" 108 ) 109 110 for test_case in "${tests[@]}"; do 111 IFS=':' read -r service env expected_ports <<< "$test_case" 112 113 # Calculate UID and ports 114 local service_uid 115 service_uid=$(calculate_service_uid "$service" "$env") 116 117 local actual_ports 118 actual_ports=$(calculate_service_ports "$service_uid" 3 | tr '\n' ',' | sed 's/,$//') 119 120 if [[ "$actual_ports" == "$expected_ports" ]]; then 121 echo "$service:$env ports = $actual_ports" 122 else 123 echo "$service:$env ports = $actual_ports (expected $expected_ports)" 124 fi 125 done 126} 127 128test_port_conflicts() { 129 echo "Testing port conflict detection..." 130 131 # Generate all service port ranges 132 local services=("station:prod" "station:test" "gazette:prod" "gazette:test") 133 local all_ports=() 134 135 for service_env in "${services[@]}"; do 136 IFS=':' read -r service env <<< "$service_env" 137 local service_uid 138 service_uid=$(calculate_service_uid "$service" "$env") 139 140 # Get first 3 ports for this service 141 local ports 142 ports=$(calculate_service_ports "$service_uid" 3) 143 all_ports+=($ports) 144 done 145 146 # Check for duplicates 147 local unique_ports 148 unique_ports=$(printf '%s\n' "${all_ports[@]}" | sort -n | uniq) 149 local total_ports=${#all_ports[@]} 150 local unique_count 151 unique_count=$(echo "$unique_ports" | wc -l) 152 153 if [[ $total_ports -eq $unique_count ]]; then 154 echo "✓ No port conflicts detected ($total_ports unique ports)" 155 else 156 echo "✗ Port conflicts found! $total_ports ports, $unique_count unique" 157 echo "Duplicate ports:" 158 printf '%s\n' "${all_ports[@]}" | sort -n | uniq -d 159 fi 160} 161 162main() { 163 echo "Machine Setup Function Tests" 164 echo "============================" 165 echo 166 167 test_sheet_numbers 168 echo 169 170 test_uid_calculation 171 echo 172 173 test_nfs_path_generation 174 echo 175 176 test_xdg_paths 177 echo 178 179 test_port_allocation 180 echo 181 182 test_port_conflicts 183 echo 184 185 echo "All function tests completed!" 186} 187 188main "$@"