#!/bin/sh
set -ex

# Test various cases around LP: #2073284 and LP: #2073285

# One might expect this test to actually be a series of unit tests that resets
# the test environment every time, but that is expensive and I don't think it's
# worth it for what we're trying to achieve by testing, so these are
# deliberately squashed together into a single test run.

# Skip if systemd is not available
command -v systemctl >/dev/null || return 77
[ -d /run/systemd/system ] || return 77

units=$(dpkg -L sysstat|grep -E '/lib/systemd/system/sysstat.*\.(service|timer)$'|while read file; do echo $(basename "$file");done)

assert_all_units_enabled() {
    local acceptable=1
    for unit in $units; do
        status=$(systemctl is-enabled "$unit")
        case "$status" in
            enabled|static) ;;
            *)
                echo "Error: $unit is-enabled reports $status"
                acceptable=0
                ;;
        esac
    done
    [ "$acceptable" = "1" ] || exit 1
}

assert_all_units_conditions_met() {
    local acceptable=1
    for unit in $units; do
        # To test ConditionResult we must try to restart the unit, which is
        # generally harmless for testing sysstat units
        systemctl restart "$unit"
        status=$(systemctl show --property=ConditionResult "$unit")
        case "$status" in
            ConditionResult=yes)
                echo "Correct: $unit ConditionResult is $status"
		;;
            *)
                echo "Error: $unit ConditionResult is $status"
                acceptable=0
                ;;
        esac
    done
    [ "$acceptable" = "1" ] || exit 1
}

assert_all_services_conditions_unmet() {
    # Note: exceptionally this tests *services*, not *units* (so excluding
    # timers) since we only impose conditions on the units (otherwise a change
    # to the condition wouldn't apply immediately).
    local acceptable=1
    for unit in $units; do
        # Skip if not a service unit
        case "$unit" in
            *.service) ;;
            *) continue ;;
        esac
        # To test ConditionResult we must try to restart the unit, which is
        # generally harmless for testing sysstat units
        systemctl restart "$unit"
        status=$(systemctl show --property=ConditionResult "$unit")
        case "$status" in
            ConditionResult=no)
                echo "Correct: $unit ConditionResult is $status"
		;;
            *)
                echo "Error: $unit ConditionResult is $status"
                acceptable=0
                ;;
        esac
    done
    [ "$acceptable" = "1" ] || exit 1
}

# LP: #2073285: in Ubuntu, sysstat should be enabled by default
. /etc/default/sysstat
if [ "$ENABLED" = "true" ]; then
    echo "Correct: /etc/default/sysstat sets ENABLED=true"
else
    echo "Error: /etc/default/sysstat does not set ENABLED=true"
    exit 1
fi

# LP: #2073284: units' enablement state should match ENABLED=true status
assert_all_units_enabled

# LP: #2073284: all units should have conditions met
assert_all_units_conditions_met

# LP: #2073284: even after preset-all, units should remain enabled
echo "Disabling sysstat via /etc/default/sysstat"
sed -i s/true/false/ /etc/default/sysstat
echo "Running preset-all"
systemctl preset-all
assert_all_units_enabled

# ...however services should no longer have conditions met

# Note: exceptionally this tests *services*, not *units* (so excluding
# timers) since we only impose conditions on the units (otherwise a change
# to the condition wouldn't apply immediately).
assert_all_services_conditions_unmet
