unitbus 0.1.7

Rust SDK for Linux systemd: control units/jobs via D-Bus (systemctl-like), run transient tasks, and query journald logs (sdjournal default, optional journalctl JSON).
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#![cfg(target_os = "linux")]

// Linux/systemd integration tests.
//
// These are ignored by default and are intended to be run on a real systemd host:
// - `UNITBUS_ITEST_UNIT`: a safe unit name to restart (e.g. "cron.service" in a test VM)
// - `UNITBUS_ITEST_DROPIN_UNIT`: a unit name to write drop-ins for (requires root/policy)

use std::future::Future;
use std::time::Duration;

use unitbus::{UnitBus, UnitStartMode};

fn block_on<T>(fut: impl Future<Output = T>) -> T {
    #[cfg(feature = "rt-async-io")]
    {
        smol::block_on(fut)
    }

    #[cfg(feature = "rt-tokio")]
    {
        let rt = tokio::runtime::Runtime::new().expect("init tokio runtime");
        rt.block_on(fut)
    }
}

fn env(name: &str) -> Option<String> {
    match std::env::var(name) {
        Ok(v) if !v.trim().is_empty() => Some(v),
        _ => None,
    }
}

fn find_executable(candidates: &[&str]) -> Option<String> {
    for c in candidates {
        if std::path::Path::new(c).exists() {
            return Some((*c).to_string());
        }
    }
    None
}

#[test]
#[ignore]
fn manager_list_units_and_info_read_only() {
    block_on(async {
        let bus = UnitBus::connect_system().await?;

        let units = match bus.manager().list_units().await {
            Ok(v) => v,
            Err(unitbus::Error::PermissionDenied { .. }) => {
                eprintln!("permission denied; skipping manager list_units");
                return Ok(());
            }
            Err(unitbus::Error::BackendUnavailable { .. }) => {
                eprintln!("system bus/systemd unavailable; skipping manager list_units");
                return Ok(());
            }
            Err(e) => return Err(e),
        };

        assert!(!units.is_empty(), "expected at least one unit");
        for u in units.iter().take(50) {
            assert!(!u.name.trim().is_empty(), "unit name must not be empty");
            assert!(
                u.unit_path.starts_with("/org/freedesktop/systemd1/unit/"),
                "unexpected unit path: {}",
                u.unit_path
            );

            if u.job_id.is_some() {
                assert!(
                    u.job_path.is_some(),
                    "job_path should exist when job_id exists"
                );
            }
        }

        let info = match bus.manager().info().await {
            Ok(i) => i,
            Err(unitbus::Error::PermissionDenied { .. }) => {
                eprintln!("permission denied; skipping manager info");
                return Ok(());
            }
            Err(unitbus::Error::BackendUnavailable { .. }) => {
                eprintln!("system bus/systemd unavailable; skipping manager info");
                return Ok(());
            }
            Err(e) => return Err(e),
        };

        assert!(
            info.system_state.is_some() || info.version.is_some() || info.virtualization.is_some(),
            "expected at least one manager info field"
        );

        Ok::<(), unitbus::Error>(())
    })
    .unwrap();
}

#[test]
#[ignore]
fn manager_list_units_filtered_active_only_contains_active() {
    block_on(async {
        let bus = UnitBus::connect_system().await?;

        let units = match bus.manager().list_units_filtered(&["active"]).await {
            Ok(v) => v,
            Err(unitbus::Error::PermissionDenied { .. }) => {
                eprintln!("permission denied; skipping list_units_filtered");
                return Ok(());
            }
            Err(unitbus::Error::BackendUnavailable { .. }) => {
                eprintln!("system bus/systemd unavailable; skipping list_units_filtered");
                return Ok(());
            }
            Err(e) => return Err(e),
        };

        for u in units {
            assert_eq!(
                u.active_state.as_str(),
                "active",
                "expected only active units"
            );
        }

        Ok::<(), unitbus::Error>(())
    })
    .unwrap();
}

#[test]
#[ignore]
fn can_read_unit_properties_by_path_read_only() {
    block_on(async {
        let bus = UnitBus::connect_system().await?;

        let units = match bus.manager().list_units().await {
            Ok(v) => v,
            Err(unitbus::Error::PermissionDenied { .. }) => {
                eprintln!("permission denied; skipping");
                return Ok(());
            }
            Err(unitbus::Error::BackendUnavailable { .. }) => {
                eprintln!("system bus/systemd unavailable; skipping");
                return Ok(());
            }
            Err(e) => return Err(e),
        };

        let Some(u) = units.first() else {
            return Ok(());
        };

        let props = bus
            .units()
            .get_unit_properties_by_path(&u.unit_path)
            .await?;
        let id = props.get_opt_str("Id").unwrap_or("");
        assert!(!id.is_empty(), "expected Unit.Id to be present");

        // Only a best-effort check: non-service units return None.
        let _service_props = bus
            .units()
            .get_service_properties_by_path(&u.unit_path)
            .await?;

        Ok::<(), unitbus::Error>(())
    })
    .unwrap();
}

#[test]
#[ignore]
fn restart_and_wait() {
    let unit = match env("UNITBUS_ITEST_UNIT") {
        Some(u) => u,
        None => {
            eprintln!("set UNITBUS_ITEST_UNIT to a safe systemd unit to restart");
            return;
        }
    };

    block_on(async {
        let bus = UnitBus::connect_system().await?;
        let job = bus.units().restart(&unit, UnitStartMode::Replace).await?;
        let outcome = job.wait(Duration::from_secs(30)).await?;
        eprintln!("outcome={outcome:?}");
        Ok::<(), unitbus::Error>(())
    })
    .unwrap();
}

#[cfg(all(
    feature = "tasks",
    any(feature = "journal-cli", feature = "journal-sdjournal")
))]
#[test]
#[ignore]
fn run_task_echo_and_fetch_logs() {
    let echo = match find_executable(&["/bin/echo", "/usr/bin/echo"]) {
        Some(p) => p,
        None => {
            eprintln!("cannot find /bin/echo or /usr/bin/echo; skipping");
            return;
        }
    };

    block_on(async {
        let bus = UnitBus::connect_system().await?;

        let mut spec = unitbus::TaskSpec::default();
        spec.argv = vec![echo, "hello".to_string()];
        spec.timeout = Duration::from_secs(10);
        spec.name_hint = Some("itest".to_string());

        let task = bus.tasks().run(spec).await?;
        let res = task.wait(Duration::from_secs(30)).await?;

        assert_eq!(
            res.exit_status,
            Some(0),
            "expected successful exit; status={:?}",
            res.unit_status
        );

        let mut filter = unitbus::JournalFilter::default();
        filter.unit = Some(task.unit.clone());
        filter.limit = 50;
        filter.timeout = Some(Duration::from_secs(5));

        let logs = match bus.journal().query(filter).await {
            Ok(r) => r.entries,
            Err(unitbus::Error::PermissionDenied { .. }) => {
                eprintln!("journal permission denied; skipping log assertion");
                return Ok(());
            }
            Err(e) => return Err(e),
        };

        let mut saw = false;
        for e in logs {
            if let Some(m) = e.message.as_deref()
                && m.contains("hello")
            {
                saw = true;
                break;
            }
        }
        assert!(saw, "expected at least one log line containing 'hello'");

        Ok::<(), unitbus::Error>(())
    })
    .unwrap();
}

#[cfg(all(
    feature = "tasks",
    any(feature = "journal-cli", feature = "journal-sdjournal")
))]
#[test]
#[ignore]
fn run_task_failure_can_diagnose() {
    let false_bin = match find_executable(&["/bin/false", "/usr/bin/false"]) {
        Some(p) => p,
        None => {
            eprintln!("cannot find /bin/false or /usr/bin/false; skipping");
            return;
        }
    };

    block_on(async {
        let bus = UnitBus::connect_system().await?;

        let mut spec = unitbus::TaskSpec::default();
        spec.argv = vec![false_bin];
        spec.timeout = Duration::from_secs(10);
        spec.name_hint = Some("itest-fail".to_string());

        let task = bus.tasks().run(spec).await?;
        let res = task.wait(Duration::from_secs(30)).await?;

        assert_ne!(
            res.exit_status,
            Some(0),
            "expected non-zero exit; status={:?}",
            res.unit_status
        );

        let diag = match bus
            .journal()
            .diagnose_unit_failure(&task.unit, Default::default())
            .await
        {
            Ok(d) => d,
            Err(unitbus::Error::PermissionDenied { .. }) => {
                eprintln!("journal permission denied; skipping diagnose assertion");
                return Ok(());
            }
            Err(e) => return Err(e),
        };

        assert_eq!(diag.status.id, task.unit);
        assert!(diag.logs.len() <= 200);
        Ok::<(), unitbus::Error>(())
    })
    .unwrap();
}

#[cfg(feature = "config")]
#[test]
#[ignore]
fn dropin_apply_remove_idempotent() {
    let unit = match env("UNITBUS_ITEST_DROPIN_UNIT") {
        Some(u) => u,
        None => {
            eprintln!("set UNITBUS_ITEST_DROPIN_UNIT to a unit to write drop-ins for");
            return;
        }
    };

    block_on(async {
        let bus = UnitBus::connect_system().await?;

        let mut spec = unitbus::DropInSpec::default();
        spec.unit = unit.clone();
        spec.name = "unitbus-itest".to_string();
        spec.environment
            .insert("UNITBUS_ITEST".to_string(), "1".to_string());

        let r1 = match bus.config().apply_dropin(spec.clone()).await {
            Ok(r) => r,
            Err(unitbus::Error::PermissionDenied { .. }) => {
                eprintln!("drop-in write permission denied; skipping");
                return Ok(());
            }
            Err(e) => return Err(e),
        };
        assert!(r1.requires_daemon_reload);

        let r2 = bus.config().apply_dropin(spec).await?;
        assert!(!r2.changed, "expected idempotent apply");

        let rm1 = bus.config().remove_dropin(&unit, "unitbus-itest").await?;
        assert!(rm1.requires_daemon_reload);

        let rm2 = bus.config().remove_dropin(&unit, "unitbus-itest").await?;
        assert!(!rm2.changed, "expected idempotent remove");

        Ok::<(), unitbus::Error>(())
    })
    .unwrap();
}

#[cfg(feature = "config")]
#[test]
#[ignore]
fn install_uninstall_service_unit_file() {
    let unit = match env("UNITBUS_ITEST_UNITFILE_UNIT") {
        Some(u) => u,
        None => {
            eprintln!(
                "set UNITBUS_ITEST_UNITFILE_UNIT to a unique service name (e.g. unitbus-itest.service)"
            );
            return;
        }
    };

    let exe = match find_executable(&["/bin/sleep", "/usr/bin/sleep", "/bin/true", "/usr/bin/true"])
    {
        Some(p) => p,
        None => {
            eprintln!("cannot find /bin/sleep or /bin/true; skipping");
            return;
        }
    };

    block_on(async {
        let bus = UnitBus::connect_system().await?;

        let argv = if exe.ends_with("/sleep") {
            vec![exe, "1".to_string()]
        } else {
            vec![exe]
        };

        let mut spec = unitbus::ServiceUnitSpec::default();
        spec.unit = unit.clone();
        spec.description = Some("unitbus integration test service unit".to_string());
        spec.service_type = Some(unitbus::ServiceType::Oneshot);
        spec.exec_start = argv;
        spec.wanted_by = vec!["multi-user.target".to_string()];

        let unit_name = spec.canonical_unit_name()?;

        let install = match bus
            .config()
            .install_service_unit(spec, Default::default())
            .await
        {
            Ok(r) => r,
            Err(unitbus::Error::PermissionDenied { .. }) => {
                eprintln!("permission denied; skipping install_service_unit");
                return Ok(());
            }
            Err(e) => {
                let _ = bus.config().remove_unit_file(&unit_name).await;
                let _ = bus.config().daemon_reload().await;
                return Err(e);
            }
        };

        let uninstall = match bus
            .config()
            .uninstall_unit(&install.unit, Default::default())
            .await
        {
            Ok(r) => r,
            Err(unitbus::Error::PermissionDenied { .. }) => {
                eprintln!(
                    "permission denied; skipping uninstall cleanup (manual cleanup may be required)"
                );
                return Ok(());
            }
            Err(e) => return Err(e),
        };

        assert!(
            uninstall.removed.path_removed.contains(&install.unit),
            "expected removed file path to mention unit name"
        );

        Ok::<(), unitbus::Error>(())
    })
    .unwrap();
}