ferrodock 1.10.0

A fast, native container manager for Docker, written in Rust.
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Renders the app offscreen against the local Docker Engine and saves
//! PNGs, for checking the design and refreshing the README screenshots:
//!
//! ```sh
//! SCREENSHOTS=docs/screenshots cargo test screenshots -- --ignored
//! ```

use std::time::{Duration, Instant};

use eframe::egui;
use egui_kittest::Harness;
use egui_kittest::kittest::Queryable as _;

use super::FerrodockApp;
use super::settings::Theme;

const SIZE: egui::Vec2 = egui::vec2(1240.0, 740.0);

fn harness() -> Harness<'static, FerrodockApp> {
    Harness::builder()
        .with_size(SIZE)
        .with_pixels_per_point(2.0)
        .wgpu()
        .build_eframe(|cc| FerrodockApp::new(cc))
}

/// Runs frames until the worker has had time to load everything.
fn settle(h: &mut Harness<'static, FerrodockApp>, time: Duration) {
    let start = Instant::now();
    while start.elapsed() < time {
        h.step();
        std::thread::sleep(Duration::from_millis(50));
    }
    h.run_steps(4);
}

fn save(h: &mut Harness<'static, FerrodockApp>, name: &str) {
    // Keep the pointer (and hover effects) out of the picture.
    h.event(egui::Event::PointerGone);
    h.run_steps(3);
    let dir = std::env::var("SCREENSHOTS").unwrap_or_else(|_| "target/screenshots".into());
    std::fs::create_dir_all(&dir).unwrap();
    let image = h.render().expect("offscreen rendering works");
    let path = format!("{dir}/{name}.png");
    image.save(&path).unwrap();
    println!("saved {path}");
}

fn set_theme(h: &mut Harness<'static, FerrodockApp>, theme: Theme) {
    h.state_mut().settings.theme = theme;
    let ctx = h.ctx.clone();
    h.state().settings.apply(&ctx);
    h.run_steps(3);
}

/// Opens a page and selects its first item that `pick` likes.
fn open(h: &mut Harness<'static, FerrodockApp>, page: usize, select: Option<String>) {
    h.state_mut().open_page(page);
    if let Some(id) = select {
        h.state_mut().pages[page].select(&id);
    }
    settle(h, Duration::from_millis(1500));
}

fn click(h: &mut Harness<'static, FerrodockApp>, label: &str) {
    h.get_by_label(label).click();
    h.run_steps(2);
}

/// The first running container with a published port, for the details shots.
fn busiest(h: &Harness<'static, FerrodockApp>) -> Option<String> {
    let containers = &h.state().store.containers;
    containers
        .iter()
        .find(|c| c.is_running() && !c.ports.is_empty())
        .or_else(|| containers.iter().find(|c| c.is_running()))
        .map(|c| c.id.clone())
}

/// Every screenshot used in the README and on the website.
#[test]
#[ignore]
fn screenshots() {
    let mut h = harness();
    settle(&mut h, Duration::from_secs(3));
    set_theme(&mut h, Theme::Light);

    open(&mut h, 0, None);
    // Let a few CPU readings come in for the sparklines.
    settle(&mut h, Duration::from_secs(10));
    save(&mut h, "containers");

    let busy = busiest(&h);
    // A container that writes a steady stream of logs, if there is one.
    let chatty = h
        .state()
        .store
        .containers
        .iter()
        .find(|c| c.name == "payments")
        .or_else(|| {
            h.state()
                .store
                .containers
                .iter()
                .find(|c| c.name == "api-server")
        })
        .map(|c| c.id.clone())
        .or(busy.clone());
    open(&mut h, 0, chatty);
    click(&mut h, "Logs");
    click(&mut h, "Time");
    settle(&mut h, Duration::from_secs(2));
    save(&mut h, "logs");

    click(&mut h, "Stats");
    settle(&mut h, Duration::from_secs(6));
    save(&mut h, "stats");

    click(&mut h, "Top");
    settle(&mut h, Duration::from_secs(3));
    save(&mut h, "processes");

    let volume = h.state().store.volumes.first().map(|v| v.name.clone());
    open(&mut h, 3, volume);
    save(&mut h, "volumes");
    h.state_mut().pages[3].clear_selection();

    let network = h
        .state()
        .store
        .networks
        .iter()
        .find(|n| !n.is_builtin())
        .map(|n| n.id.clone());
    open(&mut h, 4, network);
    save(&mut h, "network");
    click(&mut h, "New network");
    settle(&mut h, Duration::from_millis(500));
    save(&mut h, "new-network");
    h.key_press(egui::Key::Escape);
    h.run_steps(3);
    h.state_mut().pages[4].clear_selection();

    open(&mut h, 5, None);
    save(&mut h, "disk");

    let image = h.state().store.images.first().map(|i| i.id.clone());
    open(&mut h, 2, image);
    click(&mut h, "Run…");
    settle(&mut h, Duration::from_secs(1));
    save(&mut h, "run");
    h.key_press(egui::Key::Escape);
    h.run_steps(3);
    click(&mut h, "HISTORY");
    settle(&mut h, Duration::from_secs(1));
    save(&mut h, "history");
    h.state_mut().pages[2].clear_selection();

    h.state().updater.pretend(crate::update::Status::Available(
        crate::update::Available::example("1.0.0"),
    ));
    open(&mut h, 0, None);
    save(&mut h, "update-available");
    h.state().updater.pretend(crate::update::Status::Idle);

    set_theme(&mut h, Theme::Dark);
    open(&mut h, 0, busy);
    click(&mut h, "Overview");
    save(&mut h, "dark-mode");
    click(&mut h, "More actions");
    click(&mut h, "Duplicate…");
    settle(&mut h, Duration::from_millis(500));
    save(&mut h, "duplicate");
    h.key_press(egui::Key::Escape);
    h.run_steps(3);
    click(&mut h, "More actions");
    click(&mut h, "Edit…");
    settle(&mut h, Duration::from_millis(500));
    save(&mut h, "edit");
    h.key_press(egui::Key::Escape);
    h.run_steps(3);
    click(&mut h, "Change…");
    settle(&mut h, Duration::from_millis(500));
    save(&mut h, "limits");
}

/// The states new users see: nothing found, and no Docker at all.
#[test]
#[ignore]
fn screenshots_of_empty_states() {
    let mut h = harness();
    settle(&mut h, Duration::from_secs(3));
    h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
    settle(&mut h, Duration::from_millis(500));
    save(&mut h, "bulk");
    h.key_press(egui::Key::Escape);
    h.run_steps(3);

    h.state_mut().search = "no-such-thing".into();
    settle(&mut h, Duration::from_millis(500));
    save(&mut h, "empty-search");

    // SAFETY: tests that use DOCKER_HOST run one at a time (`--ignored`,
    // single test); nothing else reads it concurrently here.
    unsafe { std::env::set_var("DOCKER_HOST", "unix:///nonexistent/docker.sock") };
    let mut h = harness();
    settle(&mut h, Duration::from_secs(3));
    save(&mut h, "no-docker");
    unsafe { std::env::remove_var("DOCKER_HOST") };
}

/// The Files tab of the first container, a folder and a previewed file.
/// Works with stopped containers too (their files come from the archive
/// API).
#[test]
#[ignore]
fn screenshots_of_files() {
    let mut h = harness();
    settle(&mut h, Duration::from_secs(3));
    set_theme(&mut h, Theme::Light);
    let first = h.state().store.containers.first().map(|c| c.id.clone());
    open(&mut h, 0, first);
    click(&mut h, "Files");
    settle(&mut h, Duration::from_secs(2));
    save(&mut h, "files");
    click(&mut h, "etc");
    settle(&mut h, Duration::from_secs(2));
    click(&mut h, "hosts");
    settle(&mut h, Duration::from_secs(2));
    save(&mut h, "files-preview");
    // A link to a file opens as a preview, not an empty folder.
    click(&mut h, "mtab");
    settle(&mut h, Duration::from_secs(3));
    save(&mut h, "files-link");

    // A volume, read-only through a helper container.
    let volume = h.state().store.volumes.first().map(|v| v.name.clone());
    open(&mut h, 3, volume);
    click(&mut h, "Browse files");
    settle(&mut h, Duration::from_secs(3));
    save(&mut h, "volume-files");
    click(&mut h, "Done browsing");
    settle(&mut h, Duration::from_secs(1));
}

/// The Terminal tab with a live shell, in a throwaway container.
#[test]
#[ignore]
fn screenshots_of_terminal() {
    let runtime = tokio::runtime::Runtime::new().unwrap();
    let client = crate::docker::DockerClient::connect().unwrap();
    let image = runtime
        .block_on(client.list_images())
        .unwrap()
        .into_iter()
        .next()
        .expect("an image to run");
    let spec = crate::docker::RunSpec {
        image: image.id,
        name: "ferrodock-shell-demo".into(),
        command: "sleep 600".into(),
        ..Default::default()
    };
    let _ = runtime.block_on(client.perform(
        crate::docker::Action::Remove(crate::docker::Resource::Containers),
        &spec.name,
    ));
    let id = runtime.block_on(client.run(&spec)).unwrap();

    let mut h = harness();
    settle(&mut h, Duration::from_secs(3));
    set_theme(&mut h, Theme::Light);
    open(&mut h, 0, Some(id.clone()));
    click(&mut h, "Terminal");
    settle(&mut h, Duration::from_secs(2));
    for line in ["ls -l /", "echo \"Hello from $(hostname)\""] {
        h.event(egui::Event::Text(line.into()));
        h.key_press(egui::Key::Enter);
        settle(&mut h, Duration::from_secs(1));
    }
    save(&mut h, "terminal");
    h.event(egui::Event::Text("top".into()));
    h.key_press(egui::Key::Enter);
    settle(&mut h, Duration::from_secs(2));
    set_theme(&mut h, Theme::Dark);
    save(&mut h, "terminal-top");

    let _ = runtime.block_on(client.perform(crate::docker::Action::Stop, &id));
    let _ = runtime.block_on(client.perform(
        crate::docker::Action::Remove(crate::docker::Resource::Containers),
        &id,
    ));
}

/// A container without the asked-for shell, then the debug shell next to
/// it. Set `FERRODOCK_DEBUG_IMAGE` to a local image if Docker Hub can't be
/// reached.
#[test]
#[ignore]
fn screenshots_of_debug_shell() {
    let runtime = tokio::runtime::Runtime::new().unwrap();
    let client = crate::docker::DockerClient::connect().unwrap();
    let image = runtime
        .block_on(client.list_images())
        .unwrap()
        .into_iter()
        .next()
        .expect("an image to run");
    let spec = crate::docker::RunSpec {
        image: image.id,
        name: "ferrodock-debug-demo".into(),
        command: "sleep 600".into(),
        ..Default::default()
    };
    let remove = |id: &str| {
        let _ = runtime.block_on(client.perform(crate::docker::Action::Stop, id));
        let _ = runtime.block_on(client.perform(
            crate::docker::Action::Remove(crate::docker::Resource::Containers),
            id,
        ));
    };
    remove(&spec.name);
    let id = runtime.block_on(client.run(&spec)).unwrap();

    let mut h = harness();
    settle(&mut h, Duration::from_secs(3));
    set_theme(&mut h, Theme::Light);
    open(&mut h, 0, Some(id.clone()));
    click(&mut h, "Terminal");
    settle(&mut h, Duration::from_secs(1));
    // Ask for a shell the container doesn't have.
    h.get_by_label("Shell").click();
    h.run_steps(2);
    h.event(egui::Event::Text("/bin/no-such-shell".into()));
    click(&mut h, "New session");
    settle(&mut h, Duration::from_secs(3));
    save(&mut h, "no-shell");
    click(&mut h, "Open a debug shell");
    settle(&mut h, Duration::from_secs(4));
    for line in ["ps", "ls /target/"] {
        h.event(egui::Event::Text(line.into()));
        h.key_press(egui::Key::Enter);
        settle(&mut h, Duration::from_secs(1));
    }
    save(&mut h, "debug-shell");
    remove(&id);
}

/// Docker Hub search, and the Tag dialog, on the Images page.
#[test]
#[ignore]
fn screenshots_of_image_tools() {
    let mut h = harness();
    settle(&mut h, Duration::from_secs(3));
    set_theme(&mut h, Theme::Light);
    let first = h.state().store.images.first().map(|i| i.id.clone());
    open(&mut h, 2, first);
    h.get_by_role_and_label(egui::accesskit::Role::TextInput, "Pull")
        .click();
    h.run_steps(2);
    h.event(egui::Event::Text("nginx".into()));
    // Results come by themselves once typing pauses.
    settle(&mut h, Duration::from_secs(5));
    save(&mut h, "image-search");
    click(&mut h, "More actions");
    click(&mut h, "Tag…");
    settle(&mut h, Duration::from_millis(500));
    h.event(egui::Event::Text("ghcr.io/me/app:1.0".into()));
    h.run_steps(3);
    save(&mut h, "image-tag");
}

/// The engine picker, and switching to another engine without restarting.
/// A second Docker context ("staging") reaches the same daemon through
/// another socket path, so the switch is real but the data stays the same.
#[test]
#[ignore]
fn screenshots_of_engines() {
    let config = std::env::temp_dir().join(format!("ferrodock-config-{}", std::process::id()));
    let meta = config.join("contexts/meta/0123abcd");
    std::fs::create_dir_all(&meta).unwrap();
    let socket = config.join("staging.sock");
    let _ = std::fs::remove_file(&socket);
    #[cfg(unix)]
    std::os::unix::fs::symlink("/var/run/docker.sock", &socket).unwrap();
    std::fs::write(
        meta.join("meta.json"),
        format!(
            r#"{{"Name":"staging","Endpoints":{{"docker":{{"Host":"unix://{}"}}}}}}"#,
            socket.display()
        ),
    )
    .unwrap();
    // Only this test runs in its process when it's run on its own.
    unsafe { std::env::set_var("DOCKER_CONFIG", &config) };

    let mut h = harness();
    settle(&mut h, Duration::from_secs(3));
    set_theme(&mut h, Theme::Light);
    h.state_mut().settings.engine = None;
    click(&mut h, "Docker engine");
    settle(&mut h, Duration::from_millis(500));
    save(&mut h, "engines");
    click(&mut h, "staging");
    settle(&mut h, Duration::from_secs(4));
    save(&mut h, "engine-switched");
    let state = h.state();
    assert_eq!(state.store.engine.as_deref(), Some("staging"));
    assert!(
        !state.store.containers.is_empty(),
        "reloaded from the new engine"
    );
    assert!(matches!(
        state.store.connection,
        crate::app::store::Connection::Connected { .. }
    ));
    std::fs::remove_dir_all(&config).unwrap();
}

/// Brings a two-service project up from its compose.yaml through the UI,
/// then shows its merged logs. Takes it down again at the end.
#[test]
#[ignore]
fn screenshots_of_compose_up() {
    use egui::accesskit::Role;
    let runtime = tokio::runtime::Runtime::new().unwrap();
    let client = crate::docker::DockerClient::connect().unwrap();
    let image = runtime
        .block_on(client.list_images())
        .unwrap()
        .into_iter()
        .next()
        .expect("an image to run");
    let tag = image.tags.first().cloned().unwrap_or(image.id);
    let dir = std::env::temp_dir().join("shop");
    std::fs::create_dir_all(&dir).unwrap();
    let file = dir.join("compose.yaml");
    std::fs::write(
        &file,
        format!(
            "services:\n\
             \x20 web:\n    image: {tag}\n    command: sh -c 'while true; do echo GET / 200; sleep 1; done'\n\
             \x20 worker:\n    image: {tag}\n    command: sh -c 'while true; do echo processed job; sleep 2; done'\n"
        ),
    )
    .unwrap();

    let mut h = harness();
    settle(&mut h, Duration::from_secs(3));
    set_theme(&mut h, Theme::Light);
    open(&mut h, 1, None);
    h.get_by_role_and_label(Role::TextInput, "Up").click();
    h.run_steps(2);
    h.event(egui::Event::Text(file.display().to_string()));
    h.run_steps(2);
    h.get_by_role_and_label(Role::Button, "Up").click();
    settle(&mut h, Duration::from_secs(8));
    save(&mut h, "compose-up");
    let up_ok = h
        .state()
        .store
        .compose
        .run
        .as_ref()
        .is_some_and(|j| j.ended == Some(None));
    h.state_mut().pages[1].select("shop");
    settle(&mut h, Duration::from_secs(1));
    click(&mut h, "Logs");
    settle(&mut h, Duration::from_secs(5));
    save(&mut h, "compose-logs");

    let down = ["-p", "shop", "down", "-t", "1"].map(str::to_owned);
    let _ = runtime.block_on(crate::docker::compose_cli::stream(
        crate::docker::compose_cli::command(None, None, &down),
        |_| {},
    ));
    std::fs::remove_dir_all(&dir).unwrap();
    assert!(up_ok, "compose up finished without errors");
}

/// The container list grouped by Compose project.
#[test]
#[ignore]
fn screenshots_of_grouping() {
    let mut h = harness();
    settle(&mut h, Duration::from_secs(3));
    set_theme(&mut h, Theme::Light);
    open(&mut h, 0, None);
    save(&mut h, "before-grouping");
    click(&mut h, "Group by project");
    settle(&mut h, Duration::from_secs(1));
    save(&mut h, "grouped");
    click(&mut h, "Group by project");
    h.run_steps(2);
}

/// Ticked containers with the bulk bar, the removal question, and the
/// Reset Docker question (never confirmed).
#[test]
#[ignore]
fn screenshots_of_bulk_and_reset() {
    let mut h = harness();
    settle(&mut h, Duration::from_secs(3));
    set_theme(&mut h, Theme::Light);
    open(&mut h, 0, None);
    let names: Vec<String> = h
        .state()
        .store
        .containers
        .iter()
        .take(3)
        .map(|c| c.name.clone())
        .collect();
    for name in &names {
        click(&mut h, &format!("Select {name}"));
    }
    save(&mut h, "bulk-selected");
    click(&mut h, &format!("Remove {}…", names.len()));
    save(&mut h, "bulk-remove");
    click(&mut h, "Cancel");
    click(&mut h, "Select all");
    save(&mut h, "bulk-all");
    click(&mut h, "Select none");

    open(&mut h, 5, None);
    save(&mut h, "disk-danger-zone");
    click(&mut h, "Reset Docker…");
    h.get_by_label("Type “reset” to confirm").type_text("res");
    h.run_steps(3);
    save(&mut h, "reset");
    click(&mut h, "Cancel");
    assert!(h.state().confirmation.is_none());
}

/// With the engine down: Ferrodock starts it at launch and connects.
/// Needs a stopped engine that `systemctl start docker` (or the platform's
/// starter) brings up.
#[test]
#[ignore]
fn screenshots_of_engine_start() {
    let mut h = harness();
    set_theme(&mut h, Theme::Light);
    settle(&mut h, Duration::from_millis(1500));
    save(&mut h, "engine-starting");
    let start = Instant::now();
    while !matches!(h.state().store.connection, super::Connection::Connected { .. })
        && start.elapsed() < Duration::from_secs(60)
    {
        settle(&mut h, Duration::from_millis(500));
    }
    settle(&mut h, Duration::from_secs(2));
    save(&mut h, "engine-started");
    assert!(matches!(
        h.state().store.connection,
        super::Connection::Connected { .. }
    ));
}