#!/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" skip_if_rootless_cgroupsv1 "Can't use --cgroups=split w/ CGv1 (#17456)" skip_if_journald_unavailable "quadlet isn't really usable without journal" test -x "$QUADLET" || die "Cannot run quadlet tests without executable \$QUADLET ($QUADLET)" 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/ echo "$_LOG_PROMPT $QUADLET $_DASHUSER $UNIT_DIR" QUADLET_UNIT_DIRS="$quadlet_tmpdir" run $QUADLET $_DASHUSER $UNIT_DIR echo "$output" 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 echo "$_LOG_PROMPT systemctl $startargs start $service" run systemctl $startargs start "$service" echo "$output" assert $status -eq 0 "Error starting systemd unit $service" echo "$_LOG_PROMPT systemctl status $service" run systemctl status "$service" echo "$output" assert $status -eq $statusexit "systemctl status $service" echo "$_LOG_PROMPT systemctl show --value --property=ActiveState $service" run systemctl show --value --property=ActiveState "$service" echo "$output" assert $status -eq 0 "systemctl show $service" 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 $service after systemctl stop" fi rm -f "$UNIT_DIR/$service" systemctl daemon-reload } function create_secret() { local secret_name=$(random_string) local secret_file=$PODMAN_TMPDIR/secret_$(random_string) local secret=$(random_string) echo $secret > $secret_file run_podman secret create $secret_name $secret_file SECRET_NAME=$secret_name SECRET=$secret } function remove_secret() { local secret_name="$1" run_podman secret rm $secret_name } @test "quadlet - basic" { local quadlet_file=$PODMAN_TMPDIR/basic_$(random_string).container cat > $quadlet_file < $quadlet_file < $quadlet_file < $quadlet_file < $quadlet_file < $quadlet_file < $quadlet_vol_file < $quadlet_file < $quadlet_file < $quadlet_network_file < $quadlet_file <$yaml_source < $quadlet_file < $quadlet_file < $quadlet_file < $quadlet_file < $quadlet_file < $quadlet_file <