OXYGENHUB
# 47c1d64dec8a oxygenflow / crates / flow-core / src / tests.rs
10.7 KiB · Rust · last changed Runners sign their receipts 1 month ago source blame history raw
1
use crate::*;
2
3
const ROOTFS: &str = "9b5c2a1e8f3d4e7a6b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a";
4
5
fn doc(body: &str) -> String {
6
    format!(
7
        "schema-version 1\n\nsandbox \"alpine\" {{\n    runtime \"microvm\"\n    rootfs \"cas://blob/{ROOTFS}\"\n    memory-limit \"512MiB\"\n    cpu-limit 2\n    network \"isolated\"\n}}\n\n{body}"
8
    )
9
}
10
11
fn problems(src: &str) -> Vec<(usize, String)> {
12
    Pipeline::parse(src).unwrap_err().0.into_iter().map(|p| (p.line, p.message)).collect()
13
}
14
15
#[test]
16
fn parses_the_m0_example() {
17
    let p = Pipeline::parse(&doc(r#"job "lint" {
18
    sandbox "alpine"
19
    scope "src/**" "README.md"
20
    env {
21
        LANG "C.UTF-8"
22
    }
23
    exec "grep" "-rn" "TODO" "src"
24
}
25
26
job "test" {
27
    sandbox "alpine"
28
    depends-on "lint"
29
    exec "sh" "-eu" "test.sh"
30
}
31
"#))
32
    .unwrap();
33
    let sb = &p.sandboxes["alpine"];
34
    assert_eq!((sb.runtime, sb.memory_mib, sb.cpus, sb.network), (Runtime::Microvm, 512, 2, Network::Isolated));
35
    assert_eq!(sb.rootfs, ROOTFS);
36
    let lint = &p.jobs["lint"];
37
    assert_eq!(lint.exec, ["grep", "-rn", "TODO", "src"]);
38
    assert_eq!(lint.scope, ["src/**", "README.md"]);
39
    assert_eq!(lint.env["LANG"], "C.UTF-8");
40
    assert_eq!(p.jobs["test"].depends_on, ["lint"]);
41
    assert_eq!(p.order().iter().map(|j| j.name.as_str()).collect::<Vec<_>>(), ["lint", "test"]);
42
    assert_eq!(p.depths()["test"], 1);
43
}
44
45
#[test]
46
fn defaults_and_gib() {
47
    let p = Pipeline::parse(&format!(
48
        "schema-version 1\nsandbox \"s\" {{\n  rootfs \"cas://blob/{ROOTFS}\"\n  memory-limit \"2GiB\"\n}}\njob \"a\" {{\n  sandbox \"s\"\n  exec \"true\"\n}}\n"
49
    ))
50
    .unwrap();
51
    let s = &p.sandboxes["s"];
52
    assert_eq!((s.runtime, s.memory_mib, s.cpus, s.network), (Runtime::Microvm, 2048, 1, Network::Isolated));
53
}
54
55
#[test]
56
fn shell_metacharacters_stay_literal_argv() {
57
    // Spec §10.1: every byte reaches argv as written; nothing adds a shell.
58
    let p = Pipeline::parse(&doc(r#"job "x" { sandbox "alpine"; exec "echo" "a; rm -rf / && $(id) | `x`" "b\nc"; }"#)).unwrap();
59
    assert_eq!(p.jobs["x"].exec, ["echo", "a; rm -rf / && $(id) | `x`", "b\nc"]);
60
}
61
62
#[test]
63
fn problems_carry_lines_and_are_all_reported() {
64
    let src = doc(r#"job "a" {
65
    sandbox "missing"
66
    exec "true"
67
    mount { source "oxy://workspace"; }
68
}
69
job "b" {
70
    sandbox "alpine"
71
    depends-on "nope"
72
    exec
73
}
74
flavour "x"
75
"#);
76
    let ps = problems(&src);
77
    let has = |line: usize, text: &str| ps.iter().any(|(l, m)| *l == line && m.contains(text));
78
    assert!(has(11, "which is not declared") && has(11, "\"missing\""), "{ps:?}");
79
    assert!(has(14, "mount source \"oxy://workspace\" is not in M0 yet"), "{ps:?}");
80
    assert!(has(16, "\"nope\""), "{ps:?}");
81
    assert!(has(19, "`exec` needs a program"), "{ps:?}");
82
    assert!(has(21, "unknown node `flavour`"), "{ps:?}");
83
}
84
85
#[test]
86
fn typed_values_are_not_coerced() {
87
    let src = format!(
88
        "schema-version 1\nsandbox \"s\" {{\n  rootfs \"cas://blob/{ROOTFS}\"\n  cpu-limit \"2\"\n  memory-limit \"1.5GiB\"\n}}\njob \"a\" {{\n  sandbox \"s\"\n  exec \"echo\" 1\n}}\n"
89
    );
90
    let ps = problems(&src);
91
    assert!(ps.iter().any(|(l, m)| *l == 4 && m.contains("integer, not a string")), "{ps:?}");
92
    assert!(ps.iter().any(|(l, m)| *l == 5 && m.contains("whole number")), "{ps:?}");
93
    assert!(ps.iter().any(|(l, m)| *l == 9 && m.contains("strings, not an integer")), "{ps:?}");
94
}
95
96
#[test]
97
fn m0_refuses_what_it_cannot_honour() {
98
    let src = format!("schema-version 1\nsandbox \"s\" {{\n  runtime \"wasm\"\n  rootfs \"cas://blob/{ROOTFS}\"\n  network \"egress\"\n}}\njob \"a\" {{ sandbox \"s\"; exec \"true\"; }}\n");
99
    let ps = problems(&src);
100
    assert!(ps.iter().any(|(l, m)| *l == 3 && m.contains("wasm")), "{ps:?}");
101
    assert!(ps.iter().any(|(l, m)| *l == 5 && m.contains("isolated")), "{ps:?}");
102
    assert!(problems("schema-version 2\n").iter().any(|(_, m)| m.contains("schema-version 2")));
103
    assert!(problems(&format!(
104
        "schema-version 1\nsandbox \"s\" {{ rootfs \"cas://blob/{}\"; }}\njob \"a\" {{ sandbox \"s\"; exec \"true\"; }}",
105
        &ROOTFS[..60]
106
    ))
107
    .iter()
108
    .any(|(_, m)| m.contains("64 lower-case hex")));
109
}
110
111
#[test]
112
fn cycles_are_found() {
113
    let ps = problems(&doc(
114
        "job \"a\" { sandbox \"alpine\"; depends-on \"c\"; exec \"true\"; }\njob \"b\" { sandbox \"alpine\"; depends-on \"a\"; exec \"true\"; }\njob \"c\" { sandbox \"alpine\"; depends-on \"b\"; exec \"true\"; }\njob \"d\" { sandbox \"alpine\"; depends-on \"d\"; exec \"true\"; }\n",
115
    ));
116
    assert!(ps.iter().any(|(_, m)| m.contains("cycle: a → c → b → a")), "{ps:?}");
117
    assert!(ps.iter().any(|(_, m)| m.contains("\"d\" depends on itself")), "{ps:?}");
118
}
119
120
#[test]
121
fn invalid_kdl_has_a_line() {
122
    let ps = problems("schema-version 1\njob \"a\" {\n  exec \"unterminated\n}\n");
123
    assert!(!ps.is_empty() && ps[0].0 >= 2, "{ps:?}");
124
}
125
126
#[test]
127
fn canonical_form_ignores_layout_and_order() {
128
    let a = Pipeline::parse(&doc(
129
        "job \"b\" { sandbox \"alpine\"; exec \"true\"; }\njob \"a\" {\n  sandbox \"alpine\"\n  depends-on \"b\"\n  exec \"x\"\n}\n",
130
    ))
131
    .unwrap();
132
    let b = Pipeline::parse(&doc("// comment\njob \"a\" { exec \"x\"; depends-on \"b\"; sandbox \"alpine\"; }\n\n\njob \"b\" { exec \"true\"; sandbox \"alpine\"; }\n")).unwrap();
133
    assert_eq!(a.canonical(), b.canonical());
134
    assert_eq!(a.hash(), b.hash());
135
    let c = Pipeline::parse(&doc(
136
        "job \"b\" { sandbox \"alpine\"; exec \"true\"; }\njob \"a\" { sandbox \"alpine\"; depends-on \"b\"; exec \"y\"; }\n",
137
    ))
138
    .unwrap();
139
    assert_ne!(a.hash(), c.hash());
140
    let sb = &a.sandboxes["alpine"];
141
    assert_ne!(a.jobs["a"].hash(sb), c.jobs["a"].hash(sb));
142
    assert_eq!(a.jobs["b"].hash(sb), c.jobs["b"].hash(sb), "an unchanged job keeps its hash");
143
    // The canonical form round-trips through Oxygen's strict decoder.
144
    oxy_core::cbor::decode(&a.canonical()).unwrap();
145
}
146
147
#[test]
148
fn scope_tree_hash_sees_only_the_scope() {
149
    let f = |b: u8| [b; 32];
150
    let scope = vec!["src/**".to_string()];
151
    let base = scope_tree_hash(&scope, [("src/a.rs", f(1)), ("README.md", f(2))]);
152
    assert_eq!(base, scope_tree_hash(&scope, [("README.md", f(9)), ("src/a.rs", f(1))]), "outside the scope, and order, do not count");
153
    assert_ne!(base, scope_tree_hash(&scope, [("src/a.rs", f(3)), ("README.md", f(2))]));
154
    assert_ne!(scope_tree_hash(&[], [("README.md", f(2))]), scope_tree_hash(&[], [("README.md", f(9))]), "no scope is the whole tree");
155
}
156
157
#[test]
158
fn receipts_hash_as_oxygen_objects() {
159
    let r = Receipt {
160
        job_name: "lint".into(),
161
        pipeline_hash: [1; 32],
162
        scope_tree_hash: [2; 32],
163
        inputs: vec![],
164
        environment: [3; 32],
165
        outputs: vec![],
166
        exit_code: 0,
167
        duration_ms: 1234,
168
    };
169
    let body = r.encode();
170
    let mut want = format!("oxy1 receipt {}\0", body.len()).into_bytes();
171
    want.extend_from_slice(&body);
172
    assert_eq!(r.hash(), *blake3::hash(&want).as_bytes());
173
    let json = serde_json::to_string(&r).unwrap();
174
    assert_eq!(serde_json::from_str::<Receipt>(&json).unwrap(), r);
175
    // The same receipt as the Oxygen object a runner signs.
176
    assert_eq!(r.to_oxy().hash().0, r.hash());
177
    assert_eq!(r.to_oxy().encode(), body);
178
    let mut r2 = r.clone();
179
    r2.exit_code = 1;
180
    assert_ne!(r.hash(), r2.hash());
181
}
182
183
#[test]
184
fn ephemeral_mounts_parse_sort_and_hash() {
185
    let plain = Pipeline::parse(&doc("job \"b\" {\n    sandbox \"alpine\"\n    exec \"cargo\" \"build\"\n}\n")).unwrap();
186
    let p = Pipeline::parse(&doc(r#"job "b" {
187
    sandbox "alpine"
188
    mount {
189
        source "ephemeral://tmpfs"
190
        target "/tmp/cache"
191
        size "256MiB"
192
    }
193
    mount {
194
        source "ephemeral://disk"
195
        target "/src/target"
196
        size "8GiB"
197
    }
198
    exec "cargo" "build"
199
}
200
"#))
201
    .unwrap();
202
    let b = &p.jobs["b"];
203
    assert_eq!(
204
        b.mounts,
205
        vec![
206
            Mount { source: MountSource::Disk, target: "/src/target".into(), size_mib: 8192 },
207
            Mount { source: MountSource::Tmpfs, target: "/tmp/cache".into(), size_mib: 256 },
208
        ],
209
        "sorted by target"
210
    );
211
    assert_ne!(p.hash(), plain.hash(), "mounts are part of the job");
212
    // A job without mounts encodes as before mounts existed: no field 6.
213
    let v = oxy_core::cbor::decode(&plain.canonical()).unwrap();
214
    let text = format!("{v:?}");
215
    assert!(!text.contains("ephemeral"), "{text}");
216
    let json = serde_json::to_value(&plain.jobs["b"]).unwrap();
217
    assert!(json.get("mounts").is_none(), "absent from the API too");
218
    let back: Job = serde_json::from_value(json).unwrap();
219
    assert!(back.mounts.is_empty());
220
}
221
222
#[test]
223
fn mounts_are_checked() {
224
    let job = |mount: &str| doc(&format!("job \"b\" {{\n    sandbox \"alpine\"\n    mount {{\n{mount}\n    }}\n    exec \"true\"\n}}\n"));
225
    let has = |src: String, want: &str| {
226
        let ps = problems(&src);
227
        assert!(ps.iter().any(|(_, m)| m.contains(want)), "{want:?} not in {ps:?}");
228
    };
229
    has(job("source \"oxy://workspace\"\ntarget \"/src/x\"\nsize \"1GiB\""), "not in M0 yet");
230
    has(job("source \"ephemeral://disk\"\ntarget \"/etc/x\"\nsize \"1GiB\""), "under /src or /tmp");
231
    has(job("source \"ephemeral://disk\"\ntarget \"/src\"\nsize \"1GiB\""), "under /src or /tmp");
232
    has(job("source \"ephemeral://disk\"\ntarget \"/src/../etc\"\nsize \"1GiB\""), "without . or ..");
233
    has(job("source \"ephemeral://disk\"\ntarget \"/src/x/\"\nsize \"1GiB\""), "trailing /");
234
    has(job("source \"ephemeral://disk\"\ntarget \"/src/x\"\nsize \"8MiB\""), "from 16MiB to 64GiB");
235
    has(job("source \"ephemeral://disk\"\ntarget \"/src/x\"\nsize \"65GiB\""), "from 16MiB to 64GiB");
236
    has(job("source \"ephemeral://disk\"\ntarget \"/src/x\"\nsize \"lots\""), "whole number");
237
    has(job("source \"ephemeral://disk\"\ntarget \"/src/x\""), "needs a size");
238
    has(job("source \"ephemeral://disk\"\ntarget \"/src/x\"\nsize \"1GiB\"\nreadonly #true"), "`readonly` is not in M0 yet");
239
    has(job("source \"ephemeral://disk\"\nsource \"ephemeral://tmpfs\"\ntarget \"/src/x\"\nsize \"1GiB\""), "given twice");
240
    let two = |a: &str, b: &str| {
241
        doc(&format!(
242
            "job \"b\" {{\n    sandbox \"alpine\"\n    mount {{ source \"ephemeral://disk\"; target \"{a}\"; size \"1GiB\"; }}\n    mount {{ source \"ephemeral://disk\"; target \"{b}\"; size \"1GiB\"; }}\n    exec \"true\"\n}}\n"
243
        ))
244
    };
245
    has(two("/src/t", "/src/t/deps"), "overlaps");
246
    has(two("/src/t", "/src/t"), "overlaps");
247
    assert!(Pipeline::parse(&two("/src/t", "/src/tt")).is_ok(), "a shared prefix is not nesting");
248
    let five: String =
249
        (0..5).map(|i| format!("    mount {{ source \"ephemeral://tmpfs\"; target \"/tmp/m{i}\"; size \"16MiB\"; }}\n")).collect();
250
    has(doc(&format!("job \"b\" {{\n    sandbox \"alpine\"\n{five}    exec \"true\"\n}}\n")), "at most 4 mounts");
251
}