podup 3.2.0

Translate and run docker-compose files on rootless Podman
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
//! Tests for the `extends` / multi-`-f` service merge.
//!
//! Split out of `merge.rs` to keep that file within the source line limit.

use crate::parse_str;

#[test]
fn extends_unions_sequence_fields() {
	let yaml = r#"
services:
  base:
    image: alpine
    ports:
      - "80:80"
      - "81:81"
  app:
    extends: base
    ports:
      - "90:90"
"#;
	let file = parse_str(yaml).unwrap();
	// Compose `extends` combines sequences (base first, then the extending
	// service's items) rather than replacing the base wholesale.
	assert_eq!(file.services["app"].ports.len(), 3);
}

#[test]
fn extends_dedups_identical_sequence_entries() {
	let yaml = r#"
services:
  base:
    image: alpine
    ports:
      - "80:80"
  app:
    extends: base
    ports:
      - "80:80"
      - "90:90"
"#;
	let file = parse_str(yaml).unwrap();
	// An exact duplicate from the extending service is dropped.
	assert_eq!(file.services["app"].ports.len(), 2);
}

#[test]
fn absent_list_field_falls_back_to_base() {
	let yaml = r#"
services:
  base:
    image: alpine
    ports:
      - "80:80"
  app:
    extends: base
"#;
	let file = parse_str(yaml).unwrap();
	assert_eq!(file.services["app"].ports.len(), 1);
}

#[test]
fn labels_are_merged_with_override_winning() {
	let yaml = r#"
services:
  base:
    image: alpine
    labels:
      a: base
      keep: base
  app:
    extends: base
    labels:
      a: over
      b: over
"#;
	let file = parse_str(yaml).unwrap();
	let labels = file.services["app"].labels.to_map();
	assert_eq!(labels.get("a").map(|s| s.as_str()), Some("over"));
	assert_eq!(labels.get("keep").map(|s| s.as_str()), Some("base"));
	assert_eq!(labels.get("b").map(|s| s.as_str()), Some("over"));
}

#[test]
fn empty_override_keeps_base_depends_on() {
	let yaml = r#"
services:
  db:
    image: postgres
  base:
    image: alpine
    depends_on:
      - db
  app:
    extends: base
"#;
	let file = parse_str(yaml).unwrap();
	assert_eq!(
		file.services["app"].depends_on.service_names(),
		vec!["db".to_string()]
	);
}

#[test]
fn extends_unions_depends_on() {
	let yaml = r#"
services:
  db:
    image: postgres
  cache:
    image: redis
  base:
    image: alpine
    depends_on:
      - db
  app:
    extends: base
    depends_on:
      - cache
"#;
	let file = parse_str(yaml).unwrap();
	// compose-go unions the base and extending depends_on rather than letting
	// the override replace the base wholesale.
	let mut names = file.services["app"].depends_on.service_names();
	names.sort();
	assert_eq!(names, vec!["cache".to_string(), "db".to_string()]);
}

#[test]
fn extends_depends_on_override_wins_on_conflict() {
	let yaml = r#"
services:
  db:
    image: postgres
  base:
    image: alpine
    depends_on:
      db:
        condition: service_started
  app:
    extends: base
    depends_on:
      db:
        condition: service_healthy
"#;
	let file = parse_str(yaml).unwrap();
	// On an overlapping key the extending service's condition wins.
	assert_eq!(
		file.services["app"].depends_on.condition_for("db"),
		crate::compose::types::ServiceCondition::ServiceHealthy
	);
}

#[test]
fn absent_override_keeps_base_environment() {
	let yaml = r#"
services:
  base:
    image: alpine
    environment:
      A: "1"
  app:
    extends: base
"#;
	let file = parse_str(yaml).unwrap();
	let env = file.services["app"].environment.to_map();
	assert_eq!(env.get("A").and_then(|v| v.clone()).as_deref(), Some("1"));
}

/// #1078: `dns` and its siblings are appended, not replaced. docker compose
/// concatenates these sequences; replacing meant an override adding one
/// nameserver silently removed every other one.
#[test]
fn scalar_or_list_field_override_appends_to_base() {
	let yaml = r#"
services:
  base:
    image: alpine
    dns:
      - 1.1.1.1
  app:
    extends: base
    dns:
      - 9.9.9.9
"#;
	let file = parse_str(yaml).unwrap();
	assert_eq!(
		file.services["app"].dns.to_list(),
		vec!["1.1.1.1", "9.9.9.9"],
		"the base's nameserver must survive an override that adds one"
	);
}

/// #1078: `env_file` is appended too — the base's files are still read, in
/// order, followed by the override's.
#[test]
fn env_file_override_appends_to_base() {
	let yaml = r#"
services:
  base:
    image: alpine
    env_file:
      - base.env
  app:
    extends: base
    env_file:
      - app.env
"#;
	let file = parse_str(yaml).unwrap();
	let entries = file.services["app"].env_file.to_entries();
	assert_eq!(entries.len(), 2, "both env files must be read");
	assert_eq!(entries[0].path(), "base.env");
	assert_eq!(entries[1].path(), "app.env");
}

#[test]
fn depends_on_unions_when_base_has_none() {
	// The base declares no depends_on; the extending service's dependencies are
	// carried through unchanged (merge_depends_on base-empty branch).
	let yaml = r#"
services:
  base:
    image: alpine
  db:
    image: postgres
  app:
    extends: base
    depends_on:
      - db
"#;
	let file = parse_str(yaml).unwrap();
	assert!(file.services["app"]
		.depends_on
		.service_names()
		.contains(&"db".to_string()));
}

/// #1078, the one the issue calls worse than a wrong value: a service on
/// `backend` in the base and `monitoring` in the override silently lost
/// `backend`. It dropped off the network and service discovery failed at run
/// time, far from the config that caused it.
#[test]
fn networks_are_unioned_not_replaced() {
	let yaml = r#"
services:
  base:
    image: alpine
    networks:
      - backend
  app:
    extends: base
    networks:
      - monitoring
networks:
  backend:
  monitoring:
"#;
	let file = parse_str(yaml).unwrap();
	let names = file.services["app"].networks.names();
	assert!(
		names.contains(&"backend".to_string()),
		"the base's network must survive: {names:?}"
	);
	assert!(
		names.contains(&"monitoring".to_string()),
		"the override's network must be added: {names:?}"
	);
}

/// A bare name in the override must not erase per-network config the base
/// set — the union keeps the config unless the override supplies its own.
#[test]
fn network_union_keeps_base_config_for_a_bare_override_entry() {
	let yaml = r#"
services:
  base:
    image: alpine
    networks:
      backend:
        aliases:
          - db
  app:
    extends: base
    networks:
      - backend
networks:
  backend:
"#;
	let file = parse_str(yaml).unwrap();
	let cfg = file.services["app"].networks.config_for("backend");
	assert!(
		cfg.is_some_and(|c| c
			.aliases
			.as_ref()
			.is_some_and(|a| a.contains(&"db".to_string()))),
		"a bare override entry must not wipe the base's aliases"
	);
}

/// #1078: `sysctls` merges per key like `environment` and `labels`, rather
/// than the override replacing the whole map.
#[test]
fn sysctls_merge_per_key() {
	let yaml = r#"
services:
  base:
    image: alpine
    sysctls:
      net.core.somaxconn: "1024"
      net.ipv4.tcp_syncookies: "1"
  app:
    extends: base
    sysctls:
      net.core.somaxconn: "4096"
"#;
	let file = parse_str(yaml).unwrap();
	let m = file.services["app"].sysctls.to_map();
	assert_eq!(
		m.get("net.core.somaxconn").map(String::as_str),
		Some("4096"),
		"the override wins for a key both set"
	);
	assert_eq!(
		m.get("net.ipv4.tcp_syncookies").map(String::as_str),
		Some("1"),
		"a key only the base sets must survive"
	);
}

/// The union must survive serialization: `config` renders the merged service
/// back to YAML, and a map whose values are all `None` must still emit its
/// network names rather than an empty block.
#[test]
fn unioned_networks_serialize_back_to_their_names() {
	let yaml = r#"
services:
  base:
    image: alpine
    networks:
      - backend
  app:
    extends: base
    networks:
      - monitoring
networks:
  backend:
  monitoring:
"#;
	let file = parse_str(yaml).unwrap();
	let rendered = serde_yaml::to_string(&file.services["app"].networks).unwrap();
	assert!(
		rendered.contains("backend") && rendered.contains("monitoring"),
		"serialized form lost the network names: {rendered}"
	);
}

/// #1078: a base and an override mounting different sources at the *same*
/// target used to emit both, and `podman create` refused the container with
/// `duplicate mount destination`. Remapping a path in an override is exactly
/// what the override is for, so the later one wins.
#[test]
fn volumes_at_the_same_target_are_replaced_not_duplicated() {
	let yaml = r#"
services:
  base:
    image: alpine
    volumes:
      - ./a:/data
      - ./logs:/var/log
  app:
    extends: base
    volumes:
      - ./b:/data
"#;
	let file = parse_str(yaml).unwrap();
	let mounts = &file.services["app"].volumes;
	let at_data: Vec<_> = mounts.iter().filter(|m| m.target() == "/data").collect();
	assert_eq!(at_data.len(), 1, "one mount per target: {mounts:?}");
	assert!(
		format!("{:?}", at_data[0]).contains("./b"),
		"the override's source must win: {:?}",
		at_data[0]
	);
	// A target only the base declares is untouched.
	assert!(
		mounts.iter().any(|m| m.target() == "/var/log"),
		"an unrelated base mount must survive: {mounts:?}"
	);
}

/// #1078: `!override` takes the overriding value whole instead of appending.
/// Both tags were accepted and silently ignored, so a file asking for
/// replacement got a merge — the opposite of what it said.
#[test]
fn override_tag_replaces_instead_of_appending() {
	let base =
		parse_str("services:\n  web:\n    image: alpine\n    ports: [\"8080:80\"]\n").unwrap();
	let over = parse_str("services:\n  web:\n    ports: !override [\"9090:80\"]\n").unwrap();
	let raw: serde_yaml::Value =
		serde_yaml::from_str("services:\n  web:\n    ports: !override [\"9090:80\"]\n").unwrap();
	let directives = crate::compose::tags::collect(&raw);

	let merged = crate::compose::extends::merge_service_tagged(
		base.services["web"].clone(),
		over.services["web"].clone(),
		directives.get("web"),
	);
	assert_eq!(
		merged.ports.len(),
		1,
		"!override must replace, not append: {:?}",
		merged.ports
	);
}

/// `!reset` drops the key entirely, leaving the type's default.
#[test]
fn reset_tag_clears_the_base_value() {
	let base = parse_str("services:\n  web:\n    image: alpine\n    dns: [\"1.1.1.1\"]\n").unwrap();
	let over = parse_str("services:\n  web:\n    dns: !reset []\n").unwrap();
	let raw: serde_yaml::Value =
		serde_yaml::from_str("services:\n  web:\n    dns: !reset []\n").unwrap();
	let directives = crate::compose::tags::collect(&raw);

	let merged = crate::compose::extends::merge_service_tagged(
		base.services["web"].clone(),
		over.services["web"].clone(),
		directives.get("web"),
	);
	assert!(
		merged.dns.to_list().is_empty(),
		"!reset must clear the base: {:?}",
		merged.dns
	);
}

/// Without a tag the ordinary merge rule still applies — the tags must not
/// change the default behaviour of anything they are not on.
#[test]
fn an_untagged_key_still_merges_normally() {
	let base = parse_str("services:\n  web:\n    image: alpine\n    dns: [\"1.1.1.1\"]\n").unwrap();
	let over = parse_str("services:\n  web:\n    dns: [\"9.9.9.9\"]\n").unwrap();
	let merged = crate::compose::extends::merge_service_tagged(
		base.services["web"].clone(),
		over.services["web"].clone(),
		None,
	);
	assert_eq!(merged.dns.to_list(), vec!["1.1.1.1", "9.9.9.9"]);
}