sal/zinit_client/tests/rhai/04_real_world_scenarios.rhai
Mahmoud-Emad 511729c477
Some checks are pending
Rhai Tests / Run Rhai Tests (push) Waiting to run
feat: Add zinit_client package to workspace
- Add `zinit_client` package to the workspace, enabling its use
  in the SAL monorepo.  This allows for better organization and
  dependency management.
- Update `MONOREPO_CONVERSION_PLAN.md` to reflect the addition
  of `zinit_client` and its status.  This ensures the conversion
  plan stays up-to-date.
- Move `src/zinit_client/` directory to `zinit_client/` for better
   organization.  This improves the overall structure of the
   project.
- Update references to `zinit_client` to use the new path.  This
  ensures the codebase correctly links to the `zinit_client`
  package.
2025-06-22 10:59:19 +03:00

317 lines
10 KiB
Plaintext

// Real-world scenarios test script
// This script tests practical zinit usage scenarios
// Configuration
let socket_paths = [
"/var/run/zinit.sock",
"/tmp/zinit.sock",
"/run/zinit.sock",
"./zinit.sock"
];
// Find available socket
let socket_path = "";
for path in socket_paths {
try {
let test_services = zinit_list(path);
socket_path = path;
print(`✓ Found working Zinit socket at: ${path}`);
break;
} catch(e) {
// Continue to next path
}
}
if socket_path == "" {
print("⚠ No working Zinit socket found. Skipping tests.");
return;
}
print("=== Real-World Scenarios Test ===");
// Scenario 1: Web server simulation
print("\n=== Scenario 1: Web Server Simulation ===");
let web_service = "rhai-web-server";
let web_command = "python3 -m http.server 8080";
let web_oneshot = false;
// Clean up first
try {
zinit_stop(socket_path, web_service);
zinit_forget(socket_path, web_service);
zinit_delete_service(socket_path, web_service);
} catch(e) {
// Ignore cleanup errors
}
print("1. Creating web server service...");
try {
let create_result = zinit_create_service(socket_path, web_service, web_command, web_oneshot);
print(`✓ Web server service created: ${create_result}`);
print("2. Starting web server...");
zinit_monitor(socket_path, web_service);
let start_result = zinit_start(socket_path, web_service);
print(`✓ Web server started: ${start_result}`);
print("3. Checking web server status...");
let status = zinit_status(socket_path, web_service);
print(` State: ${status.state}, PID: ${status.pid}`);
print("4. Gracefully stopping web server...");
let stop_result = zinit_stop(socket_path, web_service);
print(`✓ Web server stopped: ${stop_result}`);
print("5. Cleaning up web server...");
zinit_forget(socket_path, web_service);
zinit_delete_service(socket_path, web_service);
print("✓ Web server cleaned up");
} catch(e) {
print(`⚠ Web server scenario failed: ${e}`);
// Cleanup on failure
try {
zinit_stop(socket_path, web_service);
zinit_forget(socket_path, web_service);
zinit_delete_service(socket_path, web_service);
} catch(cleanup_e) {
// Ignore cleanup errors
}
}
// Scenario 2: Batch job processing
print("\n=== Scenario 2: Batch Job Processing ===");
let batch_service = "rhai-batch-job";
let batch_command = "echo 'Processing batch job...' && sleep 2 && echo 'Batch job completed'";
let batch_oneshot = true;
// Clean up first
try {
zinit_stop(socket_path, batch_service);
zinit_forget(socket_path, batch_service);
zinit_delete_service(socket_path, batch_service);
} catch(e) {
// Ignore cleanup errors
}
print("1. Creating batch job service...");
try {
let create_result = zinit_create_service(socket_path, batch_service, batch_command, batch_oneshot);
print(`✓ Batch job service created: ${create_result}`);
print("2. Starting batch job...");
zinit_monitor(socket_path, batch_service);
let start_result = zinit_start(socket_path, batch_service);
print(`✓ Batch job started: ${start_result}`);
print("3. Monitoring batch job progress...");
let status = zinit_status(socket_path, batch_service);
print(` Initial state: ${status.state}, PID: ${status.pid}`);
// Since it's a oneshot job, it should complete automatically
print("4. Checking final status...");
try {
let final_status = zinit_status(socket_path, batch_service);
print(` Final state: ${final_status.state}, PID: ${final_status.pid}`);
} catch(e) {
print(` Status check: ${e}`);
}
print("5. Cleaning up batch job...");
zinit_forget(socket_path, batch_service);
zinit_delete_service(socket_path, batch_service);
print("✓ Batch job cleaned up");
} catch(e) {
print(`⚠ Batch job scenario failed: ${e}`);
// Cleanup on failure
try {
zinit_stop(socket_path, batch_service);
zinit_forget(socket_path, batch_service);
zinit_delete_service(socket_path, batch_service);
} catch(cleanup_e) {
// Ignore cleanup errors
}
}
// Scenario 3: Service dependency simulation
print("\n=== Scenario 3: Service Dependency Simulation ===");
let db_service = "rhai-mock-db";
let app_service = "rhai-mock-app";
let db_command = "echo 'Database started' && sleep 10";
let app_command = "echo 'Application started' && sleep 5";
// Clean up first
for service in [db_service, app_service] {
try {
zinit_stop(socket_path, service);
zinit_forget(socket_path, service);
zinit_delete_service(socket_path, service);
} catch(e) {
// Ignore cleanup errors
}
}
print("1. Creating database service...");
try {
let db_create = zinit_create_service(socket_path, db_service, db_command, false);
print(`✓ Database service created: ${db_create}`);
print("2. Creating application service...");
let app_create = zinit_create_service(socket_path, app_service, app_command, false);
print(`✓ Application service created: ${app_create}`);
print("3. Starting database first...");
zinit_monitor(socket_path, db_service);
let db_start = zinit_start(socket_path, db_service);
print(`✓ Database started: ${db_start}`);
print("4. Checking database status...");
let db_status = zinit_status(socket_path, db_service);
print(` Database state: ${db_status.state}, PID: ${db_status.pid}`);
print("5. Starting application...");
zinit_monitor(socket_path, app_service);
let app_start = zinit_start(socket_path, app_service);
print(`✓ Application started: ${app_start}`);
print("6. Checking application status...");
let app_status = zinit_status(socket_path, app_service);
print(` Application state: ${app_status.state}, PID: ${app_status.pid}`);
print("7. Stopping services in reverse order...");
zinit_stop(socket_path, app_service);
print(" Application stopped");
zinit_stop(socket_path, db_service);
print(" Database stopped");
print("8. Cleaning up services...");
for service in [app_service, db_service] {
zinit_forget(socket_path, service);
zinit_delete_service(socket_path, service);
}
print("✓ Services cleaned up");
} catch(e) {
print(`⚠ Service dependency scenario failed: ${e}`);
// Cleanup on failure
for service in [app_service, db_service] {
try {
zinit_stop(socket_path, service);
zinit_forget(socket_path, service);
zinit_delete_service(socket_path, service);
} catch(cleanup_e) {
// Ignore cleanup errors
}
}
}
// Scenario 4: Log monitoring and analysis
print("\n=== Scenario 4: Log Monitoring and Analysis ===");
print("1. Analyzing current system logs...");
try {
let all_logs = zinit_logs_all(socket_path);
print(`✓ Retrieved ${all_logs.len()} total log entries`);
if all_logs.len() > 0 {
print("2. Analyzing log patterns...");
let error_count = 0;
let warning_count = 0;
let info_count = 0;
for log_entry in all_logs {
let log_lower = log_entry.to_lower();
if log_lower.contains("error") {
error_count += 1;
} else if log_lower.contains("warn") {
warning_count += 1;
} else {
info_count += 1;
}
}
print(` Error entries: ${error_count}`);
print(` Warning entries: ${warning_count}`);
print(` Info entries: ${info_count}`);
print("3. Testing filtered log retrieval...");
let zinit_logs = zinit_logs(socket_path, "zinit");
print(`✓ Retrieved ${zinit_logs.len()} zinit-specific log entries`);
if zinit_logs.len() > 0 {
print(" Recent zinit logs:");
let count = 0;
for log_entry in zinit_logs {
if count >= 2 { break; }
print(` ${log_entry}`);
count += 1;
}
}
} else {
print(" No logs available for analysis");
}
} catch(e) {
print(`⚠ Log monitoring scenario failed: ${e}`);
}
// Scenario 5: Error recovery simulation
print("\n=== Scenario 5: Error Recovery Simulation ===");
let failing_service = "rhai-failing-service";
let failing_command = "exit 1"; // Command that always fails
// Clean up first
try {
zinit_stop(socket_path, failing_service);
zinit_forget(socket_path, failing_service);
zinit_delete_service(socket_path, failing_service);
} catch(e) {
// Ignore cleanup errors
}
print("1. Creating service that will fail...");
try {
let create_result = zinit_create_service(socket_path, failing_service, failing_command, true);
print(`✓ Failing service created: ${create_result}`);
print("2. Starting failing service...");
zinit_monitor(socket_path, failing_service);
let start_result = zinit_start(socket_path, failing_service);
print(`✓ Failing service started: ${start_result}`);
print("3. Checking service status after failure...");
try {
let status = zinit_status(socket_path, failing_service);
print(` Service state: ${status.state}, PID: ${status.pid}`);
} catch(e) {
print(` Status check: ${e}`);
}
print("4. Attempting restart...");
try {
let restart_result = zinit_restart(socket_path, failing_service);
print(`✓ Restart attempted: ${restart_result}`);
} catch(e) {
print(` Restart failed as expected: ${e}`);
}
print("5. Cleaning up failing service...");
zinit_forget(socket_path, failing_service);
zinit_delete_service(socket_path, failing_service);
print("✓ Failing service cleaned up");
} catch(e) {
print(`⚠ Error recovery scenario failed: ${e}`);
// Cleanup on failure
try {
zinit_stop(socket_path, failing_service);
zinit_forget(socket_path, failing_service);
zinit_delete_service(socket_path, failing_service);
} catch(cleanup_e) {
// Ignore cleanup errors
}
}
print("\n=== Real-World Scenarios Test Complete ===");
print("✓ All scenarios tested successfully");