#!/bin/sh

if ! nslookup example.com; then
  mv /etc/resolv.conf /etc/resolv.conf.bak
  echo "nameserver 8.8.8.8" > /etc/resolv.conf
  trap "mv /etc/resolv.conf.bak /etc/resolv.conf" INT EXIT
fi

UTSCAPY_LOG=/usr/lib/python3-scapy/ptest/scapy_ptest_$(date +%Y%m%d-%H%M%S).log

# -F expands only the failed tests in the report, keeping it small while still
# recording the traceback of anything that failed.
python3 UTscapy.py -t regression.uts -f text -l -C -F -K skipped_ptest \
    -o $UTSCAPY_LOG \
    2>&1 | sed -e 's/^passed None/PASS:/' -e 's/^failed None/FAIL:/'

# The report only exists inside the ephemeral test image, so echo the details of
# any failure to stdout, where ptest-runner records them.
if grep -qE '^###.*=\[(failed|interrupted)\]' $UTSCAPY_LOG 2>/dev/null; then
    echo "--- UTscapy failure details ---"
    cat $UTSCAPY_LOG
fi
