cfgd-core 0.4.0

Core library for cfgd — shared types, providers, reconciler, state
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
use super::*;

#[test]
fn detect_returns_valid_platform() {
    let platform = Platform::detect();
    // We can't assert specific values since tests run on different platforms,
    // but we can verify the struct is populated
    assert!(!format!("{}", platform.os).is_empty());
    assert!(!format!("{}", platform.arch).is_empty());
}

#[test]
fn native_manager_mapping() {
    let cases: &[(Os, Distro, &str, &str)] = &[
        (Os::MacOS, Distro::MacOS, "14.0", "brew"),
        (Os::Linux, Distro::Ubuntu, "22.04", "apt"),
        (Os::Linux, Distro::Debian, "12", "apt"),
        (Os::Linux, Distro::Fedora, "39", "dnf"),
        (Os::Linux, Distro::RHEL, "7.9", "yum"),
        (Os::Linux, Distro::RHEL, "8.9", "dnf"),
        (Os::Linux, Distro::Arch, "", "pacman"),
        (Os::Linux, Distro::Alpine, "3.19", "apk"),
        (Os::Linux, Distro::OpenSUSE, "15.5", "zypper"),
        (Os::FreeBSD, Distro::FreeBSD, "14.0", "pkg"),
    ];
    for (os, distro, version, expected) in cases {
        let p = Platform {
            os: os.clone(),
            distro: distro.clone(),
            version: version.to_string(),
            arch: Arch::X86_64,
        };
        assert_eq!(
            p.native_manager(),
            *expected,
            "failed for {:?}/{:?}",
            os,
            distro
        );
    }
}

#[test]
fn parse_os_release_ubuntu() {
    let content = r#"
NAME="Ubuntu"
VERSION="22.04.3 LTS (Jammy Jellyfish)"
ID=ubuntu
ID_LIKE=debian
VERSION_ID="22.04"
"#;
    let (distro, version) = parse_os_release_content_to_distro(content);
    assert_eq!(distro, Distro::Ubuntu);
    assert_eq!(version, "22.04");
}

#[test]
fn parse_os_release_fedora() {
    let content = r#"
NAME="Fedora Linux"
ID=fedora
VERSION_ID=39
"#;
    let (distro, version) = parse_os_release_content_to_distro(content);
    assert_eq!(distro, Distro::Fedora);
    assert_eq!(version, "39");
}

#[test]
fn parse_os_release_arch() {
    let content = r#"
NAME="Arch Linux"
ID=arch
"#;
    let (distro, version) = parse_os_release_content_to_distro(content);
    assert_eq!(distro, Distro::Arch);
    assert_eq!(version, "");
}

#[test]
fn parse_os_release_alpine() {
    let content = r#"
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.19.0
"#;
    let (distro, version) = parse_os_release_content_to_distro(content);
    assert_eq!(distro, Distro::Alpine);
    assert_eq!(version, "3.19.0");
}

#[test]
fn parse_os_release_derivative() {
    let content = r#"
NAME="Linux Mint"
ID=linuxmint
ID_LIKE="ubuntu debian"
VERSION_ID="21.2"
"#;
    let (distro, version) = parse_os_release_content_to_distro(content);
    assert_eq!(distro, Distro::Ubuntu);
    assert_eq!(version, "21.2");
}

#[test]
fn parse_os_release_opensuse_leap() {
    let content = r#"
NAME="openSUSE Leap"
ID="opensuse-leap"
VERSION_ID="15.5"
"#;
    let (distro, version) = parse_os_release_content_to_distro(content);
    assert_eq!(distro, Distro::OpenSUSE);
    assert_eq!(version, "15.5");
}

#[test]
fn parse_os_release_centos() {
    let content = r#"
NAME="CentOS Linux"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
"#;
    let (distro, version) = parse_os_release_content_to_distro(content);
    assert_eq!(distro, Distro::CentOS);
    assert_eq!(version, "7");
}

#[test]
fn enum_display_formatting() {
    // Arch
    assert_eq!(format!("{}", Arch::X86_64), "x86_64");
    assert_eq!(format!("{}", Arch::Aarch64), "aarch64");
    assert_eq!(format!("{}", Arch::Other("riscv64".into())), "riscv64");
    // Os
    assert_eq!(format!("{}", Os::Linux), "linux");
    assert_eq!(format!("{}", Os::MacOS), "macos");
    assert_eq!(format!("{}", Os::FreeBSD), "freebsd");
    // Distro
    assert_eq!(format!("{}", Distro::Ubuntu), "ubuntu");
    assert_eq!(format!("{}", Distro::RHEL), "rhel");
    assert_eq!(format!("{}", Distro::OpenSUSE), "opensuse");
}

/// Helper: parse os-release content string into (Distro, version).
/// Delegates to the shared `distro_from_os_release_content`.
fn parse_os_release_content_to_distro(content: &str) -> (Distro, String) {
    distro_from_os_release_content(content)
}

#[test]
fn platform_matches_any_os() {
    let p = Platform {
        os: Os::Linux,
        distro: Distro::Ubuntu,
        version: "22.04".into(),
        arch: Arch::X86_64,
    };
    assert!(p.matches_any(&["linux".into()]));
    assert!(!p.matches_any(&["macos".into()]));
}

#[test]
fn platform_matches_any_distro() {
    let p = Platform {
        os: Os::Linux,
        distro: Distro::Ubuntu,
        version: "22.04".into(),
        arch: Arch::X86_64,
    };
    assert!(p.matches_any(&["ubuntu".into()]));
    assert!(!p.matches_any(&["fedora".into()]));
}

#[test]
fn platform_matches_any_arch() {
    let p = Platform {
        os: Os::Linux,
        distro: Distro::Ubuntu,
        version: "22.04".into(),
        arch: Arch::Aarch64,
    };
    assert!(p.matches_any(&["aarch64".into()]));
    assert!(!p.matches_any(&["x86_64".into()]));
}

#[test]
fn platform_matches_any_empty_matches_all() {
    let p = Platform {
        os: Os::MacOS,
        distro: Distro::MacOS,
        version: "14.0".into(),
        arch: Arch::Aarch64,
    };
    assert!(p.matches_any(&[]));
}

#[test]
fn platform_matches_any_multiple_tags() {
    let p = Platform {
        os: Os::Linux,
        distro: Distro::Fedora,
        version: "39".into(),
        arch: Arch::X86_64,
    };
    // Any match is sufficient
    assert!(p.matches_any(&["macos".into(), "linux".into()]));
    assert!(p.matches_any(&["ubuntu".into(), "fedora".into()]));
    assert!(!p.matches_any(&["macos".into(), "freebsd".into()]));
}

// --- native_manager: additional distro mappings ---

#[test]
fn native_manager_centos_7_uses_yum() {
    let p = Platform {
        os: Os::Linux,
        distro: Distro::CentOS,
        version: "7.9".into(),
        arch: Arch::X86_64,
    };
    assert_eq!(p.native_manager(), "yum");
}

#[test]
fn native_manager_centos_8_uses_dnf() {
    let p = Platform {
        os: Os::Linux,
        distro: Distro::CentOS,
        version: "8.5".into(),
        arch: Arch::X86_64,
    };
    assert_eq!(p.native_manager(), "dnf");
}

#[test]
fn native_manager_manjaro() {
    let p = Platform {
        os: Os::Linux,
        distro: Distro::Manjaro,
        version: "23.0".into(),
        arch: Arch::X86_64,
    };
    assert_eq!(p.native_manager(), "pacman");
}

#[test]
fn native_manager_windows() {
    let p = Platform {
        os: Os::Windows,
        distro: Distro::Windows,
        version: String::new(),
        arch: Arch::X86_64,
    };
    assert_eq!(p.native_manager(), "winget");
}

#[test]
fn native_manager_unknown_defaults_to_apt() {
    let p = Platform {
        os: Os::Linux,
        distro: Distro::Unknown,
        version: String::new(),
        arch: Arch::X86_64,
    };
    assert_eq!(p.native_manager(), "apt");
}

// --- distro_from_os_release_content: ID_LIKE derivative detection ---

#[test]
fn parse_os_release_debian_only_derivative() {
    // A distro with ID_LIKE=debian (not ubuntu)
    let content = r#"
NAME="Raspberry Pi OS"
ID=raspbian
ID_LIKE=debian
VERSION_ID="11"
"#;
    let (distro, version) = distro_from_os_release_content(content);
    assert_eq!(distro, Distro::Debian);
    assert_eq!(version, "11");
}

#[test]
fn parse_os_release_fedora_derivative() {
    // A distro with ID_LIKE containing fedora
    let content = r#"
NAME="Nobara"
ID=nobara
ID_LIKE="fedora"
VERSION_ID="38"
"#;
    let (distro, version) = distro_from_os_release_content(content);
    assert_eq!(distro, Distro::Fedora);
    assert_eq!(version, "38");
}

#[test]
fn parse_os_release_rhel_derivative() {
    // A distro with ID_LIKE containing rhel
    let content = r#"
NAME="Rocky Linux"
ID=rocky
ID_LIKE="rhel centos fedora"
VERSION_ID="9.2"
"#;
    let (distro, version) = distro_from_os_release_content(content);
    assert_eq!(distro, Distro::Fedora);
    assert_eq!(version, "9.2");
}

#[test]
fn parse_os_release_arch_derivative() {
    // A distro with ID_LIKE containing arch
    let content = r#"
NAME="EndeavourOS"
ID=endeavouros
ID_LIKE=arch
VERSION_ID="2023.11.17"
"#;
    let (distro, version) = distro_from_os_release_content(content);
    assert_eq!(distro, Distro::Arch);
    assert_eq!(version, "2023.11.17");
}

#[test]
fn parse_os_release_suse_derivative() {
    // A distro with ID_LIKE containing suse
    let content = r#"
NAME="GeckoLinux"
ID=geckolinux
ID_LIKE="suse opensuse"
VERSION_ID="999"
"#;
    let (distro, version) = distro_from_os_release_content(content);
    assert_eq!(distro, Distro::OpenSUSE);
    assert_eq!(version, "999");
}

#[test]
fn parse_os_release_unknown_distro() {
    let content = r#"
NAME="Exotic Linux"
ID=exotic
VERSION_ID="1.0"
"#;
    let (distro, version) = distro_from_os_release_content(content);
    assert_eq!(distro, Distro::Unknown);
    assert_eq!(version, "1.0");
}

#[test]
fn parse_os_release_rhel_id() {
    let content = "ID=rhel\nVERSION_ID=9.2\n";
    let (distro, version) = distro_from_os_release_content(content);
    assert_eq!(distro, Distro::RHEL);
    assert_eq!(version, "9.2");
}

#[test]
fn parse_os_release_redhat_id() {
    let content = "ID=redhat\nVERSION_ID=8\n";
    let (distro, version) = distro_from_os_release_content(content);
    assert_eq!(distro, Distro::RHEL);
    assert_eq!(version, "8");
}

#[test]
fn parse_os_release_archlinux_id() {
    let content = "ID=archlinux\n";
    let (distro, _) = distro_from_os_release_content(content);
    assert_eq!(distro, Distro::Arch);
}

#[test]
fn parse_os_release_opensuse_tumbleweed() {
    let content = "ID=opensuse-tumbleweed\nVERSION_ID=20231201\n";
    let (distro, version) = distro_from_os_release_content(content);
    assert_eq!(distro, Distro::OpenSUSE);
    assert_eq!(version, "20231201");
}

#[test]
fn parse_os_release_manjaro() {
    let content = "ID=manjaro\nVERSION_ID=23.0\n";
    let (distro, version) = distro_from_os_release_content(content);
    assert_eq!(distro, Distro::Manjaro);
    assert_eq!(version, "23.0");
}

// --- parse_os_release_content: edge cases ---

#[test]
fn parse_os_release_content_handles_comments_and_blanks() {
    let content = "# This is a comment\n\nID=ubuntu\n\n# Another comment\nVERSION_ID=\"22.04\"\n";
    let fields = parse_os_release_content(content);
    assert_eq!(fields.get("ID").unwrap(), "ubuntu");
    assert_eq!(fields.get("VERSION_ID").unwrap(), "22.04");
}

#[test]
fn parse_os_release_content_handles_single_quotes() {
    let content = "ID='fedora'\nVERSION_ID='39'\n";
    let fields = parse_os_release_content(content);
    assert_eq!(fields.get("ID").unwrap(), "fedora");
    assert_eq!(fields.get("VERSION_ID").unwrap(), "39");
}

#[test]
fn parse_os_release_content_no_equals() {
    let content = "NOEQUALS\nID=test\n";
    let fields = parse_os_release_content(content);
    assert!(!fields.contains_key("NOEQUALS"));
    assert_eq!(fields.get("ID").unwrap(), "test");
}

// --- Display and as_str coverage ---

#[test]
fn os_windows_display() {
    assert_eq!(format!("{}", Os::Windows), "windows");
}

#[test]
fn distro_display_all_variants() {
    let cases: &[(Distro, &str)] = &[
        (Distro::CentOS, "centos"),
        (Distro::Manjaro, "manjaro"),
        (Distro::FreeBSD, "freebsd"),
        (Distro::MacOS, "macos"),
        (Distro::Windows, "windows"),
        (Distro::Unknown, "unknown"),
    ];
    for (distro, expected) in cases {
        assert_eq!(distro.as_str(), *expected);
        assert_eq!(format!("{}", distro), *expected);
    }
}

#[cfg(unix)]
#[test]
fn read_command_output_returns_trimmed_stdout_on_success() {
    let s = super::read_command_output("printf", &["hello world"]).expect("Ok");
    assert_eq!(s, "hello world");
}

#[cfg(unix)]
#[test]
fn read_command_output_errors_on_nonzero_with_stderr_payload() {
    // `sh -c 'echo boom >&2; exit 3'` — nonzero exit + stderr text. Error
    // must include the stderr in the message rather than the exit code.
    let err = super::read_command_output("sh", &["-c", "echo boom >&2; exit 3"])
        .expect_err("nonzero must error");
    let msg = err.to_string();
    assert!(
        msg.contains("boom") && msg.contains("sh"),
        "error must include stderr + command name: {msg}"
    );
}

#[cfg(unix)]
#[test]
fn read_command_output_errors_on_nonzero_without_stderr_falls_back_to_exit_code() {
    // `sh -c 'exit 7'` — nonzero exit, empty stderr. Error must mention
    // exit code in lieu of stderr.
    let err = super::read_command_output("sh", &["-c", "exit 7"]).expect_err("nonzero must error");
    let msg = err.to_string();
    assert!(
        msg.contains("exit code") && msg.contains("7"),
        "empty-stderr error must surface exit code: {msg}"
    );
}

#[test]
fn read_command_output_errors_on_missing_binary() {
    let err = super::read_command_output("definitely-not-a-real-command-xyzzy", &[])
        .expect_err("missing binary must error");
    let _ = err.to_string();
}