forjar 1.23.0

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
#![allow(unused_imports)]
use super::tests_helpers::{make_base_resource, make_config};
use super::*;
use std::collections::HashMap;

#[test]
fn test_fj036_plan_all_noop_when_converged() {
    // Config where all resources have matching hashes -> plan shows all NoOp
    let config = make_config();
    let order = vec!["pkg".to_string(), "conf".to_string(), "svc".to_string()];

    // Build locks with matching hashes for every resource
    let mut resources = indexmap::IndexMap::new();
    for id in &order {
        let resource = &config.resources[id];
        resources.insert(
            id.clone(),
            ResourceLock {
                resource_type: resource.resource_type.clone(),
                status: ResourceStatus::Converged,
                applied_at: None,
                duration_seconds: None,
                hash: hash_desired_state(resource),
                observed: None,
                details: HashMap::new(),
            },
        );
    }
    let lock = StateLock {
        schema: "1.0".to_string(),
        machine: "m1".to_string(),
        hostname: "m1".to_string(),
        generated_at: "2026-01-01T00:00:00Z".to_string(),
        generator: "forjar".to_string(),
        blake3_version: "1.8".to_string(),
        resources,
    };
    let mut locks = HashMap::new();
    locks.insert("m1".to_string(), lock);

    let p = plan(&config, &order, &locks, None);

    assert_eq!(p.unchanged, 3, "all 3 resources should be unchanged");
    assert_eq!(p.to_create, 0, "nothing to create");
    assert_eq!(p.to_update, 0, "nothing to update");
    assert_eq!(p.to_destroy, 0, "nothing to destroy");
    assert_eq!(p.changes.len(), 3, "all 3 resources should appear in plan");
    assert!(
        p.changes.iter().all(|c| c.action == PlanAction::NoOp),
        "every action should be NoOp"
    );
}

#[test]
fn test_fj036_plan_respects_resource_filter() {
    // With 3 resources, filter execution_order to 1, verify only that 1 appears in plan
    let config = make_config();
    let locks = HashMap::new();

    // Only include "conf" in execution order (not pkg or svc)
    let filtered_order = vec!["conf".to_string()];
    let p = plan(&config, &filtered_order, &locks, None);

    assert_eq!(p.changes.len(), 1, "only 1 resource should appear in plan");
    assert_eq!(
        p.changes[0].resource_id, "conf",
        "the planned resource should be 'conf'"
    );
    assert_eq!(
        p.changes[0].action,
        PlanAction::Create,
        "conf should be Create since no locks exist"
    );
}

#[test]
fn test_fj036_plan_absent_resource_destroy() {
    // Resource with state=absent that exists in lock -> plan shows Destroy action
    let yaml = "\nversion: \"1.0\"\nname: test\nmachines:\n  m1:\n    hostname: m1\n    addr: 127.0.0.1\nresources:\n  old-config:\n    type: file\n    machine: m1\n    path: /etc/old.conf\n    state: absent\n";
    let config: ForjarConfig = serde_yaml_ng::from_str(yaml).unwrap();
    let order = vec!["old-config".to_string()];

    // Create a lock entry for this resource (it exists on the machine)
    let mut lock_resources = indexmap::IndexMap::new();
    lock_resources.insert(
        "old-config".to_string(),
        ResourceLock {
            resource_type: ResourceType::File,
            status: ResourceStatus::Converged,
            applied_at: None,
            duration_seconds: None,
            hash: "blake3:some_existing_hash".to_string(),
            observed: None,
            details: HashMap::new(),
        },
    );
    let lock = StateLock {
        schema: "1.0".to_string(),
        machine: "m1".to_string(),
        hostname: "m1".to_string(),
        generated_at: "2026-01-01T00:00:00Z".to_string(),
        generator: "forjar".to_string(),
        blake3_version: "1.8".to_string(),
        resources: lock_resources,
    };
    let mut locks = HashMap::new();
    locks.insert("m1".to_string(), lock);

    let p = plan(&config, &order, &locks, None);

    assert_eq!(p.to_destroy, 1, "should have 1 destroy action");
    assert_eq!(p.changes.len(), 1);
    assert_eq!(
        p.changes[0].action,
        PlanAction::Destroy,
        "absent resource with existing lock should be Destroy"
    );
    assert_eq!(p.changes[0].resource_id, "old-config");
}

/// GH-229: the plan must reach a fixed point for `state: absent`.
///
/// A successful destroy writes the resource back into the lock as `converged`
/// with the ABSENT form's desired hash. Before the fix, the planner treated
/// mere presence in the lock as "still exists" and re-emitted Destroy forever,
/// so `apply` never converged (observed as a permanent "N to destroy" on
/// infra's lambda-labs).
#[test]
fn test_gh229_absent_converged_to_absent_is_noop() {
    let yaml = "\nversion: \"1.0\"\nname: test\nmachines:\n  m1:\n    hostname: m1\n    addr: 127.0.0.1\nresources:\n  old-config:\n    type: file\n    machine: m1\n    path: /etc/old.conf\n    state: absent\n";
    let config: ForjarConfig = serde_yaml_ng::from_str(yaml).unwrap();
    let order = vec!["old-config".to_string()];

    // Exactly what apply records after the destroy succeeds: converged, with
    // the hash of the ABSENT declaration.
    let absent_hash = hash_desired_state(&config.resources["old-config"]);
    let mut lock_resources = indexmap::IndexMap::new();
    lock_resources.insert(
        "old-config".to_string(),
        ResourceLock {
            resource_type: ResourceType::File,
            status: ResourceStatus::Converged,
            applied_at: None,
            duration_seconds: None,
            hash: absent_hash,
            observed: None,
            details: HashMap::new(),
        },
    );
    let lock = StateLock {
        schema: "1.0".to_string(),
        machine: "m1".to_string(),
        hostname: "m1".to_string(),
        generated_at: "2026-01-01T00:00:00Z".to_string(),
        generator: "forjar".to_string(),
        blake3_version: "1.8".to_string(),
        resources: lock_resources,
    };
    let mut locks = HashMap::new();
    locks.insert("m1".to_string(), lock);

    let p = plan(&config, &order, &locks, None);

    assert_eq!(
        p.to_destroy, 0,
        "an already-destroyed absent resource must not be re-destroyed"
    );
    assert_eq!(p.unchanged, 1, "it should count as unchanged");
}

/// GH-229 companion: the fix must NOT silently stop destroying things.
///
/// A resource that converged while declared PRESENT and is now redeclared
/// `absent` still has to be destroyed. `state` is a component of
/// hash_desired_state, so the lock's present-form hash cannot match the absent
/// form — which is exactly what keeps these two cases apart.
#[test]
fn test_gh229_absent_converged_as_present_still_destroys() {
    let present_yaml = "\nversion: \"1.0\"\nname: test\nmachines:\n  m1:\n    hostname: m1\n    addr: 127.0.0.1\nresources:\n  old-config:\n    type: file\n    machine: m1\n    path: /etc/old.conf\n    content: hello\n";
    let absent_yaml = "\nversion: \"1.0\"\nname: test\nmachines:\n  m1:\n    hostname: m1\n    addr: 127.0.0.1\nresources:\n  old-config:\n    type: file\n    machine: m1\n    path: /etc/old.conf\n    content: hello\n    state: absent\n";
    let present: ForjarConfig = serde_yaml_ng::from_str(present_yaml).unwrap();
    let config: ForjarConfig = serde_yaml_ng::from_str(absent_yaml).unwrap();
    let order = vec!["old-config".to_string()];

    let present_hash = hash_desired_state(&present.resources["old-config"]);
    assert_ne!(
        present_hash,
        hash_desired_state(&config.resources["old-config"]),
        "state must participate in the desired-state hash, or the two cases alias"
    );

    let mut lock_resources = indexmap::IndexMap::new();
    lock_resources.insert(
        "old-config".to_string(),
        ResourceLock {
            resource_type: ResourceType::File,
            status: ResourceStatus::Converged,
            applied_at: None,
            duration_seconds: None,
            hash: present_hash,
            observed: None,
            details: HashMap::new(),
        },
    );
    let lock = StateLock {
        schema: "1.0".to_string(),
        machine: "m1".to_string(),
        hostname: "m1".to_string(),
        generated_at: "2026-01-01T00:00:00Z".to_string(),
        generator: "forjar".to_string(),
        blake3_version: "1.8".to_string(),
        resources: lock_resources,
    };
    let mut locks = HashMap::new();
    locks.insert("m1".to_string(), lock);

    let p = plan(&config, &order, &locks, None);

    assert_eq!(
        p.to_destroy, 1,
        "present->absent transition must still destroy"
    );
    assert_eq!(p.changes[0].action, PlanAction::Destroy);
}

/// GH-229: a destroy that FAILED must be retried, not swallowed by the new
/// NoOp path.
#[test]
fn test_gh229_absent_failed_destroy_is_retried() {
    let yaml = "\nversion: \"1.0\"\nname: test\nmachines:\n  m1:\n    hostname: m1\n    addr: 127.0.0.1\nresources:\n  old-config:\n    type: file\n    machine: m1\n    path: /etc/old.conf\n    state: absent\n";
    let config: ForjarConfig = serde_yaml_ng::from_str(yaml).unwrap();
    let order = vec!["old-config".to_string()];

    // Same hash as the absent declaration, but the apply did not succeed.
    let absent_hash = hash_desired_state(&config.resources["old-config"]);
    let mut lock_resources = indexmap::IndexMap::new();
    lock_resources.insert(
        "old-config".to_string(),
        ResourceLock {
            resource_type: ResourceType::File,
            status: ResourceStatus::Failed,
            applied_at: None,
            duration_seconds: None,
            hash: absent_hash,
            observed: None,
            details: HashMap::new(),
        },
    );
    let lock = StateLock {
        schema: "1.0".to_string(),
        machine: "m1".to_string(),
        hostname: "m1".to_string(),
        generated_at: "2026-01-01T00:00:00Z".to_string(),
        generator: "forjar".to_string(),
        blake3_version: "1.8".to_string(),
        resources: lock_resources,
    };
    let mut locks = HashMap::new();
    locks.insert("m1".to_string(), lock);

    let p = plan(&config, &order, &locks, None);

    assert_eq!(p.to_destroy, 1, "a failed destroy must be retried");
}

#[test]
fn test_plan_absent_resource_no_lock() {
    let yaml = "\nversion: \"1.0\"\nname: test\nmachines:\n  m1:\n    hostname: m1\n    addr: 127.0.0.1\nresources:\n  gone-file:\n    type: file\n    machine: m1\n    path: /tmp/gone.txt\n    state: absent\n";
    let config: ForjarConfig = serde_yaml_ng::from_str(yaml).unwrap();
    let order = vec!["gone-file".to_string()];
    let locks = HashMap::new();

    let p = plan(&config, &order, &locks, None);

    // GH-339: these three assertions were the defect, written as a spec.
    // "No lock exists" is a fact about forjar's bookkeeping; /tmp/gone.txt may
    // well be sitting on the machine, put there by something else. That is the
    // usual reason to declare a path absent at all.
    assert_eq!(
        p.to_destroy, 1,
        "an unlocked absent resource must plan a destroy — the lock says nothing \
         about what is on the machine"
    );
    assert_eq!(p.unchanged, 0);
    assert_eq!(p.changes.len(), 1);
    assert_eq!(p.changes[0].action, PlanAction::Destroy);
}

#[test]
fn test_plan_converged_hash_match() {
    let config = make_config();
    let order = vec!["conf".to_string()];

    let resource = &config.resources["conf"];
    let desired_hash = hash_desired_state(resource);

    let mut resources = indexmap::IndexMap::new();
    resources.insert(
        "conf".to_string(),
        ResourceLock {
            resource_type: ResourceType::File,
            status: ResourceStatus::Converged,
            applied_at: None,
            duration_seconds: None,
            hash: desired_hash,
            observed: None,
            details: HashMap::new(),
        },
    );
    let lock = StateLock {
        schema: "1.0".to_string(),
        machine: "m1".to_string(),
        hostname: "m1".to_string(),
        generated_at: "2026-01-01T00:00:00Z".to_string(),
        generator: "forjar".to_string(),
        blake3_version: "1.8".to_string(),
        resources,
    };
    let mut locks = HashMap::new();
    locks.insert("m1".to_string(), lock);

    let p = plan(&config, &order, &locks, None);

    assert_eq!(
        p.unchanged, 1,
        "converged resource with matching hash should be NoOp"
    );
    assert_eq!(p.to_update, 0);
    assert_eq!(p.to_create, 0);
    assert_eq!(p.changes[0].action, PlanAction::NoOp);
}

#[test]
fn test_plan_converged_hash_mismatch() {
    let config = make_config();
    let order = vec!["conf".to_string()];

    let mut resources = indexmap::IndexMap::new();
    resources.insert(
        "conf".to_string(),
        ResourceLock {
            resource_type: ResourceType::File,
            status: ResourceStatus::Converged,
            applied_at: None,
            duration_seconds: None,
            hash: "blake3:old_stale_hash_that_does_not_match".to_string(),
            observed: None,
            details: HashMap::new(),
        },
    );
    let lock = StateLock {
        schema: "1.0".to_string(),
        machine: "m1".to_string(),
        hostname: "m1".to_string(),
        generated_at: "2026-01-01T00:00:00Z".to_string(),
        generator: "forjar".to_string(),
        blake3_version: "1.8".to_string(),
        resources,
    };
    let mut locks = HashMap::new();
    locks.insert("m1".to_string(), lock);

    let p = plan(&config, &order, &locks, None);

    assert_eq!(
        p.to_update, 1,
        "converged resource with mismatched hash should be Update"
    );
    assert_eq!(p.unchanged, 0);
    assert_eq!(p.changes[0].action, PlanAction::Update);
}

#[test]
fn test_fj204_count_plans_expanded_resources() {
    let yaml = "\nversion: \"1.0\"\nname: test-count\nmachines:\n  m1:\n    hostname: m1\n    addr: 1.2.3.4\nresources:\n  shard:\n    type: file\n    machine: m1\n    path: \"/data/shard-{{index}}\"\n    content: \"shard={{index}}\"\n    count: 3\n";
    let mut config: ForjarConfig = serde_yaml_ng::from_str(yaml).unwrap();
    super::super::parser::expand_resources(&mut config);
    let order = vec![
        "shard-0".to_string(),
        "shard-1".to_string(),
        "shard-2".to_string(),
    ];
    let locks = HashMap::new();
    let p = plan(&config, &order, &locks, None);
    assert_eq!(p.to_create, 3);
    assert_eq!(p.changes.len(), 3);
    assert_eq!(p.changes[0].resource_id, "shard-0");
    assert_eq!(p.changes[1].resource_id, "shard-1");
    assert_eq!(p.changes[2].resource_id, "shard-2");
}

#[test]
fn test_fj203_for_each_plans_expanded_resources() {
    let yaml = "\nversion: \"1.0\"\nname: test-foreach\nmachines:\n  m1:\n    hostname: m1\n    addr: 1.2.3.4\nresources:\n  vhost:\n    type: file\n    machine: m1\n    path: \"/etc/nginx/{{item}}.conf\"\n    content: \"server {{item}}\"\n    for_each: [api, web]\n";
    let mut config: ForjarConfig = serde_yaml_ng::from_str(yaml).unwrap();
    super::super::parser::expand_resources(&mut config);
    let order = vec!["vhost-api".to_string(), "vhost-web".to_string()];
    let locks = HashMap::new();
    let p = plan(&config, &order, &locks, None);
    assert_eq!(p.to_create, 2);
    assert_eq!(p.changes[0].resource_id, "vhost-api");
    assert_eq!(p.changes[1].resource_id, "vhost-web");
}

#[test]
fn test_fj204_count_mixed_with_regular() {
    let yaml = "\nversion: \"1.0\"\nname: test\nmachines:\n  m1:\n    hostname: m1\n    addr: 1.2.3.4\nresources:\n  base:\n    type: file\n    machine: m1\n    path: \"/etc/base.conf\"\n  node:\n    type: file\n    machine: m1\n    path: \"/data/node-{{index}}\"\n    content: \"node={{index}}\"\n    count: 2\n";
    let mut config: ForjarConfig = serde_yaml_ng::from_str(yaml).unwrap();
    super::super::parser::expand_resources(&mut config);

    let order = vec![
        "base".to_string(),
        "node-0".to_string(),
        "node-1".to_string(),
    ];
    let locks = HashMap::new();
    let p = plan(&config, &order, &locks, None);
    assert_eq!(p.to_create, 3, "base + node-0 + node-1");
}