Skip to content
This repository was archived by the owner on Dec 15, 2025. It is now read-only.

Commit ea28584

Browse files
committed
...
1 parent b79d056 commit ea28584

File tree

7 files changed

+455
-10
lines changed

7 files changed

+455
-10
lines changed

Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,15 @@ thiserror = "1.0"
2020
clap = "2.33"
2121
git-version = "0.3.5"
2222
command-group = "1.0.8"
23+
24+
[lib]
25+
name = "zinit"
26+
path = "src/lib.rs"
27+
28+
[[bin]]
29+
name = "zinit"
30+
path = "src/main.rs"
31+
32+
[[bin]]
33+
name = "testapp"
34+
path = "src/bin/testapp.rs"

src/app/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
mod api;
1+
pub mod api;
22

33
use crate::zinit;
44
use anyhow::{Context, Result};

src/bin/testapp.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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!("\nListing 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!("\nStarting 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!("\nStarting 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!("\nStopping 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!("\nKilling 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!("\nForgetting 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!("\nShutting down zinit...");
103+
client.shutdown().await?;
104+
105+
println!("\nTest completed successfully!");
106+
Ok(())
107+
}

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extern crate serde;
2+
#[macro_use]
3+
extern crate anyhow;
4+
#[macro_use]
5+
extern crate log;
6+
extern crate tokio;
7+
8+
pub mod app;
9+
pub mod manager;
10+
pub mod zinit;
11+
pub mod testapp;

src/main.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
extern crate serde;
2-
#[macro_use]
3-
extern crate anyhow;
4-
#[macro_use]
5-
extern crate log;
6-
extern crate tokio;
1+
extern crate zinit;
2+
73
use anyhow::Result;
84
use clap::{App, Arg, SubCommand};
95
use git_version::git_version;
106

11-
mod app;
12-
mod manager;
13-
mod zinit;
7+
use zinit::app;
148

159
const GIT_VERSION: &str = git_version!(args = ["--tags", "--always", "--dirty=-modified"]);
1610

0 commit comments

Comments
 (0)