Skip to content

Commit

Permalink
test passing
Browse files Browse the repository at this point in the history
  • Loading branch information
leon3s committed Jan 13, 2024
1 parent e9e6876 commit 1227fb5
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 118 deletions.
25 changes: 18 additions & 7 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"editor.rulers": [
80
],
"editor.rulers": [80],
"editor.tabSize": 2,
"editor.detectIndentation": false,
"editor.trimAutoWhitespace": true,
Expand All @@ -12,8 +10,21 @@
"rust-analyzer.checkOnSave": true,
"rust-analyzer.check.command": "clippy",
"rust-analyzer.cargo.noDefaultFeatures": true,
"rust-analyzer.cargo.features": [
"dev",
"test"
],
"rust-analyzer.cargo.features": ["dev", "test"],
"cSpell.words": [
"canonicalize",
"chrono",
"crond",
"curr",
"dialoguer",
"dotenv",
"dotenvy",
"errno",
"Insertable",
"nanocl",
"Nanocld",
"nstore",
"ntex",
"statefile"
]
}
2 changes: 1 addition & 1 deletion bin/nanocl/src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
/// It will install nanocl system containers
pub async fn exec_install(args: &InstallOpts) -> IoResult<()> {
let home_dir = std::env::var("HOME").map_err(|err| {
IoError::interupted("Unable to get $HOME env variable", &err.to_string())
IoError::interrupted("Unable to get $HOME env variable", &err.to_string())

Check warning on line 25 in bin/nanocl/src/commands/install.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocl/src/commands/install.rs#L25

Added line #L25 was not covered by tests
})?;
let detected_host = utils::docker::detect_docker_host()?;
let (docker_host, is_docker_desktop) = match &args.docker_host {
Expand Down
19 changes: 9 additions & 10 deletions bin/nanocl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async fn execute_arg(cli_args: &Cli) -> IoResult<()> {
}

/// Nanocl is a command line interface for the Nanocl Daemon.
/// It will translate the conresponding commands to the Nanocl Daemon API.
/// It will translate the corresponding commands to the Nanocl Daemon API.
/// You can use it to manage your cargoes and virtual machines.
#[ntex::main]
async fn main() -> std::io::Result<()> {
Expand All @@ -88,7 +88,7 @@ async fn main() -> std::io::Result<()> {
std::process::exit(0);
})
.map_err(|err| {
IoError::interupted("Signal", &format!("Unable to register ctrl-c: {err}"))
IoError::interrupted("Signal", &format!("Unable to register ctrl-c: {err}"))

Check warning on line 91 in bin/nanocl/src/main.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocl/src/main.rs#L91

Added line #L91 was not covered by tests
})?;
if let Err(err) = execute_arg(&args).await {
err.print_and_exit();
Expand Down Expand Up @@ -253,24 +253,24 @@ mod tests {
async fn state_apply_default_statefile_name() {
let tests_path = Path::new("../../tests")
.canonicalize()
.expect("Can't cannonicalize tests folder path");
.expect("Can't canonicalize tests folder path");
env::set_current_dir(tests_path).expect("Can't move in tests folder");
assert_cli_ok!("state", "apply", "-y");
let tests_path = Path::new("./without_s_option")
.canonicalize()
.expect("Can't cannonicalize without_s_option folder path");
.expect("Can't canonicalize without_s_option folder path");
env::set_current_dir(tests_path)
.expect("Can't move in without_s_option folder");
assert_cli_ok!("state", "apply", "-y");
let tests_path = Path::new("../without_s_option_yml")
.canonicalize()
.expect("Can't cannonicalize without_s_option_yml folder path");
.expect("Can't canonicalize without_s_option_yml folder path");
env::set_current_dir(tests_path)
.expect("Can't move in without_s_option_yml folder");
assert_cli_ok!("state", "apply", "-y");
let tests_path = Path::new("../../bin/nanocl")
.canonicalize()
.expect("Can't cannonicalize tests folder path");
.expect("Can't canonicalize tests folder path");
env::set_current_dir(tests_path).expect("Can't move back in nanocl folder");
assert_cli_err!("state", "apply", "-y");
}
Expand Down Expand Up @@ -300,7 +300,7 @@ mod tests {
}

#[ntex::test]
async fn state_apply_args_advenced() {
async fn state_apply_args_advanced() {
assert_cli_ok!(
"state",
"apply",
Expand Down Expand Up @@ -365,7 +365,7 @@ mod tests {
assert!(
Path::new("/tmp/toto")
.canonicalize()
.expect("Can't cannonicalize bind /tmp/toto folder path")
.expect("Can't canonicalize bind /tmp/toto folder path")
.exists(),
"Relative bind was not created",
);
Expand Down Expand Up @@ -472,7 +472,6 @@ mod tests {
ntex::rt::spawn(async {
assert_cli_ok!("cargo", "stats", CARGO_NAME);
});
assert_cli_ok!("cargo", "restart", CARGO_NAME);
assert_cli_ok!("cargo", "stop", CARGO_NAME);
assert_cli_ok!("cargo", "ls");
assert_cli_ok!("cargo", "ls", "-q");
Expand All @@ -494,7 +493,7 @@ mod tests {

#[ntex::test]
async fn cargo_inspect_invalid() {
assert_cli_err!("cargo", "inspect", "ewfwefew");
assert_cli_err!("cargo", "inspect", "unknown-cargo");
}

#[ntex::test]
Expand Down
6 changes: 3 additions & 3 deletions bin/nanocld/src/models/raw_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl RawEventEmitter {
fn check_connection(&mut self) -> IoResult<()> {
let mut alive_clients = Vec::new();
let mut inner = self.inner.lock().map_err(|err| {
IoError::interupted("RawEmitterMutex", err.to_string().as_str())
IoError::interrupted("RawEmitterMutex", err.to_string().as_str())

Check warning on line 78 in bin/nanocld/src/models/raw_emitter.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/models/raw_emitter.rs#L78

Added line #L78 was not covered by tests
})?;
for client in &inner.clients {
if client.try_send(Bytes::from("")).is_err() {
Expand Down Expand Up @@ -106,7 +106,7 @@ impl RawEventEmitter {
/// Send an event to all clients
pub fn emit(&self, e: &Event) -> IoResult<()> {
let inner = self.inner.lock().map_err(|err| {
IoError::interupted("RawEmitterMutex", err.to_string().as_str())
IoError::interrupted("RawEmitterMutex", err.to_string().as_str())

Check warning on line 109 in bin/nanocld/src/models/raw_emitter.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/models/raw_emitter.rs#L109

Added line #L109 was not covered by tests
})?;
for client in &inner.clients {
match e.try_to_bytes() {
Expand All @@ -128,7 +128,7 @@ impl RawEventEmitter {
.inner
.lock()
.map_err(|err| {
IoError::interupted("RawEmitterMutex", err.to_string().as_str())
IoError::interrupted("RawEmitterMutex", err.to_string().as_str())

Check warning on line 131 in bin/nanocld/src/models/raw_emitter.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/models/raw_emitter.rs#L131

Added line #L131 was not covered by tests
})?
.clients
.push(tx);
Expand Down
4 changes: 2 additions & 2 deletions bin/nanocld/src/models/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl SystemState {
.send(SystemEventKind::Emit(event))
.await
.map_err(|err| {
IoError::interupted("EventEmitter", err.to_string().as_str())
IoError::interrupted("EventEmitter", err.to_string().as_str())

Check warning on line 187 in bin/nanocld/src/models/system.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/models/system.rs#L187

Added line #L187 was not covered by tests
})?;
Ok(())
}
Expand All @@ -207,7 +207,7 @@ impl SystemState {
.send(SystemEventKind::Subscribe(sx))
.await
.map_err(|err| {
IoError::interupted("EventEmitter", err.to_string().as_str())
IoError::interrupted("EventEmitter", err.to_string().as_str())

Check warning on line 210 in bin/nanocld/src/models/system.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/models/system.rs#L210

Added line #L210 was not covered by tests
})?;
Ok(rx)
}
Expand Down
26 changes: 11 additions & 15 deletions bin/nanocld/src/models/task_manager.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::{
sync::{Arc, Mutex},
collections::HashMap,
future::IntoFuture,
ops::Deref,
};

use ntex::{rt, web};
use futures_util::{Future, FutureExt};
use futures_util::Future;

use nanocl_error::io::{IoResult, IoError};

Expand All @@ -15,15 +13,15 @@ use nanocl_stubs::system::NativeEventAction;
#[derive(Clone)]

Check warning on line 13 in bin/nanocld/src/models/task_manager.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/models/task_manager.rs#L13

Added line #L13 was not covered by tests
pub struct ObjTask {
pub kind: NativeEventAction,
pub fut: Arc<rt::JoinHandle<IoResult<()>>>,
pub fut: Arc<Mutex<rt::JoinHandle<IoResult<()>>>>,
}

impl ObjTask {
pub fn new<F>(kind: NativeEventAction, task: F) -> Self
where
F: Future<Output = IoResult<()>> + 'static,
{
let fut = Arc::new(rt::spawn(task));
let fut = Arc::new(Mutex::new(rt::spawn(task)));
Self { kind, fut }
}

Check warning on line 26 in bin/nanocld/src/models/task_manager.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/models/task_manager.rs#L20-L26

Added lines #L20 - L26 were not covered by tests
}
Expand All @@ -42,9 +40,7 @@ impl TaskManager {
let key = key.to_owned();
let tasks = Arc::clone(&self.tasks);
web::block(move || {
let mut tasks = tasks
.lock()
.map_err(|err| IoError::interupted("Task", err.to_string().as_str()))?;
let mut tasks = tasks.lock()?;
log::debug!("Adding task: {key} {}", task.kind);
tasks.insert(key.clone(), task.clone());
Ok::<_, IoError>(())
Expand All @@ -57,13 +53,13 @@ impl TaskManager {
let key = key.to_owned();
let tasks = Arc::clone(&self.tasks);
web::block(move || {
let mut tasks = tasks
.lock()
.map_err(|err| IoError::interupted("Task", err.to_string().as_str()))?;
let mut tasks = tasks.lock().map_err(|err| {
IoError::interrupted("Task", err.to_string().as_str())
})?;
let task = tasks.get(&key);
if let Some(task) = task {
log::debug!("Removing task: {key} {}", task.kind);
task.fut.abort();
task.fut.lock()?.abort();
}
tasks.remove(&key);
Ok::<_, IoError>(())
Expand All @@ -76,9 +72,9 @@ impl TaskManager {
let key = key.to_owned();
let tasks = Arc::clone(&self.tasks);
let res = web::block(move || {
let tasks = tasks
.lock()
.map_err(|err| IoError::interupted("Task", err.to_string().as_str()))?;
let tasks = tasks.lock().map_err(|err| {
IoError::interrupted("Task", err.to_string().as_str())
})?;
Ok::<_, IoError>(tasks.get(&key).cloned())
})
.await;
Expand Down
7 changes: 2 additions & 5 deletions bin/nanocld/src/objects/job.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use bollard_next::container::RemoveContainerOptions;
use futures_util::{StreamExt, stream::FuturesUnordered};

use nanocl_error::http::{HttpResult, HttpError};
use nanocl_error::http::HttpResult;
use nanocl_stubs::{
job::{Job, JobPartial, JobInspect},
process::ProcessKind,
system::{NativeEventAction, ObjPsStatusPartial, ObjPsStatusKind},
system::{ObjPsStatusPartial, ObjPsStatusKind},
};

use crate::{
Expand Down
12 changes: 6 additions & 6 deletions bin/nanocld/src/subsystem/docker_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,28 @@ async fn exec_docker(
Ok(())
}

/// Create a new thread with his own loop to analize events from docker
pub fn analize(state: &SystemState) {
/// Create a new thread with his own loop to analyze events from docker
pub fn analyze(state: &SystemState) {
let state = state.clone();
rt::Arbiter::new().exec_fn(move || {
rt::spawn(async move {
loop {
let mut streams =
state.docker_api.events(None::<EventsOptions<String>>);
log::info!("event::analize_docker: stream connected");
log::info!("event::analyze_docker: stream connected");
while let Some(event) = streams.next().await {
match event {
Ok(event) => {
if let Err(err) = exec_docker(&event, &state).await {
log::warn!("event::analize_docker: {err}")
log::warn!("event::analyze_docker: {err}")

Check warning on line 101 in bin/nanocld/src/subsystem/docker_event.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/subsystem/docker_event.rs#L101

Added line #L101 was not covered by tests
}
}
Err(err) => {
log::warn!("event::analize_docker: {err}");
log::warn!("event::analyze_docker: {err}");

Check warning on line 105 in bin/nanocld/src/subsystem/docker_event.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/subsystem/docker_event.rs#L105

Added line #L105 was not covered by tests
}
}
}
log::warn!("event::analize_docker: disconnected trying to reconnect");
log::warn!("event::analyze_docker: disconnected trying to reconnect");

Check warning on line 109 in bin/nanocld/src/subsystem/docker_event.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/subsystem/docker_event.rs#L109

Added line #L109 was not covered by tests
ntex::time::sleep(std::time::Duration::from_secs(1)).await;
}
});
Expand Down
Loading

0 comments on commit 1227fb5

Please sign in to comment.