A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.4 KiB

use async_trait::async_trait;
use schematics::async_utils::{run, sleep};
use schematics::loggers::{Logger, StoringLogger};
use schematics::resources::{AcmeUser, Cert, Csr, GitCheckout};
use schematics::symbols::Symbol;
use schematics::Setup;
use schematics::SymbolRunner;
use std::cell::RefCell;
use std::error::Error;
use std::fmt::Debug;
use std::rc::Rc;
use std::time::Duration;
#[derive(Clone, Debug)]
struct TestSymbolRunner {
count: Rc<RefCell<usize>>,
}
#[async_trait(?Send)]
impl SymbolRunner for TestSymbolRunner {
async fn run_symbol<S: Symbol + Debug, L: Logger>(
&self,
_symbol: &S,
_logger: &L,
_force: bool,
) -> Result<bool, Box<dyn Error>> {
*self.count.borrow_mut() += 1;
sleep(Duration::from_millis(0)).await;
Ok(false)
}
}
#[test]
fn can_create_an_acme_user() {
let count = Rc::new(RefCell::new(0));
let runner = TestSymbolRunner {
count: Rc::clone(&count),
};
let setup = Setup::new(runner, StoringLogger::new());
assert_eq!((run(setup.add(AcmeUser)).unwrap().0).0, "acme");
}
#[test]
fn runs_only_once() {
run(async {
let count = Rc::new(RefCell::new(0));
let runner = TestSymbolRunner {
count: Rc::clone(&count),
};
let setup = Setup::new(runner, StoringLogger::new());
assert_eq!(
(setup.add(Csr("somehost")).await.unwrap().0)
.as_ref()
.to_str()
.unwrap(),
"/etc/ssl/local_certs/somehost.csr",
);
assert_eq!(
(setup.add(Csr("somehost")).await.unwrap().0)
.as_ref()
.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 setup = Setup::new(runner, StoringLogger::new());
assert_eq!(
(run(setup.add(Cert("somehost"))).unwrap().0)
.as_ref()
.to_str()
.unwrap(),
"/etc/ssl/local_certs/somehost.crt",
);
assert_eq!(*count.borrow(), 19);
}
#[test]
fn can_create_a_git_checkout() {
let count = Rc::new(RefCell::new(0));
let runner = TestSymbolRunner {
count: Rc::clone(&count),
};
let setup = Setup::new(runner, StoringLogger::new());
run(setup.add(GitCheckout(
"/tmp/somepath".into(),
"/tmp/some_src_repo",
"master",
)))
.unwrap();
assert_eq!(*count.borrow(), 3);
}