aboutsummaryrefslogtreecommitdiffstats
path: root/tests/migrations.rs
blob: 7b78b8e3cdbed795bff0fb1dd12dda716ab2a13a (plain)
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
//! The cases of docs/migrations.md, as far as the first version goes: a renamed slot
//! and a rollback across it, a type that changes without a converter, and what is
//! kept.  G1 and G2 are generations, S1 and S2 their schemas.

mod common;

use common::Host;
use enstate::view::Change;
use serde_json::{Value, json};

fn secrets(slots: Value, views: Value) -> Value {
    json!({ "schema": 1, "channels": { "secrets": { "slots": slots } }, "views": views })
}

fn rotate(host: &Host, name: &str, slot: &str, bytes: &[u8]) -> std::path::PathBuf {
    let address = format!("secrets/{slot}");
    host.submission(
        name,
        json!({ "channel": "secrets", "items": [{ "address": address, "op": "set" }] }),
        &[(address.as_str(), bytes)],
    )
}

/// Case 1: S2 renames `tinc.key` to `retiolum.key`.  Switching to G2 needs no
/// submission; a rotation under G2 stores the state under S2; rolling back to G1
/// gives it the rotated key, by S2's rename read backwards.
#[test]
fn a_renamed_slot_and_a_rollback() {
    let host = Host::new("rename");
    let s1 = host.schema(secrets(
        json!({ "tinc.key": { "type": "application/octet-stream", "secret": true } }),
        json!({ "tinc.key": { "from": ["secrets/tinc.key"], "reactions": { "restart": ["tinc.service"] } } }),
    ));
    let s2_value = secrets(
        json!({ "retiolum.key": { "type": "application/octet-stream", "secret": true, "renamedFrom": "tinc.key" } }),
        json!({ "retiolum.key": { "from": ["secrets/retiolum.key"], "reactions": { "restart": ["tincr.service"] } } }),
    );
    // G1 runs, K submitted.
    assert!(
        host.apply(&s1, &rotate(&host, "k", "tinc.key", b"K"))
            .accepted
    );
    let first = host.snapshot("secrets").unwrap();

    // Before switching to G2: it would see K, under its new name.
    let s2 = host.schema(s2_value.clone());
    let migration = host.migrate(&s2, true);
    assert!(migration.accepted, "{migration:?}");
    assert!(
        migration.channels["secrets"].seen.notes[0]
            .contains("secrets/retiolum.key: from secrets/tinc.key, renamed"),
        "{migration:?}"
    );
    assert!(host.view("tinc.key", "tinc.key").is_some());

    // Switching: the views follow, the snapshot isn't rewritten.
    let migration = host.migrate(&s2, false);
    assert!(
        migration.accepted && migration.failed.is_empty(),
        "{migration:?}"
    );
    assert_eq!(host.view("retiolum.key", "retiolum.key").unwrap(), b"K");
    assert!(host.view("tinc.key", "tinc.key").is_none());
    assert!(host.reactions().contains("restart tincr.service"));
    let stored = host.store().current("secrets").unwrap().unwrap();
    assert_eq!(stored.id, first);
    assert_eq!(stored.schema, s1.1);

    // Rotating under G2 stores the state under S2, and nothing is kept: tinc.key went
    // into retiolum.key.
    let plan = host.apply(&s2, &rotate(&host, "k2", "retiolum.key", b"K'"));
    assert!(plan.accepted && plan.warnings.is_empty(), "{plan:?}");
    let stored = host.store().current("secrets").unwrap().unwrap();
    assert_eq!(stored.schema, s2.1);
    assert_eq!(stored.content.keys().collect::<Vec<_>>(), ["retiolum.key"]);
    assert!(stored.kept.is_empty());

    // Rolling back to G1: tinc.key is the rotated key, from S2's rename.
    let s1 = host.schema(serde_json::from_slice(&s1.1).unwrap());
    let migration = host.migrate(&s1, false);
    assert!(migration.accepted, "{migration:?}");
    assert!(
        migration.channels["secrets"].seen.notes[0].contains(
            "secrets/tinc.key: from secrets/retiolum.key, which the schema it was accepted under renamed from it"
        ),
        "{migration:?}"
    );
    assert_eq!(host.view("tinc.key", "tinc.key").unwrap(), b"K'");
    assert!(host.view("retiolum.key", "retiolum.key").is_none());
}

/// Trying an older generation and back costs nothing: the snapshot stays as it was.
#[test]
fn switching_back_and_forth_rewrites_nothing() {
    let host = Host::new("back-and-forth");
    let s1 = host.schema(secrets(
        json!({ "a": { "type": "application/octet-stream" } }),
        json!({ "a": { "from": ["secrets/a"] } }),
    ));
    assert!(host.apply(&s1, &rotate(&host, "a", "a", b"A")).accepted);
    let id = host.snapshot("secrets").unwrap();
    let s2 = host.schema(secrets(
        json!({ "b": { "type": "application/octet-stream", "renamedFrom": "a" } }),
        json!({ "b": { "from": ["secrets/b"] } }),
    ));
    assert!(host.migrate(&s2, false).accepted);
    let s1 = host.schema(serde_json::from_slice(&s1.1).unwrap());
    assert!(host.migrate(&s1, false).accepted);
    assert!(host.migrate(&s2, false).accepted);
    assert_eq!(host.snapshot("secrets").unwrap(), id);
    assert_eq!(host.view("b", "b").unwrap(), b"A");
}

/// Case 2, without converters (not in the first version): a slot whose type changes
/// can't be filled, so the switch is refused before switching, naming the slot; after
/// a switch anyway (a boot), the views made from it aren't there.
#[test]
fn a_type_without_a_converter() {
    let host = Host::new("type");
    let fleet = |media_type: &str| {
        json!({
            "schema": 1,
            "channels": { "fleet": { "slots": { "hosts": { "type": media_type } } } },
            "views": { "hosts": { "from": ["fleet/hosts"] } }
        })
    };
    let s1 = host.schema(fleet("application/x-hosts1+json"));
    let first = host.submission(
        "first",
        json!({ "channel": "fleet", "items": [{ "address": "fleet/hosts/ne", "op": "set" }] }),
        &[("fleet/hosts", br#"{"ne": {"addr": "a"}}"#)],
    );
    assert!(host.apply(&s1, &first).accepted);
    let s2 = host.schema(fleet("application/x-hosts2+json"));
    let migration = host.migrate(&s2, true);
    assert!(!migration.accepted);
    assert_eq!(
        migration.refused,
        ["required slot fleet/hosts can't be filled"]
    );
    assert!(
        migration.channels["fleet"].seen.notes[0].contains(
            "fleet/hosts: type application/x-hosts1+json is now application/x-hosts2+json, and there is no converter"
        ),
        "{migration:?}"
    );
    // The dry run changed nothing; a switch anyway takes the view away.
    assert!(host.view("hosts", "hosts").is_some());
    let migration = host.migrate(&s2, false);
    assert!(!migration.accepted);
    assert!(
        migration
            .views
            .iter()
            .any(|view| view.name == "hosts" && view.change == Change::Removed)
    );
    assert!(host.view("hosts", "hosts").is_none());
    // A submission made for S2 fills it, and the old entry goes: it is older.
    let fresh = host.submission(
        "fresh",
        json!({ "channel": "fleet", "items": [{ "address": "fleet/hosts/ne", "op": "set" }] }),
        &[("fleet/hosts", br#"{"ne": {"addrs": ["a", "b"]}}"#)],
    );
    let plan = host.apply(&s2, &fresh);
    assert!(plan.accepted, "{plan:?}");
    assert_eq!(
        plan.warnings,
        [
            "goes, as fleet/hosts is filled anew: the entry of type application/x-hosts1+json kept from before"
        ]
    );
    assert!(host.view("hosts", "hosts").is_some());
    // Back to G1 is refused before switching: nothing in S2 makes S1's type.
    let s1 = host.schema(serde_json::from_slice(&s1.1).unwrap());
    let migration = host.migrate(&s1, true);
    assert_eq!(
        migration.refused,
        ["required slot fleet/hosts can't be filled"]
    );
    assert!(migration.channels["fleet"].seen.notes[0].contains("is now application/x-hosts1+json"));
}

/// What no slot wants is kept, and fills its slot again after a rollback; a complete
/// submission lets it go.
#[test]
fn what_is_kept() {
    let host = Host::new("kept");
    let both = secrets(
        json!({
            "a": { "type": "application/octet-stream" },
            "extra": { "type": "application/octet-stream", "required": false }
        }),
        json!({ "extra": { "from": ["secrets/extra"] } }),
    );
    let s1 = host.schema(both.clone());
    let first = host.submission(
        "first",
        json!({ "channel": "secrets", "complete": true, "items": [
            { "address": "secrets/a", "op": "set" },
            { "address": "secrets/extra", "op": "set" }
        ] }),
        &[("secrets/a", b"A"), ("secrets/extra", b"E")],
    );
    assert!(host.apply(&s1, &first).accepted);
    let s2 = host.schema(secrets(
        json!({ "a": { "type": "application/octet-stream" } }),
        json!({}),
    ));
    let migration = host.migrate(&s2, false);
    assert_eq!(migration.channels["secrets"].kept, ["extra"]);
    assert!(host.view("extra", "extra").is_none());
    let plan = host.apply(&s2, &rotate(&host, "a2", "a", b"A2"));
    assert_eq!(
        plan.warnings,
        ["kept, in no slot of the schema: secrets/extra"]
    );
    let stored = host.store().current("secrets").unwrap().unwrap();
    assert_eq!(stored.kept["extra"].1, b"E");
    // Back to G1: extra is there again.
    let s1 = host.schema(both);
    let migration = host.migrate(&s1, false);
    assert!(migration.channels["secrets"].seen.notes[0].contains("from what was kept"));
    assert_eq!(host.view("extra", "extra").unwrap(), b"E");
    // Under G2 again, a complete submission lets it go.
    let s2 = host.schema(secrets(
        json!({ "a": { "type": "application/octet-stream" } }),
        json!({}),
    ));
    let complete = host.submission(
        "complete",
        json!({ "channel": "secrets", "complete": true, "items": [{ "address": "secrets/a", "op": "set" }] }),
        &[("secrets/a", b"A3")],
    );
    let plan = host.apply(&s2, &complete);
    assert_eq!(
        plan.warnings,
        ["goes, as the submission is complete: secrets/extra, which was kept"]
    );
    assert!(
        host.store()
            .current("secrets")
            .unwrap()
            .unwrap()
            .kept
            .is_empty()
    );
}