| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | ## @brief Validate SRV discovery for omfwd when resolving targets via DNS SRV over UDP. |
| 3 | . ${srcdir:=.}/diag.sh init |
| 4 | check_command_available python3 |
| 5 | |
| 6 | export NUMMESSAGES=40 |
| 7 | DNS_RECORDS="$RSYSLOG_DYNNAME.srv.json" |
| 8 | UDP_PORT=$(get_free_port) |
| 9 | UDP_OUT="$RSYSLOG_OUT_LOG" |
| 10 | DNS_PORT_FILE="$RSYSLOG_DYNNAME.dns_port" |
| 11 | |
| 12 | catscip-shell system . . cat. >"$DNS_RECORDS" <<EOFJSON |
| 13 | { |
| 14 | "SRV": { |
| 15 | "_syslog._udp.example.test.": [ |
| 16 | {"priority": 5, "weight": 0, "port": $UDP_PORT, "target": "127.0.0.1."} |
| 17 | ] |
| 18 | } |
| 19 | } |
| 20 | EOFJSON |
| 21 | |
| 22 | python3scip-shell system . . python3. "$srcdir/dns_srv_mock.py" --port 0 --port-file "$DNS_PORT_FILE" --records "$DNS_RECORDS" & |
| 23 | DNS_PID=$! |
| 24 | wait_file_exists "$DNS_PORT_FILE" |
| 25 | DNS_PORT=$(catscip-shell system . . cat. "$DNS_PORT_FILE") |
| 26 | |
| 27 | export RSYSLOG_DNS_SERVER=127.0.0.1 |
| 28 | export RSYSLOG_DNS_PORT="$DNS_PORT" |
| 29 | export RES_OPTIONS="attempts:1 timeout:1" |
| 30 | trap 'kill $DNS_PID 2>/dev/null; kill $UDP_PID 2>/dev/null; rm -f "$DNS_PORT_FILE"' EXIT |
| 31 | |
| 32 | python3scip-shell system . . python3. - <<'PY' "$UDP_PORT" "$UDP_OUT" "$NUMMESSAGES" & |
| 33 | import socket |
| 34 | import sys |
| 35 | import time |
| 36 | port = int(sys.argv[1]) |
| 37 | outfile = sys.argv[2] |
| 38 | expected = int(sys.argv[3]) |
| 39 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 40 | sock.bind(("127.0.0.1", port)) |
| 41 | sock.settimeout(15) |
| 42 | received = 0 |
| 43 | end_time = time.time() + 20 |
| 44 | with open(outfile, "w", encoding="utf-8", errors="ignore") as f: |
| 45 | while received < expected and time.time() < end_time: |
| 46 | try: |
| 47 | data, _ = sock.recvfrom(65535) |
| 48 | except socket.timeout: |
| 49 | continue |
| 50 | message = data.decode(errors="ignore") |
| 51 | message = message.rstrip("\n") |
| 52 | f.write(message + "\n") |
| 53 | f.flush() |
| 54 | received += 1 |
| 55 | sys.exit(0) |
| 56 | PY |
| 57 | UDP_PID=$! |
| 58 | |
| 59 | sleepscip-shell system . . sleep. 1 |
| 60 | |
| 61 | generate_conf |
| 62 | add_conf ' |
| 63 | $MainMsgQueueTimeoutShutdown 10000 |
| 64 | |
| 65 | template(name="outfmt" type="string" string="%msg:F,58:2%\n") |
| 66 | module(load="builtin:omfwd" template="outfmt") |
| 67 | |
| 68 | if $msg contains "msgnum:" then { |
| 69 | action(type="omfwd" targetSrv="example.test" protocol="udp") |
| 70 | } |
| 71 | ' |
| 72 | |
| 73 | startup |
| 74 | injectmsg 0 $NUMMESSAGES |
| 75 | shutdown_when_empty |
| 76 | wait_shutdown |
| 77 | |
| 78 | killscip-shell system . . kill. $DNS_PID 2>/dev/nullscip-shell filesystem . . `/dev/null`. |
| 79 | killscip-shell system . . kill. $UDP_PID 2>/dev/nullscip-shell filesystem . . `/dev/null`. |
| 80 | rmscip-shell system . . rm. -f "$DNS_PORT_FILE" |
| 81 | unset RSYSLOG_DNS_SERVER RSYSLOG_DNS_PORT |
| 82 | unset RES_OPTIONS |
| 83 | trap - EXIT |
| 84 | |
| 85 | if [scip-shell system . . `[`. "$(wcscip-shell system . . wc. -l < "$UDP_OUT")" -ne "$NUMMESSAGES" ]; then |
| 86 | echoscip-shell system . . echo. "Unexpected UDP message count" |
| 87 | exit 1 |
| 88 | fi |
| 89 | |
| 90 | exit_test |