|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""GPS receiver basic test |
| 3 | +
|
| 4 | +Verify that two simulated GPS receivers are detected and report valid |
| 5 | +fixes via the ietf-hardware operational datastore. |
| 6 | +
|
| 7 | +The test injects NMEA sentences through QEMU pipe chardev FIFOs, |
| 8 | +which appear as virtio serial ports inside the guest. |
| 9 | +""" |
| 10 | +import infamy |
| 11 | +import infamy.gps as gps |
| 12 | +from infamy.util import until, wait_boot |
| 13 | + |
| 14 | +# Fun facts: The top of mount everest |
| 15 | +test_lat = 27.9881 |
| 16 | +test_lon = 86.9250 |
| 17 | +test_alt = 8848.86 |
| 18 | + |
| 19 | + |
| 20 | +def _near(a, b, tol): |
| 21 | + return abs(a - b) <= tol |
| 22 | + |
| 23 | +def verify_position(target, name="gps0"): |
| 24 | + state = gps.get_gps_state(target, name) |
| 25 | + |
| 26 | + try: |
| 27 | + lat = float(state["latitude"]) |
| 28 | + lon = float(state["longitude"]) |
| 29 | + alt = float(state["altitude"]) |
| 30 | + except (KeyError, TypeError, ValueError): |
| 31 | + test.fail() |
| 32 | + |
| 33 | + if not _near(lat, test_lat, 0.01): |
| 34 | + test.fail() |
| 35 | + if not _near(lon, test_lon, 0.01): |
| 36 | + test.fail() |
| 37 | + if not _near(alt, test_alt, 100): |
| 38 | + test.fail() |
| 39 | + |
| 40 | + try: |
| 41 | + sat_used = int(state["satellites-used"]) |
| 42 | + except (KeyError, TypeError, ValueError): |
| 43 | + test.fail() |
| 44 | + |
| 45 | + if sat_used != 8: |
| 46 | + test.fail() |
| 47 | + |
| 48 | +with infamy.Test() as test: |
| 49 | + with test.step("Set up topology and attach to target DUT"): |
| 50 | + env = infamy.Env() |
| 51 | + target = env.attach("target", "mgmt") |
| 52 | + |
| 53 | + phys_name = env.ltop.mapping["target"][None] |
| 54 | + |
| 55 | + if not target.has_feature("infix-hardware", "gps"): |
| 56 | + test.skip() |
| 57 | + |
| 58 | + # These are hacks, will only work on virtual devices |
| 59 | + pipe0 = f"/tmp/{phys_name}-gps" |
| 60 | + pipe1 = f"/tmp/{phys_name}-gps1" |
| 61 | + |
| 62 | + with gps.NMEAGenerator(pipe0, lat=test_lat, lon=test_lon, alt=test_alt), \ |
| 63 | + gps.NMEAGenerator(pipe1, lat=test_lat, lon=test_lon, alt=test_alt): |
| 64 | + |
| 65 | + with test.step("Configure GPS hardware components"): |
| 66 | + target.put_config_dicts({"ietf-hardware": { |
| 67 | + "hardware": { |
| 68 | + "component": [ |
| 69 | + { |
| 70 | + "name": "gps0", |
| 71 | + "class": "infix-hardware:gps", |
| 72 | + "infix-hardware:gps-receiver": {} |
| 73 | + }, |
| 74 | + { |
| 75 | + "name": "gps1", |
| 76 | + "class": "infix-hardware:gps", |
| 77 | + "infix-hardware:gps-receiver": {} |
| 78 | + } |
| 79 | + ] |
| 80 | + } |
| 81 | + }}) |
| 82 | + |
| 83 | + with test.step("Verify both GPS receivers are activated"): |
| 84 | + until(lambda: gps.is_activated(target, "gps0"), attempts=500) |
| 85 | + until(lambda: gps.is_activated(target, "gps1"), attempts=500) |
| 86 | + |
| 87 | + with test.step("Verify both GPS receivers have a fix"): |
| 88 | + until(lambda: gps.has_fix(target, "gps0"), attempts=60) |
| 89 | + until(lambda: gps.has_fix(target, "gps1"), attempts=60) |
| 90 | + |
| 91 | + with test.step("Verify gps0 position is near the coordinates"): |
| 92 | + verify_position(target, "gps0") |
| 93 | + |
| 94 | + with test.step("Verify gps1 position is near the coordinates"): |
| 95 | + verify_position(target, "gps1") |
| 96 | + |
| 97 | + with test.step("Save the configuration to startup configuration and reboot"): |
| 98 | + target.startup_override() |
| 99 | + target.copy("running", "startup") |
| 100 | + target.reboot() |
| 101 | + if not wait_boot(target, env): |
| 102 | + test.fail() |
| 103 | + target = env.attach("target", "mgmt", test_reset=False) |
| 104 | + |
| 105 | + with test.step("Verify both GPS receivers are activated"): |
| 106 | + until(lambda: gps.is_activated(target, "gps0"), attempts=500) |
| 107 | + until(lambda: gps.is_activated(target, "gps1"), attempts=500) |
| 108 | + |
| 109 | + with test.step("Verify both GPS receivers have a fix"): |
| 110 | + until(lambda: gps.has_fix(target, "gps0"), attempts=60) |
| 111 | + until(lambda: gps.has_fix(target, "gps1"), attempts=60) |
| 112 | + |
| 113 | + with test.step("Verify gps0 position is near the coordinates"): |
| 114 | + verify_position(target, "gps0") |
| 115 | + |
| 116 | + with test.step("Verify gps1 position is near the coordinates"): |
| 117 | + verify_position(target, "gps1") |
| 118 | + |
| 119 | + test.succeed() |
0 commit comments