diff --git a/Makefile b/Makefile index e2eb72ccf0..ecf642fa37 100644 --- a/Makefile +++ b/Makefile @@ -593,7 +593,7 @@ localbenchmarks: test-binaries localsystem: # Wipe existing config, database, and cache: start with clean slate. $(RM) -rf ${HOME}/.local/share/containers ${HOME}/.config/containers - if timeout -v 1 true; then PODMAN=$(CURDIR)/bin/podman bats test/system/; else echo "Skipping $@: 'timeout -v' unavailable'"; fi + if timeout -v 1 true; then PODMAN=$(CURDIR)/bin/podman QUADLET=$(CURDIR)/bin/quadlet bats test/system/; else echo "Skipping $@: 'timeout -v' unavailable'"; fi .PHONY: remotesystem remotesystem: diff --git a/hack/bats b/hack/bats index 9bc2d2c152..1e732d3ac9 100755 --- a/hack/bats +++ b/hack/bats @@ -54,6 +54,7 @@ $0 also passes through \$OCI_RUNTIME, should you need to test that. # By default, test the podman in our working directory. # Some tests cd out of our workdir, so abs path is important export PODMAN=${PODMAN:-$(pwd)/bin/podman} +export QUADLET=${QUADLET:-$(pwd)/bin/quadlet} # Because 'make' doesn't do this by default chcon -t container_runtime_exec_t $PODMAN diff --git a/test/system/252-quadlet.bats b/test/system/252-quadlet.bats new file mode 100644 index 0000000000..e583344acd --- /dev/null +++ b/test/system/252-quadlet.bats @@ -0,0 +1,263 @@ +#!/usr/bin/env bats -*- bats -*- +# +# Tests generated configurations for systemd. +# + +load helpers +load helpers.systemd + +UNIT_FILES=() + +function start_time() { + sleep_to_next_second # Ensure we're on a new second with no previous logging + STARTED_TIME=$(date "+%F %R:%S") # Start time for new log time +} + +function setup() { + skip_if_remote "quadlet tests are meaningless over remote" + + start_time + + basic_setup +} + +function teardown() { + for UNIT_FILE in ${UNIT_FILES[@]}; do + if [[ -e "$UNIT_FILE" ]]; then + local service=$(basename "$UNIT_FILE") + run systemctl stop "$service" + if [ $status -ne 0 ]; then + echo "# WARNING: systemctl stop failed in teardown: $output" >&3 + fi + rm -f "$UNIT_FILE" + fi + done + systemctl daemon-reload + + basic_teardown +} + +# Converts the quadlet file and installs the result it in $UNIT_DIR +function run_quadlet() { + local sourcefile="$1" + local service=$(quadlet_to_service_name "$sourcefile") + + # quadlet always works on an entire directory, so copy the file + # to transform to a tmpdir + local quadlet_tmpdir=$(mktemp -d --tmpdir=$PODMAN_TMPDIR quadlet.XXXXXX) + cp $sourcefile $quadlet_tmpdir/ + + QUADLET_UNIT_DIRS="$quadlet_tmpdir" run $QUADLET $_DASHUSER $UNIT_DIR + assert $status -eq 0 "Failed to convert quadlet file: $sourcefile" + is "$output" "" "quadlet should report no errors" + + # Ensure this is teared down + UNIT_FILES+=("$UNIT_DIR/$service") + + QUADLET_SERVICE_NAME="$service" + QUADLET_SYSLOG_ID="$(basename $service .service)" + QUADLET_CONTAINER_NAME="systemd-$QUADLET_SYSLOG_ID" + + cat $UNIT_DIR/$QUADLET_SERVICE_NAME +} + +function service_setup() { + local service="$1" + local option="$2" + + systemctl daemon-reload + + local startargs="" + local statusexit=0 + local activestate="active" + + # If option wait, start and wait for service to exist + if [ "$option" == "wait" ]; then + startargs="--wait" + statusexit=3 + local activestate="inactive" + fi + + run systemctl $startargs start "$service" + assert $status -eq 0 "Error starting systemd unit $service: $output" + + run systemctl status "$service" + assert $status -eq $statusexit "systemctl status $service: $output" + + run systemctl show -P ActiveState "$service" + assert $status -eq 0 "systemctl show $service: $output" + is "$output" $activestate +} + +# Helper to stop a systemd service running a container +function service_cleanup() { + local service="$1" + local expected_state="$2" + + run systemctl stop "$service" + assert $status -eq 0 "Error stopping systemd unit $service: $output" + + # Regression test for #11304: confirm that unit stops into correct state + if [[ -n "$expected_state" ]]; then + run systemctl show --property=ActiveState "$service" + assert "$output" = "ActiveState=$expected_state" \ + "state of service after systemctl stop" + fi + + rm -f "$UNIT_DIR/$service" + systemctl daemon-reload +} + +@test "quadlet - basic" { + local quadlet_file=$PODMAN_TMPDIR/basic_$(random_string).container + cat > $quadlet_file < $quadlet_file < $quadlet_file < $quadlet_file < $quadlet_file < $quadlet_vol_file < $quadlet_file <