aboutsummaryrefslogtreecommitdiffstats
path: root/tests/receptor.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/receptor.rs')
-rw-r--r--tests/receptor.rs103
1 files changed, 6 insertions, 97 deletions
diff --git a/tests/receptor.rs b/tests/receptor.rs
index 496140b..034e2a8 100644
--- a/tests/receptor.rs
+++ b/tests/receptor.rs
@@ -2,107 +2,15 @@
//! reactions, packing, materializing, and the command line. Validators and renderers
//! are shell scripts.
-use std::os::unix::fs::PermissionsExt;
-use std::path::{Path, PathBuf};
+mod common;
+
use std::process::Command;
-use enstate::plan::{self, Plan};
-use enstate::schema::Schema;
-use enstate::store::Store;
+use common::Host;
use enstate::submission::Submission;
use enstate::view::Change;
use serde_json::{Value, json};
-struct Host {
- root: PathBuf,
-}
-
-impl Host {
- fn new(name: &str) -> Self {
- let root = Path::new(env!("CARGO_TARGET_TMPDIR"))
- .join("receptor")
- .join(name);
- let _ = std::fs::remove_dir_all(&root);
- std::fs::create_dir_all(root.join("programs")).unwrap();
- Self { root }
- }
-
- fn store(&self) -> Store {
- Store::new(&self.root.join("state"), &self.root.join("run"))
- }
-
- /// A shell script, with the tests' PATH (programs get an empty environment).
- fn program(&self, name: &str, body: &str) -> String {
- let path = self.root.join("programs").join(name);
- let script = format!(
- "#!/bin/sh\nPATH={}\nset -eu\n{body}\n",
- std::env::var("PATH").unwrap()
- );
- std::fs::write(&path, script).unwrap();
- std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap();
- path.display().to_string()
- }
-
- fn schema(&self, schema: Value) -> (Schema, Vec<u8>) {
- let bytes = serde_json::to_vec_pretty(&schema).unwrap();
- std::fs::write(self.root.join("schema.json"), &bytes).unwrap();
- (Schema::from_slice(&bytes).unwrap(), bytes)
- }
-
- /// A submission named `name`: its manifest, and files below `channels/`.
- fn submission(&self, name: &str, manifest: Value, files: &[(&str, &[u8])]) -> PathBuf {
- let dir = self.root.join("submissions").join(name);
- let _ = std::fs::remove_dir_all(&dir);
- std::fs::create_dir_all(&dir).unwrap();
- let mut manifest = manifest;
- manifest["submission"] = json!(1);
- std::fs::write(dir.join("manifest.json"), manifest.to_string()).unwrap();
- for (path, bytes) in files {
- let path = dir.join("channels").join(path);
- std::fs::create_dir_all(path.parent().unwrap()).unwrap();
- std::fs::write(path, bytes).unwrap();
- }
- dir
- }
-
- fn plan(&self, schema: &Schema, dir: &Path) -> Plan {
- let submission = Submission::load(dir).unwrap();
- plan::plan(schema, &self.store(), &submission).unwrap().plan
- }
-
- fn apply(&self, schema: &(Schema, Vec<u8>), dir: &Path) -> Plan {
- let submission = Submission::load(dir).unwrap();
- let store = self.store();
- let _lock = store.lock().unwrap();
- plan::plan(&schema.0, &store, &submission)
- .unwrap()
- .apply(&store, &schema.1, &self.systemctl())
- .unwrap()
- }
-
- /// A systemctl that writes down what it was asked.
- fn systemctl(&self) -> PathBuf {
- let log = self.root.join("systemctl.log");
- PathBuf::from(self.program("systemctl", &format!("echo \"$*\" >> {}", log.display())))
- }
-
- fn reactions(&self) -> String {
- std::fs::read_to_string(self.root.join("systemctl.log")).unwrap_or_default()
- }
-
- fn view(&self, view: &str, file: &str) -> Option<Vec<u8>> {
- std::fs::read(self.root.join("run/views").join(view).join(file)).ok()
- }
-
- fn mode(&self, view: &str, file: &str) -> u32 {
- std::fs::metadata(self.root.join("run/views").join(view).join(file))
- .unwrap()
- .permissions()
- .mode()
- & 0o777
- }
-}
-
/// A host's secrets: a key, its public half, an optional extra; a validator across
/// the key pair; each entry its own view, the key restarting a service.
fn secrets_schema(host: &Host) -> Value {
@@ -338,6 +246,7 @@ fn refusals_and_what_is_left_out() {
("extra".to_owned(), b"broken".to_vec()),
]
.into(),
+ &Default::default(),
None,
&schema2.1,
)
@@ -483,7 +392,7 @@ fn materialize_after_boot_and_switch() {
assert!(host.apply(&schema, &first).accepted);
// Boot: /run is empty.
std::fs::remove_dir_all(host.root.join("run")).unwrap();
- let views = plan::materialize(&schema.0, &host.store()).unwrap();
+ let views = enstate::migrate::materialize(&schema.0, &host.store()).unwrap();
assert!(
views.iter().all(|view| view.change == Change::Added),
"{views:?}"
@@ -493,7 +402,7 @@ fn materialize_after_boot_and_switch() {
let mut fewer = secrets_schema(&host);
fewer["views"].as_object_mut().unwrap().remove("a.pub");
let fewer = host.schema(fewer);
- let views = plan::materialize(&fewer.0, &host.store()).unwrap();
+ let views = enstate::migrate::materialize(&fewer.0, &host.store()).unwrap();
assert!(
views
.iter()