| 1 |
#!/bin/sh |
| 2 |
set -e |
| 3 |
|
| 4 |
. `dirname $0`/assert.sh |
| 5 |
|
| 6 |
ORIG_TZ=`readlink /etc/localtime | sed 's#^.*zoneinfo/##'` |
| 7 |
echo "original tz: $ORIG_TZ" |
| 8 |
|
| 9 |
echo 'timedatectl works' |
| 10 |
assert_in "Local time:" "`timedatectl --no-pager`" |
| 11 |
|
| 12 |
echo 'change timezone' |
| 13 |
assert_eq "`timedatectl --no-pager set-timezone Europe/Moscow 2>&1`" "" |
| 14 |
assert_eq "`readlink /etc/localtime | sed 's#^.*zoneinfo/##'`" "Europe/Moscow" |
| 15 |
assert_in "Time.*zone: Europe/Moscow (MSK, +" "`timedatectl --no-pager`" |
| 16 |
|
| 17 |
echo 'reset timezone to original' |
| 18 |
assert_eq "`timedatectl --no-pager set-timezone $ORIG_TZ 2>&1`" "" |
| 19 |
assert_eq "`readlink /etc/localtime | sed 's#^.*zoneinfo/##'`" "$ORIG_TZ" |
| 20 |
|
| 21 |
# test setting UTC vs. LOCAL in /etc/adjtime |
| 22 |
if [ -e /etc/adjtime ]; then |
| 23 |
ORIG_ADJTIME=`cat /etc/adjtime` |
| 24 |
trap "echo '$ORIG_ADJTIME' > /etc/adjtime" EXIT INT QUIT PIPE |
| 25 |
else |
| 26 |
trap "rm -f /etc/adjtime" EXIT INT QUIT PIPE |
| 27 |
fi |
| 28 |
|
| 29 |
echo 'no adjtime file' |
| 30 |
rm -f /etc/adjtime |
| 31 |
timedatectl set-local-rtc 0 |
| 32 |
assert_true '[ ! -e /etc/adjtime ]' |
| 33 |
timedatectl set-local-rtc 1 |
| 34 |
assert_eq "`cat /etc/adjtime`" "0.0 0 0 |
| 35 |
0 |
| 36 |
LOCAL" |
| 37 |
timedatectl set-local-rtc 0 |
| 38 |
assert_true '[ ! -e /etc/adjtime ]' |
| 39 |
|
| 40 |
echo 'UTC set in adjtime file' |
| 41 |
printf '0.0 0 0\n0\nUTC\n' > /etc/adjtime |
| 42 |
timedatectl set-local-rtc 0 |
| 43 |
assert_eq "`cat /etc/adjtime`" "0.0 0 0 |
| 44 |
0 |
| 45 |
UTC" |
| 46 |
timedatectl set-local-rtc 1 |
| 47 |
assert_eq "`cat /etc/adjtime`" "0.0 0 0 |
| 48 |
0 |
| 49 |
LOCAL" |
| 50 |
|
| 51 |
echo 'non-zero values in adjtime file' |
| 52 |
printf '0.1 123 0\n0\nLOCAL\n' > /etc/adjtime |
| 53 |
timedatectl set-local-rtc 0 |
| 54 |
assert_eq "`cat /etc/adjtime`" "0.1 123 0 |
| 55 |
0 |
| 56 |
UTC" |
| 57 |
timedatectl set-local-rtc 1 |
| 58 |
assert_eq "`cat /etc/adjtime`" "0.1 123 0 |
| 59 |
0 |
| 60 |
LOCAL" |
| 61 |
|
| 62 |
echo 'fourth line adjtime file' |
| 63 |
printf '0.0 0 0\n0\nLOCAL\nsomethingelse\n' > /etc/adjtime |
| 64 |
timedatectl set-local-rtc 0 |
| 65 |
assert_eq "`cat /etc/adjtime`" "0.0 0 0 |
| 66 |
0 |
| 67 |
UTC |
| 68 |
somethingelse" |
| 69 |
timedatectl set-local-rtc 1 |
| 70 |
assert_eq "`cat /etc/adjtime`" "0.0 0 0 |
| 71 |
0 |
| 72 |
LOCAL |
| 73 |
somethingelse" |
| 74 |
|
| 75 |
echo 'no final newline in adjtime file' |
| 76 |
printf '0.0 0 0\n0\nUTC' > /etc/adjtime |
| 77 |
timedatectl set-local-rtc 0 |
| 78 |
assert_true '[ ! -e /etc/adjtime ]' |
| 79 |
printf '0.0 0 0\n0\nUTC' > /etc/adjtime |
| 80 |
timedatectl set-local-rtc 1 |
| 81 |
assert_eq "`cat /etc/adjtime`" "0.0 0 0 |
| 82 |
0 |
| 83 |
LOCAL" |
| 84 |
|
| 85 |
echo 'only one line in adjtime file' |
| 86 |
printf '0.0 0 0\n' > /etc/adjtime |
| 87 |
timedatectl set-local-rtc 0 |
| 88 |
assert_true '[ ! -e /etc/adjtime ]' |
| 89 |
printf '0.0 0 0\n' > /etc/adjtime |
| 90 |
timedatectl set-local-rtc 1 |
| 91 |
assert_eq "`cat /etc/adjtime`" "0.0 0 0 |
| 92 |
0 |
| 93 |
LOCAL" |
| 94 |
|
| 95 |
echo 'only one line in adjtime file, no final newline' |
| 96 |
printf '0.0 0 0' > /etc/adjtime |
| 97 |
timedatectl set-local-rtc 0 |
| 98 |
assert_true '[ ! -e /etc/adjtime ]' |
| 99 |
printf '0.0 0 0' > /etc/adjtime |
| 100 |
timedatectl set-local-rtc 1 |
| 101 |
assert_eq "`cat /etc/adjtime`" "0.0 0 0 |
| 102 |
0 |
| 103 |
LOCAL" |
| 104 |
|
| 105 |
echo 'only two lines in adjtime file' |
| 106 |
printf '0.0 0 0\n0\n' > /etc/adjtime |
| 107 |
timedatectl set-local-rtc 0 |
| 108 |
assert_true '[ ! -e /etc/adjtime ]' |
| 109 |
printf '0.0 0 0\n0\n' > /etc/adjtime |
| 110 |
timedatectl set-local-rtc 1 |
| 111 |
assert_eq "`cat /etc/adjtime`" "0.0 0 0 |
| 112 |
0 |
| 113 |
LOCAL" |
| 114 |
|
| 115 |
|
| 116 |
echo 'only two lines in adjtime file, no final newline' |
| 117 |
printf '0.0 0 0\n0' > /etc/adjtime |
| 118 |
timedatectl set-local-rtc 0 |
| 119 |
assert_true '[ ! -e /etc/adjtime ]' |
| 120 |
printf '0.0 0 0\n0' > /etc/adjtime |
| 121 |
timedatectl set-local-rtc 1 |
| 122 |
assert_eq "`cat /etc/adjtime`" "0.0 0 0 |
| 123 |
0 |
| 124 |
LOCAL" |
| 125 |
|
| 126 |
echo 'unknown value in 3rd line of adjtime file' |
| 127 |
printf '0.0 0 0\n0\nFOO\n' > /etc/adjtime |
| 128 |
timedatectl set-local-rtc 0 |
| 129 |
assert_true '[ ! -e /etc/adjtime ]' |
| 130 |
printf '0.0 0 0\n0\nFOO\n' > /etc/adjtime |
| 131 |
timedatectl set-local-rtc 1 |
| 132 |
assert_eq "`cat /etc/adjtime`" "0.0 0 0 |
| 133 |
0 |
| 134 |
LOCAL" |
| 135 |
|
| 136 |
# timesyncd has ConditionVirtualization=!container by default; drop/mock that for testing |
| 137 |
if systemd-detect-virt --container --quiet; then |
| 138 |
systemctl disable --quiet --now systemd-timesyncd |
| 139 |
mkdir -p /run/systemd/system/systemd-timesyncd.service.d |
| 140 |
printf '[Unit]\nConditionVirtualization=\n[Service]\nType=simple\nAmbientCapabilities=\nExecStart=\nExecStart=/bin/sleep infinity' > /run/systemd/system/systemd-timesyncd.service.d/container.conf |
| 141 |
systemctl daemon-reload |
| 142 |
fi |
| 143 |
|
| 144 |
mon=$(mktemp -t dbusmon.XXXXXX) |
| 145 |
trap "rm -f $mon" EXIT INT QUIT PIPE |
| 146 |
|
| 147 |
assert_ntp() { |
| 148 |
V=$(busctl get-property org.freedesktop.timedate1 /org/freedesktop/timedate1 org.freedesktop.timedate1 NTP) |
| 149 |
assert_eq "$V" "b $1" |
| 150 |
} |
| 151 |
|
| 152 |
start_mon() { |
| 153 |
dbus-monitor --system "type='signal', member='PropertiesChanged', path='/org/freedesktop/timedate1'" > $mon & |
| 154 |
MONPID=$! |
| 155 |
} |
| 156 |
|
| 157 |
wait_mon() { |
| 158 |
for retry in $(seq 10); do |
| 159 |
grep -q "$1" $mon && break |
| 160 |
sleep 1 |
| 161 |
done |
| 162 |
assert_in "$2" "$(cat $mon)" |
| 163 |
kill $MONPID |
| 164 |
wait $MONPID 2>/dev/null || true |
| 165 |
} |
| 166 |
|
| 167 |
echo 'disable NTP' |
| 168 |
timedatectl set-ntp false |
| 169 |
while [ "$(systemctl --no-pager show systemd-timesyncd --property ActiveState)" != "ActiveState=inactive" ]; do sleep 1; done |
| 170 |
assert_ntp false |
| 171 |
assert_rc 3 systemctl is-active --quiet systemd-timesyncd |
| 172 |
|
| 173 |
echo 'enable NTP' |
| 174 |
start_mon |
| 175 |
timedatectl set-ntp true |
| 176 |
wait_mon "NTP" "boolean true" |
| 177 |
assert_ntp true |
| 178 |
while [ "$(systemctl --no-pager show systemd-timesyncd --property ActiveState)" != "ActiveState=active" ]; do sleep 1; done |
| 179 |
assert_rc 0 systemctl is-active --quiet systemd-timesyncd |
| 180 |
|
| 181 |
echo 're-disable NTP' |
| 182 |
start_mon |
| 183 |
timedatectl set-ntp false |
| 184 |
wait_mon "NTP" "boolean false" |
| 185 |
assert_ntp false |
| 186 |
assert_rc 3 systemctl is-active --quiet systemd-timesyncd |