594167
From 014a660c937c1c5ebfc05667466a16b44e446c60 Mon Sep 17 00:00:00 2001
594167
From: Yu Watanabe <watanabe.yu+github@gmail.com>
594167
Date: Fri, 10 Jun 2022 13:55:54 +0900
594167
Subject: [PATCH] test: import timedated test from debian/ubuntu test suite
594167
594167
(cherry picked from commit 759ed0a2533da8840dea315d07f92e6bb0272cdd)
594167
594167
Related: #2087652
594167
---
594167
 test/TEST-45-TIMEDATE/Makefile  |   1 +
594167
 test/TEST-45-TIMEDATE/test.sh   |  10 ++
594167
 test/units/assert.sh            |  44 ++++++
594167
 test/units/testsuite-45.service |  10 ++
594167
 test/units/testsuite-45.sh      | 246 ++++++++++++++++++++++++++++++++
594167
 5 files changed, 311 insertions(+)
594167
 create mode 120000 test/TEST-45-TIMEDATE/Makefile
594167
 create mode 100755 test/TEST-45-TIMEDATE/test.sh
594167
 create mode 100644 test/units/assert.sh
594167
 create mode 100644 test/units/testsuite-45.service
594167
 create mode 100755 test/units/testsuite-45.sh
594167
594167
diff --git a/test/TEST-45-TIMEDATE/Makefile b/test/TEST-45-TIMEDATE/Makefile
594167
new file mode 120000
594167
index 0000000000..e9f93b1104
594167
--- /dev/null
594167
+++ b/test/TEST-45-TIMEDATE/Makefile
594167
@@ -0,0 +1 @@
594167
+../TEST-01-BASIC/Makefile
594167
\ No newline at end of file
594167
diff --git a/test/TEST-45-TIMEDATE/test.sh b/test/TEST-45-TIMEDATE/test.sh
594167
new file mode 100755
594167
index 0000000000..27edf4a3b9
594167
--- /dev/null
594167
+++ b/test/TEST-45-TIMEDATE/test.sh
594167
@@ -0,0 +1,10 @@
594167
+#!/usr/bin/env bash
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+set -e
594167
+
594167
+TEST_DESCRIPTION="test timedated"
594167
+
594167
+# shellcheck source=test/test-functions
594167
+. "${TEST_BASE_DIR:?}/test-functions"
594167
+
594167
+do_test "$@"
594167
diff --git a/test/units/assert.sh b/test/units/assert.sh
594167
new file mode 100644
594167
index 0000000000..db67dad268
594167
--- /dev/null
594167
+++ b/test/units/assert.sh
594167
@@ -0,0 +1,44 @@
594167
+#!/usr/bin/env bash
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+
594167
+# utility functions for shell tests
594167
+
594167
+assert_true() {
594167
+    local rc
594167
+
594167
+    set +e
594167
+    "$@"
594167
+    rc=$?
594167
+    set -e
594167
+    if [[ "$rc" != "0" ]]; then
594167
+        echo "FAIL: command '$*' failed with exit code $rc" >&2
594167
+        exit 1
594167
+    fi
594167
+}
594167
+
594167
+
594167
+assert_eq() {
594167
+    if [[ "$1" != "$2" ]]; then
594167
+        echo "FAIL: expected: '$2' actual: '$1'" >&2
594167
+        exit 1
594167
+    fi
594167
+}
594167
+
594167
+assert_in() {
594167
+    if ! echo "$2" | grep -q "$1"; then
594167
+        echo "FAIL: '$1' not found in:" >&2
594167
+        echo "$2" >&2
594167
+        exit 1
594167
+    fi
594167
+}
594167
+
594167
+assert_rc() {
594167
+    local exp=$1
594167
+    local rc
594167
+    shift
594167
+    set +e
594167
+    "$@"
594167
+    rc=$?
594167
+    set -e
594167
+    assert_eq "$rc" "$exp"
594167
+}
594167
diff --git a/test/units/testsuite-45.service b/test/units/testsuite-45.service
594167
new file mode 100644
594167
index 0000000000..79c0a6f117
594167
--- /dev/null
594167
+++ b/test/units/testsuite-45.service
594167
@@ -0,0 +1,10 @@
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+[Unit]
594167
+Description=TEST-45-TIMEDATE
594167
+
594167
+[Service]
594167
+ExecStartPre=rm -f /failed /testok
594167
+ExecStart=/usr/lib/systemd/tests/testdata/units/%N.sh
594167
+StandardOutput=journal+console
594167
+StandardError=journal+console
594167
+Type=oneshot
594167
diff --git a/test/units/testsuite-45.sh b/test/units/testsuite-45.sh
594167
new file mode 100755
594167
index 0000000000..ac7860dccd
594167
--- /dev/null
594167
+++ b/test/units/testsuite-45.sh
594167
@@ -0,0 +1,246 @@
594167
+#!/usr/bin/env bash
594167
+# SPDX-License-Identifier: LGPL-2.1-or-later
594167
+
594167
+set -eux
594167
+set -o pipefail
594167
+
594167
+# shellcheck source=test/units/assert.sh
594167
+. "$(dirname "$0")"/assert.sh
594167
+
594167
+test_timezone() {
594167
+    local ORIG_TZ=
594167
+
594167
+    if [[ -L /etc/localtime ]]; then
594167
+        ORIG_TZ=$(readlink /etc/localtime | sed 's#^.*zoneinfo/##')
594167
+        echo "original tz: $ORIG_TZ"
594167
+    fi
594167
+
594167
+    echo 'timedatectl works'
594167
+    assert_in "Local time:" "$(timedatectl --no-pager)"
594167
+
594167
+    echo 'change timezone'
594167
+    assert_eq "$(timedatectl --no-pager set-timezone Europe/Kiev 2>&1)" ""
594167
+    assert_eq "$(readlink /etc/localtime | sed 's#^.*zoneinfo/##')" "Europe/Kiev"
594167
+    assert_in "Time.*zone: Europe/Kiev (EEST, +" "$(timedatectl --no-pager)"
594167
+
594167
+    if [[ -n "$ORIG_TZ" ]]; then
594167
+        echo 'reset timezone to original'
594167
+        assert_eq "$(timedatectl --no-pager set-timezone "$ORIG_TZ" 2>&1)" ""
594167
+        assert_eq "$(readlink /etc/localtime | sed 's#^.*zoneinfo/##')" "$ORIG_TZ"
594167
+    fi
594167
+}
594167
+
594167
+restore_adjtime() {
594167
+    if [[ -e /etc/adjtime.bak ]]; then
594167
+        mv /etc/adjtime.bak /etc/adjtime
594167
+    else
594167
+        rm /etc/adjtime
594167
+    fi
594167
+}
594167
+
594167
+check_adjtime_not_exist() {
594167
+    if [[ -e /etc/adjtime ]]; then
594167
+        echo "/etc/adjtime unexpectedly exists." >&2
594167
+        exit 1
594167
+    fi
594167
+}
594167
+
594167
+test_adjtime() {
594167
+    # test setting UTC vs. LOCAL in /etc/adjtime
594167
+    if [[ -e /etc/adjtime ]]; then
594167
+        mv /etc/adjtime /etc/adjtime.bak
594167
+    fi
594167
+
594167
+    trap restore_adjtime EXIT
594167
+
594167
+    echo 'no adjtime file'
594167
+    rm -f /etc/adjtime
594167
+    timedatectl set-local-rtc 0
594167
+    check_adjtime_not_exist
594167
+    timedatectl set-local-rtc 1
594167
+    assert_eq "$(cat /etc/adjtime)" "0.0 0 0
594167
+0
594167
+LOCAL"
594167
+    timedatectl set-local-rtc 0
594167
+    check_adjtime_not_exist
594167
+
594167
+    echo 'UTC set in adjtime file'
594167
+    printf '0.0 0 0\n0\nUTC\n' > /etc/adjtime
594167
+    timedatectl set-local-rtc 0
594167
+    assert_eq "$(cat /etc/adjtime)" "0.0 0 0
594167
+0
594167
+UTC"
594167
+    timedatectl set-local-rtc 1
594167
+    assert_eq "$(cat /etc/adjtime)" "0.0 0 0
594167
+0
594167
+LOCAL"
594167
+
594167
+    echo 'non-zero values in adjtime file'
594167
+    printf '0.1 123 0\n0\nLOCAL\n' > /etc/adjtime
594167
+    timedatectl set-local-rtc 0
594167
+    assert_eq "$(cat /etc/adjtime)" "0.1 123 0
594167
+0
594167
+UTC"
594167
+    timedatectl set-local-rtc 1
594167
+    assert_eq "$(cat /etc/adjtime)" "0.1 123 0
594167
+0
594167
+LOCAL"
594167
+
594167
+    echo 'fourth line adjtime file'
594167
+    printf '0.0 0 0\n0\nLOCAL\nsomethingelse\n' > /etc/adjtime
594167
+    timedatectl set-local-rtc 0
594167
+    assert_eq "$(cat /etc/adjtime)" "0.0 0 0
594167
+0
594167
+UTC
594167
+somethingelse"
594167
+    timedatectl set-local-rtc 1
594167
+    assert_eq "$(cat /etc/adjtime)" "0.0 0 0
594167
+0
594167
+LOCAL
594167
+somethingelse"
594167
+
594167
+    echo 'no final newline in adjtime file'
594167
+    printf '0.0 0 0\n0\nUTC' > /etc/adjtime
594167
+    timedatectl set-local-rtc 0
594167
+    check_adjtime_not_exist
594167
+    printf '0.0 0 0\n0\nUTC' > /etc/adjtime
594167
+    timedatectl set-local-rtc 1
594167
+    assert_eq "$(cat /etc/adjtime)" "0.0 0 0
594167
+0
594167
+LOCAL"
594167
+
594167
+    echo 'only one line in adjtime file'
594167
+    printf '0.0 0 0\n' > /etc/adjtime
594167
+    timedatectl set-local-rtc 0
594167
+    check_adjtime_not_exist
594167
+    printf '0.0 0 0\n' > /etc/adjtime
594167
+    timedatectl set-local-rtc 1
594167
+    assert_eq "$(cat /etc/adjtime)" "0.0 0 0
594167
+0
594167
+LOCAL"
594167
+
594167
+    echo 'only one line in adjtime file, no final newline'
594167
+    printf '0.0 0 0' > /etc/adjtime
594167
+    timedatectl set-local-rtc 0
594167
+    check_adjtime_not_exist
594167
+    printf '0.0 0 0' > /etc/adjtime
594167
+    timedatectl set-local-rtc 1
594167
+    assert_eq "$(cat /etc/adjtime)" "0.0 0 0
594167
+0
594167
+LOCAL"
594167
+
594167
+    echo 'only two lines in adjtime file'
594167
+    printf '0.0 0 0\n0\n' > /etc/adjtime
594167
+    timedatectl set-local-rtc 0
594167
+    check_adjtime_not_exist
594167
+    printf '0.0 0 0\n0\n' > /etc/adjtime
594167
+    timedatectl set-local-rtc 1
594167
+    assert_eq "$(cat /etc/adjtime)" "0.0 0 0
594167
+0
594167
+LOCAL"
594167
+
594167
+    echo 'only two lines in adjtime file, no final newline'
594167
+    printf '0.0 0 0\n0' > /etc/adjtime
594167
+    timedatectl set-local-rtc 0
594167
+    check_adjtime_not_exist
594167
+    printf '0.0 0 0\n0' > /etc/adjtime
594167
+    timedatectl set-local-rtc 1
594167
+    assert_eq "$(cat /etc/adjtime)" "0.0 0 0
594167
+0
594167
+LOCAL"
594167
+
594167
+    echo 'unknown value in 3rd line of adjtime file'
594167
+    printf '0.0 0 0\n0\nFOO\n' > /etc/adjtime
594167
+    timedatectl set-local-rtc 0
594167
+    check_adjtime_not_exist
594167
+    printf '0.0 0 0\n0\nFOO\n' > /etc/adjtime
594167
+    timedatectl set-local-rtc 1
594167
+    assert_eq "$(cat /etc/adjtime)" "0.0 0 0
594167
+0
594167
+LOCAL"
594167
+
594167
+    restore_adjtime
594167
+    trap - EXIT
594167
+}
594167
+
594167
+assert_ntp() {
594167
+    assert_eq "$(busctl get-property org.freedesktop.timedate1 /org/freedesktop/timedate1 org.freedesktop.timedate1 NTP)" "b $1"
594167
+}
594167
+
594167
+start_mon() {
594167
+    busctl monitor --match="type='signal',sender=org.freedesktop.timedate1,member='PropertiesChanged',path=/org/freedesktop/timedate1" >"$mon" &
594167
+    MONPID=$!
594167
+}
594167
+
594167
+wait_mon() {
594167
+    for ((i=0;i<10;i++)); do
594167
+        if (( i != 0 )); then sleep 1; fi
594167
+        if grep -q "$1" "$mon"; then break; fi
594167
+    done
594167
+    assert_in "$2" "$(cat "$mon")"
594167
+    kill "$MONPID"
594167
+    wait "$MONPID" 2>/dev/null || true
594167
+}
594167
+
594167
+test_ntp() {
594167
+    # timesyncd has ConditionVirtualization=!container by default; drop/mock that for testing
594167
+    if systemd-detect-virt --container --quiet; then
594167
+        systemctl disable --quiet --now systemd-timesyncd
594167
+        mkdir -p /run/systemd/system/systemd-timesyncd.service.d
594167
+        cat >/run/systemd/system/systemd-timesyncd.service.d/container.conf <
594167
+[Unit]
594167
+ConditionVirtualization=
594167
+
594167
+[Service]
594167
+Type=simple
594167
+AmbientCapabilities=
594167
+ExecStart=
594167
+ExecStart=/bin/sleep infinity
594167
+EOF
594167
+        systemctl daemon-reload
594167
+    fi
594167
+
594167
+    mon=$(mktemp -t dbusmon.XXXXXX)
594167
+
594167
+    echo 'disable NTP'
594167
+    timedatectl set-ntp false
594167
+    for ((i=0;i<10;i++)); do
594167
+        if (( i != 0 )); then sleep 1; fi
594167
+        if [[ "$(systemctl --no-pager show systemd-timesyncd --property ActiveState)" == "ActiveState=inactive" ]]; then
594167
+            break;
594167
+        fi
594167
+    done
594167
+    assert_eq "$(systemctl --no-pager show systemd-timesyncd --property ActiveState)" "ActiveState=inactive"
594167
+    assert_ntp "false"
594167
+    assert_rc 3 systemctl is-active --quiet systemd-timesyncd
594167
+
594167
+    echo 'enable NTP'
594167
+    start_mon
594167
+    timedatectl set-ntp true
594167
+    wait_mon "NTP" "BOOLEAN true"
594167
+    assert_ntp "true"
594167
+    for ((i=0;i<10;i++)); do
594167
+        if (( i != 0 )); then sleep 1; fi
594167
+        if [[ "$(systemctl --no-pager show systemd-timesyncd --property ActiveState)" == "ActiveState=active" ]]; then
594167
+            break;
594167
+        fi
594167
+    done
594167
+    assert_eq "$(systemctl --no-pager show systemd-timesyncd --property ActiveState)" "ActiveState=active"
594167
+    assert_rc 0 systemctl is-active --quiet systemd-timesyncd
594167
+
594167
+    echo 're-disable NTP'
594167
+    start_mon
594167
+    timedatectl set-ntp false
594167
+    wait_mon "NTP" "BOOLEAN false"
594167
+    assert_ntp "false"
594167
+    assert_rc 3 systemctl is-active --quiet systemd-timesyncd
594167
+}
594167
+
594167
+: >/failed
594167
+
594167
+test_timezone
594167
+test_adjtime
594167
+test_ntp
594167
+
594167
+touch /testok
594167
+rm /failed