test_ntp.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import os
  2. import re
  3. import time
  4. import infra.basetest
  5. class TestNtp(infra.basetest.BRTest):
  6. rootfs_overlay = \
  7. infra.filepath("tests/package/test_ntp/rootfs-overlay")
  8. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  9. f"""
  10. BR2_PACKAGE_NTP=y
  11. BR2_PACKAGE_NTP_NTPD=y
  12. BR2_PACKAGE_NTP_NTPQ=y
  13. BR2_ROOTFS_OVERLAY="{rootfs_overlay}"
  14. BR2_TARGET_ROOTFS_CPIO=y
  15. # BR2_TARGET_ROOTFS_TAR is not set
  16. """
  17. def dict_from_ntpq_output(self, output):
  18. d = {}
  19. for line in output:
  20. if ':' not in line:
  21. continue
  22. fields = re.split(r":", line, maxsplit=2)
  23. name = fields[0].strip()
  24. value = fields[1].strip()
  25. d[name] = value
  26. return d
  27. def test_run(self):
  28. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  29. self.emulator.boot(arch="armv5",
  30. kernel="builtin",
  31. options=["-initrd", cpio_file])
  32. self.emulator.login()
  33. # Check our binaries can execute.
  34. self.assertRunOk("ntpd --version")
  35. self.assertRunOk("ntpq --version")
  36. # The ntp daemon is expected to be started from init startup
  37. # scripts, for the Buildroot package recipe. We wait a bit
  38. # here to let the daemon settle. The next test step checks for
  39. # the local peer to be the system peer (by checking the
  40. # '*'). If querying the peers too soon after startup the peer
  41. # will not be marked as such.
  42. time.sleep(3 * self.timeout_multiplier)
  43. # We query the ntp daemon peers. From our test configuration
  44. # file, we should have exactly one.
  45. out, ret = self.emulator.run("ntpq --peers")
  46. self.assertEqual(ret, 0)
  47. # ntpq --peers produces two lines of headers. So we check we
  48. # have at least 3 lines of output.
  49. self.assertGreaterEqual(len(out), 3)
  50. # We check we see our undisciplined local clock and it's the
  51. # system peer.
  52. self.assertTrue(out[2].startswith("*LOCAL(0)"))
  53. # We query the refid variable. We expect to see our
  54. # undisciplined local clock.
  55. out, ret = self.emulator.run("ntpq -c 'readvar 0 refid'")
  56. self.assertEqual(ret, 0)
  57. self.assertEqual(out[0], "refid=LOCAL(0)")
  58. # We query the ntp system info. We check the reference ID is
  59. # the same as in the test configuration file.
  60. out, ret = self.emulator.run("ntpq -c sysinfo")
  61. self.assertEqual(ret, 0)
  62. sysinfo = self.dict_from_ntpq_output(out)
  63. refid = "reference ID"
  64. self.assertIn(refid, sysinfo)
  65. self.assertEqual(sysinfo[refid], "127.127.1.0")
  66. # Finally, we query the ntp system statistics. We check we can
  67. # see some uptime. We waited a bit at the beginning of this
  68. # test, plus the few queries we previously did should have
  69. # accumulated some uptime.
  70. out, ret = self.emulator.run("ntpq -c sysstats")
  71. self.assertEqual(ret, 0)
  72. sysstats = self.dict_from_ntpq_output(out)
  73. up = "uptime"
  74. self.assertIn(up, sysstats)
  75. self.assertGreater(int(sysstats[up]), 0)