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
//! FJ-129: Integration tests — apply, drift, re-apply cycle.

use super::test_fixtures::*;
use super::*;

#[test]
fn test_fj129_apply_then_drift_no_change() {
    // Apply a file, then check drift — should find no drift
    let tmp = tempfile::tempdir().unwrap();
    let file_path = tmp.path().join("drift-no-change.txt");
    let config = drift_config(file_path.to_str().unwrap());
    let state_dir = tempfile::tempdir().unwrap();
    let cfg = ApplyConfig {
        config: &config,
        state_dir: state_dir.path(),
        force: false,
        dry_run: false,
        machine_filter: None,
        resource_filter: None,
        tag_filter: None,
        group_filter: None,
        timeout_secs: None,
        force_unlock: false,
        progress: false,
        retry: 0,
        parallel: None,
        resource_timeout: None,
        rollback_on_failure: false,
        max_parallel: None,
        trace: false,
        run_id: None,
        refresh: false,
        force_tag: None,
    };
    apply(&cfg).unwrap();

    // Load lock and check drift
    let lock = state::load_lock(state_dir.path(), "local")
        .unwrap()
        .unwrap();
    let findings = crate::tripwire::drift::detect_drift(&lock);
    assert!(
        findings.is_empty(),
        "no drift expected immediately after apply"
    );
}

#[test]
fn test_fj129_apply_then_drift_after_modification() {
    // Apply a file, modify it externally, then check drift
    let tmp = tempfile::tempdir().unwrap();
    let file_path = tmp.path().join("drift-tampered.txt");
    let config = drift_config(file_path.to_str().unwrap());
    let state_dir = tempfile::tempdir().unwrap();
    let cfg = ApplyConfig {
        config: &config,
        state_dir: state_dir.path(),
        force: false,
        dry_run: false,
        machine_filter: None,
        resource_filter: None,
        tag_filter: None,
        group_filter: None,
        timeout_secs: None,
        force_unlock: false,
        progress: false,
        retry: 0,
        parallel: None,
        resource_timeout: None,
        rollback_on_failure: false,
        max_parallel: None,
        trace: false,
        run_id: None,
        refresh: false,
        force_tag: None,
    };
    apply(&cfg).unwrap();

    // Tamper with the file
    std::fs::write(&file_path, "tampered content").unwrap();

    // Drift detection should find the change
    let lock = state::load_lock(state_dir.path(), "local")
        .unwrap()
        .unwrap();
    let findings = crate::tripwire::drift::detect_drift(&lock);
    assert_eq!(
        findings.len(),
        1,
        "should detect drift after file modification"
    );
    assert_eq!(findings[0].resource_id, "test-file");
    assert!(findings[0].detail.contains("content changed"));
}

#[test]
fn test_fj129_apply_drift_reapply_cycle() {
    // Full cycle: apply → drift (no change) → tamper → drift (found) → re-apply → drift (no change)
    let tmp = tempfile::tempdir().unwrap();
    let file_path = tmp.path().join("drift-cycle.txt");
    let config = drift_config(file_path.to_str().unwrap());
    let state_dir = tempfile::tempdir().unwrap();

    // Step 1: Initial apply
    let cfg = ApplyConfig {
        config: &config,
        state_dir: state_dir.path(),
        force: false,
        dry_run: false,
        machine_filter: None,
        resource_filter: None,
        tag_filter: None,
        group_filter: None,
        timeout_secs: None,
        force_unlock: false,
        progress: false,
        retry: 0,
        parallel: None,
        resource_timeout: None,
        rollback_on_failure: false,
        max_parallel: None,
        trace: false,
        run_id: None,
        refresh: false,
        force_tag: None,
    };
    let r1 = apply(&cfg).unwrap();
    assert_eq!(r1[0].resources_converged, 1);

    // Step 2: Verify no drift
    let lock1 = state::load_lock(state_dir.path(), "local")
        .unwrap()
        .unwrap();
    assert!(crate::tripwire::drift::detect_drift(&lock1).is_empty());

    // Step 3: Tamper
    std::fs::write(&file_path, "unauthorized change").unwrap();
    let findings = crate::tripwire::drift::detect_drift(&lock1);
    assert_eq!(findings.len(), 1);

    // Step 4: Re-apply (force to overwrite tampered file)
    let cfg2 = ApplyConfig {
        config: &config,
        state_dir: state_dir.path(),
        force: true,
        dry_run: false,
        machine_filter: None,
        resource_filter: None,
        tag_filter: None,
        group_filter: None,
        timeout_secs: None,
        force_unlock: false,
        progress: false,
        retry: 0,
        parallel: None,
        resource_timeout: None,
        rollback_on_failure: false,
        max_parallel: None,
        trace: false,
        run_id: None,
        refresh: false,
        force_tag: None,
    };
    let r2 = apply(&cfg2).unwrap();
    assert_eq!(r2[0].resources_converged, 1);

    // Step 5: Verify no drift after re-apply
    let lock2 = state::load_lock(state_dir.path(), "local")
        .unwrap()
        .unwrap();
    assert!(
        crate::tripwire::drift::detect_drift(&lock2).is_empty(),
        "no drift expected after re-apply"
    );
}

#[test]
fn test_fj129_multi_resource_dependency_order() {
    // Verify that dependent resources are applied in correct order
    let yaml = r#"
version: "1.0"
name: dep-order
machines:
  local:
    hostname: localhost
    addr: 127.0.0.1
resources:
  dir-first:
    type: file
    machine: local
    state: directory
    path: /tmp/forjar-test-dep-order
    mode: "0755"
  file-second:
    type: file
    machine: local
    path: /tmp/forjar-test-dep-order/config.txt
    content: "depends on dir"
    depends_on: [dir-first]
policy:
  lock_file: true
  tripwire: true
"#;
    let config: ForjarConfig = serde_yaml_ng::from_str(yaml).unwrap();
    let dir = tempfile::tempdir().unwrap();
    let cfg = ApplyConfig {
        config: &config,
        state_dir: dir.path(),
        force: false,
        dry_run: false,
        machine_filter: None,
        resource_filter: None,
        tag_filter: None,
        group_filter: None,
        timeout_secs: None,
        force_unlock: false,
        progress: false,
        retry: 0,
        parallel: None,
        resource_timeout: None,
        rollback_on_failure: false,
        max_parallel: None,
        trace: false,
        run_id: None,
        refresh: false,
        force_tag: None,
    };
    let results = apply(&cfg).unwrap();
    assert_eq!(results[0].resources_converged, 2);

    // Verify both artifacts exist
    assert!(std::path::Path::new("/tmp/forjar-test-dep-order").is_dir());
    assert!(std::path::Path::new("/tmp/forjar-test-dep-order/config.txt").exists());
    let content = std::fs::read_to_string("/tmp/forjar-test-dep-order/config.txt").unwrap();
    assert_eq!(content.trim(), "depends on dir");

    // Idempotency check
    let r2 = apply(&cfg).unwrap();
    assert_eq!(r2[0].resources_unchanged, 2);

    // Clean up
    let _ = std::fs::remove_dir_all("/tmp/forjar-test-dep-order");
}

#[test]
fn test_fj129_config_change_triggers_update() {
    // Apply config A, then apply config B (different content), verify UPDATE
    let yaml_a = r#"
version: "1.0"
name: change-detect
machines:
  local:
    hostname: localhost
    addr: 127.0.0.1
resources:
  changeable:
    type: file
    machine: local
    path: /tmp/forjar-test-change-detect.txt
    content: "version A"
policy:
  lock_file: true
"#;
    let yaml_b = r#"
version: "1.0"
name: change-detect
machines:
  local:
    hostname: localhost
    addr: 127.0.0.1
resources:
  changeable:
    type: file
    machine: local
    path: /tmp/forjar-test-change-detect.txt
    content: "version B"
policy:
  lock_file: true
"#;
    let dir = tempfile::tempdir().unwrap();

    // Apply version A
    let config_a: ForjarConfig = serde_yaml_ng::from_str(yaml_a).unwrap();
    let cfg_a = ApplyConfig {
        config: &config_a,
        state_dir: dir.path(),
        force: false,
        dry_run: false,
        machine_filter: None,
        resource_filter: None,
        tag_filter: None,
        group_filter: None,
        timeout_secs: None,
        force_unlock: false,
        progress: false,
        retry: 0,
        parallel: None,
        resource_timeout: None,
        rollback_on_failure: false,
        max_parallel: None,
        trace: false,
        run_id: None,
        refresh: false,
        force_tag: None,
    };
    let r1 = apply(&cfg_a).unwrap();
    assert_eq!(r1[0].resources_converged, 1);
    assert_eq!(
        std::fs::read_to_string("/tmp/forjar-test-change-detect.txt")
            .unwrap()
            .trim(),
        "version A"
    );

    // Apply version B (content changed) — should detect update
    let config_b: ForjarConfig = serde_yaml_ng::from_str(yaml_b).unwrap();
    let cfg_b = ApplyConfig {
        config: &config_b,
        state_dir: dir.path(),
        force: false,
        dry_run: false,
        machine_filter: None,
        resource_filter: None,
        tag_filter: None,
        group_filter: None,
        timeout_secs: None,
        force_unlock: false,
        progress: false,
        retry: 0,
        parallel: None,
        resource_timeout: None,
        rollback_on_failure: false,
        max_parallel: None,
        trace: false,
        run_id: None,
        refresh: false,
        force_tag: None,
    };
    let r2 = apply(&cfg_b).unwrap();
    assert_eq!(
        r2[0].resources_converged, 1,
        "changed content should trigger re-apply"
    );
    assert_eq!(
        std::fs::read_to_string("/tmp/forjar-test-change-detect.txt")
            .unwrap()
            .trim(),
        "version B"
    );

    let _ = std::fs::remove_file("/tmp/forjar-test-change-detect.txt");
}

#[test]
fn test_fj129_event_log_full_lifecycle() {
    // Verify event log records full lifecycle: started → resource_started → converged → completed
    let config = local_config();
    let dir = tempfile::tempdir().unwrap();
    let cfg = ApplyConfig {
        config: &config,
        state_dir: dir.path(),
        force: false,
        dry_run: false,
        machine_filter: None,
        resource_filter: None,
        tag_filter: None,
        group_filter: None,
        timeout_secs: None,
        force_unlock: false,
        progress: false,
        retry: 0,
        parallel: None,
        resource_timeout: None,
        rollback_on_failure: false,
        max_parallel: None,
        trace: false,
        run_id: None,
        refresh: false,
        force_tag: None,
    };
    apply(&cfg).unwrap();

    let events_path = dir.path().join("local").join("events.jsonl");
    let content = std::fs::read_to_string(&events_path).unwrap();
    let lines: Vec<&str> = content.lines().collect();

    // Should have at least 3 events: apply_started, resource_started, resource_converged, apply_completed
    assert!(
        lines.len() >= 3,
        "expected at least 3 events, got {}",
        lines.len()
    );

    // Verify event types appear in order
    assert!(
        lines[0].contains("apply_started"),
        "first event should be apply_started"
    );
    assert!(
        content.contains("resource_started"),
        "should contain resource_started"
    );
    assert!(
        content.contains("resource_converged"),
        "should contain resource_converged"
    );
    assert!(
        lines.last().unwrap().contains("apply_completed"),
        "last event should be apply_completed"
    );

    let _ = std::fs::remove_file("/tmp/forjar-test-executor.txt");
}