canic-host 0.100.0

Host-side App build, Fleet install, deployment, and release-set library for Canic workspaces
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
427
428
429
430
431
432
433
434
435
436
437
use super::*;
use crate::test_support::temp_dir;
use std::{
    fs,
    sync::{Arc, Barrier},
};

#[test]
fn catalog_reads_network_scoped_fleet_rows_in_canonical_order() {
    let root = fixture("list");
    let network = CanonicalNetworkId::public_ic();
    write_catalog(
        &root,
        network,
        vec![
            entry(network, 1, "alpha", "shop", "staging", "aaaaa-aa"),
            entry(network, 2, "zeta", "shop", "production", "2vxsx-fae"),
        ],
    );

    let report = build_fleet_catalog_report(&request(&root, "staging")).expect("Fleet catalog");

    assert_eq!(report.canonical_network_id, network);
    assert_eq!(report.environment, "staging");
    assert_eq!(
        report
            .entries
            .iter()
            .map(|entry| entry.fleet_name.as_str())
            .collect::<Vec<_>>(),
        vec!["alpha", "zeta"]
    );
    assert_eq!(report.entries[0].app.as_str(), "shop");
    assert_eq!(report.entries[0].environment, "staging");
    fs::remove_dir_all(root).expect("remove fixture");
}

#[test]
fn environment_aliases_read_the_same_canonical_network_catalog() {
    let root = fixture("aliases");
    let network = CanonicalNetworkId::public_ic();
    write_catalog(
        &root,
        network,
        vec![entry(
            network,
            1,
            "shop-production",
            "shop",
            "production",
            "aaaaa-aa",
        )],
    );

    let staging = build_fleet_catalog_report(&request(&root, "staging")).expect("staging catalog");
    let production =
        build_fleet_catalog_report(&request(&root, "production")).expect("production catalog");

    assert_eq!(
        staging.canonical_network_id,
        production.canonical_network_id
    );
    assert_eq!(staging.entries, production.entries);
    fs::remove_dir_all(root).expect("remove fixture");
}

#[test]
fn missing_catalog_has_no_fleet_entries() {
    let root = fixture("missing");

    let report = build_fleet_catalog_report(&request(&root, "staging")).expect("empty catalog");

    assert!(report.entries.is_empty());
    fs::remove_dir_all(root).expect("remove fixture");
}

#[test]
fn catalog_commit_is_canonical_durable_and_exactly_idempotent() {
    let root = fixture("commit");
    let network = CanonicalNetworkId::public_ic();
    let requested = entry(network, 7, "shop-staging", "shop", "staging", "aaaaa-aa");

    let committed =
        commit_fleet_catalog_entry(&root, requested.clone()).expect("commit Fleet catalog");
    let bytes = fs::read(&committed.path).expect("read committed catalog");
    assert!(committed.advanced);
    assert_eq!(committed.entry, requested);
    assert_eq!(
        committed.catalog_hash,
        <[u8; 32]>::from(Sha256::digest(&bytes))
    );
    assert_eq!(bytes.last(), Some(&b'\n'));

    let mut repeated_request = requested;
    repeated_request.environment = "production".to_string();
    repeated_request.deployed_at_unix_secs = 999;
    let repeated =
        commit_fleet_catalog_entry(&root, repeated_request).expect("repeat exact Fleet authority");
    assert!(!repeated.advanced);
    assert_eq!(repeated.entry.environment, "staging");
    assert_eq!(repeated.entry.deployed_at_unix_secs, 54);
    assert_eq!(repeated.catalog_hash, committed.catalog_hash);
    assert_eq!(
        fs::read(&repeated.path).expect("read unchanged catalog"),
        bytes
    );

    fs::remove_dir_all(root).expect("remove fixture");
}

#[test]
fn catalog_commit_rejects_name_id_and_root_conflicts_without_mutation() {
    let root = fixture("commit-conflict");
    let network = CanonicalNetworkId::public_ic();
    let first = entry(network, 7, "shop-staging", "shop", "staging", "aaaaa-aa");
    let committed = commit_fleet_catalog_entry(&root, first).expect("commit first Fleet");
    let bytes = fs::read(&committed.path).expect("read first catalog");

    let conflicts = [
        entry(network, 8, "shop-staging", "other", "staging", "2vxsx-fae"),
        entry(network, 7, "other-staging", "other", "staging", "2vxsx-fae"),
        entry(network, 8, "other-staging", "other", "staging", "aaaaa-aa"),
    ];
    for conflict in conflicts {
        assert!(matches!(
            commit_fleet_catalog_entry(&root, conflict),
            Err(FleetCatalogError::Conflict { .. })
        ));
        assert_eq!(
            fs::read(&committed.path).expect("read unchanged catalog"),
            bytes
        );
    }

    fs::remove_dir_all(root).expect("remove fixture");
}

#[test]
fn concurrent_catalog_commits_preserve_both_fleet_rows() {
    let root = fixture("commit-concurrent");
    let network = CanonicalNetworkId::public_ic();
    let barrier = Arc::new(Barrier::new(3));
    let mut handles = Vec::new();
    for requested in [
        entry(network, 7, "alpha", "shop", "staging", "aaaaa-aa"),
        entry(network, 8, "zeta", "shop", "production", "2vxsx-fae"),
    ] {
        let root = root.clone();
        let barrier = Arc::clone(&barrier);
        handles.push(std::thread::spawn(move || {
            barrier.wait();
            commit_fleet_catalog_entry(&root, requested).expect("commit concurrent Fleet")
        }));
    }
    barrier.wait();
    for handle in handles {
        handle.join().expect("join catalog writer");
    }

    let report = build_fleet_catalog_report(&request(&root, "staging")).expect("read both Fleets");
    assert_eq!(
        report
            .entries
            .iter()
            .map(|entry| entry.fleet_name.as_str())
            .collect::<Vec<_>>(),
        ["alpha", "zeta"]
    );

    fs::remove_dir_all(root).expect("remove fixture");
}

#[cfg(unix)]
#[test]
fn catalog_commit_rejects_a_symlinked_network_lock() {
    use std::os::unix::fs::symlink;

    let root = fixture("commit-lock-symlink");
    let network = CanonicalNetworkId::public_ic();
    let lock_path = fleet_catalog_lock_path(&root, network);
    fs::create_dir_all(lock_path.parent().expect("lock parent")).expect("create lock parent");
    let target = root.join("outside.lock");
    fs::write(&target, []).expect("write lock target");
    symlink(&target, &lock_path).expect("symlink network lock");

    assert!(matches!(
        commit_fleet_catalog_entry(
            &root,
            entry(network, 7, "shop-staging", "shop", "staging", "aaaaa-aa")
        ),
        Err(FleetCatalogError::NotRegular { path }) if path == lock_path
    ));
    assert!(!fleet_catalog_path(&root, network).exists());

    fs::remove_dir_all(root).expect("remove fixture");
}

#[test]
fn catalog_rejects_wrong_network_unsorted_names_and_duplicate_ids() {
    let root = fixture("invalid");
    let network = CanonicalNetworkId::public_ic();
    let path = fleet_catalog_path(&root, network);

    write_catalog_record(
        &path,
        FleetCatalogRecord {
            schema_version: FLEET_CATALOG_SCHEMA_VERSION,
            canonical_network_id: FleetId::from_generated_bytes([8; 32])
                .to_string()
                .parse()
                .expect("network-shaped text"),
            entries: Vec::new(),
        },
    );
    assert!(matches!(
        build_fleet_catalog_report(&request(&root, "staging")),
        Err(FleetCatalogError::Invalid { .. })
    ));

    write_catalog(
        &root,
        network,
        vec![
            entry(network, 1, "zeta", "shop", "staging", "aaaaa-aa"),
            entry(network, 2, "alpha", "shop", "staging", "2vxsx-fae"),
        ],
    );
    assert!(matches!(
        build_fleet_catalog_report(&request(&root, "staging")),
        Err(FleetCatalogError::Invalid { .. })
    ));

    write_catalog(
        &root,
        network,
        vec![
            entry(network, 1, "alpha", "shop", "staging", "aaaaa-aa"),
            entry(network, 1, "zeta", "shop", "staging", "2vxsx-fae"),
        ],
    );
    assert!(matches!(
        build_fleet_catalog_report(&request(&root, "staging")),
        Err(FleetCatalogError::Invalid { .. })
    ));
    fs::remove_dir_all(root).expect("remove fixture");
}

#[test]
fn catalog_rejects_malformed_unknown_field_and_invalid_identity_rows() {
    let root = fixture("malformed");
    let network = CanonicalNetworkId::public_ic();
    let path = fleet_catalog_path(&root, network);
    fs::create_dir_all(path.parent().expect("catalog parent")).expect("catalog directory");
    fs::write(&path, b"{not-json").expect("malformed catalog");
    assert!(matches!(
        build_fleet_catalog_report(&request(&root, "staging")),
        Err(FleetCatalogError::Decode { .. })
    ));

    let mut value = serde_json::to_value(FleetCatalogRecord {
        schema_version: FLEET_CATALOG_SCHEMA_VERSION,
        canonical_network_id: network,
        entries: vec![entry(
            network,
            1,
            "shop-production",
            "bad/app",
            "staging",
            "aaaaa-aa",
        )],
    })
    .expect("catalog value");
    value
        .as_object_mut()
        .expect("catalog object")
        .insert("legacy".to_string(), serde_json::Value::Bool(true));
    fs::write(&path, serde_json::to_vec(&value).expect("catalog JSON")).expect("unknown field");
    assert!(matches!(
        build_fleet_catalog_report(&request(&root, "staging")),
        Err(FleetCatalogError::Decode { .. })
    ));

    write_catalog(
        &root,
        network,
        vec![entry(
            network,
            1,
            "shop-production",
            "bad/app",
            "staging",
            "aaaaa-aa",
        )],
    );
    assert!(matches!(
        build_fleet_catalog_report(&request(&root, "staging")),
        Err(FleetCatalogError::Invalid { .. })
    ));
    fs::remove_dir_all(root).expect("remove fixture");
}

#[cfg(unix)]
#[test]
fn catalog_rejects_symlinked_authority() {
    use std::os::unix::fs::symlink;

    let root = fixture("symlink");
    let network = CanonicalNetworkId::public_ic();
    let path = fleet_catalog_path(&root, network);
    fs::create_dir_all(path.parent().expect("catalog parent")).expect("catalog directory");
    let target = root.join("catalog-target.json");
    write_catalog_record(
        &target,
        FleetCatalogRecord {
            schema_version: FLEET_CATALOG_SCHEMA_VERSION,
            canonical_network_id: network,
            entries: Vec::new(),
        },
    );
    symlink(&target, &path).expect("catalog symlink");

    assert!(matches!(
        build_fleet_catalog_report(&request(&root, "staging")),
        Err(FleetCatalogError::NotRegular { .. })
    ));
    fs::remove_dir_all(root).expect("remove fixture");
}

#[cfg(unix)]
#[test]
fn catalog_rejects_special_file_authority() {
    use rustix::fs::{CWD, Mode, mkfifoat};

    let root = fixture("special-file");
    let network = CanonicalNetworkId::public_ic();
    let path = fleet_catalog_path(&root, network);
    fs::create_dir_all(path.parent().expect("catalog parent")).expect("catalog directory");
    mkfifoat(CWD, &path, Mode::from_raw_mode(0o600)).expect("catalog FIFO");

    assert!(matches!(
        build_fleet_catalog_report(&request(&root, "staging")),
        Err(FleetCatalogError::NotRegular { .. })
    ));
    fs::remove_file(path).expect("remove FIFO");
    fs::remove_dir_all(root).expect("remove fixture");
}

#[test]
fn catalog_inspect_and_text_use_fleet_identity_terms() {
    let root = fixture("inspect");
    let network = CanonicalNetworkId::public_ic();
    write_catalog(
        &root,
        network,
        vec![entry(
            network,
            9,
            "shop-production",
            "shop",
            "production",
            "aaaaa-aa",
        )],
    );

    let report = inspect_fleet_catalog_report(&request(&root, "staging"), "shop-production")
        .expect("inspect Fleet");
    let text = fleet_catalog_report_text(&report);

    assert_eq!(report.entries.len(), 1);
    assert!(text.contains("Fleet catalog:"));
    assert!(text.contains("fleet_id:"));
    assert!(text.contains("app: shop"));
    assert!(!text.contains("deployment target"));
    assert!(matches!(
        inspect_fleet_catalog_report(&request(&root, "staging"), "unknown"),
        Err(FleetCatalogError::UnknownFleet { .. })
    ));
    fs::remove_dir_all(root).expect("remove fixture");
}

fn fixture(name: &str) -> PathBuf {
    let root = temp_dir(&format!("canic-fleet-catalog-{name}"));
    fs::create_dir_all(&root).expect("create project root");
    fs::write(
        root.join("icp.yaml"),
        "environments:\n  - name: staging\n    network: ic\n  - name: production\n    network: ic\n",
    )
    .expect("write ICP config");
    root
}

fn request(root: &Path, environment: &str) -> FleetCatalogRequest {
    FleetCatalogRequest {
        project_root: root.to_path_buf(),
        environment: environment.to_string(),
        generated_at: "unix:54".to_string(),
    }
}

fn write_catalog(root: &Path, network: CanonicalNetworkId, entries: Vec<FleetCatalogEntryV1>) {
    write_catalog_record(
        &fleet_catalog_path(root, network),
        FleetCatalogRecord {
            schema_version: FLEET_CATALOG_SCHEMA_VERSION,
            canonical_network_id: network,
            entries,
        },
    );
}

fn write_catalog_record(path: &Path, catalog: FleetCatalogRecord) {
    fs::create_dir_all(path.parent().expect("catalog parent")).expect("catalog directory");
    fs::write(
        path,
        serde_json::to_vec_pretty(&catalog).expect("catalog JSON"),
    )
    .expect("write catalog");
}

fn entry(
    network: CanonicalNetworkId,
    id_byte: u8,
    fleet_name: &str,
    app: &str,
    environment: &str,
    root_principal: &str,
) -> FleetCatalogEntryV1 {
    FleetCatalogEntryV1 {
        canonical_network_id: network,
        fleet_id: FleetId::from_generated_bytes([id_byte; 32]),
        fleet_name: fleet_name.parse().expect("Fleet name"),
        app: AppId::from(app),
        environment: environment.to_string(),
        deployed_at_unix_secs: 54,
        root_principal: root_principal.to_string(),
    }
}