docker-pose 0.5.0

Command line tool to play with 🐳 Docker Compose files.
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
use docker_pose::ComposeYaml;
use pretty_assertions::assert_eq;
use serde_yaml::Error;

#[test]
fn get_services_list() -> Result<(), Error> {
    let yaml = "
services:
  app: the-app
  # comments should not ...
  app1:
    # ... affects results
    image: some-image
    ports:
      - 8000:8000
  app2:
    image: another-image:2.0
    ports:
      - 9000:9000
    depends_on:
      - app1

volumes:
  - no-body-cares
    ";
    let compose = ComposeYaml::new(&yaml)?;
    let services_map = compose.get_root_element("services").unwrap();
    let services_names = compose.get_root_element_names("services");
    assert_eq!(services_map.len(), services_names.len());
    assert_eq!(services_names, vec!["app", "app1", "app2"]);
    Ok(())
}

#[test]
fn get_services_list_filter_by_image_tag() -> Result<(), Error> {
    let yaml = "
services:
  app: the-app
  app1:
    image: some-image
    ports:
      - 8000:8000
  app2:
    image: another-image:2.0
    ports:
      - 9000:9000
  app3:
    image: another-image:2.0.1
    ports:
      - 9001:9001
    depends_on:
      - app2
  app4:
    image: me:2.0
    ports:
      - 9003:9003

volumes:
  - no-body-cares
    ";
    let compose = ComposeYaml::new(&yaml)?;
    let services = compose.filter_services_by_image_tag("2.0");
    let services_names = services.iter().map(|e| e.0.as_str()).collect::<Vec<_>>();
    assert_eq!(services_names, vec!["app2", "app4"]);
    Ok(())
}

#[test]
fn get_services_empty_list() -> Result<(), Error> {
    let yaml = "
services: []
volumes:
  - no-body-cares
    ";
    let compose = ComposeYaml::new(&yaml)?;
    assert!(compose.get_root_element("services").is_none());
    assert!(compose.get_root_element_names("services").is_empty());
    Ok(())
}

#[test]
fn get_services_no_list() -> Result<(), Error> {
    let yaml = "
volumes:
  - no-body-cares
    ";
    let compose = ComposeYaml::new(&yaml)?;
    assert!(compose.get_root_element("services").is_none());
    assert!(compose.get_root_element_names("services").is_empty());
    Ok(())
}

#[test]
fn get_service_envs() -> Result<(), Error> {
    let yaml = r#"
services:
  app: the-app
  app1:
    image: some-image
    ports:
      - 8000:8000
    environment:
      - PORT=8000
      - KAFKA_BROKERS=kafka:9092
      - TITLE="App 1"
      - DESC_1='App 1 is the "Best"'
      - EMPTY=
      - UNDEFINED
      - UNDEFINED_TOO
    "#;
    let compose = ComposeYaml::new(&yaml)?;
    let app1 = compose.get_service("app1").expect("app1 not found");
    let envs = compose.get_service_envs(&app1);
    assert_eq!(
        envs.unwrap_or(Vec::default()),
        vec![
            "PORT=8000",
            "KAFKA_BROKERS=kafka:9092",
            "TITLE=\"App 1\"",
            "DESC_1='App 1 is the \"Best\"'",
            "EMPTY=",
            "UNDEFINED=",
            "UNDEFINED_TOO=",
        ]
    );
    Ok(())
}

#[test]
fn get_services() -> Result<(), Error> {
    let yaml = "
services:
  app: the-app
  app-1:
    image: some-image
    ports:
      - 8000:8000
    ";
    let compose = ComposeYaml::new(&yaml)?;
    let app = compose.get_service("app");
    let app1 = compose.get_service("app-1");
    let not_exist = compose.get_service("does-not-exist");
    assert!(app.is_none()); // Because it's malformed
    assert!(app1.is_some());
    assert!(not_exist.is_none());
    Ok(())
}

#[test]
fn get_service_envs_empty() -> Result<(), Error> {
    let yaml = "
services:
  app: the-app
  app1:
    image: some-image
    ports:
      - 8000:8000
    ";
    let compose = ComposeYaml::new(&yaml)?;
    let app1 = compose.get_service("app1").expect("app1 not found");
    let envs = compose.get_service_envs(&app1);
    assert!(envs.is_none());
    Ok(())
}

#[test]
fn get_service_envs_with_map_notation() -> Result<(), Error> {
    let yaml = r#"
services:
  app: the-app
  app1:
    image: some-image
    ports:
      - 8000:8000
    environment:
      PORT: 8000
      KAFKA_BROKERS: "kafka:9092"
      TITLE: "App 1"
  app2:
    image: another-image:2.0
    ports:
      - 9000:9000
    "#;
    let compose = ComposeYaml::new(&yaml)?;
    let app1 = compose.get_service("app1").expect("app1 not found");
    let envs = compose.get_service_envs(&app1);
    assert_eq!(
        envs.unwrap_or(Vec::default()),
        vec!["PORT=8000", "KAFKA_BROKERS=kafka:9092", "TITLE=\"App 1\"",]
    );
    Ok(())
}

#[test]
fn get_service_envs_with_map_notation_and_quoted() -> Result<(), Error> {
    let yaml = r#"
services:
  app1:
    image: some-image
    environment:
      PORT: 8000
      KAFKA_PORT: "9092"
      DB_PORT: '5432'
      TITLE: "App 1"
      DESC: 'Desc 1'
    "#;
    let compose = ComposeYaml::new(&yaml)?;
    let app1 = compose.get_service("app1").expect("app1 not found");
    let envs = compose.get_service_envs(&app1);
    assert_eq!(
        envs.unwrap_or(Vec::default()),
        vec![
            "PORT=8000",
            "KAFKA_PORT=9092",
            "DB_PORT=5432",
            "TITLE=\"App 1\"",
            "DESC=\"Desc 1\"",
        ]
    );
    Ok(())
}

#[test]
fn get_profiles() -> Result<(), Error> {
    let yaml = "
services:
  app: the-app
  psql:
    image: postgres
    profiles:
      - tools
  web:
    image: web-server
  app-provision:
    image: app
    profiles:
      - tools
      - provision
    ";
    let compose = ComposeYaml::new(&yaml)?;
    let profiles = compose.get_profiles_names();
    assert_eq!(profiles, Some(vec!["provision", "tools"]));
    Ok(())
}

#[test]
fn get_profiles_vector_notation() -> Result<(), Error> {
    let yaml = "
services:
  app: the-app
  app-provision:
    image: app
    profiles: [tools, provision]
    ";
    let compose = ComposeYaml::new(&yaml)?;
    let profiles = compose.get_profiles_names();
    assert_eq!(profiles, Some(vec!["provision", "tools"]));
    Ok(())
}

#[test]
fn get_profiles_none() -> Result<(), Error> {
    let yaml = "
volumes:
  data:
    driver: local
    ";
    let compose = ComposeYaml::new(&yaml)?;
    let profiles = compose.get_profiles_names();
    assert!(profiles.is_none());
    Ok(())
}

#[test]
fn get_images() -> Result<(), Error> {
    let yaml = "
services:
  app0: the-app
  app:
    image: app
  web:
    image: namespace.server.com/image:master
  psql:
    image: postgres:16.1
    profiles:
      - tools
  nginx:
    image: nginx:stable
  app-provision:
    image: app
    profiles:
      - tools
      - provision
  app-with-another-tag:
    image: app:1.0
    ";
    let compose = ComposeYaml::new(&yaml)?;
    let images = compose.get_images(None, None);
    assert_eq!(
        images,
        Some(vec![
            "app".to_string(),     // used twice, but included once
            "app:1.0".to_string(), // same image but with different version
            "namespace.server.com/image:master".to_string(),
            "nginx:stable".to_string(),
            "postgres:16.1".to_string(),
        ])
    );
    Ok(())
}

#[test]
fn get_images_filter_by_tag() -> Result<(), Error> {
    let yaml = "
services:
  app:
    image: app
  web:
    image: namespace.server.com/image:master
  psql:
    image: postgres:16.1
  nginx:
    image: nginx:master
  app-provision:
    image: app
  app-with-another-tag:
    image: app:1.0
    ";
    let compose = ComposeYaml::new(&yaml)?;
    let images = compose.get_images(Some("master"), None);
    assert_eq!(
        images,
        Some(vec![
            "namespace.server.com/image:master".to_string(),
            "nginx:master".to_string(),
        ])
    );
    Ok(())
}

#[test]
fn get_images_none() -> Result<(), Error> {
    let yaml = "
volumes:
  data:
    driver: local
    ";
    let compose = ComposeYaml::new(&yaml)?;
    let images = compose.get_images(None, None);
    assert!(images.is_none());
    Ok(())
}

#[test]
fn get_service_depends() -> Result<(), Error> {
    let yaml = r#"
services:
  app:
    image: the-app
    depends_on:
      - x
  postgres:
    image: postgres
  app1:
    image: some-image
    ports:
      - 8000:8000
    depends_on:
      - postgres
      - app
    "#;
    let compose = ComposeYaml::new(&yaml)?;
    let app1 = compose.get_service("app1").expect("app1 not found");
    let depends_on = compose.get_service_depends_on(&app1);
    assert_eq!(
        depends_on.unwrap_or(Vec::default()),
        vec!["postgres", "app"]
    );
    Ok(())
}

#[test]
fn get_service_dependants() -> Result<(), Error> {
    let yaml = r#"
services:
  app:
    image: the-app
    depends_on:
      - x
  postgres:
    image: postgres
  app1:
    image: some-image
    ports:
      - 8000:8000
    depends_on:
      - postgres
      - app
    "#;
    let compose = ComposeYaml::new(&yaml)?;
    let services = vec!["app".to_string()];
    let depends_on = compose.get_services_dependants(&services);
    assert_eq!(depends_on.unwrap_or(Vec::default()), vec!["app1"]);
    Ok(())
}

#[test]
fn get_service_dependants_expanded() -> Result<(), Error> {
    let yaml = r#"
services:
  app:
    image: the-app
    depends_on:
      - x
  postgres:
    image: postgres
  app1:
    image: some-image
    ports:
      - 8000:8000
    depends_on:
      - postgres
      - app
  app2:
    image: some-image-2:dev
    ports:
      - 8001:8001
    depends_on:
      app:
        condition: service_started
      postgres:
        condition: service_started
    "#;
    let compose = ComposeYaml::new(&yaml)?;
    let services = vec!["app".to_string()];
    let depends_on = compose.get_services_dependants(&services);
    assert_eq!(depends_on.unwrap_or(Vec::default()), vec!["app1", "app2"]);
    Ok(())
}

#[test]
fn get_services_depends() -> Result<(), Error> {
    let yaml = r#"
services:
  app:
    image: the-app
    depends_on:
      - x
  postgres:
    image: postgres
  app1:
    image: some-image
    ports:
      - 8000:8000
    depends_on:
      - postgres
      - app
    "#;
    let compose = ComposeYaml::new(&yaml)?;
    let services = vec!["app".to_string(), "app1".to_string()];
    let all_deps_op = compose.get_services_depends_on(&services);
    assert_eq!(
        all_deps_op,
        Ok(vec!["postgres".to_string(), "x".to_string()])
    );
    Ok(())
}

#[test]
fn get_services_depends_not_found() -> Result<(), Error> {
    let yaml = r#"
services:
  app:
    image: the-app
    depends_on:
      - x
  postgres:
    image: postgres
  app1:
    image: some-image
    ports:
      - 8000:8000
    depends_on:
      - postgres
      - app
    "#;
    let compose = ComposeYaml::new(&yaml)?;
    let services = vec![
        "app".to_string(),
        "dont_exist1".to_string(),
        "dont_exist2".to_string(),
    ];
    let all_deps_op = compose.get_services_depends_on(&services);
    assert_eq!(
        all_deps_op,
        Err(vec!["dont_exist1".to_string(), "dont_exist2".to_string()])
    );
    Ok(())
}

#[test]
fn get_service_depends_array_notation() -> Result<(), Error> {
    let yaml = r#"
services:
  app:
    image: the-app
    depends_on: [x, postgres]
  postgres:
    image: postgres
  app1:
    image: some-image
    ports:
      - 8000:8000
    depends_on:
      - postgres
      - app
    "#;
    let compose = ComposeYaml::new(&yaml)?;
    let app1 = compose.get_service("app").expect("app not found");
    let depends_on = compose.get_service_depends_on(&app1);
    assert_eq!(depends_on.unwrap_or(Vec::default()), vec!["x", "postgres"]);
    Ok(())
}

#[test]
fn get_service_depends_with_conditions() -> Result<(), Error> {
    let yaml = r#"
services:
  app:
    image: the-app
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
  postgres:
    image: postgres
    "#;
    let compose = ComposeYaml::new(&yaml)?;
    let app1 = compose.get_service("app").expect("app not found");
    let depends_on = compose.get_service_depends_on(&app1);
    assert_eq!(
        depends_on.unwrap_or(Vec::default()),
        vec!["postgres", "redis"]
    );
    Ok(())
}

#[test]
fn get_service_no_depends() -> Result<(), Error> {
    let yaml = r#"
services:
  app:
    image: the-app
  postgres:
    image: postgres
    "#;
    let compose = ComposeYaml::new(&yaml)?;
    let app1 = compose.get_service("app").expect("app not found");
    let depends_on = compose.get_service_depends_on(&app1);
    assert!(depends_on.is_none());
    Ok(())
}