liminal-server 0.25.4

Standalone server for the liminal messaging bus
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

use super::{Lifecycle, LifecycleError, Placer, ServiceState};
use crate::config::types::ServiceTemplateConfig;

/// Records every placement and refuses those whose child id is listed.
#[derive(Debug, Default)]
struct FakeLys {
    placed: Mutex<Vec<(String, String, String)>>,
    refuse: Option<&'static str>,
}

impl Placer for FakeLys {
    fn place(
        &self,
        operation: &str,
        child: (&str, &str),
        parent: (&str, &str),
    ) -> Result<(), String> {
        if self.refuse.is_some_and(|kind| kind == child.0) {
            return Err("WithheldFromAgents".to_owned());
        }
        assert_eq!(parent, ("liminal.network", "net"));
        self.placed
            .lock()
            .map_err(|_| "poisoned".to_owned())?
            .push((operation.to_owned(), child.0.to_owned(), child.1.to_owned()));
        Ok(())
    }
}

/// A fresh directory under the target's temporary space.
fn scratch(name: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!("liminal-lifecycle-{name}-{}", std::process::id()));
    let _gone = std::fs::remove_dir_all(&dir);
    dir
}

/// A template running `script` under `/bin/sh -c`.
fn template(script: &str) -> ServiceTemplateConfig {
    ServiceTemplateConfig {
        name: "echo".to_owned(),
        command: PathBuf::from("/bin/sh"),
        args: vec!["-c".to_owned(), script.to_owned()],
        subjects: vec!["echo".to_owned()],
        env: Vec::new(),
        stop_timeout_ms: 2_000,
    }
}

fn pipe(dir: &Path, name: &str) -> Result<PathBuf, LifecycleError> {
    std::fs::create_dir_all(dir).map_err(|error| LifecycleError::Store(error.to_string()))?;
    let path = dir.join(name);
    if let Ok(metadata) = std::fs::symlink_metadata(&path) {
        use std::os::unix::fs::FileTypeExt;
        if metadata.file_type().is_fifo() {
            return Ok(path);
        }
        return Err(LifecycleError::Process(
            "test FIFO path contains another shape".into(),
        ));
    }
    let status = std::process::Command::new("/usr/bin/mkfifo")
        .arg(&path)
        .status()
        .map_err(|error| LifecycleError::Store(error.to_string()))?;
    if !status.success() {
        return Err(LifecycleError::Process("test FIFO creation failed".into()));
    }
    Ok(path)
}
fn quoted(path: &Path) -> String {
    format!("'{}'", path.display().to_string().replace('\'', "'\\''"))
}
fn parked(dir: &Path) -> Result<String, LifecycleError> {
    Ok(format!("read value < {}", quoted(&pipe(dir, "parked")?)))
}
fn ready(dir: &Path) -> Result<(), LifecycleError> {
    let mut byte = [0];
    std::fs::File::open(dir.join("ready"))
        .and_then(|mut file| file.read_exact(&mut byte))
        .map_err(|error| LifecycleError::Store(error.to_string()))
}

fn open(dir: &Path, script: &str, lys: Arc<FakeLys>) -> Arc<Lifecycle> {
    let command = (|| {
        let signal = pipe(dir, "ready")?;
        let body = if matches!(script, "park" | "ignore-term") {
            parked(dir)?
        } else {
            script.into()
        };
        Ok::<_, LifecycleError>(format!(
            "{}printf . > {}; {body}",
            if script == "ignore-term" {
                "trap '' TERM; "
            } else {
                ""
            },
            quoted(&signal)
        ))
    })()
    .unwrap_or_else(|error| unreachable!("fixture signal setup failed: {error}"));
    let lifecycle = Lifecycle::open(
        &[template(&command)],
        dir.to_path_buf(),
        "net".to_owned(),
        Some(lys),
    )
    .unwrap_or_else(|error| unreachable!("the registry opens: {error}"));
    lifecycle.listening("127.0.0.1:1".to_owned());
    lifecycle
}

/// Waits for the registry's own durable commit or process-close signal.
fn wait_record(
    lifecycle: &Lifecycle,
    id: &str,
    done: impl Fn(&super::ServiceRecord, bool) -> bool,
) -> Result<(), LifecycleError> {
    let mut registry = lifecycle.lock()?;
    loop {
        let record = registry
            .records
            .get(id)
            .ok_or_else(|| LifecycleError::UnknownService(id.into()))?;
        if done(record, registry.running.contains_key(id)) {
            return Ok(());
        }
        registry = lifecycle
            .changed
            .wait(registry)
            .map_err(|_| LifecycleError::Store("test registry signal poisoned".into()))?;
    }
}

#[test]
fn create_places_the_service_and_subjects_then_records_asleep() -> Result<(), LifecycleError> {
    let dir = scratch("create");
    let lys = Arc::new(FakeLys::default());
    let lifecycle = open(&dir, "true", Arc::clone(&lys));
    let record = lifecycle.create("echo", "one", "agent-a", "op-1", 10)?;
    assert_eq!(record.state, ServiceState::Asleep);
    assert_eq!(record.subjects, vec!["echo.one".to_owned()]);
    assert_eq!(record.created_by, "agent-a");
    let placed = lys.placed.lock().map(|p| p.clone()).unwrap_or_default();
    let kinds: Vec<(&str, &str)> = placed
        .iter()
        .map(|(_, kind, id)| (kind.as_str(), id.as_str()))
        .collect();
    assert_eq!(
        kinds,
        vec![
            ("liminal.service", record.id.as_str()),
            ("liminal.subject", "echo.one")
        ]
    );
    // The same operation from the same holder is the same service, placed once.
    assert_eq!(
        lifecycle.create("echo", "one", "agent-a", "op-1", 11)?,
        record
    );
    assert_eq!(lys.placed.lock().map(|p| p.len()).unwrap_or_default(), 2);
    Ok(())
}

#[test]
fn create_refused_by_lys_records_nothing() {
    let dir = scratch("refused");
    let lys = Arc::new(FakeLys {
        refuse: Some("liminal.subject"),
        ..FakeLys::default()
    });
    let lifecycle = open(&dir, "true", lys);
    let refused = lifecycle.create("echo", "one", "agent-a", "op-1", 10);
    assert_eq!(
        refused,
        Err(LifecycleError::Placement("WithheldFromAgents".to_owned()))
    );
    let listed = lifecycle.list(|_| true).unwrap_or_default();
    assert_eq!(listed["services"], serde_json::json!([]));
}

#[test]
fn create_refuses_an_unknown_template_a_bad_name_and_a_taken_name() -> Result<(), LifecycleError> {
    let dir = scratch("names");
    let lifecycle = open(&dir, "true", Arc::new(FakeLys::default()));
    assert_eq!(
        lifecycle.create("nope", "one", "a", "op-1", 1),
        Err(LifecycleError::UnknownTemplate("nope".to_owned()))
    );
    assert_eq!(
        lifecycle.create("echo", "o.ne", "a", "op-1", 1),
        Err(LifecycleError::NameInvalid)
    );
    lifecycle.create("echo", "one", "a", "op-1", 1)?;
    assert_eq!(
        lifecycle.create("echo", "one", "b", "op-2", 1),
        Err(LifecycleError::NameTaken("one".to_owned()))
    );
    Ok(())
}

#[test]
fn registry_survives_a_restart_with_every_transition() -> Result<(), LifecycleError> {
    let dir = scratch("restart");
    let lys = Arc::new(FakeLys::default());
    let first = open(&dir, "true", Arc::clone(&lys));
    let made = first.create("echo", "one", "agent-a", "op-1", 10)?;
    let held = first.retire(&made.id, "agent-a", 20)?;
    drop(first);
    let second = open(&dir, "true", lys);
    let read = second.record(&made.id)?;
    assert_eq!(read, held);
    assert_eq!(read.state, ServiceState::Retired);
    assert_eq!(read.transitions.len(), 2);
    Ok(())
}

#[test]
fn deploy_starts_the_template_and_goes_live_on_registration() -> Result<(), LifecycleError> {
    let dir = scratch("deploy");
    let lifecycle = open(&dir, "park", Arc::new(FakeLys::default()));
    let made = lifecycle.create("echo", "one", "agent-a", "op-1", 10)?;
    lifecycle.deploy(&made.id, "agent-b")?;
    ready(&dir)?;
    // A second deploy while it runs does nothing.
    assert!(matches!(
        lifecycle.deploy(&made.id, "agent-b"),
        Err(LifecycleError::NotNow { running: true, .. })
    ));
    lifecycle.mark_live(&made.id, 30)?;
    let live = lifecycle.record(&made.id)?;
    assert_eq!(live.state, ServiceState::Live);
    assert_eq!(
        live.transitions.last().map(|t| t.holder.as_str()),
        Some("agent-b")
    );
    let held = lifecycle.hold(&made.id, "agent-b", 40)?;
    assert_eq!(held.state, ServiceState::Held);
    assert_eq!(lifecycle.list(|_| true)?["services"][0]["running"], false);
    lifecycle.wake(&made.id, "agent-b")?;
    lifecycle.retire(&made.id, "agent-b", 50)?;
    assert_eq!(lifecycle.record(&made.id)?.state, ServiceState::Retired);
    Ok(())
}

#[test]
fn hold_stops_within_the_timeout_even_when_sigterm_is_ignored() -> Result<(), LifecycleError> {
    let dir = scratch("stubborn");
    let lifecycle = open(&dir, "ignore-term", Arc::new(FakeLys::default()));
    let made = lifecycle.create("echo", "one", "a", "op-1", 1)?;
    lifecycle.deploy(&made.id, "a")?;
    ready(&dir)?;
    lifecycle.hold(&made.id, "a", 2)?;
    assert_eq!(lifecycle.list(|_| true)?["services"][0]["running"], false);
    assert_eq!(lifecycle.record(&made.id)?.state, ServiceState::Held);
    Ok(())
}

#[test]
fn a_process_credential_admits_only_its_own_subjects() -> Result<(), LifecycleError> {
    let dir = scratch("token");
    let file = dir.join("token");
    std::fs::create_dir_all(&dir).map_err(|error| LifecycleError::Store(error.to_string()))?;
    let status = std::process::Command::new("/usr/bin/mkfifo")
        .arg(&file)
        .status()
        .map_err(|error| LifecycleError::Store(error.to_string()))?;
    assert!(status.success(), "credential signal FIFO was not created");
    let mut written = template(&format!(
        "printf %s \"$LIMINAL_TOKEN\" > \"$0\"; {}",
        parked(&dir)?
    ));
    written.args.push(file.display().to_string());
    let lifecycle = Lifecycle::open(
        &[written],
        dir,
        "net".to_owned(),
        Some(Arc::new(FakeLys::default())),
    )?;
    lifecycle.listening("127.0.0.1:1".to_owned());
    let made = lifecycle.create("echo", "one", "a", "op-1", 1)?;
    lifecycle.deploy(&made.id, "a")?;
    let mut token = [0_u8; 64];
    std::fs::File::open(&file)
        .and_then(|mut signal| signal.read_exact(&mut token))
        .map_err(|error| LifecycleError::Store(error.to_string()))?;
    let grant = lifecycle.admit_process(&token);
    assert_eq!(
        grant.as_ref().map(|g| g.service.as_str()),
        Some(made.id.as_str())
    );
    assert!(grant.as_ref().is_some_and(|g| g.serves("echo.one")));
    assert!(grant.as_ref().is_some_and(|g| !g.serves("echo.two")));
    assert_eq!(lifecycle.admit_process(b"not-a-token"), None);
    lifecycle.hold(&made.id, "a", 2)?;
    assert_eq!(lifecycle.admit_process(&token), None, "a stop revokes it");
    Ok(())
}

#[test]
fn an_unasked_exit_is_recorded_and_not_restarted() -> Result<(), LifecycleError> {
    let dir = scratch("exit");
    let lifecycle = open(&dir, "exit 3", Arc::new(FakeLys::default()));
    let made = lifecycle.create("echo", "one", "a", "op-1", 1)?;
    lifecycle.deploy(&made.id, "a")?;
    ready(&dir)?;
    wait_record(&lifecycle, &made.id, |record, running| {
        record.transitions.len() == 2 && !running
    })?;
    let record = lifecycle.record(&made.id)?;
    assert_eq!(record.state, ServiceState::Asleep);
    let note = record
        .transitions
        .last()
        .and_then(|t| t.note.clone())
        .unwrap_or_default();
    assert!(
        note.contains("exited unasked") && note.contains('3'),
        "{note}"
    );
    assert_eq!(lifecycle.list(|_| true)?["services"][0]["running"], false);
    Ok(())
}

#[test]
fn restart_live_starts_what_was_live() -> Result<(), LifecycleError> {
    let dir = scratch("boot");
    let lys = Arc::new(FakeLys::default());
    let first = open(&dir, "park", Arc::clone(&lys));
    let made = first.create("echo", "one", "a", "op-1", 1)?;
    first.deploy(&made.id, "a")?;
    ready(&dir)?;
    first.mark_live(&made.id, 2)?;
    first.shutdown();
    assert_eq!(
        first.record(&made.id)?.state,
        ServiceState::Live,
        "shutdown keeps state"
    );
    drop(first);
    let second = open(&dir, "park", lys);
    assert_eq!(second.restart_live(3), Vec::new());
    ready(&dir)?;
    assert_eq!(second.list(|_| true)?["services"][0]["running"], true);
    second.shutdown();
    Ok(())
}

#[test]
fn an_exit_after_shutdown_began_keeps_live_and_the_next_start_restarts_it()
-> Result<(), LifecycleError> {
    let dir = scratch("disconnect");
    std::fs::create_dir_all(&dir).map_err(|error| LifecycleError::Store(error.to_string()))?;
    // the worker's connection: the process leaves when its writer closes
    let connection = dir.join("connection");
    let made_fifo = std::process::Command::new("/usr/bin/mkfifo")
        .arg(&connection)
        .status()
        .map_err(|error| LifecycleError::Store(error.to_string()))?;
    assert!(made_fifo.success(), "mkfifo {}", connection.display());
    let script = format!("read _ < '{}'; exit 1", connection.display());
    let lys = Arc::new(FakeLys::default());
    let first = open(&dir, &script, Arc::clone(&lys));
    let made = first.create("echo", "one", "a", "op-1", 1)?;
    first.deploy(&made.id, "a")?;
    ready(&dir)?;
    first.mark_live(&made.id, 2)?;
    let before = first.record(&made.id)?.transitions.len();
    first.shutdown_begun();
    // the server drains: the connection closes and the process exits 1
    drop(
        std::fs::OpenOptions::new()
            .write(true)
            .open(&connection)
            .map_err(|error| LifecycleError::Store(error.to_string()))?,
    );
    wait_record(&first, &made.id, |_, running| !running)?;
    let record = first.record(&made.id)?;
    assert_eq!(record.state, ServiceState::Live, "the exit kept live");
    assert_eq!(record.transitions.len(), before, "nothing recorded");
    first.shutdown();
    drop(first);
    let second = open(&dir, "park", lys);
    assert_eq!(second.restart_live(3), Vec::new());
    ready(&dir)?;
    assert_eq!(second.list(|_| true)?["services"][0]["running"], true);
    second.shutdown();
    Ok(())
}

#[test]
fn no_placer_refuses_every_create_by_name() {
    let dir = scratch("noplacer");
    let lifecycle = Lifecycle::open(&[template("true")], dir, "net".to_owned(), None)
        .unwrap_or_else(|error| unreachable!("the registry opens: {error}"));
    assert_eq!(
        lifecycle.create("echo", "one", "a", "op-1", 1),
        Err(LifecycleError::NoPlacer)
    );
}