From 985b3aee3882f35449356ae970166401b61d6fed Mon Sep 17 00:00:00 2001 From: pepedinho <2spii94@gmail.com> Date: Sun, 12 Oct 2025 16:37:12 +0200 Subject: [PATCH 1/6] ci: add scheduler test in ci pipeline --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a19233d..a8f35c2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -56,7 +56,7 @@ jobs: - name: Clear files run: rm -rf ~/.local/share/fleetd - name: Run cargo test - run: cargo test --features no-tty --test utiles_test --test git_test --test daemon_test -- --test-threads=1 + run: cargo test --features no-tty --test utiles_test --test scheduler-test --test git_test --test daemon_test -- --test-threads=1 build: name: Build project From ebc9b7bce34125e5593bf1f087f9d20f48174122 Mon Sep 17 00:00:00 2001 From: pepedinho <2spii94@gmail.com> Date: Sun, 12 Oct 2025 16:38:10 +0200 Subject: [PATCH 2/6] ci: add scheduler test in ci pipeline --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a8f35c2..5a108cc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -56,7 +56,7 @@ jobs: - name: Clear files run: rm -rf ~/.local/share/fleetd - name: Run cargo test - run: cargo test --features no-tty --test utiles_test --test scheduler-test --test git_test --test daemon_test -- --test-threads=1 + run: cargo test --features no-tty --test utiles_test --test scheduler_test --test git_test --test daemon_test -- --test-threads=1 build: name: Build project From e799b131696f5a1d4a977abd8b216a222aafc5c1 Mon Sep 17 00:00:00 2001 From: pepedinho <2spii94@gmail.com> Date: Sun, 12 Oct 2025 16:48:48 +0200 Subject: [PATCH 3/6] ci: debug print in scheduler test --- tests/scheduler_test.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/scheduler_test.rs b/tests/scheduler_test.rs index 56ec0b7..12d376a 100644 --- a/tests/scheduler_test.rs +++ b/tests/scheduler_test.rs @@ -137,6 +137,10 @@ async fn test_independent_jobs_run_in_parallel() -> anyhow::Result<()> { let ctx = build_test_ctx("test_multiple_dependencies", jobs).await?; run_pipeline(ctx.clone()).await.unwrap(); + + dbg!(&ctx); + eprintln!("debug: log_path: {}", ctx.log_path().display()); + let content = fs::read_to_string(ctx.log_path())?; assert!(content.contains("job1")); From 35e3e505d5b286d422393bd91ab8960493414c8a Mon Sep 17 00:00:00 2001 From: pepedinho <2spii94@gmail.com> Date: Sun, 12 Oct 2025 16:59:51 +0200 Subject: [PATCH 4/6] ci: add ci step for creating logs & metrics directory --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5a108cc..e1efbbb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,6 +55,8 @@ jobs: rustup default stable - name: Clear files run: rm -rf ~/.local/share/fleetd + - name: Prepare directory + run: mkdir -p ~/.fleet/logs ~/.fleet/metrics - name: Run cargo test run: cargo test --features no-tty --test utiles_test --test scheduler_test --test git_test --test daemon_test -- --test-threads=1 From 1a86222b02ace43836af13739a194e6700590415 Mon Sep 17 00:00:00 2001 From: pepedinho <2spii94@gmail.com> Date: Sun, 12 Oct 2025 17:17:49 +0200 Subject: [PATCH 5/6] ci: add more daemon tests for each commands --- src/daemon/server.rs | 2 +- src/exec/metrics.rs | 5 +- tests/daemon_test.rs | 302 +++++++++++++++++++++---------------------- 3 files changed, 156 insertions(+), 153 deletions(-) diff --git a/src/daemon/server.rs b/src/daemon/server.rs index a0af722..0e95136 100644 --- a/src/daemon/server.rs +++ b/src/daemon/server.rs @@ -216,7 +216,7 @@ async fn handle_add_watch( } /// Stops a watch by ID if it exists in the application state. -async fn handle_stop_watch(state: Arc, id: String) -> DaemonResponse { +pub async fn handle_stop_watch(state: Arc, id: String) -> DaemonResponse { match async { let mut guard = state.watches.write().await; if let Some(w) = guard.get_mut(&id) { diff --git a/src/exec/metrics.rs b/src/exec/metrics.rs index 87a4bcb..9d324be 100644 --- a/src/exec/metrics.rs +++ b/src/exec/metrics.rs @@ -215,7 +215,10 @@ impl ExecMetrics { } pub fn rm_metrics_by_id(id: &str) -> anyhow::Result<()> { - std::fs::remove_file(ExecMetrics::get_metrics_path_by_id(id)?)?; + let path = ExecMetrics::get_metrics_path_by_id(id)?; + if path.exists() { + std::fs::remove_file(path)?; + } Ok(()) } diff --git a/tests/daemon_test.rs b/tests/daemon_test.rs index 41517f1..755a601 100644 --- a/tests/daemon_test.rs +++ b/tests/daemon_test.rs @@ -1,13 +1,34 @@ use std::{collections::HashMap, env::temp_dir, sync::Arc}; use core_lib::{ - core::{self, state::AppState}, - daemon::server::{DaemonResponse, handle_list_watches, handle_rm_watch}, + config::ProjectConfig, + core::{self, state::AppState, watcher::WatchContextBuilder}, + daemon::server::{ + DaemonResponse, handle_list_watches, handle_rm_watch, handle_stop_watch, handle_up_watch, + }, + git::repo::{Branch, Branches, Repo}, log::logger::Logger, }; use pretty_assertions::assert_eq; use tokio::{fs, sync::RwLock}; +fn build_repo() -> Repo { + Repo { + branches: Branches { + name: "main".to_string(), + branches: vec![Branch { + name: "main".to_string(), + remote: "git://github.com/pepedinho/fleet.git".to_string(), + ..Default::default() + }], + last_commit: "abc".to_string(), + last_name: "main".to_string(), + }, + name: "name".to_string(), + remote: "git://github.com/pepedinho/fleet.git".to_string(), + } +} + #[test] fn test_id_generation() { let res = core::id::short_id(); @@ -73,118 +94,103 @@ async fn test_concurent_log_writes() -> anyhow::Result<()> { Ok(()) } -// #[tokio::test] -// async fn test_handle_stop_watch_existing() -> anyhow::Result<()> { -// init_watch_file().await?; -// let id = "watch_stop".to_string(); - -// let mut map = HashMap::new(); -// let ctx = WatchContext { -// paused: false, -// project_dir: "dir".to_string(), -// branch: "main".to_string(), -// repo: Repo { -// branch: "main".to_string(), -// last_commit: "abc".to_string(), -// name: "name".to_string(), -// remote: "".to_string(), -// }, -// id: id.clone(), -// config: ProjectConfig::default(), -// }; - -// map.insert(id.clone(), ctx); -// let state = Arc::new(AppState { -// watches: RwLock::new(map), -// }); - -// let response = handle_stop_watch(state.clone(), id.clone()).await; -// remove_watch_by_id(&id).await?; - -// match response { -// DaemonResponse::Success(msg) => { -// assert!(msg.contains("Watch stopped")); -// } -// _ => panic!("Expected success response"), -// } -// Ok(()) -// } - -// #[tokio::test] -// async fn test_handle_up_watch_existing() -> anyhow::Result<()> { -// init_watch_file().await?; -// let id = "watch_up".to_string(); - -// let mut map = HashMap::new(); - -// let repo = Repo { -// branch: "main".to_string(), -// last_commit: "abc".to_string(), -// name: "name".to_string(), -// remote: "git://github.com/pepedinho/fleet.git".to_string(), -// }; -// let ctx = WatchContextBuilder::new( -// "main".to_string(), -// repo, -// ProjectConfig::default(), -// "dir".to_string(), -// id.clone(), -// ) -// .build() -// .await?; - -// ctx.logger.clean().await?; -// map.insert(id.clone(), ctx); -// let state = Arc::new(AppState { -// watches: RwLock::new(map), -// }); - -// let response = handle_up_watch(state.clone(), id.clone()).await; -// remove_watch_by_id(&id).await?; - -// match response { -// DaemonResponse::Success(msg) => { -// assert!(msg.contains("Watch up")); -// } -// _ => panic!("Excpected succes response"), -// } - -// Ok(()) -// } - -// #[tokio::test] -// async fn test_handle_rm_watch_existing() -> anyhow::Result<()> { -// init_watch_file().await?; -// let id = "rm_watch".to_string(); - -// let mut map = HashMap::new(); -// let ctx = WatchContext { -// paused: false, -// project_dir: "dir".to_string(), -// branch: "main".to_string(), -// repo: Repo { -// branch: "main".to_string(), -// last_commit: "abc".to_string(), -// name: "name".to_string(), -// remote: "".to_string(), -// }, -// id: id.clone(), -// config: ProjectConfig::default(), -// }; -// map.insert(id.clone(), ctx); - -// let state = Arc::new(AppState { -// watches: RwLock::new(map), -// }); - -// let response = handle_rm_watch(state.clone(), id.clone()).await; - -// match response { -// DaemonResponse::Success(msg) => assert!(msg.contains("was deleted")), -// _ => panic!("Expected succes response"), -// } -// Ok(()) -// } +#[tokio::test] +async fn test_handle_stop_watch_existing() -> anyhow::Result<()> { + AppState::init_watch_file().await?; + let id = "watch_stop".to_string(); + + let mut map = HashMap::new(); + let ctx = WatchContextBuilder::new( + build_repo(), + ProjectConfig::default(), + "dir".to_string(), + id.clone(), + ) + .build() + .await?; + + map.insert(id.clone(), ctx); + let state = Arc::new(AppState { + watches: RwLock::new(map), + }); + + let response = handle_stop_watch(state.clone(), id.clone()).await; + AppState::remove_watch_by_id(&id).await?; + + match response { + DaemonResponse::Success(msg) => { + assert!(msg.contains("Watch stopped")); + } + _ => panic!("Expected success response"), + } + Ok(()) +} + +#[tokio::test] +async fn test_handle_up_watch_existing() -> anyhow::Result<()> { + AppState::init_watch_file().await?; + let id = "watch_up".to_string(); + + let mut map: HashMap = HashMap::new(); + + let repo = build_repo(); + let ctx = WatchContextBuilder::new( + repo, + ProjectConfig::default(), + "dir".to_string(), + id.clone(), + ) + .build() + .await?; + + ctx.logger.clean().await?; + map.insert(id.clone(), ctx); + let state = Arc::new(AppState { + watches: RwLock::new(map), + }); + + let response = handle_up_watch(state.clone(), id.clone()).await; + AppState::remove_watch_by_id(&id).await?; + + match response { + DaemonResponse::Success(msg) => { + assert!(msg.contains("Watch up")); + } + _ => panic!("Excpected succes response"), + } + + Ok(()) +} + +#[tokio::test] +async fn test_handle_rm_watch_existing() -> anyhow::Result<()> { + AppState::init_watch_file().await?; + let id = "rm_watch".to_string(); + + let mut map = HashMap::new(); + let ctx = WatchContextBuilder::new( + build_repo(), + ProjectConfig::default(), + "dir".to_string(), + id.clone(), + ) + .build() + .await?; + map.insert(id.clone(), ctx); + + let state = Arc::new(AppState { + watches: RwLock::new(map), + }); + + let response = handle_rm_watch(state.clone(), id.clone()).await; + + dbg!(&response); + match response { + DaemonResponse::Success(msg) => assert!(msg.contains("was deleted")), + _ => panic!("Expected succes response"), + } + Ok(()) +} #[tokio::test] async fn test_handle_rm_non_existing() -> anyhow::Result<()> { @@ -201,43 +207,37 @@ async fn test_handle_rm_non_existing() -> anyhow::Result<()> { Ok(()) } -// #[tokio::test] -// async fn test_handle_list_watches_existing() -> anyhow::Result<()> { -// let mut map = HashMap::new(); -// let repo = Repo { -// branch: "main".to_string(), -// last_commit: "abc".to_string(), -// name: "name".to_string(), -// remote: "git://github.com/pepedinho/fleet.git".to_string(), -// }; -// let ctx = WatchContextBuilder::new( -// "main".to_string(), -// repo, -// ProjectConfig::default(), -// "dir".to_string(), -// "watch1".to_string(), -// ) -// .build() -// .await?; -// ctx.logger.clean().await?; - -// map.insert("watch1".to_string(), ctx); - -// let state = Arc::new(AppState { -// watches: RwLock::new(map), -// }); - -// let response = handle_list_watches(state.clone(), false).await; - -// match response { -// DaemonResponse::ListWatches(list) => { -// assert_eq!(list.len(), 1); -// assert_eq!(list[0].id, "watch1"); -// } -// _ => panic!("Expected list watches"), -// } -// Ok(()) -// } +#[tokio::test] +async fn test_handle_list_watches_existing() -> anyhow::Result<()> { + let mut map = HashMap::new(); + let repo = build_repo(); + let ctx = WatchContextBuilder::new( + repo, + ProjectConfig::default(), + "dir".to_string(), + "watch1".to_string(), + ) + .build() + .await?; + ctx.logger.clean().await?; + + map.insert("watch1".to_string(), ctx); + + let state = Arc::new(AppState { + watches: RwLock::new(map), + }); + + let response = handle_list_watches(state.clone(), false).await; + + match response { + DaemonResponse::ListWatches(list) => { + assert_eq!(list.len(), 1); + assert_eq!(list[0].id, "watch1"); + } + _ => panic!("Expected list watches"), + } + Ok(()) +} #[tokio::test] async fn test_handle_list_watches_empty() -> anyhow::Result<()> { From ed1c3137238258353f1a68b7d7e9101a265d78cb Mon Sep 17 00:00:00 2001 From: pepedinho <2spii94@gmail.com> Date: Sun, 12 Oct 2025 17:19:44 +0200 Subject: [PATCH 6/6] ci: remove push condition in tests ci --- .github/workflows/ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e1efbbb..cb2d74c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,7 +36,6 @@ jobs: test: name: Run tests runs-on: ubuntu-latest - if: github.event_name == 'push' steps: - uses: actions/checkout@v4 - name: Cache Cargo dependencies