aboutsummaryrefslogtreecommitdiffstats
path: root/tests/common/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/common/mod.rs')
-rw-r--r--tests/common/mod.rs121
1 files changed, 121 insertions, 0 deletions
diff --git a/tests/common/mod.rs b/tests/common/mod.rs
new file mode 100644
index 0000000..751d809
--- /dev/null
+++ b/tests/common/mod.rs
@@ -0,0 +1,121 @@
+//! A host on directories, for the tests: its store, programs as shell scripts,
+//! schemas, submissions, and what its views and reactions are.
+
+#![allow(dead_code)]
+
+use std::os::unix::fs::PermissionsExt;
+use std::path::{Path, PathBuf};
+
+use enstate::migrate::Migration;
+use enstate::plan::{self, Plan};
+use enstate::schema::Schema;
+use enstate::store::Store;
+use enstate::submission::Submission;
+use serde_json::{Value, json};
+
+pub struct Host {
+ pub root: PathBuf,
+}
+
+impl Host {
+ pub 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 }
+ }
+
+ pub 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).
+ pub 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()
+ }
+
+ pub 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/`.
+ pub 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
+ }
+
+ pub fn plan(&self, schema: &Schema, dir: &Path) -> Plan {
+ let submission = Submission::load(dir).unwrap();
+ plan::plan(schema, &self.store(), &submission).unwrap().plan
+ }
+
+ pub 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.
+ pub fn systemctl(&self) -> PathBuf {
+ let log = self.root.join("systemctl.log");
+ PathBuf::from(self.program("systemctl", &format!("echo \"$*\" >> {}", log.display())))
+ }
+
+ pub fn reactions(&self) -> String {
+ std::fs::read_to_string(self.root.join("systemctl.log")).unwrap_or_default()
+ }
+
+ pub fn view(&self, view: &str, file: &str) -> Option<Vec<u8>> {
+ std::fs::read(self.root.join("run/views").join(view).join(file)).ok()
+ }
+
+ pub fn mode(&self, view: &str, file: &str) -> u32 {
+ std::fs::metadata(self.root.join("run/views").join(view).join(file))
+ .unwrap()
+ .permissions()
+ .mode()
+ & 0o777
+ }
+}
+
+impl Host {
+ /// Serves the generation of `schema` from the snapshots, or says what that would do.
+ pub fn migrate(&self, schema: &(Schema, Vec<u8>), dry_run: bool) -> Migration {
+ let store = self.store();
+ let _lock = store.lock().unwrap();
+ enstate::migrate::migrate(&schema.0, &store, dry_run, &self.systemctl()).unwrap()
+ }
+
+ /// The current snapshot's id of `channel`.
+ pub fn snapshot(&self, channel: &str) -> Option<String> {
+ self.store()
+ .current(channel)
+ .unwrap()
+ .map(|snapshot| snapshot.id)
+ }
+}