forjar 1.4.2

Rust-native Infrastructure as Code — bare-metal first, BLAKE3 state, provenance tracing
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
//! FJ-52: Tests for autoinstall image generation.

#[cfg(test)]
mod tests {
    use crate::cli::image_cmd::*;
    use std::path::Path;

    fn write_config(dir: &Path, yaml: &str) -> std::path::PathBuf {
        let p = dir.join("forjar.yaml");
        std::fs::write(&p, yaml).unwrap();
        p
    }

    const SINGLE_MACHINE_YAML: &str = r#"
version: "1.0"
name: test-image
machines:
  yoga:
    hostname: yoga
    addr: 192.168.50.38
    user: noah
resources:
  pkg-curl:
    type: package
    machine: yoga
    provider: apt
    packages: [curl]
"#;

    const MULTI_MACHINE_YAML: &str = r#"
version: "1.0"
name: test-multi
machines:
  web:
    hostname: web-01
    addr: 10.0.0.1
    user: deploy
  db:
    hostname: db-01
    addr: 10.0.0.2
    user: deploy
resources:
  pkg-curl:
    type: package
    machine: web
    provider: apt
    packages: [curl]
"#;

    #[test]
    fn test_fj52_user_data_to_stdout() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), SINGLE_MACHINE_YAML);
        let result = cmd_image_user_data(
            &p,
            Some("yoga"),
            "auto-lvm",
            "en_US.UTF-8",
            "UTC",
            None,
            false,
        );
        assert!(result.is_ok(), "user_data must succeed: {result:?}");
    }

    #[test]
    fn test_fj52_user_data_to_file() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), SINGLE_MACHINE_YAML);
        let out = dir.path().join("user-data");
        let result = cmd_image_user_data(
            &p,
            Some("yoga"),
            "auto-lvm",
            "en_US.UTF-8",
            "UTC",
            Some(&out),
            false,
        );
        assert!(result.is_ok());
        assert!(out.exists(), "user-data file must exist");
        let content = std::fs::read_to_string(&out).unwrap();
        assert!(content.contains("#cloud-config"), "must have cloud-config header");
        assert!(content.contains("autoinstall:"), "must have autoinstall section");
    }

    #[test]
    fn test_fj52_user_data_json() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), SINGLE_MACHINE_YAML);
        let out = dir.path().join("user-data.yaml");
        let result = cmd_image_user_data(
            &p,
            Some("yoga"),
            "auto-lvm",
            "en_US.UTF-8",
            "UTC",
            Some(&out),
            true,
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_fj52_user_data_hostname() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), SINGLE_MACHINE_YAML);
        let out = dir.path().join("user-data");
        cmd_image_user_data(&p, Some("yoga"), "auto-lvm", "en_US.UTF-8", "UTC", Some(&out), false)
            .unwrap();
        let content = std::fs::read_to_string(&out).unwrap();
        assert!(content.contains("hostname: yoga"), "must set hostname: {content}");
    }

    #[test]
    fn test_fj52_user_data_username() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), SINGLE_MACHINE_YAML);
        let out = dir.path().join("user-data");
        cmd_image_user_data(&p, Some("yoga"), "auto-lvm", "en_US.UTF-8", "UTC", Some(&out), false)
            .unwrap();
        let content = std::fs::read_to_string(&out).unwrap();
        assert!(content.contains("username: noah"), "must set username: {content}");
    }

    #[test]
    fn test_fj52_user_data_locale_timezone() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), SINGLE_MACHINE_YAML);
        let out = dir.path().join("user-data");
        cmd_image_user_data(
            &p,
            Some("yoga"),
            "auto-lvm",
            "en_US.UTF-8",
            "America/New_York",
            Some(&out),
            false,
        )
        .unwrap();
        let content = std::fs::read_to_string(&out).unwrap();
        assert!(content.contains("locale: en_US.UTF-8"), "must set locale");
        assert!(
            content.contains("timezone: America/New_York"),
            "must set timezone"
        );
    }

    #[test]
    fn test_fj52_user_data_lvm_storage() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), SINGLE_MACHINE_YAML);
        let out = dir.path().join("user-data");
        cmd_image_user_data(&p, Some("yoga"), "auto-lvm", "en_US.UTF-8", "UTC", Some(&out), false)
            .unwrap();
        let content = std::fs::read_to_string(&out).unwrap();
        assert!(content.contains("name: lvm"), "auto-lvm must produce lvm layout");
    }

    #[test]
    fn test_fj52_user_data_zfs_storage() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), SINGLE_MACHINE_YAML);
        let out = dir.path().join("user-data");
        cmd_image_user_data(&p, Some("yoga"), "auto-zfs", "en_US.UTF-8", "UTC", Some(&out), false)
            .unwrap();
        let content = std::fs::read_to_string(&out).unwrap();
        assert!(content.contains("name: zfs"), "auto-zfs must produce zfs layout");
    }

    #[test]
    fn test_fj52_user_data_device_path() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), SINGLE_MACHINE_YAML);
        let out = dir.path().join("user-data");
        cmd_image_user_data(
            &p,
            Some("yoga"),
            "/dev/nvme0n1",
            "en_US.UTF-8",
            "UTC",
            Some(&out),
            false,
        )
        .unwrap();
        let content = std::fs::read_to_string(&out).unwrap();
        assert!(
            content.contains("/dev/nvme0n1"),
            "device path must appear: {content}"
        );
    }

    #[test]
    fn test_fj52_user_data_bad_disk() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), SINGLE_MACHINE_YAML);
        let result = cmd_image_user_data(
            &p,
            Some("yoga"),
            "garbage",
            "en_US.UTF-8",
            "UTC",
            None,
            false,
        );
        assert!(result.is_err(), "bad disk layout must fail");
        assert!(result.unwrap_err().contains("unknown disk layout"));
    }

    #[test]
    fn test_fj52_user_data_sudo() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), SINGLE_MACHINE_YAML);
        let out = dir.path().join("user-data");
        cmd_image_user_data(&p, Some("yoga"), "auto-lvm", "en_US.UTF-8", "UTC", Some(&out), false)
            .unwrap();
        let content = std::fs::read_to_string(&out).unwrap();
        assert!(
            content.contains("NOPASSWD:ALL"),
            "must configure passwordless sudo"
        );
        assert!(
            content.contains("sudoers.d/noah"),
            "must use username in sudoers path"
        );
    }

    #[test]
    fn test_fj52_user_data_firstboot_service() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), SINGLE_MACHINE_YAML);
        let out = dir.path().join("user-data");
        cmd_image_user_data(&p, Some("yoga"), "auto-lvm", "en_US.UTF-8", "UTC", Some(&out), false)
            .unwrap();
        let content = std::fs::read_to_string(&out).unwrap();
        assert!(
            content.contains("forjar-firstboot.service"),
            "must install firstboot service"
        );
        assert!(
            content.contains("forjar apply --yes"),
            "firstboot must run forjar apply"
        );
        assert!(
            content.contains("ConditionPathExists"),
            "must be idempotent"
        );
    }

    #[test]
    fn test_fj52_user_data_ssh_server() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), SINGLE_MACHINE_YAML);
        let out = dir.path().join("user-data");
        cmd_image_user_data(&p, Some("yoga"), "auto-lvm", "en_US.UTF-8", "UTC", Some(&out), false)
            .unwrap();
        let content = std::fs::read_to_string(&out).unwrap();
        assert!(
            content.contains("install-server: true"),
            "must install SSH server"
        );
        assert!(
            content.contains("openssh-server"),
            "must include openssh-server package"
        );
    }

    #[test]
    fn test_fj52_single_machine_auto_resolve() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), SINGLE_MACHINE_YAML);
        // No --machine specified, should auto-resolve single machine
        let result = cmd_image_user_data(&p, None, "auto-lvm", "en_US.UTF-8", "UTC", None, false);
        assert!(result.is_ok(), "single machine must auto-resolve");
    }

    #[test]
    fn test_fj52_multi_machine_requires_flag() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), MULTI_MACHINE_YAML);
        let result = cmd_image_user_data(&p, None, "auto-lvm", "en_US.UTF-8", "UTC", None, false);
        assert!(result.is_err(), "multi-machine without --machine must fail");
        let err = result.unwrap_err();
        assert!(
            err.contains("--machine"),
            "error must mention --machine: {err}"
        );
    }

    #[test]
    fn test_fj52_multi_machine_with_flag() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), MULTI_MACHINE_YAML);
        let result =
            cmd_image_user_data(&p, Some("web"), "auto-lvm", "en_US.UTF-8", "UTC", None, false);
        assert!(result.is_ok(), "selecting a machine must succeed");
    }

    #[test]
    fn test_fj52_missing_machine() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), SINGLE_MACHINE_YAML);
        let result = cmd_image_user_data(
            &p,
            Some("nonexistent"),
            "auto-lvm",
            "en_US.UTF-8",
            "UTC",
            None,
            false,
        );
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("not found"));
    }

    #[test]
    fn test_fj52_iso_missing_base() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), SINGLE_MACHINE_YAML);
        let result = cmd_image_iso(
            &p,
            Some("yoga"),
            Path::new("/nonexistent/base.iso"),
            Path::new("/tmp/out.iso"),
            "auto-lvm",
            "en_US.UTF-8",
            "UTC",
            false,
        );
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("base ISO not found"));
    }

    #[test]
    fn test_fj52_user_data_forjar_binary_copy() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), SINGLE_MACHINE_YAML);
        let out = dir.path().join("user-data");
        cmd_image_user_data(&p, Some("yoga"), "auto-lvm", "en_US.UTF-8", "UTC", Some(&out), false)
            .unwrap();
        let content = std::fs::read_to_string(&out).unwrap();
        assert!(
            content.contains("/usr/local/bin/forjar"),
            "must copy forjar binary to target"
        );
    }

    #[test]
    fn test_fj52_user_data_config_copy() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), SINGLE_MACHINE_YAML);
        let out = dir.path().join("user-data");
        cmd_image_user_data(&p, Some("yoga"), "auto-lvm", "en_US.UTF-8", "UTC", Some(&out), false)
            .unwrap();
        let content = std::fs::read_to_string(&out).unwrap();
        assert!(
            content.contains("/etc/forjar/forjar.yaml"),
            "must copy forjar config to target"
        );
    }

    #[test]
    fn test_fj52_expand_tilde() {
        let home = std::env::var("HOME").unwrap_or_default();
        let result = super::tests::expand_tilde_wrapper("~/foo/bar");
        if !home.is_empty() {
            assert_eq!(result, format!("{home}/foo/bar"));
        }
        // No tilde
        assert_eq!(
            super::tests::expand_tilde_wrapper("/absolute/path"),
            "/absolute/path"
        );
    }

    // Wrapper to call private fn
    pub(super) fn expand_tilde_wrapper(path: &str) -> String {
        crate::cli::image_cmd::expand_tilde(path)
    }

    #[test]
    fn test_fj52_user_data_static_ip_comment() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_config(dir.path(), SINGLE_MACHINE_YAML);
        let out = dir.path().join("user-data");
        cmd_image_user_data(&p, Some("yoga"), "auto-lvm", "en_US.UTF-8", "UTC", Some(&out), false)
            .unwrap();
        let content = std::fs::read_to_string(&out).unwrap();
        assert!(
            content.contains("192.168.50.38"),
            "must include static IP comment"
        );
    }

    #[test]
    fn test_fj52_user_data_root_user() {
        let dir = tempfile::tempdir().unwrap();
        let yaml = r#"
version: "1.0"
name: test
machines:
  srv:
    hostname: server
    addr: 10.0.0.5
    user: root
resources:
  pkg:
    type: package
    machine: srv
    provider: apt
    packages: [vim]
"#;
        let p = write_config(dir.path(), yaml);
        let out = dir.path().join("user-data");
        cmd_image_user_data(&p, Some("srv"), "auto-lvm", "en_US.UTF-8", "UTC", Some(&out), false)
            .unwrap();
        let content = std::fs::read_to_string(&out).unwrap();
        assert!(content.contains("username: root"), "must use root username");
    }
}