#!/bin/bash # Test functions for machine setup scripts # Safe testing without system modifications set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Source shared library functions source "$SCRIPT_DIR/lib.sh" test_namespace_numbers() { echo "Testing namespace number generation..." # Test cases with expected namespace numbers local tests=( "dynamicalsystem:1" # Should hash to 1 (backward compatible) "mycompany:7" # Example custom namespace "acmecorp:3" # Another example ) for test_case in "${tests[@]}"; do IFS=':' read -r namespace expected <<< "$test_case" actual=$(get_namespace_number "$namespace") if [[ "$actual" == "$expected" ]]; then echo "✓ $namespace = $actual" else echo "✗ $namespace = $actual (expected $expected)" fi done } test_uid_calculation() { echo "Testing UID calculation..." # Test cases for default namespace (dynamicalsystem) local tests=( "station:prod:11000" # dynamicalsystem namespace (N=1), S=0 "station:test:11010" # dynamicalsystem namespace (N=1), S=0 "gazette:prod:11100" # dynamicalsystem namespace (N=1), S=1 "gazette:test:11110" # dynamicalsystem namespace (N=1), S=1 ) for test_case in "${tests[@]}"; do IFS=':' read -r service env expected <<< "$test_case" actual=$(calculate_service_uid "$service" "$env") if [[ "$actual" == "$expected" ]]; then echo "✓ $service:$env = $actual" else echo "✗ $service:$env = $actual (expected $expected)" fi done # Test with custom namespace echo "Testing with custom namespace (mycompany)..." export TIN_NAMESPACE="mycompany" # mycompany hashes to N=7 local custom_tests=( "station:prod:17000" # mycompany namespace (N=7), S=0 "gazette:prod:17100" # mycompany namespace (N=7), S=1 ) for test_case in "${custom_tests[@]}"; do IFS=':' read -r service env expected <<< "$test_case" actual=$(calculate_service_uid "$service" "$env") if [[ "$actual" == "$expected" ]]; then echo "✓ [mycompany] $service:$env = $actual" else echo "✗ [mycompany] $service:$env = $actual (expected $expected)" fi done # Reset namespace unset TIN_NAMESPACE } test_nfs_path_generation() { echo "Testing NFS path generation..." local namespace="dynamicalsystem" # Default for testing local tests=( "station:prod:/volume1/${namespace}/station/prod" "gazette:test:/volume1/${namespace}/gazette/test" ) for test_case in "${tests[@]}"; do IFS=':' read -r service env expected <<< "$test_case" actual="/volume1/${namespace}/${service}/${env}" if [[ "$actual" == "$expected" ]]; then echo "✓ $service:$env = $actual" else echo "✗ $service:$env = $actual (expected $expected)" fi done } test_xdg_paths() { echo "Testing XDG path generation..." export XDG_STATE_HOME="$HOME/.local/state" export XDG_DATA_HOME="$HOME/.local/share" export XDG_CONFIG_HOME="$HOME/.config" local service="gazette" local namespace="${TIN_NAMESPACE:-dynamicalsystem}" # Use env var or default echo "XDG paths for $service:" echo " State: ${XDG_STATE_HOME}/${namespace}/@${service}" echo " Data: ${XDG_DATA_HOME}/${namespace}/@${service}" echo " Config: ${XDG_CONFIG_HOME}/${namespace}/@${service}" } calculate_service_ports() { local service_uid="$1" local port_count="${2:-3}" for ((i=0; i