@@ -11,6 +11,7 @@ load helpers.registry
11
11
load helpers.systemd
12
12
13
13
UNIT_FILES=()
14
+ SERVICES_TO_STOP=()
14
15
15
16
function start_time() {
16
17
sleep_to_next_second # Ensure we're on a new second with no previous logging
@@ -26,16 +27,32 @@ function setup() {
26
27
27
28
start_time
28
29
30
+ # Clear arrays for each test
31
+ SERVICES_TO_STOP=()
32
+
29
33
basic_setup
30
34
}
31
35
32
36
function teardown() {
37
+ # Stop manually specified services
38
+ for service in ${SERVICES_TO_STOP[@]} ; do
39
+ run systemctl stop " $service "
40
+ if [ $status -ne 0 ]; then
41
+ echo " # WARNING: systemctl stop failed in teardown: $output " >&3
42
+ fi
43
+ run systemctl reset-failed " $service "
44
+ done
45
+
33
46
for UNIT_FILE in ${UNIT_FILES[@]} ; do
34
47
if [[ -e " $UNIT_FILE " ]]; then
35
48
local service=$( basename " $UNIT_FILE " )
36
- run systemctl stop " $service "
37
- if [ $status -ne 0 ]; then
38
- echo " # WARNING: systemctl stop failed in teardown: $output " >&3
49
+ # Skip stopping template services (those ending with '@')
50
+ # as they cannot be stopped directly without an instance name
51
+ if [[ ! " $service " =~ @\. service$ ]]; then
52
+ run systemctl stop " $service "
53
+ if [ $status -ne 0 ]; then
54
+ echo " # WARNING: systemctl stop failed in teardown: $output " >&3
55
+ fi
39
56
fi
40
57
run systemctl reset-failed " $service "
41
58
rm -f " $UNIT_FILE "
437
454
run_podman volume rm $volume_name
438
455
}
439
456
457
+ # A quadlet container template depends on a quadlet volume and network templates
458
+ @test " quadlet - template dependency" {
459
+ # Save the unit name to use as the volume template for the container template
460
+ local quadlet_vol_unit=dep_$( safename) @.volume
461
+ local quadlet_vol_file=$PODMAN_TMPDIR /${quadlet_vol_unit}
462
+ cat > $quadlet_vol_file << EOF
463
+ [Volume]
464
+ EOF
465
+
466
+ local quadlet_tmpdir=$( mktemp -d --tmpdir=$PODMAN_TMPDIR quadlet.XXXXXX)
467
+ # Have quadlet create the systemd unit file for the volume template unit
468
+ run_quadlet " $quadlet_vol_file " " $quadlet_tmpdir "
469
+
470
+ # Save the volume service name since the variable will be overwritten
471
+ local vol_service=$QUADLET_SERVICE_NAME
472
+ local volume_name=systemd-$( basename $quadlet_vol_file .volume)
473
+ # For template units, the volume name should have -%i appended
474
+ volume_name=${volume_name%@ } -%i
475
+
476
+ # Save the unit name to use as the network template for the container template
477
+ local quadlet_net_unit=dep_$( safename) @.network
478
+ local quadlet_net_file=$PODMAN_TMPDIR /${quadlet_net_unit}
479
+ cat > $quadlet_net_file << EOF
480
+ [Network]
481
+ EOF
482
+
483
+ # Have quadlet create the systemd unit file for the network template unit
484
+ run_quadlet " $quadlet_net_file " " $quadlet_tmpdir "
485
+
486
+ # Save the network service name since the variable will be overwritten
487
+ local net_service=$QUADLET_SERVICE_NAME
488
+ local network_name=systemd-$( basename $quadlet_net_file .network)
489
+ # For template units, the network name should have -%i appended
490
+ network_name=${network_name%@ } -%i
491
+
492
+ local quadlet_file=$PODMAN_TMPDIR /user_$( safename) @.container
493
+ cat > $quadlet_file << EOF
494
+ [Container]
495
+ Image=$IMAGE
496
+ Exec=top
497
+ Volume=$quadlet_vol_unit :/tmp
498
+ Network=$quadlet_net_unit
499
+ EOF
500
+
501
+ # Have quadlet create the systemd unit file for the container template unit
502
+ run_quadlet " $quadlet_file " " $quadlet_tmpdir "
503
+
504
+ # Save the container service name for readability
505
+ local container_service=$QUADLET_SERVICE_NAME
506
+
507
+ # Create instance names for the template units
508
+ local instance_name=" test"
509
+ local vol_service_instance=" ${vol_service%@* } @${instance_name} .service"
510
+ local net_service_instance=" ${net_service%@* } @${instance_name} .service"
511
+ local container_service_instance=" ${container_service%@* } @${instance_name} .service"
512
+ local volume_name_instance=" systemd-dep_$( safename) -${instance_name} "
513
+ local network_name_instance=" systemd-dep_$( safename) -${instance_name} "
514
+
515
+ # Volume should not exist
516
+ run_podman 1 volume exists ${volume_name_instance}
517
+ # Network should not exist
518
+ run_podman 1 network exists ${network_name_instance}
519
+
520
+ # Start the container service instance which should also trigger the start of the volume service instance
521
+ service_setup $container_service_instance
522
+
523
+ # Add the service instances to SERVICES_TO_STOP for proper cleanup
524
+ # SERVICES_TO_STOP+=("$container_service_instance")
525
+ SERVICES_TO_STOP+=(" $vol_service_instance " )
526
+ SERVICES_TO_STOP+=(" $net_service_instance " )
527
+
528
+ # Volume system unit instance should be active
529
+ run systemctl show --property=ActiveState " $vol_service_instance "
530
+ assert " $output " = " ActiveState=active" \
531
+ " volume template instance should be active via dependency"
532
+
533
+ # Network system unit instance should be active
534
+ run systemctl show --property=ActiveState " $net_service_instance "
535
+ assert " $output " = " ActiveState=active" \
536
+ " network template instance should be active via dependency"
537
+
538
+ # Volume should exist
539
+ run_podman volume exists ${volume_name_instance}
540
+
541
+ # Network should exist
542
+ run_podman network exists ${network_name_instance}
543
+
544
+ # Clean up the created resources
545
+ service_cleanup $container_service_instance failed
546
+ run_podman volume rm $volume_name_instance
547
+ run_podman network rm $network_name_instance
548
+ }
549
+
440
550
# A quadlet container depends on a named quadlet volume
441
551
@test " quadlet - named volume dependency" {
442
552
local volume_name=" v-$( safename) "
0 commit comments