canic-cli 0.50.0

Operator CLI for Canic deployment backup and restore workflows
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use super::*;
use crate::test_support::temp_dir;
use std::fs;

// Ensure fleet listing options accept network selection.
#[test]
fn parses_fleet_options() {
    let options = FleetOptions::parse([
        OsString::from(crate::cli::globals::INTERNAL_NETWORK_OPTION),
        OsString::from("ic"),
    ])
    .expect("parse fleet options");

    assert_eq!(options.network, "ic");
}

// Ensure fleet delete options require exactly one fleet name.
#[test]
fn parses_delete_fleet_options() {
    let options =
        DeleteFleetOptions::parse([OsString::from("demo")]).expect("parse delete options");

    assert_eq!(options.fleet, "demo");
}

// Ensure fleet check requires one fleet name.
#[test]
fn parses_check_fleet() {
    let options =
        FleetCheckOptions::parse_test([OsString::from("test")]).expect("parse check options");

    assert_eq!(options.fleet, "test");
}

// Ensure role list requires one fleet name.
#[test]
fn parses_role_list_fleet() {
    let options =
        RoleListOptions::parse_test([OsString::from("demo")]).expect("parse role list options");

    assert_eq!(options.fleet, "demo");
}

// Ensure role inspect requires fleet and role names.
#[test]
fn parses_role_inspect_fleet_and_role() {
    let options = RoleInspectOptions::parse_test([OsString::from("demo"), OsString::from("app")])
        .expect("parse role inspect options");

    assert_eq!(options.fleet, "demo");
    assert_eq!(options.role, "app");
}

// Ensure role declaration requires fleet, role, and package path.
#[test]
fn parses_role_declare_fleet_role_and_package() {
    let options = RoleDeclareOptions::parse_test([
        OsString::from("demo"),
        OsString::from("store"),
        OsString::from("--package"),
        OsString::from("store"),
    ])
    .expect("parse role declare options");

    assert_eq!(options.fleet, "demo");
    assert_eq!(options.role, "store");
    assert_eq!(options.package, "store");
}

// Ensure role attachment requires fleet, role, and subnet and defaults to singleton.
#[test]
fn parses_role_attach_fleet_role_and_subnet() {
    let options = RoleAttachOptions::parse_test([
        OsString::from("demo"),
        OsString::from("store"),
        OsString::from("--subnet"),
        OsString::from("prime"),
    ])
    .expect("parse role attach options");

    assert_eq!(options.fleet, "demo");
    assert_eq!(options.role, "store");
    assert_eq!(options.subnet, "prime");
    assert_eq!(options.kind, "singleton");
}

// Ensure role attachment accepts explicit non-singleton kind.
#[test]
fn parses_role_attach_kind() {
    let options = RoleAttachOptions::parse_test([
        OsString::from("demo"),
        OsString::from("worker"),
        OsString::from("--subnet"),
        OsString::from("prime"),
        OsString::from("--kind"),
        OsString::from("replica"),
    ])
    .expect("parse role attach options");

    assert_eq!(options.kind, "replica");
}

// Ensure role rename requires fleet, old role, and new role names.
#[test]
fn parses_role_rename_fleet_old_role_and_new_role() {
    let options = RoleRenameOptions::parse_test([
        OsString::from("demo"),
        OsString::from("hub"),
        OsString::from("router"),
    ])
    .expect("parse role rename options");

    assert_eq!(options.fleet, "demo");
    assert_eq!(options.old_role, "hub");
    assert_eq!(options.new_role, "router");
}

// Ensure unknown fleet check options fail through usage.
#[test]
fn rejects_unknown_check_option() {
    let err = FleetCheckOptions::parse_test([OsString::from("--unknown")])
        .expect_err("parse should fail");

    std::assert_matches!(err, FleetCommandError::Usage(_));
}

// Ensure fleet deletion requires the exact fleet name as confirmation.
#[test]
fn confirm_delete_fleet_requires_exact_name() {
    let target = Path::new("/tmp/canic/fleets/demo");
    let mut output = Vec::new();

    confirm_delete_fleet("demo", target, io::Cursor::new(b"demo\n"), &mut output)
        .expect("confirm delete");

    let output = String::from_utf8(output).expect("utf8 prompt");
    assert!(output.contains("Delete Canic fleet?"));
    assert!(output.contains("fleet: demo"));
    assert!(output.contains("Type the fleet name to confirm"));

    let err = confirm_delete_fleet("demo", target, io::Cursor::new(b"yes\n"), Vec::new())
        .expect_err("wrong confirmation should cancel");
    std::assert_matches!(err, FleetCommandError::DeleteCancelled);
}

// Ensure delete resolves the fleet config parent, not an arbitrary path.
#[test]
fn delete_target_resolves_config_parent() {
    let root = temp_dir("canic-fleet-delete-target");
    let demo = write_fleet_config(&root, "demo");
    let staging = write_fleet_config(&root, "staging");
    let choices = vec![demo.join("canic.toml"), staging.join("canic.toml")];

    let target = delete_target_dir_from_choices(&root, &choices, "staging").expect("delete target");

    fs::remove_dir_all(&root).expect("remove temp root");
    assert_eq!(target, staging);
}

// Ensure fleet listing renders deterministic config-defined rows.
#[test]
fn renders_fleet_list_table() {
    let table = render_fleet_list_from_rows(vec![
        FleetListRow {
            fleet: "demo".to_string(),
            network: "local".to_string(),
            config: "fleets/demo/canic.toml".to_string(),
            canisters: "4 (root, app, user_hub, user_shard)".to_string(),
        },
        FleetListRow {
            fleet: "staging".to_string(),
            network: "local".to_string(),
            config: "fleets/staging/canic.toml".to_string(),
            canisters: "2 (root, app)".to_string(),
        },
    ]);

    assert_eq!(
        table,
        [
            "FLEET     NETWORK   CONFIG                      CANISTERS",
            "-------   -------   -------------------------   -----------------------------------",
            "demo      local     fleets/demo/canic.toml      4 (root, app, user_hub, user_shard)",
            "staging   local     fleets/staging/canic.toml   2 (root, app)",
        ]
        .join("\n")
    );
}

// Ensure role lifecycle list renders declared-only and attached state.
#[test]
fn renders_role_lifecycle_table() {
    let table = render_role_lifecycle_rows(&[
        ConfiguredRoleLifecycle {
            fleet: "demo".to_string(),
            role: "root".to_string(),
            display: "demo.root".to_string(),
            declaration_kind: "root".to_string(),
            package: Some("canisters/root".to_string()),
            attached: true,
            state: "attached".to_string(),
            topology: Some("prime/root".to_string()),
        },
        ConfiguredRoleLifecycle {
            fleet: "demo".to_string(),
            role: "store".to_string(),
            display: "demo.store".to_string(),
            declaration_kind: "canister".to_string(),
            package: Some("canisters/store".to_string()),
            attached: false,
            state: "declared".to_string(),
            topology: None,
        },
    ]);

    assert_eq!(
        table,
        [
            "ROLE         PACKAGE           STATE      TOPOLOGY",
            "----------   ---------------   --------   ----------",
            "demo.root    canisters/root    attached   prime/root",
            "demo.store   canisters/store   declared   -",
        ]
        .join("\n")
    );
}

// Ensure role inspection explains build and deploy eligibility.
#[test]
fn renders_declared_only_role_inspection() {
    let output = render_role_inspection(&ConfiguredRoleLifecycle {
        fleet: "demo".to_string(),
        role: "store".to_string(),
        display: "demo.store".to_string(),
        declaration_kind: "canister".to_string(),
        package: Some("canisters/store".to_string()),
        attached: false,
        state: "declared".to_string(),
        topology: None,
    });

    assert!(output.contains("role: demo.store"));
    assert!(output.contains("cargo check: allowed"));
    assert!(output.contains("deploy artifact: blocked: role is declared-only"));
    assert!(output.contains("canic fleet role attach demo store --subnet <subnet>"));
}

// Ensure declaration output stays explicit about config-only state.
#[test]
fn renders_declared_role_output() {
    let root = Path::new("/workspace");
    let config = root.join("fleets/demo/canic.toml");
    let output = render_declared_role(
        &DeclaredFleetRole {
            fleet: "demo".to_string(),
            role: "store".to_string(),
            display: "demo.store".to_string(),
            package: "store".to_string(),
        },
        root,
        &config,
    );

    assert!(output.contains("Declared fleet role:"));
    assert!(output.contains("role: demo.store"));
    assert!(output.contains("package: store"));
    assert!(output.contains("config: fleets/demo/canic.toml"));
    assert!(output.contains("state: declared"));
    assert!(output.contains("canic fleet role attach demo store --subnet <subnet>"));
}

// Ensure attachment output points at artifact build as the next step.
#[test]
fn renders_attached_role_output() {
    let root = Path::new("/workspace");
    let config = root.join("fleets/demo/canic.toml");
    let output = render_attached_role(
        &AttachedFleetRole {
            fleet: "demo".to_string(),
            role: "store".to_string(),
            display: "demo.store".to_string(),
            subnet: "prime".to_string(),
            kind: "singleton".to_string(),
            topology: "prime/store".to_string(),
        },
        root,
        &config,
    );

    assert!(output.contains("Attached fleet role:"));
    assert!(output.contains("role: demo.store"));
    assert!(output.contains("kind: singleton"));
    assert!(output.contains("topology: prime/store"));
    assert!(output.contains("config: fleets/demo/canic.toml"));
    assert!(output.contains("state: attached"));
    assert!(output.contains("canic build demo store"));
}

// Ensure rename output reports config and package metadata updates.
#[test]
fn renders_renamed_role_output() {
    let root = Path::new("/workspace");
    let config = root.join("fleets/demo/canic.toml");
    let manifest = root.join("fleets/demo/router/Cargo.toml");
    let output = render_renamed_role(
        &RenamedFleetRole {
            fleet: "demo".to_string(),
            old_role: "hub".to_string(),
            new_role: "router".to_string(),
            old_display: "demo.hub".to_string(),
            new_display: "demo.router".to_string(),
            package_manifest: Some(manifest),
            package_manifest_note: None,
        },
        root,
        &config,
    );

    assert!(output.contains("Renamed fleet role:"));
    assert!(output.contains("old: demo.hub"));
    assert!(output.contains("new: demo.router"));
    assert!(output.contains("config: fleets/demo/canic.toml"));
    assert!(output.contains("package_manifest: fleets/demo/router/Cargo.toml"));
    assert!(output.contains("canic fleet role inspect demo router"));
}

// Ensure fleet command help lists the command family without search.
#[test]
fn fleet_usage_lists_subcommands_and_examples() {
    let text = usage();

    assert!(text.contains("Manage Canic fleets"));
    assert!(text.contains("Usage: canic fleet"));
    assert!(text.contains("check"));
    assert!(text.contains("create"));
    assert!(text.contains("delete"));
    assert!(text.contains("list"));
    assert!(text.contains("role"));
    assert!(!text.contains("sync"));
    assert!(!text.contains("current"));
    assert!(!text.contains("use"));
    assert!(!text.contains("search"));
    assert!(text.contains("Examples:"));
}

// Ensure fleet role help lists read-only lifecycle commands.
#[test]
fn fleet_role_usage_lists_subcommands_and_examples() {
    let text = role_usage();

    assert!(text.contains("Manage fleet role lifecycle"));
    assert!(text.contains("Usage: canic fleet role"));
    assert!(text.contains("declare"));
    assert!(text.contains("attach"));
    assert!(text.contains("rename"));
    assert!(text.contains("list"));
    assert!(text.contains("inspect"));
    assert!(text.contains("Examples:"));
}

// Ensure fleet check help explains read-only ICP config checks.
#[test]
fn fleet_check_usage_lists_options_and_examples() {
    let text = check_usage();

    assert!(text.contains("Check icp.yaml for one Canic fleet"));
    assert!(text.contains("Usage: canic fleet check <name>"));
    assert!(!text.contains("--fleet"));
    assert!(text.contains("Examples:"));
}

// Ensure fleet create help explains creation.
#[test]
fn fleet_create_usage_lists_options_and_examples() {
    let text = create_usage();

    assert!(text.contains("Create a minimal Canic fleet"));
    assert!(text.contains("Usage: canic fleet create"));
    assert!(!text.contains("--network"));
    assert!(text.contains("--yes"));
    assert!(text.contains("Examples:"));
}

// Ensure fleet list help explains network selection.
#[test]
fn fleet_list_usage_lists_options_and_examples() {
    let text = list_usage();

    assert!(text.contains("List config-defined Canic fleets"));
    assert!(text.contains("Usage: canic fleet list"));
    assert!(!text.contains("--network"));
    assert!(text.contains("Examples:"));
}

// Ensure fleet delete help explains the destructive confirmation.
#[test]
fn delete_usage_lists_confirmation() {
    let text = delete_usage();

    assert!(text.contains("Delete a config-defined Canic fleet directory"));
    assert!(text.contains("Usage: canic fleet delete"));
    assert!(text.contains("<name>"));
    assert!(text.contains("type the"));
}

// Ensure role list help takes explicit fleet identity.
#[test]
fn role_list_usage_lists_fleet_argument() {
    let text = role_list_usage();

    assert!(text.contains("Usage: canic fleet role list <fleet>"));
    assert!(text.contains("Examples:"));
}

// Ensure role inspect help takes explicit fleet and role identity.
#[test]
fn role_inspect_usage_lists_fleet_and_role_arguments() {
    let text = role_inspect_usage();

    assert!(text.contains("Usage: canic fleet role inspect <fleet> <role>"));
    assert!(text.contains("Examples:"));
}

// Ensure role declare help takes explicit fleet, role, and package path.
#[test]
fn role_declare_usage_lists_required_package() {
    let text = role_declare_usage();

    assert!(text.contains("Usage: canic fleet role declare"));
    assert!(text.contains("<fleet>"));
    assert!(text.contains("<role>"));
    assert!(text.contains("--package <path>"));
    assert!(text.contains("Examples:"));
}

// Ensure role attach help takes explicit fleet, role, and subnet.
#[test]
fn role_attach_usage_lists_required_subnet() {
    let text = role_attach_usage();

    assert!(text.contains("Usage: canic fleet role attach"));
    assert!(text.contains("<fleet>"));
    assert!(text.contains("<role>"));
    assert!(text.contains("--subnet <subnet>"));
    assert!(text.contains("--kind <kind>"));
    assert!(text.contains("Examples:"));
}

// Ensure role rename help takes explicit fleet, old role, and new role identity.
#[test]
fn role_rename_usage_lists_fleet_old_role_and_new_role_arguments() {
    let text = role_rename_usage();

    assert!(text.contains("Usage: canic fleet role rename <fleet> <old-role> <new-role>"));
    assert!(text.contains("Examples:"));
}

// Render precomputed config rows for focused table tests.
fn render_fleet_list_from_rows(rows: Vec<FleetListRow>) -> String {
    render_fleet_rows(rows)
}

fn write_fleet_config(root: &Path, name: &str) -> PathBuf {
    let dir = root.join("fleets").join(name);
    fs::create_dir_all(dir.join("root")).expect("create root dir");
    fs::write(dir.join("root/Cargo.toml"), "").expect("write root manifest");
    fs::write(
        dir.join("canic.toml"),
        format!(
            r#"
[fleet]
name = "{name}"

[roles.root]
kind = "root"

[subnets.prime.canisters.root]
kind = "root"
"#
        ),
    )
    .expect("write canic config");
    dir
}