test_rrdtool.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import os
  2. import infra.basetest
  3. class TestRRDTool(infra.basetest.BRTest):
  4. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  5. """
  6. BR2_PACKAGE_RRDTOOL=y
  7. BR2_PACKAGE_RRDTOOL_RRDGRAPH=y
  8. BR2_TARGET_ROOTFS_CPIO=y
  9. # BR2_TARGET_ROOTFS_TAR is not set
  10. """
  11. def test_run(self):
  12. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  13. self.emulator.boot(arch="armv5",
  14. kernel="builtin",
  15. options=["-initrd", cpio_file])
  16. self.emulator.login()
  17. # We check the program can run.
  18. self.assertRunOk("rrdtool --version")
  19. # This test sequence is inspired from parts of the RRDTool
  20. # tutorial:
  21. # https://oss.oetiker.ch/rrdtool/tut/rrdtutorial.en.html
  22. rrd_file = "test.rrd"
  23. data_source = "speed"
  24. cmd = f"rrdtool create {rrd_file}"
  25. cmd += " --start 920804400"
  26. cmd += f" DS:{data_source}:COUNTER:600:U:U"
  27. cmd += " RRA:AVERAGE:0.5:1:24"
  28. cmd += " RRA:AVERAGE:0.5:6:10"
  29. self.assertRunOk(cmd)
  30. # Some data to fill in our database, from the tutorial page.
  31. data = [12345, 12357, 12363, 12363, 12363,
  32. 12373, 12383, 12393, 12399, 12405,
  33. 12411, 12415, 12420, 12422, 12423]
  34. timestamp = 920804700 # timestamp of: Sun Mar 7 12:05:00 PM CET 1999
  35. # We check we can put our data in the database. We start at
  36. # our timestamp, then increase by 5 minutes.
  37. for d in data:
  38. cmd = f"rrdtool update {rrd_file} {timestamp}:{d}"
  39. timestamp += 300
  40. self.assertRunOk(cmd)
  41. # We check we can read back our data.
  42. cmd = f"rrdtool fetch {rrd_file}"
  43. cmd += " AVERAGE --start 920804400 --end 920809200"
  44. out, ret = self.emulator.run(cmd)
  45. self.assertEqual(ret, 0)
  46. self.assertEqual(out[0].strip(), data_source)
  47. # We check that at time=920805600 we have speed=0.
  48. data_line = next(ln for ln in out if ln.strip().startswith('920805600:'))
  49. speed = float(data_line.split(':')[1])
  50. self.assertAlmostEqual(speed, 0.)
  51. # We generate a first simple image graph.
  52. cmd = "rrdtool graph speed.png"
  53. cmd += " --start 920804400 --end 920808000"
  54. cmd += f" DEF:myspeed={rrd_file}:{data_source}:AVERAGE"
  55. cmd += " LINE2:myspeed#FF0000"
  56. self.assertRunOk(cmd, timeout=20)
  57. # We check the output file exists and has a size
  58. # greater than zero.
  59. self.assertRunOk("test -s speed.png")
  60. # We generate a slightly more complex graph.
  61. cmd = "rrdtool graph speed2.png"
  62. cmd += " --start 920804400 --end 920808000"
  63. cmd += " --vertical-label m/s"
  64. cmd += f" DEF:myspeed={rrd_file}:{data_source}:AVERAGE"
  65. cmd += " CDEF:realspeed=myspeed,1000,\\*"
  66. cmd += " LINE2:realspeed#FF0000"
  67. self.assertRunOk(cmd, timeout=20)
  68. # Again, we check the output file exists.
  69. self.assertRunOk("test -s speed2.png")
  70. # Finally, we generate the last graph, with some rules.
  71. cmd = "rrdtool graph speed3.png"
  72. cmd += " --start 920804400 --end 920808000"
  73. cmd += " --vertical-label km/h"
  74. cmd += f" DEF:myspeed={rrd_file}:{data_source}:AVERAGE"
  75. cmd += " \"CDEF:kmh=myspeed,3600,*\""
  76. cmd += " CDEF:fast=kmh,100,GT,kmh,0,IF"
  77. cmd += " CDEF:good=kmh,100,GT,0,kmh,IF"
  78. cmd += " HRULE:100#0000FF:\"Maximum allowed\""
  79. cmd += " AREA:good#00FF00:\"Good speed\""
  80. cmd += " AREA:fast#FF0000:\"Too fast\""
  81. self.assertRunOk(cmd, timeout=20)
  82. # And again, we check the output file exists.
  83. self.assertRunOk("test -s speed3.png")