Files
snapd/tests/lib/systemd.sh
Sergio Cazzolato cfccc9a891 tests: replace the systemd.sh helper by the tests.systemd tool in tests/lib (#12663)
* Repleace the systemd.sh helper by the tests.systemd tool in tests/lib

This is the first part of the migration to start using the tests.systemd
tool instead of the systemd.sh helper

* Fix shellcheck issues

* move function to get active snapd units to state.sh

It is the only place where the function is used

* Update missing functions in nested.sh

* Squashed 'tests/lib/external/snapd-testing-tools/' changes from 1c8efb77e1..14aa43a405

14aa43a405 new feature to re-run failed spread tests (#39)
604cb782db Fix shellcheck in systemd tool
bfc71082c8 Update the tests.systemd to allow parameters waiting for service status
8a2d0a99df Adding quiet tool and removing set +-x from tests.pkgs
d90935d2a4 A comment explaining about the default values for wait-for
3232c5dba7 Add support for ubuntu 23.04
a7164fba07 remove fedora 35 support, add fedora 37 support
89b9eb5301 Update systems supported
92bb6a0664 Include snap-sufix in the snaps.name tool

git-subtree-dir: tests/lib/external/snapd-testing-tools
git-subtree-split: 14aa43a405f344b056db4fec7aee78709c41959a

* use -n for tests.systemd wait-for-service

* Squashed 'tests/lib/external/snapd-testing-tools/' changes from 14aa43a405..162e93bd35

162e93bd35 update tests.systemd CLI options to be the same than retry command

git-subtree-dir: tests/lib/external/snapd-testing-tools
git-subtree-split: 162e93bd356f202b68408d0391b34e93a4f98755

* Squashed 'tests/lib/external/snapd-testing-tools/' changes from 162e93bd35..01a2a83b4b

01a2a83b4b Update tests.systemd to have stop units as systemd.sh

git-subtree-dir: tests/lib/external/snapd-testing-tools
git-subtree-split: 01a2a83b4b58262e13ca2578ac286a41b780fa7d

* Squashed 'tests/lib/external/snapd-testing-tools/' changes from 01a2a83b4b..9089ff5c02

9089ff5c02 Update tests to use the new tests.systemd stop-unit
44ecd5e56a Move tests.systemd stop-units to stop-unit

git-subtree-dir: tests/lib/external/snapd-testing-tools
git-subtree-split: 9089ff5c02a1cf074a05fb7c58af93274f63a446

* Use the new tests.systemd stop-unit command

* Squashed 'tests/lib/external/snapd-testing-tools/' changes from 9089ff5c02..75e8a426a5

75e8a426a5 make sure the unit is removed in tests.systemd test

git-subtree-dir: tests/lib/external/snapd-testing-tools
git-subtree-split: 75e8a426a59fb02ee74d5bb7c60c19403c9146a0

* Fix error in test muinstaller-real

the vm needs to be removed before creating a new one

* Squashed 'tests/lib/external/snapd-testing-tools/' changes from 75e8a426a5..63540b845a

63540b845a Fix error messages in remote pull and push

git-subtree-dir: tests/lib/external/snapd-testing-tools
git-subtree-split: 63540b845a542ee0a71257e0a726a618201ebb5d

* update how stop is used in muinstaller-real nested test

* Squashed 'tests/lib/external/snapd-testing-tools/' changes from 63540b845a..e5ae22a5d4

e5ae22a5d4 systemd units can be overwritten

git-subtree-dir: tests/lib/external/snapd-testing-tools
git-subtree-split: e5ae22a5d42b55d6b33c3f692968e72c62a619dc
2023-04-04 23:02:29 -03:00

68 lines
2.1 KiB
Bash

#!/bin/bash
# Create and start a persistent systemd unit that survives reboots. Use as:
# systemd_create_and_start_unit "name" "my-service --args"
# The third arg supports "overrides" which allow to customize the service
# as needed, e.g.:
# systemd_create_and_start_unit "name" "start" "[Unit]\nAfter=foo"
systemd_create_and_start_unit() {
printf '[Unit]\nDescription=Support for test %s\n[Service]\nType=simple\nExecStart=%s\n[Install]\nWantedBy=multi-user.target\n' "${SPREAD_JOB:-unknown}" "$2" > "/etc/systemd/system/$1.service"
if [ -n "${3:-}" ]; then
mkdir -p "/etc/systemd/system/$1.service.d"
# shellcheck disable=SC2059
printf "$3" >> "/etc/systemd/system/$1.service.d/override.conf"
fi
systemctl daemon-reload
systemctl enable "$1"
systemctl start "$1"
wait_for_service "$1" active 30
}
systemd_stop_and_remove_unit() {
systemctl stop "$1" || true
systemctl disable "$1" || true
rm -f "/etc/systemd/system/$1.service"
rm -rf "/etc/systemd/system/$1.service.d"
systemctl daemon-reload
}
wait_for_service() {
local service_name="$1"
local state="${2:-active}"
local attempts="${3:-60}"
for i in $(seq "$attempts"); do
if systemctl show -p ActiveState "$service_name" | grep -q "ActiveState=$state"; then
return
fi
# show debug output every 1min
if [ "$i" -gt 0 ] && [ $(( i % 60 )) = 0 ]; then
systemctl status "$service_name" || true;
fi
sleep 1;
done
echo "service $service_name did not start"
exit 1
}
systemd_stop_units() {
for unit in "$@"; do
if systemctl is-active "$unit"; then
echo "Ensure the service is active before stopping it"
retries=20
while systemctl status "$unit" | grep -q "Active: activating"; do
if [ $retries -eq 0 ]; then
echo "$unit unit not active"
systemctl status "$unit" || true
exit 1
fi
retries=$(( retries - 1 ))
sleep 1
done
systemctl stop "$unit"
fi
done
}