prvng_core/nulib/tests/test_services.nu
Jesús Pérez 85ce530733
feat: update provisioning core CLI, libraries, and plugins
Update core components including CLI, Nushell libraries, plugins system,
and utility scripts for the provisioning system.

CLI Updates:
- Command implementations
- CLI utilities and dispatching
- Help system improvements
- Command validation

Library Updates:
- Configuration management system
- Infrastructure validation
- Extension system improvements
- Secrets management
- Workspace operations
- Cache management system

Plugin System:
- Interactive form plugin (inquire)
- KCL integration plugin
- Performance optimization plugins
- Plugin registration system

Utilities:
- Build and distribution scripts
- Installation procedures
- Testing utilities
- Development tools

Documentation:
- Library module documentation
- Extension API guides
- Plugin usage guides
- Service management documentation

All changes are backward compatible. No breaking changes.
2025-12-11 21:57:05 +00:00

359 lines
9.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"
let result = (do {
load-service-registry
} | complete)
if $result.exit_code == 0 {
let registry = $result.stdout
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
} else {
print "❌ Failed to load service registry"
false
}
}
# Test service definition retrieval
export def test-service-definition [] {
print "Testing: Service definition retrieval"
let result = (do {
get-service-definition "orchestrator"
} | complete)
if $result.exit_code == 0 {
let orchestrator = $result.stdout
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
} else {
print "❌ Failed to get service definition"
false
}
}
# Test dependency resolution
export def test-dependency-resolution [] {
print "Testing: Dependency resolution"
let result = (do {
# Test with control-center (depends on orchestrator)
let deps = (resolve-dependencies "control-center")
assert ("orchestrator" in $deps) "Should resolve orchestrator dependency"
} | complete)
if $result.exit_code == 0 {
print "✅ Dependency resolution works"
true
} else {
print "❌ Dependency resolution failed"
false
}
}
# Test dependency graph validation
export def test-dependency-graph [] {
print "Testing: Dependency graph validation"
let result = (do {
let validation = (validate-dependency-graph)
assert ($validation.valid) "Dependency graph should be valid"
assert (not $validation.has_cycles) "Should not have cycles"
} | complete)
if $result.exit_code == 0 {
print "✅ Dependency graph is valid"
true
} else {
print "❌ Dependency graph validation failed"
false
}
}
# Test startup order calculation
export def test-startup-order [] {
print "Testing: Startup order calculation"
let result = (do {
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"
} | complete)
if $result.exit_code == 0 {
print "✅ Startup order calculation works"
true
} else {
print "❌ Startup order calculation failed"
false
}
}
# Test service prerequisites validation
export def test-prerequisites-validation [] {
print "Testing: Prerequisites validation"
let result = (do {
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"
} | complete)
if $result.exit_code == 0 {
print "✅ Prerequisites validation works"
true
} else {
print "❌ Prerequisites validation failed"
false
}
}
# Test conflict detection
export def test-conflict-detection [] {
print "Testing: Conflict detection"
let result = (do {
let conflicts = (check-service-conflicts "coredns")
assert ("has_conflicts" in $conflicts) "Should have has_conflicts field"
} | complete)
if $result.exit_code == 0 {
print "✅ Conflict detection works"
true
} else {
print "❌ Conflict detection failed"
false
}
}
# Test required services check
export def test-required-services-check [] {
print "Testing: Required services check"
let result = (do {
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"
} | complete)
if $result.exit_code == 0 {
print "✅ Required services check works"
true
} else {
print "❌ Required services check failed"
false
}
}
# Test all services validation
export def test-all-services-validation [] {
print "Testing: All services validation"
let result = (do {
let validation = (validate-all-services)
assert ($validation.total_services > 0) "Should have services"
assert ("valid_services" in $validation) "Should have valid_services count"
} | complete)
if $result.exit_code == 0 {
print "✅ All services validation works"
true
} else {
print "❌ All services validation failed"
false
}
}
# Test readiness report
export def test-readiness-report [] {
print "Testing: Readiness report"
let result = (do {
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"
} | complete)
if $result.exit_code == 0 {
print "✅ Readiness report works"
true
} else {
print "❌ Readiness report failed"
false
}
}
# Test dependency tree
export def test-dependency-tree [] {
print "Testing: Dependency tree generation"
let result = (do {
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"
} | complete)
if $result.exit_code == 0 {
print "✅ Dependency tree generation works"
true
} else {
print "❌ Dependency tree generation failed"
false
}
}
# Test reverse dependencies
export def test-reverse-dependencies [] {
print "Testing: Reverse dependencies"
let result = (do {
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"
} | complete)
if $result.exit_code == 0 {
print "✅ Reverse dependencies work"
true
} else {
print "❌ Reverse dependencies failed"
false
}
}
# Test can-stop-service
export def test-can-stop-service [] {
print "Testing: Can-stop-service check"
let result = (do {
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"
} | complete)
if $result.exit_code == 0 {
print "✅ Can-stop-service check works"
true
} else {
print "❌ Can-stop-service check failed"
false
}
}
# Test service state initialization
export def test-service-state-init [] {
print "Testing: Service state initialization"
let result = (do {
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"
} | complete)
if $result.exit_code == 0 {
print "✅ Service state initialization works"
true
} else {
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 {
let result = (do {
do $test
} | complete)
if $result.exit_code == 0 {
if $result.stdout {
$passed = $passed + 1
} else {
$failed = $failed + 1
}
} else {
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"
}
}