From e3a6c05a58a2451e70975d8fabf644c56603c73d Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Tue, 10 Mar 2020 20:26:53 -0700 Subject: [PATCH] timedatectl fixes, tests, and fixtures for issue #42 --- docs/parsers/timedatectl.md | 18 +++++---- jc/parsers/timedatectl.py | 21 +++++----- tests/fixtures/centos-7.7/timedatectl.json | 1 + tests/fixtures/ubuntu-18.04/timedatectl.json | 1 + tests/test_timedatectl.py | 40 ++++++++++++++++++++ 5 files changed, 64 insertions(+), 17 deletions(-) create mode 100644 tests/fixtures/centos-7.7/timedatectl.json create mode 100644 tests/fixtures/ubuntu-18.04/timedatectl.json create mode 100644 tests/test_timedatectl.py diff --git a/docs/parsers/timedatectl.md b/docs/parsers/timedatectl.md index 7f8341bd..1f7f86b2 100644 --- a/docs/parsers/timedatectl.md +++ b/docs/parsers/timedatectl.md @@ -56,14 +56,16 @@ Returns: Dictionary. Structured data with the following schema: { - "local_time": string, - "universal_time": string, - "rtc_time": string, - "time_zone": string, - "ntp_enabled": boolean, - "ntp_synchronized": boolean, - "rtc_in_local_tz": boolean, - "dst_active": boolean + "local_time": string, + "universal_time": string, + "rtc_time": string, + "time_zone": string, + "ntp_enabled": boolean, + "ntp_synchronized": boolean, + "system_clock_synchronized": boolean, + "systemd-timesyncd.service_active": boolean, + "rtc_in_local_tz": boolean, + "dst_active": boolean } ## parse diff --git a/jc/parsers/timedatectl.py b/jc/parsers/timedatectl.py index 5c796d78..256e0c73 100644 --- a/jc/parsers/timedatectl.py +++ b/jc/parsers/timedatectl.py @@ -65,18 +65,21 @@ def process(proc_data): Dictionary. Structured data with the following schema: { - "local_time": string, - "universal_time": string, - "rtc_time": string, - "time_zone": string, - "ntp_enabled": boolean, - "ntp_synchronized": boolean, - "rtc_in_local_tz": boolean, - "dst_active": boolean + "local_time": string, + "universal_time": string, + "rtc_time": string, + "time_zone": string, + "ntp_enabled": boolean, + "ntp_synchronized": boolean, + "system_clock_synchronized": boolean, + "systemd-timesyncd.service_active": boolean, + "rtc_in_local_tz": boolean, + "dst_active": boolean } """ # boolean changes - bool_list = ['ntp_enabled', 'ntp_synchronized', 'rtc_in_local_tz', 'dst_active'] + bool_list = ['ntp_enabled', 'ntp_synchronized', 'rtc_in_local_tz', 'dst_active', + 'system_clock_synchronized', 'systemd-timesyncd.service_active'] for key in proc_data: if key in bool_list: try: diff --git a/tests/fixtures/centos-7.7/timedatectl.json b/tests/fixtures/centos-7.7/timedatectl.json new file mode 100644 index 00000000..11d2bbce --- /dev/null +++ b/tests/fixtures/centos-7.7/timedatectl.json @@ -0,0 +1 @@ +{"local_time": "Tue 2020-03-10 17:53:21 PDT", "universal_time": "Wed 2020-03-11 00:53:21 UTC", "rtc_time": "Wed 2020-03-11 00:53:21", "time_zone": "America/Los_Angeles (PDT, -0700)", "ntp_enabled": true, "ntp_synchronized": true, "rtc_in_local_tz": false, "dst_active": true} diff --git a/tests/fixtures/ubuntu-18.04/timedatectl.json b/tests/fixtures/ubuntu-18.04/timedatectl.json new file mode 100644 index 00000000..4cc48998 --- /dev/null +++ b/tests/fixtures/ubuntu-18.04/timedatectl.json @@ -0,0 +1 @@ +{"local_time": "Wed 2020-03-11 00:51:11 UTC", "universal_time": "Wed 2020-03-11 00:51:11 UTC", "rtc_time": "Wed 2020-03-11 00:51:11", "time_zone": "Etc/UTC (UTC, +0000)", "system_clock_synchronized": true, "systemd-timesyncd.service_active": true, "rtc_in_local_tz": false} diff --git a/tests/test_timedatectl.py b/tests/test_timedatectl.py new file mode 100644 index 00000000..52e086e2 --- /dev/null +++ b/tests/test_timedatectl.py @@ -0,0 +1,40 @@ +import os +import unittest +import json +import jc.parsers.timedatectl + +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) + + +class MyTests(unittest.TestCase): + + def setUp(self): + # input + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/timedatectl.out'), 'r', encoding='utf-8') as f: + self.centos_7_7_timedatectl = f.read() + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/timedatectl.out'), 'r', encoding='utf-8') as f: + self.ubuntu_18_4_timedatectl = f.read() + + # output + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/timedatectl.json'), 'r', encoding='utf-8') as f: + self.centos_7_7_timedatectl_json = json.loads(f.read()) + + with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/timedatectl.json'), 'r', encoding='utf-8') as f: + self.ubuntu_18_4_timedatectl_json = json.loads(f.read()) + + def test_timedatectl_centos_7_7(self): + """ + Test 'timedatectl' on Centos 7.7 + """ + self.assertEqual(jc.parsers.timedatectl.parse(self.centos_7_7_timedatectl, quiet=True), self.centos_7_7_timedatectl_json) + + def test_timedatectl_ubuntu_18_4(self): + """ + Test 'timedatectl' on Ubuntu 18.4 + """ + self.assertEqual(jc.parsers.timedatectl.parse(self.ubuntu_18_4_timedatectl, quiet=True), self.ubuntu_18_4_timedatectl_json) + + +if __name__ == '__main__': + unittest.main()