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
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//! Property-based tests for docker-wrapper using proptest.
//!
//! These tests verify that:
//! 1. Builder methods handle arbitrary string inputs without panicking
//! 2. Output parsing is robust against malformed input
//! 3. Command argument building is deterministic and correct

use proptest::prelude::*;

// Import the crate under test
use docker_wrapper::{
    CreateCommand, DockerCommand, ExecCommand, ImagesCommand, KillCommand, LogsCommand, PsCommand,
    RmCommand, RunCommand, StartCommand, StopCommand,
};

// ============================================================================
// Test Strategies
// ============================================================================

/// Strategy for generating arbitrary container/image names
/// Docker allows alphanumeric, underscores, hyphens, and dots
fn docker_name_strategy() -> impl Strategy<Value = String> {
    "[a-zA-Z][a-zA-Z0-9_.-]{0,127}".prop_filter("non-empty", |s| !s.is_empty())
}

/// Strategy for generating arbitrary environment variable keys
fn env_key_strategy() -> impl Strategy<Value = String> {
    "[A-Z_][A-Z0-9_]{0,63}".prop_filter("non-empty", |s| !s.is_empty())
}

/// Strategy for generating arbitrary environment variable values
fn env_value_strategy() -> impl Strategy<Value = String> {
    ".*".prop_map(|s| s.chars().take(256).collect())
}

/// Strategy for generating port numbers
fn port_strategy() -> impl Strategy<Value = u16> {
    1u16..=65535u16
}

/// Strategy for generating memory size strings
fn memory_size_strategy() -> impl Strategy<Value = String> {
    prop_oneof![
        (1u32..10000u32).prop_map(|n| format!("{n}m")),
        (1u32..100u32).prop_map(|n| format!("{n}g")),
        (1u32..1000000u32).prop_map(|n| format!("{n}k")),
        (1u32..1000000u32).prop_map(|n| n.to_string()),
    ]
}

/// Strategy for generating CPU count strings
fn cpu_strategy() -> impl Strategy<Value = String> {
    prop_oneof![
        (1u32..128u32).prop_map(|n| n.to_string()),
        (1u32..128u32, 0u32..99u32).prop_map(|(n, d)| format!("{n}.{d}")),
        Just("0.5".to_string()),
        Just("1.5".to_string()),
        Just("2.0".to_string()),
    ]
}

/// Strategy for generating arbitrary (potentially malicious) strings
fn arbitrary_string_strategy() -> impl Strategy<Value = String> {
    prop_oneof![
        // Normal strings
        "[a-zA-Z0-9_.-]{0,64}",
        // Strings with special characters
        "[^\\x00]{0,64}".prop_map(|s| s.chars().filter(|c| *c != '\0').collect()),
        // Edge cases
        Just(String::new()),
        Just(" ".to_string()),
        Just("  ".to_string()),
        Just("\t".to_string()),
        Just("\n".to_string()),
        Just("'single quotes'".to_string()),
        Just("\"double quotes\"".to_string()),
        Just("back`ticks`".to_string()),
        Just("$variable".to_string()),
        Just("${variable}".to_string()),
        Just("$(command)".to_string()),
        Just("; rm -rf /".to_string()),
        Just("| cat /etc/passwd".to_string()),
        Just("&& malicious".to_string()),
        Just("|| fallback".to_string()),
        Just("name=value".to_string()),
        Just("key:value".to_string()),
        Just("path/to/file".to_string()),
        Just("../../../etc/passwd".to_string()),
        Just("C:\\Windows\\System32".to_string()),
    ]
}

/// Strategy for generating label strings (key=value format)
fn label_strategy() -> impl Strategy<Value = String> {
    ("[a-zA-Z][a-zA-Z0-9._-]{0,63}", "[a-zA-Z0-9._-]{0,127}").prop_map(|(k, v)| format!("{k}={v}"))
}

/// Strategy for generating volume mount strings
fn volume_mount_strategy() -> impl Strategy<Value = (String, String)> {
    ("/[a-zA-Z0-9/_-]{1,64}", "/[a-zA-Z0-9/_-]{1,64}")
}

// ============================================================================
// RunCommand Property Tests
// ============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    /// Test that RunCommand handles arbitrary image names without panicking
    #[test]
    fn run_command_accepts_any_image_name(image in arbitrary_string_strategy()) {
        let cmd = RunCommand::new(image);
        let args = cmd.build_command_args();
        // Should always produce valid args starting with "run"
        prop_assert!(args.first() == Some(&"run".to_string()));
    }

    /// Test that RunCommand handles arbitrary container names
    #[test]
    fn run_command_accepts_any_container_name(
        image in docker_name_strategy(),
        name in arbitrary_string_strategy()
    ) {
        let cmd = RunCommand::new(image).name(name);
        let args = cmd.build_command_args();
        prop_assert!(args.contains(&"--name".to_string()));
    }

    /// Test that RunCommand handles arbitrary environment variables
    #[test]
    fn run_command_accepts_any_env_vars(
        image in docker_name_strategy(),
        key in env_key_strategy(),
        value in env_value_strategy()
    ) {
        let cmd = RunCommand::new(image).env(key, value);
        let args = cmd.build_command_args();
        // env vars are passed via -e flag
        prop_assert!(args.iter().any(|a| a.contains('=')));
    }

    /// Test that RunCommand handles port mappings correctly
    #[test]
    fn run_command_handles_port_mappings(
        image in docker_name_strategy(),
        host_port in port_strategy(),
        container_port in port_strategy()
    ) {
        let cmd = RunCommand::new(image).port(host_port, container_port);
        let args = cmd.build_command_args();
        prop_assert!(args.contains(&"--publish".to_string()));
        prop_assert!(args.iter().any(|a| a.contains(':')));
    }

    /// Test that RunCommand handles dynamic port mappings
    #[test]
    fn run_command_handles_dynamic_ports(
        image in docker_name_strategy(),
        container_port in port_strategy()
    ) {
        let cmd = RunCommand::new(image).dynamic_port(container_port);
        let args = cmd.build_command_args();
        prop_assert!(args.contains(&"--publish".to_string()));
    }

    /// Test that RunCommand handles memory limits
    #[test]
    fn run_command_handles_memory_limits(
        image in docker_name_strategy(),
        memory in memory_size_strategy()
    ) {
        let cmd = RunCommand::new(image).memory(memory);
        let args = cmd.build_command_args();
        prop_assert!(args.contains(&"--memory".to_string()));
    }

    /// Test that RunCommand handles CPU limits
    #[test]
    fn run_command_handles_cpu_limits(
        image in docker_name_strategy(),
        cpus in cpu_strategy()
    ) {
        let cmd = RunCommand::new(image).cpus(cpus);
        let args = cmd.build_command_args();
        prop_assert!(args.contains(&"--cpus".to_string()));
    }

    /// Test that RunCommand handles volume mounts
    #[test]
    fn run_command_handles_volumes(
        image in docker_name_strategy(),
        (source, target) in volume_mount_strategy()
    ) {
        let cmd = RunCommand::new(image).volume(source, target);
        let args = cmd.build_command_args();
        prop_assert!(args.contains(&"--volume".to_string()));
    }

    /// Test that RunCommand handles labels
    #[test]
    fn run_command_handles_labels(
        image in docker_name_strategy(),
        label in label_strategy()
    ) {
        let cmd = RunCommand::new(image).label(label);
        let args = cmd.build_command_args();
        prop_assert!(args.contains(&"--label".to_string()));
    }

    /// Test that multiple builder calls compose correctly
    #[test]
    fn run_command_builder_composition(
        image in docker_name_strategy(),
        name in docker_name_strategy(),
        host_port in port_strategy(),
        container_port in port_strategy(),
        env_key in env_key_strategy(),
        env_value in env_value_strategy()
    ) {
        let cmd = RunCommand::new(image)
            .name(name)
            .port(host_port, container_port)
            .env(env_key, env_value)
            .detach()
            .rm();

        let args = cmd.build_command_args();

        prop_assert!(args.contains(&"--name".to_string()));
        prop_assert!(args.contains(&"--publish".to_string()));
        prop_assert!(args.contains(&"--detach".to_string()));
        prop_assert!(args.contains(&"--rm".to_string()));
    }
}

// ============================================================================
// CreateCommand Property Tests
// ============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(50))]

    /// Test that CreateCommand handles arbitrary inputs
    #[test]
    fn create_command_accepts_any_image(image in arbitrary_string_strategy()) {
        let cmd = CreateCommand::new(image);
        let args = cmd.build_command_args();
        prop_assert!(args.first() == Some(&"create".to_string()));
    }

    /// Test that CreateCommand handles container names
    #[test]
    fn create_command_handles_names(
        image in docker_name_strategy(),
        name in arbitrary_string_strategy()
    ) {
        let cmd = CreateCommand::new(image).name(name);
        let args = cmd.build_command_args();
        prop_assert!(args.contains(&"--name".to_string()));
    }
}

// ============================================================================
// ExecCommand Property Tests
// ============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(50))]

    /// Test that ExecCommand handles arbitrary container IDs
    #[test]
    fn exec_command_accepts_any_container_id(container_id in arbitrary_string_strategy()) {
        let cmd = ExecCommand::new(container_id, vec!["sh".to_string()]);
        let args = cmd.build_command_args();
        prop_assert!(args.first() == Some(&"exec".to_string()));
    }

    /// Test that ExecCommand handles arbitrary commands
    #[test]
    fn exec_command_handles_commands(
        container_id in docker_name_strategy(),
        command in arbitrary_string_strategy()
    ) {
        let cmd = ExecCommand::new(container_id, vec![command]);
        let args = cmd.build_command_args();
        prop_assert!(args.first() == Some(&"exec".to_string()));
    }

    /// Test that ExecCommand handles user specifications
    #[test]
    fn exec_command_handles_user(
        container_id in docker_name_strategy(),
        user in arbitrary_string_strategy()
    ) {
        let cmd = ExecCommand::new(container_id, vec!["sh".to_string()]).user(user);
        let args = cmd.build_command_args();
        prop_assert!(args.contains(&"--user".to_string()));
    }
}

// ============================================================================
// PsCommand Property Tests
// ============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(50))]

    /// Test that PsCommand handles arbitrary filter values
    #[test]
    fn ps_command_handles_filters(filter in arbitrary_string_strategy()) {
        let cmd = PsCommand::new().filter(filter);
        let args = cmd.build_command_args();
        prop_assert!(args.contains(&"--filter".to_string()));
    }

    /// Test that PsCommand handles arbitrary format templates
    #[test]
    fn ps_command_handles_format(format in arbitrary_string_strategy()) {
        let cmd = PsCommand::new().format_template(format);
        let args = cmd.build_command_args();
        prop_assert!(args.contains(&"--format".to_string()));
    }

    /// Test that PsCommand handles last count values
    #[test]
    fn ps_command_handles_last(n in -100i32..1000i32) {
        let cmd = PsCommand::new().last(n);
        let args = cmd.build_command_args();
        prop_assert!(args.contains(&"--last".to_string()));
    }
}

// ============================================================================
// ImagesCommand Property Tests
// ============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(50))]

    /// Test that ImagesCommand handles arbitrary repository patterns
    #[test]
    fn images_command_handles_repository(repo in arbitrary_string_strategy()) {
        let cmd = ImagesCommand::new().repository(repo);
        let args = cmd.build_command_args();
        prop_assert!(args.first() == Some(&"images".to_string()));
    }

    /// Test that ImagesCommand handles arbitrary filters
    #[test]
    fn images_command_handles_filters(filter in arbitrary_string_strategy()) {
        let cmd = ImagesCommand::new().filter(filter);
        let args = cmd.build_command_args();
        prop_assert!(args.contains(&"--filter".to_string()));
    }
}

// ============================================================================
// StopCommand Property Tests
// ============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(50))]

    /// Test that StopCommand handles arbitrary container IDs
    #[test]
    fn stop_command_handles_container_ids(container_id in arbitrary_string_strategy()) {
        let cmd = StopCommand::new(container_id);
        let args = cmd.build_command_args();
        prop_assert!(args.first() == Some(&"stop".to_string()));
    }

    /// Test that StopCommand handles timeout values
    #[test]
    fn stop_command_handles_timeout(
        container_id in docker_name_strategy(),
        timeout in 0u32..3600u32
    ) {
        let cmd = StopCommand::new(container_id).timeout(timeout);
        let args = cmd.build_command_args();
        prop_assert!(args.contains(&"--timeout".to_string()));
    }
}

// ============================================================================
// StartCommand Property Tests
// ============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(50))]

    /// Test that StartCommand handles arbitrary container IDs
    #[test]
    fn start_command_handles_container_ids(container_id in arbitrary_string_strategy()) {
        let cmd = StartCommand::new(container_id);
        let args = cmd.build_command_args();
        prop_assert!(args.first() == Some(&"start".to_string()));
    }
}

// ============================================================================
// RmCommand Property Tests
// ============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(50))]

    /// Test that RmCommand handles arbitrary container IDs
    #[test]
    fn rm_command_handles_container_ids(container_id in arbitrary_string_strategy()) {
        let cmd = RmCommand::new(container_id);
        let args = cmd.build_command_args();
        prop_assert!(args.first() == Some(&"rm".to_string()));
    }

    /// Test that RmCommand handles multiple containers
    #[test]
    fn rm_command_handles_multiple_containers(
        id1 in docker_name_strategy(),
        id2 in docker_name_strategy(),
        id3 in docker_name_strategy()
    ) {
        let cmd = RmCommand::new(id1).container(id2).container(id3);
        let args = cmd.build_command_args();
        prop_assert!(args.first() == Some(&"rm".to_string()));
        // Should have 3 container IDs after the command and flags
    }
}

// ============================================================================
// KillCommand Property Tests
// ============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(50))]

    /// Test that KillCommand handles arbitrary container IDs
    #[test]
    fn kill_command_handles_container_ids(container_id in arbitrary_string_strategy()) {
        let cmd = KillCommand::new(container_id);
        let args = cmd.build_command_args();
        prop_assert!(args.first() == Some(&"kill".to_string()));
    }

    /// Test that KillCommand handles arbitrary signals
    #[test]
    fn kill_command_handles_signals(
        container_id in docker_name_strategy(),
        signal in arbitrary_string_strategy()
    ) {
        let cmd = KillCommand::new(container_id).signal(signal);
        let args = cmd.build_command_args();
        prop_assert!(args.contains(&"--signal".to_string()));
    }
}

// ============================================================================
// LogsCommand Property Tests
// ============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(50))]

    /// Test that LogsCommand handles arbitrary container IDs
    #[test]
    fn logs_command_handles_container_ids(container_id in arbitrary_string_strategy()) {
        let cmd = LogsCommand::new(container_id);
        let args = cmd.build_command_args();
        prop_assert!(args.first() == Some(&"logs".to_string()));
    }

    /// Test that LogsCommand handles tail values
    #[test]
    fn logs_command_handles_tail(
        container_id in docker_name_strategy(),
        tail in arbitrary_string_strategy()
    ) {
        let cmd = LogsCommand::new(container_id).tail(tail);
        let args = cmd.build_command_args();
        prop_assert!(args.contains(&"--tail".to_string()));
    }

    /// Test that LogsCommand handles since timestamps
    #[test]
    fn logs_command_handles_since(
        container_id in docker_name_strategy(),
        since in arbitrary_string_strategy()
    ) {
        let cmd = LogsCommand::new(container_id).since(since);
        let args = cmd.build_command_args();
        prop_assert!(args.contains(&"--since".to_string()));
    }
}

// ============================================================================
// Output Parsing Property Tests
// ============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    /// Test that ContainerId::short() never panics on any input and returns at most 12 characters
    #[test]
    fn container_id_short_never_panics(id in ".*") {
        let container_id = docker_wrapper::ContainerId(id);
        let short = container_id.short();
        // Count characters, not bytes (since short() now returns at most 12 characters)
        let char_count = short.chars().count();
        prop_assert!(char_count <= 12);
        // If original has fewer than 12 chars, short should return the whole thing
        let original_char_count = container_id.0.chars().count();
        if original_char_count < 12 {
            prop_assert_eq!(short, container_id.0.as_str());
        }
    }

    /// Test that ContainerId::as_str() returns the original value
    #[test]
    fn container_id_as_str_returns_original(id in ".*") {
        let container_id = docker_wrapper::ContainerId(id.clone());
        prop_assert_eq!(container_id.as_str(), id.as_str());
    }
}

// ============================================================================
// Builder Idempotency and Ordering Tests
// ============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(25))]

    /// Test that calling the same builder method multiple times works correctly
    #[test]
    fn run_command_multiple_envs(
        image in docker_name_strategy(),
        envs in prop::collection::vec((env_key_strategy(), env_value_strategy()), 1..10)
    ) {
        let mut cmd = RunCommand::new(image);
        for (key, value) in envs {
            cmd = cmd.env(key, value);
        }
        let args = cmd.build_command_args();
        prop_assert!(args.first() == Some(&"run".to_string()));
    }

    /// Test that calling multiple port mappings works correctly
    #[test]
    fn run_command_multiple_ports(
        image in docker_name_strategy(),
        ports in prop::collection::vec((port_strategy(), port_strategy()), 1..10)
    ) {
        let mut cmd = RunCommand::new(image);
        for (host, container) in ports {
            cmd = cmd.port(host, container);
        }
        let args = cmd.build_command_args();
        prop_assert!(args.first() == Some(&"run".to_string()));
        // Count --publish occurrences
        let publish_count = args.iter().filter(|a| *a == "--publish").count();
        prop_assert!(publish_count >= 1);
    }

    /// Test that calling multiple volume mounts works correctly
    #[test]
    fn run_command_multiple_volumes(
        image in docker_name_strategy(),
        volumes in prop::collection::vec(volume_mount_strategy(), 1..10)
    ) {
        let mut cmd = RunCommand::new(image);
        for (source, target) in volumes {
            cmd = cmd.volume(source, target);
        }
        let args = cmd.build_command_args();
        prop_assert!(args.first() == Some(&"run".to_string()));
    }

    /// Test that calling multiple labels works correctly
    #[test]
    fn run_command_multiple_labels(
        image in docker_name_strategy(),
        labels in prop::collection::vec(label_strategy(), 1..10)
    ) {
        let mut cmd = RunCommand::new(image);
        for label in labels {
            cmd = cmd.label(label);
        }
        let args = cmd.build_command_args();
        prop_assert!(args.first() == Some(&"run".to_string()));
    }
}

// ============================================================================
// Edge Case Tests
// ============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(25))]

    /// Test handling of very long strings
    #[test]
    fn run_command_handles_long_strings(
        len in 100usize..1000usize
    ) {
        let long_string: String = "a".repeat(len);
        let cmd = RunCommand::new(&long_string).name(&long_string);
        let args = cmd.build_command_args();
        prop_assert!(args.first() == Some(&"run".to_string()));
    }

    /// Test handling of unicode strings
    #[test]
    fn run_command_handles_unicode(
        unicode in "[\\p{L}\\p{N}]{0,64}"
    ) {
        let cmd = RunCommand::new(unicode);
        let args = cmd.build_command_args();
        prop_assert!(args.first() == Some(&"run".to_string()));
    }
}

// ============================================================================
// Determinism Tests
// ============================================================================

proptest! {
    #![proptest_config(ProptestConfig::with_cases(25))]

    /// Test that building args is deterministic
    #[test]
    fn run_command_is_deterministic(
        image in docker_name_strategy(),
        name in docker_name_strategy(),
        host_port in port_strategy(),
        container_port in port_strategy()
    ) {
        let cmd1 = RunCommand::new(image.clone())
            .name(name.clone())
            .port(host_port, container_port)
            .detach();

        let cmd2 = RunCommand::new(image)
            .name(name)
            .port(host_port, container_port)
            .detach();

        let args1 = cmd1.build_command_args();
        let args2 = cmd2.build_command_args();

        prop_assert_eq!(args1, args2);
    }
}