New architecture

This commit is contained in:
Adrian Heine 2019-12-26 20:50:23 +01:00
parent e4b3424ba6
commit 907a4962c5
61 changed files with 2742 additions and 3100 deletions

View file

@ -2,7 +2,7 @@ use schematics::symbols::file::File as FileSymbol;
use schematics::symbols::Symbol;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::path::{Path, PathBuf};
use tempdir::TempDir;
fn get_dir(content: Option<&str>) -> TempDir {
@ -18,11 +18,8 @@ fn get_dir(content: Option<&str>) -> TempDir {
tmp_dir
}
fn get_symbol(path: &Path) -> FileSymbol<&str, String> {
FileSymbol::new(
String::from(path.join("filename").to_str().unwrap()),
"target content",
)
fn get_symbol(path: &Path) -> FileSymbol<PathBuf, &str> {
FileSymbol::new(path.join("filename"), "target content")
}
// Normal cases
@ -81,7 +78,7 @@ fn no_file() {
#[test]
fn may_not_read_file() {
let symbol = get_symbol(&Path::new("/etc/passwd"));
let symbol = get_symbol(&Path::new("/etc/shadow"));
assert_eq!(symbol.target_reached().is_err(), true);
}

72
tests/setup.rs Normal file
View file

@ -0,0 +1,72 @@
use schematics::resources::{Cert, Csr, GitCheckout};
use schematics::schema::SymbolRunner;
use schematics::symbols::Symbol;
use schematics::Setup;
use std::cell::RefCell;
use std::error::Error;
use std::fmt::Debug;
use std::rc::Rc;
struct TestSymbolRunner {
count: Rc<RefCell<usize>>,
}
impl SymbolRunner for TestSymbolRunner {
fn run_symbol<S: Symbol + Debug>(
&self,
_symbol: &S,
_force: bool,
) -> Result<bool, Box<dyn Error>> {
*self.count.borrow_mut() += 1;
Ok(false)
}
}
#[test]
fn runs_only_once() {
let count = Rc::new(RefCell::new(0));
let runner = TestSymbolRunner {
count: Rc::clone(&count),
};
let mut setup = Setup::new(runner);
assert_eq!(
(setup.add(Csr("somehost")).unwrap().0).0.to_str().unwrap(),
"/etc/ssl/local_certs/somehost.csr",
);
assert_eq!(
(setup.add(Csr("somehost")).unwrap().0).0.to_str().unwrap(),
"/etc/ssl/local_certs/somehost.csr",
);
assert_eq!(*count.borrow(), 2 + 5); // Key and CSR + 5 dirs
}
#[test]
fn can_create_an_acme_cert() {
let count = Rc::new(RefCell::new(0));
let runner = TestSymbolRunner {
count: Rc::clone(&count),
};
let mut setup = Setup::new(runner);
assert_eq!(
(setup.add(Cert("somehost")).unwrap().0).0.to_str().unwrap(),
"/etc/ssl/local_certs/somehost.crt",
);
assert_eq!(*count.borrow(), 15);
}
#[test]
fn can_create_a_git_checkout() {
let count = Rc::new(RefCell::new(0));
let runner = TestSymbolRunner {
count: Rc::clone(&count),
};
let mut setup = Setup::new(runner);
setup
.add(GitCheckout(
"/tmp/somepath".into(),
"/tmp/some_src_repo",
"master",
))
.unwrap();
assert_eq!(*count.borrow(), 3);
}

View file

@ -16,7 +16,7 @@ fn get_dir<'a, I: IntoIterator<Item = &'a &'a str>>(content: I) -> TempDir {
}
fn get_storage(path: &Path) -> SimpleStorage {
SimpleStorage::new(path.to_str().unwrap().into(), "filename".into())
SimpleStorage::new(path.join("_filename"))
}
// Normal cases
@ -29,7 +29,7 @@ fn single_file() {
assert!(
Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
.unwrap()
.is_match(&storage.write_filename())
.is_match(storage.write_filename().to_str().unwrap())
);
assert_eq!(
dir.path().join("_filename").join("12345"),
@ -46,7 +46,7 @@ fn two_files() {
assert!(
Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
.unwrap()
.is_match(&storage.write_filename())
.is_match(storage.write_filename().to_str().unwrap())
);
assert_eq!(
dir.path().join("_filename").join("23456"),
@ -63,7 +63,7 @@ fn another_two_files() {
assert!(
Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
.unwrap()
.is_match(&storage.write_filename())
.is_match(storage.write_filename().to_str().unwrap())
);
assert_eq!(
dir.path().join("_filename").join("23456"),
@ -80,7 +80,7 @@ fn three_files() {
assert!(
Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
.unwrap()
.is_match(&storage.write_filename())
.is_match(storage.write_filename().to_str().unwrap())
);
assert_eq!(
dir.path().join("_filename").join("23456"),
@ -99,7 +99,7 @@ fn empty_storage() {
assert!(
Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
.unwrap()
.is_match(&storage.write_filename())
.is_match(storage.write_filename().to_str().unwrap())
);
assert!(storage.read_filename().is_err());
assert_eq!(
@ -121,7 +121,7 @@ fn empty_storage_for_filename() {
assert!(
Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
.unwrap()
.is_match(&storage.write_filename())
.is_match(storage.write_filename().to_str().unwrap())
);
assert!(storage.read_filename().is_err());
assert_eq!(
@ -143,7 +143,7 @@ fn bad_file() {
assert!(
Regex::new(&format!("^{}/_filename/\\d+$", dir.path().display()))
.unwrap()
.is_match(&storage.write_filename())
.is_match(storage.write_filename().to_str().unwrap())
);
assert!(storage.read_filename().is_err());
assert_eq!(