sal/service_manager/tests/rhai/service_lifecycle.rhai
Mahmoud-Emad 95122dffee feat: Improve service manager testing and error handling
- Add comprehensive testing instructions to README.
- Improve error handling in examples to prevent crashes.
- Enhance launchctl error handling for production safety.
- Improve zinit error handling for production safety.
- Remove obsolete plan_to_fix.md file.
- Update Rhai integration tests for improved robustness.
- Improve service manager creation on Linux with systemd fallback.
2025-07-02 12:05:03 +03:00

178 lines
5.5 KiB
Plaintext

// Service lifecycle management test script
// This script tests REAL complete service lifecycle scenarios
print("=== Service Lifecycle Management Test ===");
// Create service manager
let manager = create_service_manager();
print("✓ Service manager created");
// Test configuration - real services for testing
let test_services = [
#{
name: "lifecycle-test-1",
binary_path: "/bin/echo",
args: ["Lifecycle test 1"],
working_directory: "/tmp",
environment: #{},
auto_restart: false
},
#{
name: "lifecycle-test-2",
binary_path: "/bin/echo",
args: ["Lifecycle test 2"],
working_directory: "/tmp",
environment: #{ "TEST_VAR": "test_value" },
auto_restart: false
}
];
let total_tests = 0;
let passed_tests = 0;
// Test 1: Service Creation and Start
print("\n1. Testing service creation and start...");
for service_config in test_services {
print(`\nStarting service: ${service_config.name}`);
try {
start(manager, service_config);
print(` ✓ Service ${service_config.name} started successfully`);
passed_tests += 1;
} catch(e) {
print(` ✗ Service ${service_config.name} start failed: ${e}`);
}
total_tests += 1;
}
// Test 2: Service Existence Check
print("\n2. Testing service existence checks...");
for service_config in test_services {
print(`\nChecking existence of: ${service_config.name}`);
try {
let service_exists = exists(manager, service_config.name);
if service_exists {
print(` ✓ Service ${service_config.name} exists: ${service_exists}`);
passed_tests += 1;
} else {
print(` ✗ Service ${service_config.name} doesn't exist after start`);
}
} catch(e) {
print(` ✗ Existence check failed for ${service_config.name}: ${e}`);
}
total_tests += 1;
}
// Test 3: Status Check
print("\n3. Testing status checks...");
for service_config in test_services {
print(`\nChecking status of: ${service_config.name}`);
try {
let service_status = status(manager, service_config.name);
print(` ✓ Service ${service_config.name} status: ${service_status}`);
passed_tests += 1;
} catch(e) {
print(` ✗ Status check failed for ${service_config.name}: ${e}`);
}
total_tests += 1;
}
// Test 4: Service List Check
print("\n4. Testing service list...");
try {
let services = list(manager);
print(` ✓ Service list retrieved (${services.len()} services)`);
// Check if our test services are in the list
for service_config in test_services {
let found = false;
for service in services {
if service.contains(service_config.name) {
found = true;
print(` ✓ Found ${service_config.name} in list`);
break;
}
}
if !found {
print(` ⚠ ${service_config.name} not found in service list`);
}
}
passed_tests += 1;
} catch(e) {
print(` ✗ Service list failed: ${e}`);
}
total_tests += 1;
// Test 5: Service Stop
print("\n5. Testing service stop...");
for service_config in test_services {
print(`\nStopping service: ${service_config.name}`);
try {
stop(manager, service_config.name);
print(` ✓ Service ${service_config.name} stopped successfully`);
passed_tests += 1;
} catch(e) {
print(` ✗ Service ${service_config.name} stop failed: ${e}`);
}
total_tests += 1;
}
// Test 6: Service Removal
print("\n6. Testing service removal...");
for service_config in test_services {
print(`\nRemoving service: ${service_config.name}`);
try {
remove(manager, service_config.name);
print(` ✓ Service ${service_config.name} removed successfully`);
passed_tests += 1;
} catch(e) {
print(` ✗ Service ${service_config.name} removal failed: ${e}`);
}
total_tests += 1;
}
// Test 7: Cleanup Verification
print("\n7. Testing cleanup verification...");
for service_config in test_services {
print(`\nVerifying removal of: ${service_config.name}`);
try {
let exists_after_remove = exists(manager, service_config.name);
if !exists_after_remove {
print(` ✓ Service ${service_config.name} correctly doesn't exist after removal`);
passed_tests += 1;
} else {
print(` ✗ Service ${service_config.name} still exists after removal`);
}
} catch(e) {
print(` ✗ Cleanup verification failed for ${service_config.name}: ${e}`);
}
total_tests += 1;
}
// Test Summary
print("\n=== Lifecycle Test Summary ===");
print(`Services tested: ${test_services.len()}`);
print(`Total operations: ${total_tests}`);
print(`Successful operations: ${passed_tests}`);
print(`Failed operations: ${total_tests - passed_tests}`);
print(`Success rate: ${(passed_tests * 100) / total_tests}%`);
if passed_tests == total_tests {
print("\n🎉 All lifecycle tests passed!");
print("Service manager is working correctly across all scenarios.");
} else {
print(`\n⚠ ${total_tests - passed_tests} test(s) failed`);
print("Some service manager operations need attention.");
}
print("\n=== Service Lifecycle Test Complete ===");
// Return test results
#{
summary: #{
total_tests: total_tests,
passed_tests: passed_tests,
success_rate: (passed_tests * 100) / total_tests,
services_tested: test_services.len()
}
}