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
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
482
483
484
//! Tests: Pre-condition checks.

#![allow(unused_imports)]
use super::check::*;
use super::commands::*;
use super::destroy::*;
use super::diff_cmd::*;
use super::dispatch::*;
use super::helpers::*;
use super::helpers_state::*;
use super::helpers_time::*;
use super::init::*;
use super::lint::*;
use super::observe::*;
use super::test_fixtures::*;
use crate::core::types::ProvenanceEvent;
use crate::core::{codegen, executor, migrate, parser, planner, resolver, secrets, state, types};
use crate::transport;
use crate::tripwire::{anomaly, drift, eventlog, tracer};
use std::path::{Path, PathBuf};

#[cfg(test)]
mod tests {
    use super::*;

    /// Helper: check if a config has any cross-machine dependencies.
    fn has_cross_machine_dependency(config: &types::ForjarConfig) -> bool {
        for (_id, resource) in &config.resources {
            let my_machines: std::collections::HashSet<String> =
                resource.machine.to_vec().into_iter().collect();
            for dep in &resource.depends_on {
                if let Some(dep_resource) = config.resources.get(dep) {
                    let dep_machines: std::collections::HashSet<String> =
                        dep_resource.machine.to_vec().into_iter().collect();
                    if my_machines.is_disjoint(&dep_machines) {
                        return true;
                    }
                }
            }
        }
        false
    }

    #[test]
    fn test_diff_machine_filter() {
        let from_dir = tempfile::tempdir().unwrap();
        let to_dir = tempfile::tempdir().unwrap();
        make_state_dir_with_lock(
            from_dir.path(),
            "m1",
            vec![("pkg", "blake3:aaa", types::ResourceStatus::Converged)],
        );
        make_state_dir_with_lock(
            from_dir.path(),
            "m2",
            vec![("svc", "blake3:bbb", types::ResourceStatus::Converged)],
        );
        make_state_dir_with_lock(
            to_dir.path(),
            "m1",
            vec![("pkg", "blake3:changed", types::ResourceStatus::Converged)],
        );
        make_state_dir_with_lock(
            to_dir.path(),
            "m2",
            vec![("svc", "blake3:bbb", types::ResourceStatus::Converged)],
        );
        // Filtering to m1 should only show m1's changes
        cmd_diff(from_dir.path(), to_dir.path(), Some("m1"), None, false).unwrap();
    }

    #[test]
    fn test_diff_empty_state_dirs() {
        let from_dir = tempfile::tempdir().unwrap();
        let to_dir = tempfile::tempdir().unwrap();
        let result = cmd_diff(from_dir.path(), to_dir.path(), None, None, false);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("no machines found"));
    }

    #[test]
    fn test_discover_machines() {
        let dir = tempfile::tempdir().unwrap();
        make_state_dir_with_lock(
            dir.path(),
            "alpha",
            vec![("f", "blake3:x", types::ResourceStatus::Converged)],
        );
        make_state_dir_with_lock(
            dir.path(),
            "beta",
            vec![("f", "blake3:y", types::ResourceStatus::Converged)],
        );
        let machines = discover_machines(dir.path());
        assert_eq!(machines, vec!["alpha", "beta"]);
    }

    // ── forjar check tests ─────────────────────────────────────────

    #[test]
    fn test_check_local_file_pass() {
        let dir = tempfile::tempdir().unwrap();
        // Create the file that check will verify
        let target = dir.path().join("check-test.txt");
        std::fs::write(&target, "hello").unwrap();

        let config = dir.path().join("forjar.yaml");
        std::fs::write(
            &config,
            format!(
                r#"
version: "1.0"
name: check-test
machines:
  local:
    hostname: localhost
    addr: 127.0.0.1
resources:
  f:
    type: file
    machine: local
    path: {}
    content: hello
"#,
                target.display()
            ),
        )
        .unwrap();
        // File exists → check should pass
        cmd_check(&config, None, None, None, false, false).unwrap();
    }

    #[test]
    fn test_check_local_file_missing_still_runs() {
        let dir = tempfile::tempdir().unwrap();
        let config = dir.path().join("forjar.yaml");
        std::fs::write(
            &config,
            r#"
version: "1.0"
name: check-test
machines:
  local:
    hostname: localhost
    addr: 127.0.0.1
resources:
  f:
    type: file
    machine: local
    path: /tmp/forjar-check-nonexistent-12345678
    content: hello
"#,
        )
        .unwrap();
        // Check script reports status (exits 0 even for missing file)
        cmd_check(&config, None, None, None, false, false).unwrap();
    }

    #[test]
    fn test_check_json_output() {
        let dir = tempfile::tempdir().unwrap();
        let target = dir.path().join("check-json-test.txt");
        std::fs::write(&target, "hello").unwrap();

        let config = dir.path().join("forjar.yaml");
        std::fs::write(
            &config,
            format!(
                r#"
version: "1.0"
name: check-test
machines:
  local:
    hostname: localhost
    addr: 127.0.0.1
resources:
  f:
    type: file
    machine: local
    path: {}
    content: hello
"#,
                target.display()
            ),
        )
        .unwrap();
        cmd_check(&config, None, None, None, true, false).unwrap();
    }

    #[test]
    fn test_fmt_normalizes_yaml() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.yaml");
        // Manually-written YAML with inconsistent spacing
        std::fs::write(
            &file,
            r#"version: "1.0"
name: fmt-test
machines:
  m:
    hostname: m
    addr: 127.0.0.1
resources:
  f:
    type: file
    machine: m
    path: /tmp/fmt-test
    content: hello
"#,
        )
        .unwrap();

        // Check should fail (not yet canonical)
        let result = cmd_fmt(&file, true);
        assert!(result.is_err());

        // Format it
        cmd_fmt(&file, false).unwrap();

        // Check should now pass
        cmd_fmt(&file, true).unwrap();
    }

    #[test]
    fn test_fmt_idempotent() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.yaml");
        std::fs::write(
            &file,
            r#"version: "1.0"
name: idempotent-test
machines:
  m:
    hostname: m
    addr: 127.0.0.1
resources:
  f:
    type: file
    machine: m
    path: /tmp/test
    content: hello
"#,
        )
        .unwrap();

        // Format it twice
        cmd_fmt(&file, false).unwrap();
        let after_first = std::fs::read_to_string(&file).unwrap();

        cmd_fmt(&file, false).unwrap();
        let after_second = std::fs::read_to_string(&file).unwrap();

        assert_eq!(after_first, after_second);
    }

    #[test]
    fn test_lint_unused_machine() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.yaml");
        std::fs::write(
            &file,
            r#"version: "1.0"
name: lint-test
machines:
  used:
    hostname: used
    addr: 127.0.0.1
  unused:
    hostname: unused
    addr: 10.0.0.1
resources:
  f:
    type: file
    machine: used
    path: /tmp/test
    content: hello
"#,
        )
        .unwrap();

        // Lint should succeed but print warnings (it returns Ok)
        cmd_lint(&file, false, false, false).unwrap();
    }

    #[test]
    fn test_lint_json_output() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.yaml");
        std::fs::write(
            &file,
            r#"version: "1.0"
name: lint-json
machines:
  m:
    hostname: m
    addr: 127.0.0.1
  orphan:
    hostname: orphan
    addr: 10.0.0.2
resources:
  f:
    type: file
    machine: m
    path: /tmp/test
    content: hello
"#,
        )
        .unwrap();

        cmd_lint(&file, true, false, false).unwrap();
    }

    #[test]
    fn test_lint_clean_config() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.yaml");
        std::fs::write(
            &file,
            r#"version: "1.0"
name: clean
machines:
  m:
    hostname: m
    addr: 127.0.0.1
resources:
  f:
    type: file
    machine: m
    path: /tmp/test
    content: hello
"#,
        )
        .unwrap();

        cmd_lint(&file, false, false, false).unwrap();
    }

    #[test]
    fn test_lint_cross_machine_dependency() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("test.yaml");
        std::fs::write(
            &file,
            r#"version: "1.0"
name: cross-dep
machines:
  web:
    hostname: web
    addr: 10.0.0.1
  db:
    hostname: db
    addr: 10.0.0.2
resources:
  app-config:
    type: file
    machine: web
    path: /etc/app.conf
    content: "host=db"
    depends_on: [db-ready]
  db-ready:
    type: file
    machine: db
    path: /tmp/db-ready
    content: "ok"
"#,
        )
        .unwrap();

        // Capture output via JSON mode to inspect warnings
        let result = cmd_lint(&file, true, false, false);
        assert!(result.is_ok());
        // The warning should mention cross-machine dependency
        // We re-run logic here to check the warning was generated
        let config = parse_and_validate(&file).unwrap();
        assert!(
            has_cross_machine_dependency(&config),
            "should detect cross-machine dependency"
        );
    }

    #[test]
    fn test_rollback_no_git_history() {
        // A file that doesn't exist in git history should fail gracefully
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("nonexistent.yaml");
        std::fs::write(
            &file,
            "version: \"1.0\"\nname: test\nmachines: {}\nresources: {}\n",
        )
        .unwrap();
        let state = dir.path().join("state");
        std::fs::create_dir_all(&state).unwrap();

        let result = cmd_rollback(&file, &state, 1, None, true, false);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("cannot read"));
    }

    #[test]
    fn test_rollback_dispatch() {
        // Verify the Rollback command variant is accepted by dispatch
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("forjar.yaml");
        std::fs::write(
            &file,
            "version: \"1.0\"\nname: rb\nmachines: {}\nresources: {}\n",
        )
        .unwrap();
        let state = dir.path().join("state");
        std::fs::create_dir_all(&state).unwrap();

        // Dispatch dry-run rollback — will fail because no git history,
        // but verifies the dispatch path is wired correctly
        let result = dispatch(
            Commands::Rollback(RollbackArgs {
                file,
                revision: 1,
                generation: None,
                machine: None,
                dry_run: true,
                yes: false,
                state_dir: state,
            }),
            0,
            true,
        );
        assert!(result.is_err()); // Expected: no git history
    }

    #[test]
    fn test_anomaly_empty_state_dir() {
        let dir = tempfile::tempdir().unwrap();
        let state = dir.path().join("state");
        std::fs::create_dir_all(&state).unwrap();

        // No machine dirs → "no resources with enough history"
        let result = cmd_anomaly(&state, None, 3, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_anomaly_detects_high_failure_rate() {
        let dir = tempfile::tempdir().unwrap();
        let state = dir.path().join("state");
        let machine_dir = state.join("m1");
        std::fs::create_dir_all(&machine_dir).unwrap();

        // Write events with high failure rate: 1 converge, 4 failures
        let mut events = String::new();
        events.push_str(
            &serde_json::to_string(&types::TimestampedEvent {
                ts: "2026-02-25T00:00:00Z".to_string(),
                event: types::ProvenanceEvent::ResourceConverged {
                    machine: "m1".to_string(),
                    resource: "flaky-pkg".to_string(),
                    duration_seconds: 1.0,
                    hash: "abc".to_string(),
                },
            })
            .unwrap(),
        );
        events.push('\n');
        for _ in 0..4 {
            events.push_str(
                &serde_json::to_string(&types::TimestampedEvent {
                    ts: "2026-02-25T00:01:00Z".to_string(),
                    event: types::ProvenanceEvent::ResourceFailed {
                        machine: "m1".to_string(),
                        resource: "flaky-pkg".to_string(),
                        error: "install failed".to_string(),
                    },
                })
                .unwrap(),
            );
            events.push('\n');
        }

        std::fs::write(machine_dir.join("events.jsonl"), &events).unwrap();

        // min_events=3, json mode so we can parse output
        let result = cmd_anomaly(&state, None, 3, false);
        assert!(result.is_ok());
    }
}