From a0f22c88d54d351b060c2d0d9f0a8cbf5f412ba2 Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 29 Nov 2026 15:02:23 +0000 Subject: enstate: --migrate, renames both ways, kept entries What a generation sees of a snapshot accepted under another schema: same name and type, renamedFrom, a rename in the stored schema read backwards, and entries kept, with their types, in the snapshot's kept/. Plans start from what is seen, and store it under the running schema; a generation change rewrites no snapshot. --migrate (and --dry-run) says what each channel sees, refuses for required slots nothing fills, puts the views in place and runs their reactions; --materialize and --status go by what is seen, too. A view made from an unfilled required slot isn't there. The rollback cases of migrations.md that need no converters or steps are tests. --- tests/common/mod.rs | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 tests/common/mod.rs (limited to 'tests/common') 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) { + 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), 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> { + 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), 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 { + self.store() + .current(channel) + .unwrap() + .map(|snapshot| snapshot.id) + } +} -- cgit v1.3.1