Compare commits
2 Commits
572fbcd3eb
...
ae0c47df35
| Author | SHA1 | Date |
|---|---|---|
|
|
ae0c47df35 | |
|
|
7bbac7db43 |
|
|
@ -6,6 +6,8 @@ use std::process::Command;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::{env, fs};
|
use std::{env, fs};
|
||||||
use tokio::time::Duration;
|
use tokio::time::Duration;
|
||||||
|
use std::fs::OpenOptions;
|
||||||
|
use std::io::{self, Write};
|
||||||
|
|
||||||
const CONFIG_PATH: &str = "settings.json";
|
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> {
|
fn save_new_config(config: &Processes, config_file: &str) -> Result<(), CustomError> {
|
||||||
match serde_json::to_string_pretty(&config) {
|
match serde_json::to_string_pretty(&config) {
|
||||||
Ok(st) => match fs::write(config_file, st) {
|
// Ok(st) => match fs::write(config_file, st) {
|
||||||
Ok(_) => Ok(()),
|
// Ok(_) => Ok(()),
|
||||||
Err(_) => Err(CustomError::Fatal),
|
// 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),
|
Err(_) => Err(CustomError::Fatal),
|
||||||
}
|
}
|
||||||
|
|
@ -263,3 +283,60 @@ fn parse_extern_config(json_string: &str) -> Option<Processes> {
|
||||||
}
|
}
|
||||||
None
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,7 +3,9 @@ use serde::{Deserialize, Serialize};
|
||||||
/// # an Error enum (next will be deleted and replaced)
|
/// # an Error enum (next will be deleted and replaced)
|
||||||
pub enum CustomError {
|
pub enum CustomError {
|
||||||
Fatal,
|
Fatal,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum ConfigActuality {
|
pub enum ConfigActuality {
|
||||||
Local,
|
Local,
|
||||||
Remote,
|
Remote,
|
||||||
|
|
|
||||||
|
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"dateOfCreation": "1",
|
||||||
|
"configServer": "",
|
||||||
|
"processes": []
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"dateOfCreation": "1",
|
||||||
|
"configServer": "",
|
||||||
|
"processes": []
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue