1
//! Contract tests for the artifact layout.
2
//!
3
//! These run on every branch and PR via `cargo test -p xtask`, which matters
4
//! because the S3 transport they guard is master-only: without them the rules
5
//! master depends on would first execute after a merge.
6

            
7
use super::*;
8

            
9
const DIGEST: &str = "@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
10

            
11
4
fn identity(run_id: &str, attempt: &str) -> Identity {
12
4
    Identity {
13
4
        commit: "abc123".into(),
14
4
        run_id: run_id.into(),
15
4
        run_attempt: attempt.into(),
16
4
        builder: format!("registry.example/nomisync-builder:1.96.1{DIGEST}"),
17
4
    }
18
4
}
19

            
20
3
fn meta(kind: Kind, sha: &str, identity: Identity) -> Meta {
21
3
    Meta {
22
3
        kind,
23
3
        sha256: sha.into(),
24
3
        bytes: 10,
25
3
        identity,
26
3
    }
27
3
}
28

            
29
#[test]
30
1
fn prefix_is_commit_then_run_id() {
31
1
    assert_eq!(
32
1
        identity("77", "1").prefix("buck"),
33
1
        "s3://buck/abc123/77",
34
        "the prefix is what makes cross-run and cross-commit mixing unreachable"
35
    );
36
1
}
37

            
38
#[test]
39
1
fn different_bytes_give_a_different_object_key() {
40
1
    let a = meta(Kind::Coverage, "deadbeef", identity("77", "1")).object_key();
41
1
    let b = meta(Kind::Coverage, "cafebabe", identity("77", "1")).object_key();
42
1
    assert_ne!(
43
1
        a, b,
44
        "content addressing is what makes an overwrite-with-different-bytes impossible"
45
    );
46
1
}
47

            
48
#[test]
49
1
fn meta_round_trips() {
50
1
    let original = meta(Kind::Release, "beef", identity("77", "2"));
51
1
    let parsed = Meta::from_text(&original.to_text()).expect("meta should parse");
52
1
    assert_eq!(parsed, original);
53
1
}
54

            
55
#[test]
56
1
fn meta_is_parsed_not_evaluated() {
57
    // A meta is fetched from object storage. The shell version had to avoid
58
    // sourcing it; here the equivalent mistake is not expressible, and this
59
    // pins that a shell-ish payload survives as inert text.
60
1
    let text = "kind=coverage\nobject=$(touch /tmp/pwned).tar\nsha256=dead\nbytes=1\n\
61
1
                commit=abc123\nrun_id=77\nrun_attempt=1\nbuilder=x@sha256:y\n";
62
1
    let parsed = Meta::from_text(text).expect("meta should parse");
63
1
    assert_eq!(parsed.sha256, "dead");
64
1
}
65

            
66
#[test]
67
1
fn meta_missing_a_field_is_an_error() {
68
1
    let text = "kind=coverage\nsha256=dead\nbytes=1\n";
69
1
    assert!(
70
1
        Meta::from_text(text).is_err(),
71
        "an incomplete meta must not parse"
72
    );
73
1
}
74

            
75
#[test]
76
1
fn latest_coverage_lives_outside_every_run_prefix() {
77
1
    assert_eq!(Meta::latest_key(Kind::Coverage), "latest/coverage.meta");
78
1
}
79

            
80
/// Environment as a map, so these never touch the real process environment.
81
5
fn env(pairs: &[(&str, &str)]) -> impl Fn(&str) -> Option<String> + use<> {
82
5
    let owned: Vec<(String, String)> = pairs
83
5
        .iter()
84
15
        .map(|(k, v)| ((*k).to_owned(), (*v).to_owned()))
85
        .collect();
86
31
    move |key| owned.iter().find(|(k, _)| k == key).map(|(_, v)| v.clone())
87
5
}
88

            
89
#[test]
90
1
fn identity_requires_a_digest_pinned_builder() {
91
    // Guards the reason the digest pin exists: a tag can be moved, so it cannot
92
    // prove two producers ran the same image.
93
1
    let err = Identity::from_source(env(&[
94
1
        ("SHA", "abc123"),
95
1
        ("GITHUB_RUN_ID", "77"),
96
1
        ("RUST_BUILDER_IMAGE_REF", "registry.example/builder:1.96.1"),
97
1
    ]))
98
1
    .expect_err("a tag-only builder ref must be refused");
99
1
    assert!(format!("{err}").contains("not digest-pinned"), "{err}");
100
1
}
101

            
102
#[test]
103
1
fn identity_refuses_a_missing_run_id() {
104
1
    let err = Identity::from_source(env(&[
105
1
        ("SHA", "abc123"),
106
1
        (
107
1
            "RUST_BUILDER_IMAGE_REF",
108
1
            "registry.example/builder:1.96.1@sha256:aa",
109
1
        ),
110
1
    ]))
111
1
    .expect_err("a missing run number must not default");
112
1
    assert!(format!("{err}").contains("GITHUB_RUN_ID"), "{err}");
113
1
}
114

            
115
#[test]
116
1
fn identity_refuses_an_empty_run_id() {
117
    // ci-build forwards this undefaulted, so empty is the shape a missing value
118
    // actually arrives in.
119
1
    let err = Identity::from_source(env(&[
120
1
        ("SHA", "abc123"),
121
1
        ("GITHUB_RUN_ID", ""),
122
1
        (
123
1
            "RUST_BUILDER_IMAGE_REF",
124
1
            "registry.example/builder:1.96.1@sha256:aa",
125
1
        ),
126
1
    ]))
127
1
    .expect_err("an empty run number must not be accepted");
128
1
    assert!(format!("{err}").contains("GITHUB_RUN_ID"), "{err}");
129
1
}
130

            
131
#[test]
132
1
fn identity_accepts_a_complete_environment() {
133
1
    let identity = Identity::from_source(env(&[
134
1
        ("SHA", "abc123"),
135
1
        ("GITHUB_RUN_ID", "77"),
136
1
        ("GITHUB_RUN_ATTEMPT", "2"),
137
1
        (
138
1
            "RUST_BUILDER_IMAGE_REF",
139
1
            "registry.example/builder:1.96.1@sha256:aa",
140
1
        ),
141
1
    ]))
142
1
    .expect("complete identity should be accepted");
143
1
    assert_eq!(identity.run_attempt, "2");
144
1
}
145

            
146
#[test]
147
1
fn a_missing_attempt_degrades_rather_than_failing() {
148
    // The attempt is provenance detail, not a path component, so a platform
149
    // that does not supply it must still be able to publish.
150
1
    let identity = Identity::from_source(env(&[
151
1
        ("SHA", "abc123"),
152
1
        ("GITHUB_RUN_ID", "77"),
153
1
        (
154
1
            "RUST_BUILDER_IMAGE_REF",
155
1
            "registry.example/builder:1.96.1@sha256:aa",
156
1
        ),
157
1
    ]))
158
1
    .expect("a missing attempt must not be fatal");
159
1
    assert_eq!(identity.run_attempt, "unknown");
160
1
}