homelab infrastructure services
at fix-docker-install 217 lines 6.3 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_namespace_numbers() { 14 echo "Testing namespace number generation..." 15 16 # Test cases with expected namespace numbers 17 local tests=( 18 "dynamicalsystem:1" # Should hash to 1 (backward compatible) 19 "mycompany:7" # Example custom namespace 20 "acmecorp:3" # Another example 21 ) 22 23 for test_case in "${tests[@]}"; do 24 IFS=':' read -r namespace expected <<< "$test_case" 25 actual=$(get_namespace_number "$namespace") 26 27 if [[ "$actual" == "$expected" ]]; then 28 echo "$namespace = $actual" 29 else 30 echo "$namespace = $actual (expected $expected)" 31 fi 32 done 33} 34 35test_uid_calculation() { 36 echo "Testing UID calculation..." 37 38 # Test cases for default namespace (dynamicalsystem) 39 local tests=( 40 "station:prod:11000" # dynamicalsystem namespace (N=1), S=0 41 "station:test:11010" # dynamicalsystem namespace (N=1), S=0 42 "gazette:prod:11100" # dynamicalsystem namespace (N=1), S=1 43 "gazette:test:11110" # dynamicalsystem namespace (N=1), S=1 44 ) 45 46 for test_case in "${tests[@]}"; do 47 IFS=':' read -r service env expected <<< "$test_case" 48 actual=$(calculate_service_uid "$service" "$env") 49 50 if [[ "$actual" == "$expected" ]]; then 51 echo "$service:$env = $actual" 52 else 53 echo "$service:$env = $actual (expected $expected)" 54 fi 55 done 56 57 # Test with custom namespace 58 echo "Testing with custom namespace (mycompany)..." 59 export TIN_NAMESPACE="mycompany" 60 61 # mycompany hashes to N=7 62 local custom_tests=( 63 "station:prod:17000" # mycompany namespace (N=7), S=0 64 "gazette:prod:17100" # mycompany namespace (N=7), S=1 65 ) 66 67 for test_case in "${custom_tests[@]}"; do 68 IFS=':' read -r service env expected <<< "$test_case" 69 actual=$(calculate_service_uid "$service" "$env") 70 71 if [[ "$actual" == "$expected" ]]; then 72 echo "✓ [mycompany] $service:$env = $actual" 73 else 74 echo "✗ [mycompany] $service:$env = $actual (expected $expected)" 75 fi 76 done 77 78 # Reset namespace 79 unset TIN_NAMESPACE 80} 81 82test_nfs_path_generation() { 83 echo "Testing NFS path generation..." 84 85 local namespace="dynamicalsystem" # Default for testing 86 local tests=( 87 "station:prod:/volume1/${namespace}/station/prod" 88 "gazette:test:/volume1/${namespace}/gazette/test" 89 ) 90 91 for test_case in "${tests[@]}"; do 92 IFS=':' read -r service env expected <<< "$test_case" 93 actual="/volume1/${namespace}/${service}/${env}" 94 95 if [[ "$actual" == "$expected" ]]; then 96 echo "$service:$env = $actual" 97 else 98 echo "$service:$env = $actual (expected $expected)" 99 fi 100 done 101} 102 103test_xdg_paths() { 104 echo "Testing XDG path generation..." 105 106 export XDG_STATE_HOME="$HOME/.local/state" 107 export XDG_DATA_HOME="$HOME/.local/share" 108 export XDG_CONFIG_HOME="$HOME/.config" 109 110 local service="gazette" 111 local namespace="${TIN_NAMESPACE:-dynamicalsystem}" # Use env var or default 112 113 echo "XDG paths for $service:" 114 echo " State: ${XDG_STATE_HOME}/${namespace}/@${service}" 115 echo " Data: ${XDG_DATA_HOME}/${namespace}/@${service}" 116 echo " Config: ${XDG_CONFIG_HOME}/${namespace}/@${service}" 117} 118 119calculate_service_ports() { 120 local service_uid="$1" 121 local port_count="${2:-3}" 122 123 for ((i=0; i<port_count; i++)); do 124 echo $((service_uid + i)) 125 done 126} 127 128test_port_allocation() { 129 echo "Testing port allocation..." 130 131 # Test cases: service:env:expected_ports 132 local tests=( 133 "station:prod:11000,11001,11002" 134 "station:test:11010,11011,11012" 135 "gazette:prod:11100,11101,11102" 136 "gazette:test:11110,11111,11112" 137 ) 138 139 for test_case in "${tests[@]}"; do 140 IFS=':' read -r service env expected_ports <<< "$test_case" 141 142 # Calculate UID and ports 143 local service_uid 144 service_uid=$(calculate_service_uid "$service" "$env") 145 146 local actual_ports 147 actual_ports=$(calculate_service_ports "$service_uid" 3 | tr '\n' ',' | sed 's/,$//') 148 149 if [[ "$actual_ports" == "$expected_ports" ]]; then 150 echo "$service:$env ports = $actual_ports" 151 else 152 echo "$service:$env ports = $actual_ports (expected $expected_ports)" 153 fi 154 done 155} 156 157test_port_conflicts() { 158 echo "Testing port conflict detection..." 159 160 # Generate all service port ranges 161 local services=("station:prod" "station:test" "gazette:prod" "gazette:test") 162 local all_ports=() 163 164 for service_env in "${services[@]}"; do 165 IFS=':' read -r service env <<< "$service_env" 166 local service_uid 167 service_uid=$(calculate_service_uid "$service" "$env") 168 169 # Get first 3 ports for this service 170 local ports 171 ports=$(calculate_service_ports "$service_uid" 3) 172 all_ports+=($ports) 173 done 174 175 # Check for duplicates 176 local unique_ports 177 unique_ports=$(printf '%s\n' "${all_ports[@]}" | sort -n | uniq) 178 local total_ports=${#all_ports[@]} 179 local unique_count 180 unique_count=$(echo "$unique_ports" | wc -l) 181 182 if [[ $total_ports -eq $unique_count ]]; then 183 echo "✓ No port conflicts detected ($total_ports unique ports)" 184 else 185 echo "✗ Port conflicts found! $total_ports ports, $unique_count unique" 186 echo "Duplicate ports:" 187 printf '%s\n' "${all_ports[@]}" | sort -n | uniq -d 188 fi 189} 190 191main() { 192 echo "Machine Setup Function Tests" 193 echo "============================" 194 echo 195 196 test_namespace_numbers 197 echo 198 199 test_uid_calculation 200 echo 201 202 test_nfs_path_generation 203 echo 204 205 test_xdg_paths 206 echo 207 208 test_port_allocation 209 echo 210 211 test_port_conflicts 212 echo 213 214 echo "All function tests completed!" 215} 216 217main "$@"