Commit 174fce88 authored by Alexis Janon's avatar Alexis Janon
Browse files

Added check_path support in create_dir and remove_dir

parent bb62ce62
......@@ -21,42 +21,48 @@ impl CreateDir {
impl Command for CreateDir {
fn exec(&mut self) -> Result<CommandResult> {
debug!("Creating directory {:?}", self.path);
match fs::create_dir(&self.path) {
Err(ref err) if err.kind() == io::ErrorKind::AlreadyExists => {
warn!("Could not create directory {:?}: already exists", self.path);
Ok(())
}
other => other,
}.map(CommandResult::from)
.map_err(|err| {
Error::from(format!(
"Could not create directory {:?}: {}",
self.path,
err.to_string()
))
})
match self.controllers.check_path(&self.path) {
Ok(_) => match fs::create_dir(&self.path) {
Err(ref err) if err.kind() == io::ErrorKind::AlreadyExists => {
warn!("Could not create directory {:?}: already exists", self.path);
Ok(())
}
other => other,
}.map(CommandResult::from)
.map_err(|err| {
Error::from(format!(
"Could not create directory {:?}: {}",
self.path,
err.to_string()
))
}),
err => err,
}
}
fn rollback(&mut self) -> Result<CommandResult> {
debug!("{{rollback}} Removing directory {:?}", self.path);
match fs::remove_dir(&self.path) {
Err(ref err) if err.kind() == io::ErrorKind::NotFound => {
warn!(
"{{rollback}} Could not remove directory {:?}: not found",
self.path
);
Ok(())
}
other => other,
}.map(CommandResult::from)
.map_err(Error::from)
.map_err(|err| {
Error::from(format!(
"{{rollback}} Could not remove directory {:?}: {}",
self.path,
err.to_string()
))
})
match self.controllers.check_path(&self.path) {
Ok(_) => match fs::remove_dir(&self.path) {
Err(ref err) if err.kind() == io::ErrorKind::NotFound => {
warn!(
"{{rollback}} Could not remove directory {:?}: not found",
self.path
);
Ok(())
}
other => other,
}.map(CommandResult::from)
.map_err(Error::from)
.map_err(|err| {
Error::from(format!(
"{{rollback}} Could not remove directory {:?}: {}",
self.path,
err.to_string()
))
}),
err => err,
}
}
}
......@@ -64,6 +70,8 @@ impl Command for CreateDir {
mod tests {
use super::*;
use configuration::sysfscontroller::{SysFsController, SysFsControllerList, SysFsType};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
......@@ -71,10 +79,16 @@ mod tests {
const NOT_EXISTING_NAME: &str = "test_create_dir_not_existing";
const FOUND: &str = "test_create_dir_found";
const NOT_FOUND: &str = "test_create_dir_not_found";
const INVALID_PATH: &str = "../test_create_dir_invalid";
#[test]
fn test_already_existing() {
let rc_controllers = Rc::new(SysFsControllerList::new());
let mut map = HashMap::new();
map.insert(
"tmp".to_owned(),
SysFsController::new(path::PathBuf::from("/tmp/"), SysFsType::Cgroup),
);
let rc_controllers = Rc::new(SysFsControllerList::from(map));
let path = PathBuf::from(format!("/tmp/{}", EXISTING_NAME));
assert!(fs::create_dir(&path).is_ok());
let mut command = CreateDir::new(path.to_path_buf(), rc_controllers);
......@@ -85,7 +99,12 @@ mod tests {
#[test]
fn test_not_existing() {
let rc_controllers = Rc::new(SysFsControllerList::new());
let mut map = HashMap::new();
map.insert(
"tmp".to_owned(),
SysFsController::new(path::PathBuf::from("/tmp/"), SysFsType::Cgroup),
);
let rc_controllers = Rc::new(SysFsControllerList::from(map));
let path = PathBuf::from(format!("/tmp/{}", NOT_EXISTING_NAME));
let mut command = CreateDir::new(path.to_path_buf(), rc_controllers);
assert!(command.exec().is_ok());
......@@ -95,7 +114,12 @@ mod tests {
#[test]
fn test_found() {
let rc_controllers = Rc::new(SysFsControllerList::new());
let mut map = HashMap::new();
map.insert(
"tmp".to_owned(),
SysFsController::new(path::PathBuf::from("/tmp/"), SysFsType::Cgroup),
);
let rc_controllers = Rc::new(SysFsControllerList::from(map));
let path = PathBuf::from(format!("/tmp/{}", NOT_FOUND));
assert!(fs::create_dir(&path).is_ok());
let mut command = CreateDir::new(path.to_path_buf(), rc_controllers);
......@@ -105,10 +129,43 @@ mod tests {
#[test]
fn test_not_found() {
let rc_controllers = Rc::new(SysFsControllerList::new());
let mut map = HashMap::new();
map.insert(
"tmp".to_owned(),
SysFsController::new(path::PathBuf::from("/tmp/"), SysFsType::Cgroup),
);
let rc_controllers = Rc::new(SysFsControllerList::from(map));
let path = PathBuf::from(format!("/tmp/{}", FOUND));
let mut command = CreateDir::new(path.to_path_buf(), rc_controllers);
assert!(command.rollback().is_ok());
assert!(!path.exists());
}
#[test]
fn test_invalid_path_exec() {
let mut map = HashMap::new();
map.insert(
"tmp".to_owned(),
SysFsController::new(path::PathBuf::from("/tmp/"), SysFsType::Cgroup),
);
let rc_controllers = Rc::new(SysFsControllerList::from(map));
let path = PathBuf::from(format!("/tmp/{}", INVALID_PATH));
let mut command = CreateDir::new(path.to_path_buf(), rc_controllers);
assert!(command.exec().is_err());
assert!(!path.exists());
}
#[test]
fn test_invalid_path_rollback() {
let mut map = HashMap::new();
map.insert(
"tmp".to_owned(),
SysFsController::new(path::PathBuf::from("/tmp/"), SysFsType::Cgroup),
);
let rc_controllers = Rc::new(SysFsControllerList::from(map));
let path = PathBuf::from(format!("/tmp/{}", INVALID_PATH));
let mut command = CreateDir::new(path.to_path_buf(), rc_controllers);
assert!(command.rollback().is_err());
assert!(!path.exists());
}
}
......@@ -22,42 +22,48 @@ impl RemoveDir {
impl Command for RemoveDir {
fn exec(&mut self) -> Result<CommandResult> {
debug!("Removing directory {:?}", self.path);
match fs::remove_dir(&self.path) {
Err(ref err) if err.kind() == io::ErrorKind::NotFound => {
warn!("Could not remove directory {:?}: not found", self.path);
Ok(())
}
other => other,
}.map(CommandResult::from)
.map_err(Error::from)
.map_err(|err| {
Error::from(format!(
"Could not remove directory {:?}: {}",
self.path,
err.to_string()
))
})
match self.controllers.check_path(&self.path) {
Ok(_) => match fs::remove_dir(&self.path) {
Err(ref err) if err.kind() == io::ErrorKind::NotFound => {
warn!("Could not remove directory {:?}: not found", self.path);
Ok(())
}
other => other,
}.map(CommandResult::from)
.map_err(Error::from)
.map_err(|err| {
Error::from(format!(
"Could not remove directory {:?}: {}",
self.path,
err.to_string()
))
}),
err => err,
}
}
fn rollback(&mut self) -> Result<CommandResult> {
debug!("{{rollback}} Creating directory {:?}", self.path);
match fs::create_dir(&self.path) {
Err(ref err) if err.kind() == io::ErrorKind::AlreadyExists => {
warn!(
"{{rollback}} Could not create directory {:?}: already exists",
self.path
);
Ok(())
}
other => other,
}.map(CommandResult::from)
.map_err(|err| {
Error::from(format!(
"{{rollback}} Could not create directory {:?}: {}",
self.path,
err.to_string()
))
})
match self.controllers.check_path(&self.path) {
Ok(_) => match fs::create_dir(&self.path) {
Err(ref err) if err.kind() == io::ErrorKind::AlreadyExists => {
warn!(
"{{rollback}} Could not create directory {:?}: already exists",
self.path
);
Ok(())
}
other => other,
}.map(CommandResult::from)
.map_err(|err| {
Error::from(format!(
"{{rollback}} Could not create directory {:?}: {}",
self.path,
err.to_string()
))
}),
err => err,
}
}
}
......@@ -65,6 +71,8 @@ impl Command for RemoveDir {
mod tests {
use super::*;
use configuration::sysfscontroller::{SysFsController, SysFsControllerList, SysFsType};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
......@@ -72,10 +80,16 @@ mod tests {
const NOT_EXISTING_NAME: &str = "test_remove_dir_not_existing";
const FOUND: &str = "test_remove_dir_found";
const NOT_FOUND: &str = "test_remove_dir_not_found";
const INVALID_PATH: &str = "../test_remove_dir_invalid";
#[test]
fn test_already_existing() {
let rc_controllers = Rc::new(SysFsControllerList::new());
let mut map = HashMap::new();
map.insert(
"tmp".to_owned(),
SysFsController::new(path::PathBuf::from("/tmp/"), SysFsType::Cgroup),
);
let rc_controllers = Rc::new(SysFsControllerList::from(map));
let path = PathBuf::from(format!("/tmp/{}", EXISTING_NAME));
assert!(fs::create_dir(&path).is_ok());
let mut command = RemoveDir::new(path.to_path_buf(), rc_controllers);
......@@ -86,7 +100,12 @@ mod tests {
#[test]
fn test_not_existing() {
let rc_controllers = Rc::new(SysFsControllerList::new());
let mut map = HashMap::new();
map.insert(
"tmp".to_owned(),
SysFsController::new(path::PathBuf::from("/tmp/"), SysFsType::Cgroup),
);
let rc_controllers = Rc::new(SysFsControllerList::from(map));
let path = PathBuf::from(format!("/tmp/{}", NOT_EXISTING_NAME));
let mut command = RemoveDir::new(path.to_path_buf(), rc_controllers);
assert!(command.rollback().is_ok());
......@@ -96,7 +115,12 @@ mod tests {
#[test]
fn test_found() {
let rc_controllers = Rc::new(SysFsControllerList::new());
let mut map = HashMap::new();
map.insert(
"tmp".to_owned(),
SysFsController::new(path::PathBuf::from("/tmp/"), SysFsType::Cgroup),
);
let rc_controllers = Rc::new(SysFsControllerList::from(map));
let path = PathBuf::from(format!("/tmp/{}", NOT_FOUND));
assert!(fs::create_dir(&path).is_ok());
let mut command = RemoveDir::new(path.to_path_buf(), rc_controllers);
......@@ -106,10 +130,43 @@ mod tests {
#[test]
fn test_not_found() {
let rc_controllers = Rc::new(SysFsControllerList::new());
let mut map = HashMap::new();
map.insert(
"tmp".to_owned(),
SysFsController::new(path::PathBuf::from("/tmp/"), SysFsType::Cgroup),
);
let rc_controllers = Rc::new(SysFsControllerList::from(map));
let path = PathBuf::from(format!("/tmp/{}", FOUND));
let mut command = RemoveDir::new(path.to_path_buf(), rc_controllers);
assert!(command.exec().is_ok());
assert!(!path.exists());
}
#[test]
fn test_invalid_path_exec() {
let mut map = HashMap::new();
map.insert(
"tmp".to_owned(),
SysFsController::new(path::PathBuf::from("/tmp/"), SysFsType::Cgroup),
);
let rc_controllers = Rc::new(SysFsControllerList::from(map));
let path = PathBuf::from(format!("/tmp/{}", INVALID_PATH));
let mut command = RemoveDir::new(path.to_path_buf(), rc_controllers);
assert!(command.exec().is_err());
assert!(!path.exists());
}
#[test]
fn test_invalid_path_rollback() {
let mut map = HashMap::new();
map.insert(
"tmp".to_owned(),
SysFsController::new(path::PathBuf::from("/tmp/"), SysFsType::Cgroup),
);
let rc_controllers = Rc::new(SysFsControllerList::from(map));
let path = PathBuf::from(format!("/tmp/{}", INVALID_PATH));
let mut command = RemoveDir::new(path.to_path_buf(), rc_controllers);
assert!(command.rollback().is_err());
assert!(!path.exists());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment