prvng_core/nulib/tests/test_services.nu
2025-10-07 10:32:04 +01:00

323 lines
8.8 KiB
Plaintext

#!/usr/bin/env nu
# Service Management System Tests
use ../lib_provisioning/services/mod.nu *
# Test service registry loading
export def test-service-registry-loading [] {
print "Testing: Service registry loading"
try {
let registry = (load-service-registry)
assert ($registry | is-not-empty) "Registry should not be empty"
assert ("orchestrator" in ($registry | columns)) "Orchestrator should be in registry"
print "✅ Service registry loads correctly"
true
} catch {
print "❌ Failed to load service registry"
false
}
}
# Test service definition retrieval
export def test-service-definition [] {
print "Testing: Service definition retrieval"
try {
let orchestrator = (get-service-definition "orchestrator")
assert ($orchestrator.name == "orchestrator") "Service name should match"
assert ($orchestrator.type == "platform") "Service type should be platform"
assert ($orchestrator.deployment.mode == "binary") "Deployment mode should be binary"
print "✅ Service definition retrieval works"
true
} catch {
print "❌ Failed to get service definition"
false
}
}
# Test dependency resolution
export def test-dependency-resolution [] {
print "Testing: Dependency resolution"
try {
# Test with control-center (depends on orchestrator)
let deps = (resolve-dependencies "control-center")
assert ("orchestrator" in $deps) "Should resolve orchestrator dependency"
print "✅ Dependency resolution works"
true
} catch {
print "❌ Dependency resolution failed"
false
}
}
# Test dependency graph validation
export def test-dependency-graph [] {
print "Testing: Dependency graph validation"
try {
let validation = (validate-dependency-graph)
assert ($validation.valid) "Dependency graph should be valid"
assert (not $validation.has_cycles) "Should not have cycles"
print "✅ Dependency graph is valid"
true
} catch {
print "❌ Dependency graph validation failed"
false
}
}
# Test startup order calculation
export def test-startup-order [] {
print "Testing: Startup order calculation"
try {
let services = ["control-center", "orchestrator"]
let order = (get-startup-order $services)
# Orchestrator should come before control-center
let orchestrator_idx = ($order | enumerate | where item == "orchestrator" | get index | get 0)
let control_center_idx = ($order | enumerate | where item == "control-center" | get index | get 0)
assert ($orchestrator_idx < $control_center_idx) "Orchestrator should start before control-center"
print "✅ Startup order calculation works"
true
} catch {
print "❌ Startup order calculation failed"
false
}
}
# Test service prerequisites validation
export def test-prerequisites-validation [] {
print "Testing: Prerequisites validation"
try {
let validation = (validate-service-prerequisites "orchestrator")
assert ("valid" in $validation) "Validation should have valid field"
assert ("can_start" in $validation) "Validation should have can_start field"
print "✅ Prerequisites validation works"
true
} catch {
print "❌ Prerequisites validation failed"
false
}
}
# Test conflict detection
export def test-conflict-detection [] {
print "Testing: Conflict detection"
try {
let conflicts = (check-service-conflicts "coredns")
assert ("has_conflicts" in $conflicts) "Should have has_conflicts field"
print "✅ Conflict detection works"
true
} catch {
print "❌ Conflict detection failed"
false
}
}
# Test required services check
export def test-required-services-check [] {
print "Testing: Required services check"
try {
let check = (check-required-services "server")
assert ("required_services" in $check) "Should have required_services field"
assert ("all_running" in $check) "Should have all_running field"
assert ("can_auto_start" in $check) "Should have can_auto_start field"
# Orchestrator should be required for server operations
assert ("orchestrator" in $check.required_services) "Orchestrator should be required for server ops"
print "✅ Required services check works"
true
} catch {
print "❌ Required services check failed"
false
}
}
# Test all services validation
export def test-all-services-validation [] {
print "Testing: All services validation"
try {
let validation = (validate-all-services)
assert ($validation.total_services > 0) "Should have services"
assert ("valid_services" in $validation) "Should have valid_services count"
print "✅ All services validation works"
true
} catch {
print "❌ All services validation failed"
false
}
}
# Test readiness report
export def test-readiness-report [] {
print "Testing: Readiness report"
try {
let report = (get-readiness-report)
assert ($report.total_services > 0) "Should have services"
assert ("running_services" in $report) "Should have running count"
assert ("services" in $report) "Should have services list"
print "✅ Readiness report works"
true
} catch {
print "❌ Readiness report failed"
false
}
}
# Test dependency tree
export def test-dependency-tree [] {
print "Testing: Dependency tree generation"
try {
let tree = (get-dependency-tree "control-center")
assert ($tree.service == "control-center") "Root should be control-center"
assert ("dependencies" in $tree) "Should have dependencies field"
print "✅ Dependency tree generation works"
true
} catch {
print "❌ Dependency tree generation failed"
false
}
}
# Test reverse dependencies
export def test-reverse-dependencies [] {
print "Testing: Reverse dependencies"
try {
let reverse_deps = (get-reverse-dependencies "orchestrator")
# Control-center, mcp-server, api-gateway depend on orchestrator
assert ("control-center" in $reverse_deps) "Control-center should depend on orchestrator"
print "✅ Reverse dependencies work"
true
} catch {
print "❌ Reverse dependencies failed"
false
}
}
# Test can-stop-service
export def test-can-stop-service [] {
print "Testing: Can-stop-service check"
try {
let can_stop = (can-stop-service "orchestrator")
assert ("can_stop" in $can_stop) "Should have can_stop field"
assert ("dependent_services" in $can_stop) "Should have dependent_services field"
print "✅ Can-stop-service check works"
true
} catch {
print "❌ Can-stop-service check failed"
false
}
}
# Test service state initialization
export def test-service-state-init [] {
print "Testing: Service state initialization"
try {
init-service-state
let state_dir = $"($env.HOME)/.provisioning/services/state"
let pid_dir = $"($env.HOME)/.provisioning/services/pids"
let log_dir = $"($env.HOME)/.provisioning/services/logs"
assert ($state_dir | path exists) "State directory should exist"
assert ($pid_dir | path exists) "PID directory should exist"
assert ($log_dir | path exists) "Log directory should exist"
print "✅ Service state initialization works"
true
} catch {
print "❌ Service state initialization failed"
false
}
}
# Run all tests
export def main [] {
print "=== Service Management System Tests ===\n"
let tests = [
test-service-registry-loading
test-service-definition
test-dependency-resolution
test-dependency-graph
test-startup-order
test-prerequisites-validation
test-conflict-detection
test-required-services-check
test-all-services-validation
test-readiness-report
test-dependency-tree
test-reverse-dependencies
test-can-stop-service
test-service-state-init
]
let mut passed = 0
let mut failed = 0
for test in $tests {
try {
if (do $test) {
$passed = $passed + 1
} else {
$failed = $failed + 1
}
} catch {
print $"❌ Test ($test) threw an error"
$failed = $failed + 1
}
print ""
}
print $"=== Test Results ==="
print $"Passed: ($passed)"
print $"Failed: ($failed)"
print $"Total: (($tests | length))"
if $failed == 0 {
print "\n✅ All tests passed!"
} else {
print $"\n❌ ($failed) tests failed"
}
}