Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -55,8 +54,10 @@ 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 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
Expand Down
2 changes: 1 addition & 1 deletion src/daemon/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppState>, id: String) -> DaemonResponse {
pub async fn handle_stop_watch(state: Arc<AppState>, id: String) -> DaemonResponse {
match async {
let mut guard = state.watches.write().await;
if let Some(w) = guard.get_mut(&id) {
Expand Down
5 changes: 4 additions & 1 deletion src/exec/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down
302 changes: 151 additions & 151 deletions tests/daemon_test.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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<String, core::watcher::WatchContext> = 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<()> {
Expand All @@ -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<()> {
Expand Down
4 changes: 4 additions & 0 deletions tests/scheduler_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down