docker-wrapper 0.11.1

A Docker CLI wrapper for Rust
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
485
486
487
//! Integration tests for ContainerGuard and ContainerGuardSet

#![cfg(feature = "testing")]

use docker_wrapper::template::HasConnectionString;
use docker_wrapper::testing::{ContainerGuard, ContainerGuardSet};
use docker_wrapper::{RedisTemplate, Template};
use std::sync::atomic::{AtomicU16, Ordering};
use std::time::Duration;

static PORT_COUNTER: AtomicU16 = AtomicU16::new(17000);

fn unique_name(prefix: &str) -> String {
    format!("{}-{}", prefix, uuid::Uuid::new_v4())
}

fn next_port() -> u16 {
    PORT_COUNTER.fetch_add(1, Ordering::SeqCst)
}

#[tokio::test]
async fn test_container_guard_basic_lifecycle() {
    let name = unique_name("guard-basic");
    let guard = ContainerGuard::new(RedisTemplate::new(&name).port(next_port()))
        .start()
        .await
        .expect("Failed to start container");

    // Container should be running
    assert!(
        guard.is_running().await.expect("Failed to check running"),
        "Container should be running"
    );

    // Should have a container ID
    assert!(guard.container_id().is_some(), "Should have container ID");

    // Should not be reused
    assert!(!guard.was_reused(), "Should not be reused");

    // Template should be accessible
    let template = guard.template();
    assert_eq!(template.config().name, name);

    // Drop the guard - container should be cleaned up
    drop(guard);

    // Give Docker a moment to clean up
    tokio::time::sleep(Duration::from_millis(500)).await;

    // Verify container is gone
    let template = RedisTemplate::new(&name);
    assert!(
        !template.is_running().await.unwrap_or(true),
        "Container should be stopped after drop"
    );
}

#[tokio::test]
async fn test_container_guard_no_remove_on_drop() {
    let name = unique_name("guard-no-remove");
    let guard = ContainerGuard::new(RedisTemplate::new(&name).port(next_port()))
        .remove_on_drop(false)
        .stop_on_drop(false)
        .start()
        .await
        .expect("Failed to start container");

    assert!(guard.is_running().await.expect("Failed to check running"));

    // Drop without cleanup
    drop(guard);

    // Container should still be running
    tokio::time::sleep(Duration::from_millis(500)).await;
    let template = RedisTemplate::new(&name);
    assert!(
        template.is_running().await.unwrap_or(false),
        "Container should still be running"
    );

    // Manual cleanup
    let _ = template.stop().await;
    let _ = template.remove().await;
}

#[tokio::test]
async fn test_container_guard_reuse_if_running() {
    let name = unique_name("guard-reuse");
    let port = next_port();

    // Start container first time
    let guard1 = ContainerGuard::new(RedisTemplate::new(&name).port(port))
        .remove_on_drop(false)
        .stop_on_drop(false)
        .start()
        .await
        .expect("Failed to start container");

    assert!(!guard1.was_reused(), "First start should not be reused");
    let container_id = guard1.container_id().map(String::from);
    drop(guard1);

    // Second guard should reuse existing container
    let guard2 = ContainerGuard::new(RedisTemplate::new(&name).port(port))
        .reuse_if_running(true)
        .start()
        .await
        .expect("Failed to start/reuse container");

    assert!(guard2.was_reused(), "Second start should reuse");
    // Reused containers don't have container_id
    assert!(
        guard2.container_id().is_none(),
        "Reused container should not have ID"
    );

    // Cleanup
    drop(guard2);
    let template = RedisTemplate::new(&name);
    let _ = template.stop().await;
    let _ = template.remove().await;

    // Verify first container had an ID
    assert!(container_id.is_some(), "Original container should have ID");
}

#[tokio::test]
async fn test_container_guard_logs() {
    let name = unique_name("guard-logs");
    let guard = ContainerGuard::new(RedisTemplate::new(&name).port(next_port()))
        .start()
        .await
        .expect("Failed to start container");

    // Wait for Redis to log something
    tokio::time::sleep(Duration::from_secs(1)).await;

    let logs = guard.logs().await.expect("Failed to get logs");

    // Redis should have logged something about starting
    assert!(!logs.is_empty(), "Logs should not be empty");

    // Container cleanup happens on drop
}

#[tokio::test]
async fn test_container_guard_manual_cleanup() {
    let name = unique_name("guard-cleanup");
    let guard = ContainerGuard::new(RedisTemplate::new(&name).port(next_port()))
        .start()
        .await
        .expect("Failed to start container");

    assert!(guard.is_running().await.expect("Failed to check running"));

    // Manual cleanup
    guard.cleanup().await.expect("Failed to cleanup");

    // Cleanup should be idempotent
    guard
        .cleanup()
        .await
        .expect("Second cleanup should succeed");

    // Container should be gone
    tokio::time::sleep(Duration::from_millis(500)).await;
    let template = RedisTemplate::new(&name);
    assert!(
        !template.is_running().await.unwrap_or(true),
        "Container should be stopped after cleanup"
    );
}

#[tokio::test]
async fn test_container_guard_wait_for_ready_method() {
    let name = unique_name("guard-wait-ready");
    let guard = ContainerGuard::new(RedisTemplate::new(&name).port(next_port()))
        .start()
        .await
        .expect("Failed to start container");

    // Explicitly call wait_for_ready
    guard
        .wait_for_ready()
        .await
        .expect("Failed to wait for ready");

    // Container should definitely be running and ready now
    assert!(
        guard.is_running().await.expect("Failed to check running"),
        "Container should be running"
    );

    // Container cleanup happens on drop
}

#[tokio::test]
async fn test_container_guard_auto_wait_for_ready() {
    let name = unique_name("guard-auto-wait");
    let guard = ContainerGuard::new(RedisTemplate::new(&name).port(next_port()))
        .wait_for_ready(true) // Enable auto-wait
        .start()
        .await
        .expect("Failed to start container");

    // Container should be immediately ready - no separate wait_for_ready call needed
    assert!(
        guard.is_running().await.expect("Failed to check running"),
        "Container should be running"
    );

    // Calling wait_for_ready again should succeed immediately since already ready
    guard
        .wait_for_ready()
        .await
        .expect("wait_for_ready should succeed on already-ready container");

    // Container cleanup happens on drop
}

#[tokio::test]
async fn test_container_guard_with_network() {
    let name = unique_name("guard-network");
    let network_name = unique_name("test-network");

    let guard = ContainerGuard::new(RedisTemplate::new(&name).port(next_port()))
        .with_network(&network_name)
        .remove_network_on_drop(true)
        .start()
        .await
        .expect("Failed to start container");

    // Container should be running
    assert!(
        guard.is_running().await.expect("Failed to check running"),
        "Container should be running"
    );

    // Network should be set
    assert_eq!(guard.network(), Some(network_name.as_str()));

    // Verify the container is on the network by checking the template config
    assert_eq!(
        guard.template().config().network,
        Some(network_name.clone())
    );

    // Container and network cleanup happens on drop
}

#[tokio::test]
async fn test_container_guard_network_no_auto_create() {
    use docker_wrapper::{DockerCommand, NetworkCreateCommand, NetworkRmCommand};

    let name = unique_name("guard-network-manual");
    let network_name = unique_name("manual-network");

    // Create the network manually first
    NetworkCreateCommand::new(&network_name)
        .driver("bridge")
        .execute()
        .await
        .expect("Failed to create network");

    let guard = ContainerGuard::new(RedisTemplate::new(&name).port(next_port()))
        .with_network(&network_name)
        .create_network(false) // Don't auto-create
        .start()
        .await
        .expect("Failed to start container");

    assert!(
        guard.is_running().await.expect("Failed to check running"),
        "Container should be running"
    );

    // Drop the guard (container will be removed but network won't since we didn't create it)
    drop(guard);

    // Give Docker a moment to clean up
    tokio::time::sleep(Duration::from_millis(500)).await;

    // Clean up network manually
    let _ = NetworkRmCommand::new(&network_name).execute().await;
}

// ============================================================================
// ContainerGuardSet tests
// ============================================================================

#[tokio::test]
async fn test_container_guard_set_basic() {
    let name1 = unique_name("guardset-1");
    let name2 = unique_name("guardset-2");

    let guards = ContainerGuardSet::new()
        .add(RedisTemplate::new(&name1).port(next_port()))
        .add(RedisTemplate::new(&name2).port(next_port()))
        .start_all()
        .await
        .expect("Failed to start containers");

    // Both containers should be in the set
    assert_eq!(guards.len(), 2);
    assert!(!guards.is_empty());
    assert!(guards.contains(&name1));
    assert!(guards.contains(&name2));

    // Names should be accessible
    let names: Vec<&str> = guards.names().collect();
    assert!(names.contains(&name1.as_str()));
    assert!(names.contains(&name2.as_str()));

    // Container cleanup happens on drop
}

#[tokio::test]
async fn test_container_guard_set_with_shared_network() {
    let name1 = unique_name("guardset-net-1");
    let name2 = unique_name("guardset-net-2");
    let network_name = unique_name("guardset-network");

    let guards = ContainerGuardSet::new()
        .with_network(&network_name)
        .add(RedisTemplate::new(&name1).port(next_port()))
        .add(RedisTemplate::new(&name2).port(next_port()))
        .start_all()
        .await
        .expect("Failed to start containers");

    // Both containers should be in the set
    assert_eq!(guards.len(), 2);

    // Network should be accessible
    assert_eq!(guards.network(), Some(network_name.as_str()));

    // Containers and network cleanup happens on drop
}

#[tokio::test]
async fn test_container_guard_set_empty() {
    let guards = ContainerGuardSet::new()
        .start_all()
        .await
        .expect("Empty set should start successfully");

    assert!(guards.is_empty());
    assert_eq!(guards.len(), 0);
    assert!(!guards.contains("nonexistent"));
}

#[tokio::test]
async fn test_container_guard_set_single_container() {
    let name = unique_name("guardset-single");

    let guards = ContainerGuardSet::new()
        .add(RedisTemplate::new(&name).port(next_port()))
        .start_all()
        .await
        .expect("Failed to start container");

    assert_eq!(guards.len(), 1);
    assert!(guards.contains(&name));

    // Container cleanup happens on drop
}

#[tokio::test]
async fn test_container_guard_set_network_no_auto_remove() {
    use docker_wrapper::{DockerCommand, NetworkRmCommand};

    let name = unique_name("guardset-net-keep");
    let network_name = unique_name("guardset-keep-network");

    {
        let guards = ContainerGuardSet::new()
            .with_network(&network_name)
            .remove_network_on_drop(false) // Don't remove network
            .add(RedisTemplate::new(&name).port(next_port()))
            .start_all()
            .await
            .expect("Failed to start container");

        assert_eq!(guards.len(), 1);
        // Guards drop here, but network should remain
    }

    // Give Docker a moment to clean up containers
    tokio::time::sleep(Duration::from_millis(500)).await;

    // Network should still exist - cleanup manually
    // If network doesn't exist, this will fail but we ignore the error
    let _ = NetworkRmCommand::new(&network_name).execute().await;
}

#[tokio::test]
async fn test_container_guard_set_wait_for_ready_disabled() {
    let name = unique_name("guardset-no-wait");

    let guards = ContainerGuardSet::new()
        .wait_for_ready(false) // Don't wait for ready
        .add(RedisTemplate::new(&name).port(next_port()))
        .start_all()
        .await
        .expect("Failed to start container");

    assert_eq!(guards.len(), 1);
    assert!(guards.contains(&name));

    // Container cleanup happens on drop
}

#[tokio::test]
async fn test_container_guard_stop_timeout() {
    let name = unique_name("guard-stop-timeout");

    // Use a short stop timeout for fast cleanup
    let guard = ContainerGuard::new(RedisTemplate::new(&name).port(next_port()))
        .stop_timeout(Duration::from_secs(1))
        .start()
        .await
        .expect("Failed to start container");

    assert!(
        guard.is_running().await.expect("Failed to check running"),
        "Container should be running"
    );

    // Container cleanup with 1 second timeout happens on drop
}

#[tokio::test]
async fn test_container_guard_stop_timeout_zero() {
    let name = unique_name("guard-stop-timeout-zero");

    // Zero timeout means immediate SIGKILL
    let guard = ContainerGuard::new(RedisTemplate::new(&name).port(next_port()))
        .stop_timeout(Duration::ZERO)
        .start()
        .await
        .expect("Failed to start container");

    assert!(
        guard.is_running().await.expect("Failed to check running"),
        "Container should be running"
    );

    // Container cleanup with immediate SIGKILL happens on drop
}

#[tokio::test]
async fn test_container_guard_connection_string() {
    let name = unique_name("guard-conn-string");
    let port = next_port();

    let guard = ContainerGuard::new(RedisTemplate::new(&name).port(port))
        .start()
        .await
        .expect("Failed to start container");

    // Test the connection_string() passthrough method
    let conn = guard.connection_string();
    assert_eq!(conn, format!("redis://localhost:{}", port));

    // Verify it matches what we'd get from the template directly
    assert_eq!(conn, guard.template().connection_string());

    // Container cleanup happens on drop
}

#[tokio::test]
async fn test_container_guard_connection_string_with_password() {
    let name = unique_name("guard-conn-string-pass");
    let port = next_port();

    let guard = ContainerGuard::new(RedisTemplate::new(&name).port(port).password("testpass123"))
        .start()
        .await
        .expect("Failed to start container");

    // Test connection string includes password
    let conn = guard.connection_string();
    assert_eq!(conn, format!("redis://:testpass123@localhost:{}", port));

    // Container cleanup happens on drop
}