1+ extern crate zinit;
2+
3+ use anyhow:: Result ;
4+ use std:: path:: Path ;
5+ use tokio:: time:: { sleep, Duration } ;
6+ use std:: env;
7+
8+ use zinit:: app:: api:: Client ;
9+ use zinit:: testapp;
10+
11+ #[ tokio:: main]
12+ async fn main ( ) -> Result < ( ) > {
13+ // Define paths for socket and config
14+ let temp_dir = env:: temp_dir ( ) ;
15+ let socket_path = temp_dir. join ( "zinit-test.sock" ) . to_str ( ) . unwrap ( ) . to_string ( ) ;
16+ let config_dir = temp_dir. join ( "zinit-test-config" ) . to_str ( ) . unwrap ( ) . to_string ( ) ;
17+
18+ println ! ( "Starting zinit with socket at: {}" , socket_path) ;
19+ println ! ( "Using config directory: {}" , config_dir) ;
20+
21+ // Start zinit in the background
22+ testapp:: start_zinit ( & socket_path, & config_dir) . await ?;
23+
24+ // Wait for zinit to initialize
25+ sleep ( Duration :: from_secs ( 2 ) ) . await ;
26+
27+ // Create a client to communicate with zinit
28+ let client = Client :: new ( & socket_path) ;
29+
30+ // Create service configurations
31+ println ! ( "Creating service configurations..." ) ;
32+
33+ // Create a find service
34+ testapp:: create_service_config ( & config_dir, "find-service" , "find / -name \" *.txt\" -type f" ) . await ?;
35+
36+ // Create a sleep service with echo
37+ testapp:: create_service_config (
38+ & config_dir,
39+ "sleep-service" ,
40+ "sh -c 'echo Starting sleep; sleep 30; echo Finished sleep'"
41+ ) . await ?;
42+
43+ // Wait for zinit to load the configurations
44+ sleep ( Duration :: from_secs ( 1 ) ) . await ;
45+
46+ // Tell zinit to monitor our services
47+ println ! ( "Monitoring services..." ) ;
48+ client. monitor ( "find-service" ) . await ?;
49+ client. monitor ( "sleep-service" ) . await ?;
50+
51+ // List all services
52+ println ! ( "\n Listing all services:" ) ;
53+ let services = client. list ( ) . await ?;
54+ for ( name, status) in services {
55+ println ! ( "Service: {} - Status: {}" , name, status) ;
56+ }
57+
58+ // Start the find service
59+ println ! ( "\n Starting find-service..." ) ;
60+ client. start ( "find-service" ) . await ?;
61+
62+ // Wait a bit and check status
63+ sleep ( Duration :: from_secs ( 2 ) ) . await ;
64+ let status = client. status ( "find-service" ) . await ?;
65+ println ! ( "find-service status: {:?}" , status) ;
66+
67+ // Start the sleep service
68+ println ! ( "\n Starting sleep-service..." ) ;
69+ client. start ( "sleep-service" ) . await ?;
70+
71+ // Wait a bit and check status
72+ sleep ( Duration :: from_secs ( 2 ) ) . await ;
73+ let status = client. status ( "sleep-service" ) . await ?;
74+ println ! ( "sleep-service status: {:?}" , status) ;
75+
76+ // Stop the find service
77+ println ! ( "\n Stopping find-service..." ) ;
78+ client. stop ( "find-service" ) . await ?;
79+
80+ // Wait a bit and check status
81+ sleep ( Duration :: from_secs ( 2 ) ) . await ;
82+ let status = client. status ( "find-service" ) . await ?;
83+ println ! ( "find-service status after stopping: {:?}" , status) ;
84+
85+ // Kill the sleep service with SIGTERM
86+ println ! ( "\n Killing sleep-service with SIGTERM..." ) ;
87+ client. kill ( "sleep-service" , "SIGTERM" ) . await ?;
88+
89+ // Wait a bit and check status
90+ sleep ( Duration :: from_secs ( 2 ) ) . await ;
91+ let status = client. status ( "sleep-service" ) . await ?;
92+ println ! ( "sleep-service status after killing: {:?}" , status) ;
93+
94+ // Cleanup - forget services
95+ println ! ( "\n Forgetting services..." ) ;
96+ if status. pid == 0 { // Only forget if it's not running
97+ client. forget ( "sleep-service" ) . await ?;
98+ }
99+ client. forget ( "find-service" ) . await ?;
100+
101+ // Shutdown zinit
102+ println ! ( "\n Shutting down zinit..." ) ;
103+ client. shutdown ( ) . await ?;
104+
105+ println ! ( "\n Test completed successfully!" ) ;
106+ Ok ( ( ) )
107+ }
0 commit comments