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
//! FJ-131/FJ-132 parser edge case tests, FJ-036 structural tests.

use super::*;
use std::path::Path;

// ---- FJ-131: Parser edge case tests ----

#[test]
fn test_fj131_parse_and_validate_valid_config() {
    let dir = tempfile::tempdir().unwrap();
    let config_path = dir.path().join("forjar.yaml");
    std::fs::write(
        &config_path,
        r#"
version: "1.0"
name: valid-config
machines:
  local:
    hostname: local
    addr: 127.0.0.1
resources:
  test-file:
    type: file
    machine: local
    path: /tmp/test.txt
    content: "hello"
"#,
    )
    .unwrap();

    let config = parse_and_validate(&config_path).unwrap();
    assert_eq!(config.name, "valid-config");
    assert!(config.resources.contains_key("test-file"));
}

#[test]
fn test_fj131_parse_and_validate_error_formatting() {
    let dir = tempfile::tempdir().unwrap();
    let config_path = dir.path().join("forjar.yaml");
    std::fs::write(
        &config_path,
        r#"
version: "1.0"
name: bad-config
machines: {}
resources:
  bad-pkg:
    type: package
    machine: unknown-machine
  bad-file:
    type: file
    machine: another-unknown
"#,
    )
    .unwrap();

    let err = parse_and_validate(&config_path).unwrap_err();
    assert!(
        err.starts_with("validation errors:\n"),
        "error should start with 'validation errors:'"
    );
    assert!(
        err.contains("  - "),
        "each error should be indented with '  - '"
    );
    let bullet_count = err.matches("  - ").count();
    assert!(
        bullet_count >= 2,
        "expected multiple errors, got {bullet_count} bullets"
    );
}

#[test]
fn test_fj131_parse_and_validate_nonexistent_file() {
    let result = parse_and_validate(Path::new("/tmp/nonexistent-forjar-config.yaml"));
    assert!(result.is_err());
}

#[test]
fn test_fj131_package_both_missing_provider_and_packages() {
    let yaml = r#"
version: "1.0"
name: test
machines:
  m:
    hostname: m
    addr: 1.1.1.1
resources:
  bad-pkg:
    type: package
    machine: m
"#;
    let config = parse_config(yaml).unwrap();
    let errors = validate_config(&config);
    let pkg_errors: Vec<_> = errors
        .iter()
        .filter(|e| e.message.contains("bad-pkg"))
        .collect();
    assert!(
        pkg_errors.len() >= 2,
        "should have at least 2 errors for missing packages AND provider, got {}",
        pkg_errors.len()
    );
    assert!(pkg_errors.iter().any(|e| e.message.contains("no packages")));
    assert!(pkg_errors.iter().any(|e| e.message.contains("no provider")));
}

#[test]
fn test_fj131_file_invalid_state_error_lists_valid_options() {
    let yaml = r#"
version: "1.0"
name: test
machines:
  m:
    hostname: m
    addr: 1.1.1.1
resources:
  bad-file:
    type: file
    machine: m
    path: /tmp/test
    state: "executable"
"#;
    let config = parse_config(yaml).unwrap();
    let errors = validate_config(&config);
    let file_err = errors
        .iter()
        .find(|e| e.message.contains("invalid state"))
        .expect("should have invalid state error");
    assert!(file_err
        .message
        .contains("file, directory, symlink, absent"));
}

#[test]
fn test_fj131_container_valid_config_no_errors() {
    let yaml = r#"
version: "1.0"
name: test
machines:
  test-box:
    hostname: test-box
    addr: container
    transport: container
    container:
      runtime: podman
      image: ubuntu:22.04
      ephemeral: false
      name: my-container
resources:
  f:
    type: file
    machine: test-box
    path: /tmp/test
    content: "hello"
"#;
    let config = parse_config(yaml).unwrap();
    let errors = validate_config(&config);
    assert!(
        errors.is_empty(),
        "valid container config should have no errors: {errors:?}"
    );
}

#[test]
fn test_fj131_service_invalid_state_lists_valid_options() {
    let yaml = r#"
version: "1.0"
name: test
machines:
  m:
    hostname: m
    addr: 1.1.1.1
resources:
  svc:
    type: service
    machine: m
    name: nginx
    state: "paused"
"#;
    let config = parse_config(yaml).unwrap();
    let errors = validate_config(&config);
    let svc_err = errors
        .iter()
        .find(|e| e.message.contains("invalid state"))
        .expect("should have invalid state error");
    assert!(svc_err
        .message
        .contains("running, stopped, enabled, disabled"));
}

#[test]
fn test_fj131_mount_invalid_state_lists_valid_options() {
    let yaml = r#"
version: "1.0"
name: test
machines:
  m:
    hostname: m
    addr: 1.1.1.1
resources:
  mnt:
    type: mount
    machine: m
    source: /dev/sda1
    path: /mnt/data
    state: "bound"
"#;
    let config = parse_config(yaml).unwrap();
    let errors = validate_config(&config);
    let mnt_err = errors
        .iter()
        .find(|e| e.message.contains("invalid state"))
        .expect("should have invalid state error");
    assert!(mnt_err.message.contains("mounted, unmounted, absent"));
}

#[test]
fn test_fj131_cron_both_missing_schedule_and_command() {
    let yaml = r#"
version: "1.0"
name: test
machines:
  m:
    hostname: m
    addr: 1.1.1.1
resources:
  bad-cron:
    type: cron
    machine: m
"#;
    let config = parse_config(yaml).unwrap();
    let errors = validate_config(&config);
    let cron_errors: Vec<_> = errors
        .iter()
        .filter(|e| e.message.contains("bad-cron"))
        .collect();
    assert!(
        cron_errors.len() >= 3,
        "expected at least 3 cron errors, got {}",
        cron_errors.len()
    );
}

#[test]
fn test_fj131_network_both_invalid_protocol_and_action() {
    let yaml = r#"
version: "1.0"
name: test
machines:
  m:
    hostname: m
    addr: 1.1.1.1
resources:
  bad-net:
    type: network
    machine: m
    port: "22"
    protocol: icmp
    action: forward
"#;
    let config = parse_config(yaml).unwrap();
    let errors = validate_config(&config);
    let net_errors: Vec<_> = errors
        .iter()
        .filter(|e| e.message.contains("bad-net"))
        .collect();
    assert!(
        net_errors.len() >= 2,
        "expected at least 2 network errors, got {}",
        net_errors.len()
    );
    assert!(net_errors.iter().any(|e| e.message.contains("protocol")));
    assert!(net_errors.iter().any(|e| e.message.contains("action")));
}

#[test]
fn test_fj131_parse_and_validate_with_recipe_expansion() {
    let dir = tempfile::tempdir().unwrap();

    let recipes_dir = dir.path().join("recipes");
    std::fs::create_dir_all(&recipes_dir).unwrap();
    std::fs::write(
        recipes_dir.join("web.yaml"),
        r#"
recipe:
  name: web-recipe
resources:
  web-file:
    type: file
    path: /tmp/web.txt
    content: "web"
"#,
    )
    .unwrap();

    let config_path = dir.path().join("forjar.yaml");
    std::fs::write(
        &config_path,
        r#"
version: "1.0"
name: recipe-test
machines:
  local:
    hostname: local
    addr: 127.0.0.1
resources:
  web:
    type: recipe
    machine: local
    recipe: web
"#,
    )
    .unwrap();

    let config = parse_and_validate(&config_path).unwrap();
    assert!(
        !config.resources.contains_key("web"),
        "recipe resource should be replaced"
    );
    assert!(
        config.resources.keys().any(|k| k.contains("web-file")),
        "expanded resource should be present"
    );
}

// ---- FJ-132 tests ----

#[test]
fn test_fj132_validate_container_no_block() {
    let yaml = r#"
version: "1.0"
name: test
machines:
  box:
    hostname: box
    addr: container
    transport: container
resources: {}
"#;
    let config = parse_config(yaml).unwrap();
    let errors = validate_config(&config);
    assert!(
        errors
            .iter()
            .any(|e| e.message.contains("no 'container' block")),
        "container transport without container block should error"
    );
}

#[test]
fn test_fj132_validate_container_bad_runtime() {
    let yaml = r#"
version: "1.0"
name: test
machines:
  box:
    hostname: box
    addr: container
    transport: container
    container:
      runtime: lxc
      image: ubuntu:22.04
resources: {}
"#;
    let config = parse_config(yaml).unwrap();
    let errors = validate_config(&config);
    assert!(
        errors
            .iter()
            .any(|e| e.message.contains("docker' or 'podman'")),
        "invalid runtime should error"
    );
}

#[test]
fn test_fj132_validate_ephemeral_no_image() {
    let yaml = r#"
version: "1.0"
name: test
machines:
  box:
    hostname: box
    addr: container
    transport: container
    container:
      runtime: docker
      ephemeral: true
resources: {}
"#;
    let config = parse_config(yaml).unwrap();
    let errors = validate_config(&config);
    assert!(
        errors
            .iter()
            .any(|e| e.message.contains("ephemeral but has no container image")),
        "ephemeral without image should error"
    );
}

#[test]
fn test_fj132_validate_file_both_content_and_source() {
    let yaml = r#"
version: "1.0"
name: test
machines:
  m:
    hostname: m
    addr: 127.0.0.1
resources:
  bad-file:
    type: file
    machine: m
    path: /etc/test.conf
    content: "inline"
    source: /builds/app.conf
"#;
    let config = parse_config(yaml).unwrap();
    let errors = validate_config(&config);
    assert!(
        errors
            .iter()
            .any(|e| e.message.contains("both content and source")),
        "file with both content and source should error"
    );
}