Skip to content

Commit

Permalink
Merge pull request canonical#9834 from sergiocazzolato/tests-normaliz…
Browse files Browse the repository at this point in the history
…e-tools-part1

tests: normalize test tools - part 1
  • Loading branch information
sergiocazzolato authored Jan 14, 2021
2 parents f51367f + 10588ad commit b573acc
Show file tree
Hide file tree
Showing 9 changed files with 637 additions and 590 deletions.
148 changes: 76 additions & 72 deletions tests/lib/tools/cleanup-state
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

show_help() {
echo "usage: cleanup-state <pre-invariant|post-invariant>"
Expand All @@ -12,84 +12,88 @@ show_help() {
echo " each test, that should not leak across tests."
}

if [ $# -eq 0 ]; then
show_help
exit 1
fi
main() {
if [ $# -eq 0 ]; then
show_help
exit 1
fi

action=
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
show_help
exit 0
;;
--)
shift
break
;;
action=
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
show_help
exit 0
;;
--)
shift
break
;;
pre-invariant)
action=pre-invariant
shift
;;
post-invariant)
action=post-invariant
shift
;;
-*)
echo "cleanup-state: unsupported argument $1" >&2
exit 1
;;
*)
echo "cleanup-state: unknown action $1" >&2
exit 1
;;
esac
done

case "$action" in
pre-invariant)
action=pre-invariant
shift
# If the root user has a systemd --user instance then ask it to reload.
# This prevents tests from leaking user-session services that stay in
# memory but are not present on disk, or have been modified on disk, as is
# common with tests that use snaps with user services _or_ with tests that
# cause installation of the snapd.session-agent.service unit via re-exec
# machinery.
#
# This is done AHEAD of the invariant checks as it is very widespread
# and fixing it in each test is not a priority right now.
#
# Note that similar treatment is not required for the "test" user as
# correct usage of tests.session ensures that the session and all the
# processes of the "test" user are terminated.
if pgrep -u root --full "systemd --user"; then
systemctl --user daemon-reload
# Following that, check if there's a snapd.session-agent.socket and
# if one exists stop it and then start it, ignoring errors. If the
# unit was removed, stopping it clears it from memory. This is
# different from restarting the unit, which doesn't do anything if
# the unit on disk is gone.
if systemctl --user is-active snapd.session-agent.socket; then
systemctl --user stop snapd.session-agent.socket
fi
# XXX: if there's a way to check if an unit exists but is stopped,
# use it here to avoid starting a non-existing unit.
systemctl --user start snapd.session-agent.socket || true

# Do the same for root's D-Bus session bus.
# This will stop the bus and any clients that
# may be connected to it.
if systemctl --user is-active dbus.socket; then
systemctl --user stop dbus.socket
fi
systemctl --user start dbus.socket || true
fi
;;
post-invariant)
action=post-invariant
shift
;;
-*)
echo "cleanup-state: unsupported argument $1" >&2
exit 1
true
;;
*)
echo "cleanup-state: unknown action $1" >&2
echo "cleanup-state: unknown action $action" >&2
exit 1
;;
esac
done

case "$action" in
pre-invariant)
# If the root user has a systemd --user instance then ask it to reload.
# This prevents tests from leaking user-session services that stay in
# memory but are not present on disk, or have been modified on disk, as is
# common with tests that use snaps with user services _or_ with tests that
# cause installation of the snapd.session-agent.service unit via re-exec
# machinery.
#
# This is done AHEAD of the invariant checks as it is very widespread
# and fixing it in each test is not a priority right now.
#
# Note that similar treatment is not required for the "test" user as
# correct usage of tests.session ensures that the session and all the
# processes of the "test" user are terminated.
if pgrep -u root --full "systemd --user"; then
systemctl --user daemon-reload
# Following that, check if there's a snapd.session-agent.socket and
# if one exists stop it and then start it, ignoring errors. If the
# unit was removed, stopping it clears it from memory. This is
# different from restarting the unit, which doesn't do anything if
# the unit on disk is gone.
if systemctl --user is-active snapd.session-agent.socket; then
systemctl --user stop snapd.session-agent.socket
fi
# XXX: if there's a way to check if an unit exists but is stopped,
# use it here to avoid starting a non-existing unit.
systemctl --user start snapd.session-agent.socket || true
}

# Do the same for root's D-Bus session bus.
# This will stop the bus and any clients that
# may be connected to it.
if systemctl --user is-active dbus.socket; then
systemctl --user stop dbus.socket
fi
systemctl --user start dbus.socket || true
fi
;;
post-invariant)
true
;;
*)
echo "cleanup-state: unknown action $action" >&2
exit 1
;;
esac
main "$@"
42 changes: 23 additions & 19 deletions tests/lib/tools/fs-state
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,28 @@ restore_file() {
fi
}

if [ $# -eq 0 ]; then
show_help
exit 0
fi
main() {
if [ $# -eq 0 ]; then
show_help
exit 0
fi

action=
while [ $# -gt 0 ]; do
case "$1" in
-h|--help|'')
show_help
exit 0
;;
*)
action=$(echo "$1" | tr '-' '_')
shift
break
;;
esac
done

action=
while [ $# -gt 0 ]; do
case "$1" in
-h|--help|'')
show_help
exit 0
;;
*)
action=$(echo "$1" | tr '-' '_')
shift
break
;;
esac
done
"$action" "$@"
}

"$action" "$@"
main "$@"
54 changes: 29 additions & 25 deletions tests/lib/tools/journal-state
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,35 @@ _sync_log(){
journalctl --sync || true
}

if [ $# -eq 0 ]; then
show_help
exit 0
fi
main() {
if [ $# -eq 0 ]; then
show_help
exit 0
fi

subcommand=$1
action=
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
show_help
exit 0
;;
*)
action=$(echo "$subcommand" | tr '-' '_')
shift
break
;;
esac
done
subcommand=$1
action=
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
show_help
exit 0
;;
*)
action=$(echo "$subcommand" | tr '-' '_')
shift
break
;;
esac
done

if [ -z "$(declare -f "$action")" ]; then
echo "journal-state: no such command $subcommand" >&2
show_help
exit 1
fi
if [ -z "$(declare -f "$action")" ]; then
echo "journal-state: no such command $subcommand" >&2
show_help
exit 1
fi

"$action" "$@"
}

"$action" "$@"
main "$@"
89 changes: 52 additions & 37 deletions tests/lib/tools/lxd-state
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
#!/bin/sh -e
case "${1:-}" in
-h|--help|'')
echo "usage: lxd-state [-h] {undo-mount-changes} ..."
;;
undo-mount-changes)
# Vanilla systems have /sys/fs/cgroup/cpuset without clone_children option.
# Using LXD to create a container enables this option, as can be seen here:
#
# -37 32 0:32 / /sys/fs/cgroup/cpuset rw,nosuid,nodev,noexec,relatime shared:15 - cgroup cgroup rw,cpuset
# +37 32 0:32 / /sys/fs/cgroup/cpuset rw,nosuid,nodev,noexec,relatime shared:15 - cgroup cgroup rw,cpuset,clone_children
#
# To restore vanilla state, disable the option now.
if [ "$(mountinfo.query /sys/fs/cgroup/cpuset .fs_type)" = cgroup ]; then
echo 0 > /sys/fs/cgroup/cpuset/cgroup.clone_children
fi
#!/bin/bash -e

# Vanilla system have /sys/fs/cgroup/unified mounted with the nsdelegate
# option which is available since kernel 4.13 Using LXD to create a
# container disables this options, as can be seen here:
#
# -32 31 0:27 / /sys/fs/cgroup/unified rw,nosuid,nodev,noexec,relatime shared:10 - cgroup2 cgroup rw,nsdelegate
# +32 31 0:27 / /sys/fs/cgroup/unified rw,nosuid,nodev,noexec,relatime shared:10 - cgroup2 cgroup rw
#
# To restore vanilla state, enable the option now, but only if the kernel supports that.
# https://lore.kernel.org/patchwork/patch/803265/
# https://github.com/systemd/systemd/commit/4095205ecccdfddb822ee8fdc44d11f2ded9be24
# The kernel version must be made compatible with the strict version
# comparison. I chose to cut at the "-" and take the stuff before it.
if [ "$(mountinfo.query /sys/fs/cgroup/unified .fs_type)" = cgroup2 ] && "$TESTSTOOLS"/version-compare --strict "$(uname -r | cut -d- -f 1)" -ge 4.13; then
mount -o remount,nsdelegate /sys/fs/cgroup/unified
fi
;;
*)
echo "lxd-state: unknown command $*" >&2
exit 1
;;
esac
show_help() {
echo "usage: lxd-state [-h] {undo-mount-changes} ..."
}

main() {
if [ $# -eq 0 ]; then
show_help
exit 0
fi

case "${1:-}" in
-h|--help)
show_help
exit 0
;;
undo-mount-changes)
# Vanilla systems have /sys/fs/cgroup/cpuset without clone_children option.
# Using LXD to create a container enables this option, as can be seen here:
#
# -37 32 0:32 / /sys/fs/cgroup/cpuset rw,nosuid,nodev,noexec,relatime shared:15 - cgroup cgroup rw,cpuset
# +37 32 0:32 / /sys/fs/cgroup/cpuset rw,nosuid,nodev,noexec,relatime shared:15 - cgroup cgroup rw,cpuset,clone_children
#
# To restore vanilla state, disable the option now.
if [ "$(mountinfo.query /sys/fs/cgroup/cpuset .fs_type)" = cgroup ]; then
echo 0 > /sys/fs/cgroup/cpuset/cgroup.clone_children
fi

# Vanilla system have /sys/fs/cgroup/unified mounted with the nsdelegate
# option which is available since kernel 4.13 Using LXD to create a
# container disables this options, as can be seen here:
#
# -32 31 0:27 / /sys/fs/cgroup/unified rw,nosuid,nodev,noexec,relatime shared:10 - cgroup2 cgroup rw,nsdelegate
# +32 31 0:27 / /sys/fs/cgroup/unified rw,nosuid,nodev,noexec,relatime shared:10 - cgroup2 cgroup rw
#
# To restore vanilla state, enable the option now, but only if the kernel supports that.
# https://lore.kernel.org/patchwork/patch/803265/
# https://github.com/systemd/systemd/commit/4095205ecccdfddb822ee8fdc44d11f2ded9be24
# The kernel version must be made compatible with the strict version
# comparison. I chose to cut at the "-" and take the stuff before it.
if [ "$(mountinfo.query /sys/fs/cgroup/unified .fs_type)" = cgroup2 ] && "$TESTSTOOLS"/version-compare --strict "$(uname -r | cut -d- -f 1)" -ge 4.13; then
mount -o remount,nsdelegate /sys/fs/cgroup/unified
fi
;;
*)
echo "lxd-state: unknown command $*" >&2
exit 1
;;
esac
}

main "$@"
Loading

0 comments on commit b573acc

Please sign in to comment.