From c986928affc59f5bbefae8c7af963eaf45fdc8c8 Mon Sep 17 00:00:00 2001 From: Julien Olivain Date: Sun, 9 Jun 2024 19:21:48 +0200 Subject: [PATCH] support/testing: add ntp runtime test Signed-off-by: Julien Olivain Signed-off-by: Arnout Vandecappelle --- DEVELOPERS | 2 + support/testing/tests/package/test_ntp.py | 86 +++++++++++++++++++ .../test_ntp/rootfs-overlay/etc/ntp.conf | 10 +++ 3 files changed, 98 insertions(+) create mode 100644 support/testing/tests/package/test_ntp.py create mode 100644 support/testing/tests/package/test_ntp/rootfs-overlay/etc/ntp.conf diff --git a/DEVELOPERS b/DEVELOPERS index 8bfd36f1d8..8a5cceba78 100644 --- a/DEVELOPERS +++ b/DEVELOPERS @@ -1869,6 +1869,8 @@ F: support/testing/tests/package/test_netsnmp/ F: support/testing/tests/package/test_nftables.py F: support/testing/tests/package/test_nftables/ F: support/testing/tests/package/test_ngrep.py +F: support/testing/tests/package/test_ntp.py +F: support/testing/tests/package/test_ntp/ F: support/testing/tests/package/test_numactl.py F: support/testing/tests/package/test_numactl/ F: support/testing/tests/package/test_octave.py diff --git a/support/testing/tests/package/test_ntp.py b/support/testing/tests/package/test_ntp.py new file mode 100644 index 0000000000..13fff6419b --- /dev/null +++ b/support/testing/tests/package/test_ntp.py @@ -0,0 +1,86 @@ +import os +import re +import time + +import infra.basetest + + +class TestNtp(infra.basetest.BRTest): + rootfs_overlay = \ + infra.filepath("tests/package/test_ntp/rootfs-overlay") + config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \ + f""" + BR2_PACKAGE_NTP=y + BR2_PACKAGE_NTP_NTPD=y + BR2_PACKAGE_NTP_NTPQ=y + BR2_ROOTFS_OVERLAY="{rootfs_overlay}" + BR2_TARGET_ROOTFS_CPIO=y + # BR2_TARGET_ROOTFS_TAR is not set + """ + + def dict_from_ntpq_output(self, output): + d = {} + for line in output: + if ':' not in line: + continue + fields = re.split(r":", line, maxsplit=2) + name = fields[0].strip() + value = fields[1].strip() + d[name] = value + return d + + def test_run(self): + cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio") + self.emulator.boot(arch="armv5", + kernel="builtin", + options=["-initrd", cpio_file]) + self.emulator.login() + + # Check our binaries can execute. + self.assertRunOk("ntpd --version") + self.assertRunOk("ntpq --version") + + # The ntp daemon is expected to be started from init startup + # scripts, for the Buildroot package recipe. We wait a bit + # here to let the daemon settle. The next test step checks for + # the local peer to be the system peer (by checking the + # '*'). If querying the peers too soon after startup the peer + # will not be marked as such. + time.sleep(3 * self.timeout_multiplier) + + # We query the ntp daemon peers. From our test configuration + # file, we should have exactly one. + out, ret = self.emulator.run("ntpq --peers") + self.assertEqual(ret, 0) + # ntpq --peers produces two lines of headers. So we check we + # have at least 3 lines of output. + self.assertGreaterEqual(len(out), 3) + # We check we see our undisciplined local clock and it's the + # system peer. + self.assertTrue(out[2].startswith("*LOCAL(0)")) + + # We query the refid variable. We expect to see our + # undisciplined local clock. + out, ret = self.emulator.run("ntpq -c 'readvar 0 refid'") + self.assertEqual(ret, 0) + self.assertEqual(out[0], "refid=LOCAL(0)") + + # We query the ntp system info. We check the reference ID is + # the same as in the test configuration file. + out, ret = self.emulator.run("ntpq -c sysinfo") + self.assertEqual(ret, 0) + sysinfo = self.dict_from_ntpq_output(out) + refid = "reference ID" + self.assertIn(refid, sysinfo) + self.assertEqual(sysinfo[refid], "127.127.1.0") + + # Finally, we query the ntp system statistics. We check we can + # see some uptime. We waited a bit at the beginning of this + # test, plus the few queries we previously did should have + # accumulated some uptime. + out, ret = self.emulator.run("ntpq -c sysstats") + self.assertEqual(ret, 0) + sysstats = self.dict_from_ntpq_output(out) + up = "uptime" + self.assertIn(up, sysstats) + self.assertGreater(int(sysstats[up]), 0) diff --git a/support/testing/tests/package/test_ntp/rootfs-overlay/etc/ntp.conf b/support/testing/tests/package/test_ntp/rootfs-overlay/etc/ntp.conf new file mode 100644 index 0000000000..2ead3deb6a --- /dev/null +++ b/support/testing/tests/package/test_ntp/rootfs-overlay/etc/ntp.conf @@ -0,0 +1,10 @@ +# Set an undisciplined local clock for testing without the need of +# network connectivity. +server 127.127.1.0 iburst prefer +fudge 127.127.1.0 stratum 10 + +# Keep standard access control setup. The test is doing local queries +# from 127.0.0.1. +restrict default nomodify nopeer noquery limited kod +restrict 127.0.0.1 +restrict [::1]