File: test_timedated.py

package info (click to toggle)
python-dbusmock 0.38.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 788 kB
  • sloc: python: 6,855; sh: 69; makefile: 4
file content (77 lines) | stat: -rw-r--r-- 2,596 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# SPDX-License-Identifier: LGPL-3.0-or-later

__author__ = "Iain Lane"
__copyright__ = """
(c) 2013 Canonical Ltd.
(c) 2017 - 2022 Martin Pitt <martin@piware.de>
"""

import shutil
import subprocess
import sys
import unittest
from pathlib import Path

import dbusmock

# timedatectl keeps changing its CLI output
TIMEDATECTL_NTP_LABEL = "(NTP enabled|synchronized|systemd-timesyncd.service active)"

have_timedatectl = shutil.which("timedatectl")


@unittest.skipUnless(have_timedatectl, "timedatectl not installed")
@unittest.skipUnless(Path("/run/systemd/system").exists(), "/run/systemd/system does not exist")
class TestTimedated(dbusmock.DBusTestCase):
    """Test mocking timedated"""

    @classmethod
    def setUpClass(cls):
        cls.start_system_bus()
        cls.dbus_con = cls.get_dbus(True)

    def setUp(self):
        (self.p_mock, _) = self.spawn_server_template("timedated", {}, stdout=subprocess.PIPE)
        self.addCleanup(self.p_mock.wait)
        self.addCleanup(self.p_mock.terminate)
        self.addCleanup(self.p_mock.stdout.close)
        self.obj_timedated = self.dbus_con.get_object("org.freedesktop.timedate1", "/org/freedesktop/timedate1")

    def run_timedatectl(self):
        return subprocess.check_output(["timedatectl"], text=True)

    def test_default_timezone(self):
        out = self.run_timedatectl()
        # timedatectl doesn't get the timezone offset information over dbus so
        # we can't mock that.
        self.assertRegex(out, "Time *zone: Etc/Utc")

    def test_changing_timezone(self):
        self.obj_timedated.SetTimezone("Africa/Johannesburg", False)
        out = self.run_timedatectl()
        # timedatectl doesn't get the timezone offset information over dbus so
        # we can't mock that.
        self.assertRegex(out, "Time *zone: Africa/Johannesburg")

    def test_default_ntp(self):
        out = self.run_timedatectl()
        self.assertRegex(out, f"{TIMEDATECTL_NTP_LABEL}: yes")

    def test_changing_ntp(self):
        self.obj_timedated.SetNTP(False, False)
        out = self.run_timedatectl()
        self.assertRegex(out, f"{TIMEDATECTL_NTP_LABEL}: no")

    def test_default_local_rtc(self):
        out = self.run_timedatectl()
        self.assertRegex(out, "RTC in local TZ: no")

    def test_changing_local_rtc(self):
        self.obj_timedated.SetLocalRTC(True, False, False)
        out = self.run_timedatectl()
        self.assertRegex(out, "RTC in local TZ: yes")


if __name__ == "__main__":
    # avoid writing to stderr
    unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout))