test_gpsd.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import os
  2. import time
  3. import infra.basetest
  4. class TestGpsd(infra.basetest.BRTest):
  5. rootfs_overlay = \
  6. infra.filepath("tests/package/test_gpsd/rootfs-overlay")
  7. # This test is using the gpsfake Python script.
  8. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  9. f"""
  10. BR2_PACKAGE_GPSD=y
  11. BR2_PACKAGE_GPSD_PYTHON=y
  12. BR2_ROOTFS_OVERLAY="{rootfs_overlay}"
  13. BR2_TARGET_ROOTFS_CPIO=y
  14. # BR2_TARGET_ROOTFS_TAR is not set
  15. """
  16. def _test_gpsd_one(self, transport=None):
  17. # Start the "gpsfake" GPS emulator.
  18. cmd = "gpsfake"
  19. cmd += " --slow --cycle 0.1 --quiet"
  20. if transport:
  21. cmd += f" --{transport}"
  22. cmd += " /root/nmea.log &> /dev/null &"
  23. self.assertRunOk(cmd)
  24. # Wait a bit, to let the gpsfake and gpsd to settle...
  25. time.sleep(3 * self.timeout_multiplier)
  26. # List the GPS devices. We should see our local UDP test GPS.
  27. out, ret = self.emulator.run("gpsctl")
  28. self.assertEqual(ret, 0)
  29. self.assertIn("NMEA0183", out[0])
  30. if transport:
  31. self.assertTrue(out[0].startswith(f"{transport}://127.0.0.1"))
  32. else:
  33. self.assertTrue(out[0].startswith("/dev/pts/1 "))
  34. # Collect some of our fake GPS data, and check we got the
  35. # coordinates from our test data file.
  36. # Our expected coordinates are:
  37. # https://www.openstreetmap.org/#map=19/43.60439/1.44336
  38. out, ret = self.emulator.run("gpscsv --header 0 --count 3")
  39. self.assertEqual(ret, 0)
  40. _, gps_lat, gps_long, _ = out[0].split(",")
  41. self.assertAlmostEqual(float(gps_lat), 43.60439)
  42. self.assertAlmostEqual(float(gps_long), 1.44336)
  43. # Terminate gpsd, wait for it to finish.
  44. self.assertRunOk("kill %1; sleep 1")
  45. def test_run(self):
  46. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  47. self.emulator.boot(arch="armv5",
  48. kernel="builtin",
  49. options=["-initrd", cpio_file])
  50. self.emulator.login()
  51. # We check the program can execute.
  52. self.assertRunOk("gpsd --version")
  53. # Since gpsd needs a real GPS device, we stop the service.
  54. self.assertRunOk("/etc/init.d/S50gpsd stop")
  55. # Test various transports for gpsd
  56. self._test_gpsd_one()
  57. self._test_gpsd_one("tcp")
  58. self._test_gpsd_one("udp")