|
| 1 | +# ******************************************************************************* |
| 2 | +# Copyright (c) 2026 Contributors to the Eclipse Foundation |
| 3 | +# |
| 4 | +# See the NOTICE file(s) distributed with this work for additional |
| 5 | +# information regarding copyright ownership. |
| 6 | +# |
| 7 | +# This program and the accompanying materials are made available under the |
| 8 | +# terms of the Apache License Version 2.0 which is available at |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# SPDX-License-Identifier: Apache-2.0 |
| 12 | +# ******************************************************************************* |
| 13 | + |
| 14 | +import logging |
| 15 | +import os |
| 16 | +import time |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from score.itf.plugins.dlt.dlt_receive import Protocol |
| 21 | +from score.itf.plugins.dlt.dlt_window import DltWindow |
| 22 | + |
| 23 | +logger = logging.getLogger(__name__) |
| 24 | + |
| 25 | + |
| 26 | +# Datarouter messages with Context ID "STAT" sent every ~5s hence 10s to reliably capture and verify |
| 27 | +CAPTURE_DURATION_SECONDS = 10 |
| 28 | + |
| 29 | +# DLT message identifiers for datarouter statistics |
| 30 | +APP_ID = "DR" |
| 31 | +CTX_ID = "STAT" |
| 32 | + |
| 33 | +_QNX_DATAROUTER_CHECK_CMD = "/proc/boot/pidin | /proc/boot/grep datarouter" |
| 34 | +_LINUX_DATAROUTER_CHECK_CMD = "ps -ef | grep datarouter | grep -v grep" |
| 35 | + |
| 36 | +# pathspace ability provides the datarouter access to the `procnto` pathname prefix space |
| 37 | +# required for mw/com message passing with mw::log frontend |
| 38 | +_QNX_DATAROUTER_START_CMD = ( |
| 39 | + "cd /usr/bin/datarouter && nohup on -A nonroot,allow,pathspace -u 1051:1091 " |
| 40 | + "./datarouter --no_adaptive_runtime > /dev/null 2>&1 &" |
| 41 | +) |
| 42 | +_LINUX_DATAROUTER_START_CMD = "cd /usr/bin/datarouter && nohup ./datarouter --no_adaptive_runtime > /dev/null 2>&1 &" |
| 43 | +# Multicast route directs all multicast traffic (224.0.0.0/4) out of the containers |
| 44 | +# network interface required for DLT messages to reach the host via the Docker bridge. |
| 45 | +_LINUX_CHECK_MULTICAST_ROUTE_CMD = "ip route add 224.0.0.0/4 dev eth0 2>/dev/null || true" |
| 46 | +_DATAROUTER_STARTUP_TIMEOUT_SEC = 2 |
| 47 | + |
| 48 | + |
| 49 | +def _is_qnx(target): |
| 50 | + _, out = target.execute("uname -s") |
| 51 | + return b"QNX" in out |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture |
| 55 | +def datarouter_running(target): |
| 56 | + is_qnx = _is_qnx(target) |
| 57 | + |
| 58 | + if is_qnx: |
| 59 | + check_cmd = _QNX_DATAROUTER_CHECK_CMD |
| 60 | + start_cmd = _QNX_DATAROUTER_START_CMD |
| 61 | + else: |
| 62 | + check_cmd = _LINUX_DATAROUTER_CHECK_CMD |
| 63 | + start_cmd = _LINUX_DATAROUTER_START_CMD |
| 64 | + target.execute(_LINUX_CHECK_MULTICAST_ROUTE_CMD) |
| 65 | + |
| 66 | + exit_code, out = target.execute(check_cmd) |
| 67 | + output = out.decode(errors="replace") |
| 68 | + |
| 69 | + if "datarouter" not in output: |
| 70 | + exit_code, _ = target.execute("test -x /usr/bin/datarouter") |
| 71 | + if exit_code != 0: |
| 72 | + pytest.fail("Datarouter binary not found at /usr/bin/datarouter on target") |
| 73 | + |
| 74 | + logger.info("Datarouter not running. Starting Datarouter..") |
| 75 | + exit_code, out = target.execute(start_cmd) |
| 76 | + logger.debug("Start command exit_code=%s", exit_code) |
| 77 | + time.sleep(_DATAROUTER_STARTUP_TIMEOUT_SEC) |
| 78 | + |
| 79 | + _, out = target.execute(check_cmd) |
| 80 | + if "datarouter" not in out.decode(errors="replace"): |
| 81 | + pytest.fail("Failed to start datarouter on target") |
| 82 | + logger.info("Datarouter started successfully..") |
| 83 | + else: |
| 84 | + logger.info("Datarouter already running!") |
| 85 | + yield |
| 86 | + |
| 87 | + |
| 88 | +def test_remote_logging(datarouter_running, dlt_config): |
| 89 | + """Verifies remote logging of Dataraouter |
| 90 | +
|
| 91 | + Starts Datarouter on the target if not already running, then |
| 92 | + the dlt_receive captures DLT messages on the test host |
| 93 | + on the multicast group for a fixed duration and checks for |
| 94 | + expected 'STAT' log messages in the captured DTL logs. |
| 95 | + """ |
| 96 | + with DltWindow( |
| 97 | + protocol=Protocol.UDP, |
| 98 | + host_ip=dlt_config.host_ip, |
| 99 | + multicast_ips=dlt_config.multicast_ips, |
| 100 | + print_to_stdout=True, |
| 101 | + binary_path=dlt_config.dlt_receive_path, |
| 102 | + ) as window: |
| 103 | + time.sleep(CAPTURE_DURATION_SECONDS) |
| 104 | + |
| 105 | + record = window.record(filters=[(APP_ID, CTX_ID)]) |
| 106 | + messages = record.find(query=dict(apid=APP_ID, ctid=CTX_ID)) |
| 107 | + message_count = len(messages) |
| 108 | + |
| 109 | + logger.debug("Found %d messages with app_id: %s, context_id: %s", message_count, APP_ID, CTX_ID) |
| 110 | + |
| 111 | + assert message_count > 1, ( |
| 112 | + f"Expected atleast one DLT message with app_id: {APP_ID} and context_id: {CTX_ID}, but got {message_count}" |
| 113 | + ) |
0 commit comments