Compare commits

...

2 Commits

Author SHA1 Message Date
prplV ae0c47df35 gitignore zero update 2024-10-30 13:55:15 +03:00
prplV 7bbac7db43 config unit tests 2024-10-30 13:54:50 +03:00
6 changed files with 126 additions and 4 deletions

2
.gitignore vendored
View File

@ -2,4 +2,4 @@
.idea
Dockerfile
Cargo.lock
settings.json
settings.json

View File

@ -6,6 +6,8 @@ use std::process::Command;
use std::sync::Arc;
use std::{env, fs};
use tokio::time::Duration;
use std::fs::OpenOptions;
use std::io::{self, Write};
const CONFIG_PATH: &str = "settings.json";
@ -249,9 +251,27 @@ fn config_comparing(local: &Processes, remote: &Processes) -> ConfigActuality {
fn save_new_config(config: &Processes, config_file: &str) -> Result<(), CustomError> {
match serde_json::to_string_pretty(&config) {
Ok(st) => match fs::write(config_file, st) {
Ok(_) => Ok(()),
Err(_) => Err(CustomError::Fatal),
// Ok(st) => match fs::write(config_file, st) {
// Ok(_) => Ok(()),
// Err(_) => Err(CustomError::Fatal),
// },
Ok(st) => {
let file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(config_file);
match file {
Ok(fs) => {
let mut writer = fs;
match writeln!(writer, "{}", st) {
Ok(_) => Ok(()),
Err(_) => Err(CustomError::Fatal),
}
},
Err(_) => return Err(CustomError::Fatal)
}
},
Err(_) => Err(CustomError::Fatal),
}
@ -263,3 +283,60 @@ fn parse_extern_config(json_string: &str) -> Option<Processes> {
}
None
}
// unit tests
#[cfg(test)]
mod config_unittests {
use super::*;
#[test]
fn parsing_valid_conf() {
assert!(load_processes("tests/examples/settings.json").is_some());
}
#[test]
fn parsing_invalid_conf() {
assert!(load_processes("tests/examples/invalid_config.json").is_none());
}
#[test]
fn configuration_comparing() {
// old one (kinda local)
let a = Processes {
date_of_creation: String::from("1"),
config_server: String::new(),
processes: vec![],
};
// new one (kinda remote)
let b = Processes {
date_of_creation: String::from("2"),
config_server: String::new(),
processes: vec![],
};
assert_eq!(config_comparing(&a, &b), ConfigActuality::Remote);
}
#[test]
fn get_actual_config_mechanism() {
assert!(get_actual_config().is_some())
}
#[test]
fn save_config() {
let a = Processes {
date_of_creation: String::from("1"),
config_server: String::new(),
processes: vec![],
};
assert!(save_new_config(&a, "tests/examples/save-conf.json").is_ok());
}
#[test]
fn save_to_zero_file() {
let a = Processes {
date_of_creation: String::from("1"),
config_server: String::new(),
processes: vec![],
};
assert!(save_new_config(&a, "tests/examples/none.json").is_ok());
}
}

View File

@ -3,7 +3,9 @@ use serde::{Deserialize, Serialize};
/// # an Error enum (next will be deleted and replaced)
pub enum CustomError {
Fatal,
}
#[derive(Debug, PartialEq)]
pub enum ConfigActuality {
Local,
Remote,

View File

@ -0,0 +1,33 @@
{
"configServer" : "localhost",
"processes": [
{
"name": "temp-process",
"path": "/home/user/monitor/runner-rs/temp-process",
"dependencies": {
"files": [
{
"filename": "dep-file",
"src": "/home/user/monitor/runner-rs/tests/examples/",
"triggers": {
"onDelete": "stop",
"onChange": "stay"
}
}
],
"services": [
{
"hostname": "ya.ru",
"port": 443,
"triggers": {
"wait": 10,
"delay": 2,
"onLost": "hold"
}
}
]
}
}
]
}

5
tests/examples/none.json Normal file
View File

@ -0,0 +1,5 @@
{
"dateOfCreation": "1",
"configServer": "",
"processes": []
}

View File

@ -0,0 +1,5 @@
{
"dateOfCreation": "1",
"configServer": "",
"processes": []
}