From 5eebbb1833c601745e091d85d2a71fe125f29f96 Mon Sep 17 00:00:00 2001
From: Anders Roxell <anders.roxell@linaro.org>
Date: Fri, 1 Nov 2024 11:04:15 +0100
Subject: [PATCH 1/3] android: noninteractive-tradefed: tradfed: create a check
 internet access function

Signed-off-by: Anders Roxell <anders.roxell@linaro.org>
---
 .../noninteractive-tradefed/tradefed.sh       | 99 ++++++++++---------
 1 file changed, 52 insertions(+), 47 deletions(-)

diff --git a/automated/android/noninteractive-tradefed/tradefed.sh b/automated/android/noninteractive-tradefed/tradefed.sh
index a5dac4ca3b3e..410cebdc679f 100755
--- a/automated/android/noninteractive-tradefed/tradefed.sh
+++ b/automated/android/noninteractive-tradefed/tradefed.sh
@@ -33,6 +33,56 @@ usage() {
     exit 1
 }
 
+check_internet_access() {
+    if [ -n "${AP_SSID}" ] && [ -n "${AP_KEY}" ]; then
+        # try to connect to wifi with the feature provided by tradefed by default
+        # when AP_SSID and AP_KEY are specified for this tradefed test action explicitly
+        TEST_PARAMS="${TEST_PARAMS} --wifi-ssid ${AP_SSID} --wifi-psk ${AP_KEY}"
+    else
+        # try to connect to wifi with the external AdbJoinWifi apk from
+        # https://github.com/steinwurf/adb-join-wifi
+        # if AP_SSID and AP_KEY are not specified for this tradefed test action
+        adb_join_wifi "${AP_SSID}" "${AP_KEY}"
+    fi
+
+    # wait for a while till the wifi connecting operation finished
+    sleep 60
+
+    SERVER="www.google.com"
+    DNS_TEST_SERVER="8.8.8.8"
+
+    info_msg "device-${ANDROID_SERIAL}: Checking network connectivity by pinging ${SERVER}..."
+
+    # Try pinging www.google.com first
+    if adb shell 'ping -c 10 '"${SERVER}"'; echo exitcode: $?' | grep -q "exitcode: 0"; then
+        report_pass "network-available"
+    else
+        info_msg "Ping to ${SERVER} failed, testing DNS by pinging ${DNS_TEST_SERVER}..."
+        report_fail "network-available"
+
+        # If ping to $DNS_TEST_SERVER succeeds, it's likely a DNS issue
+        if adb shell 'ping -c 10 '"${DNS_TEST_SERVER}"'; echo exitcode: $?' | grep -q "exitcode: 0"; then
+            report_pass "network-ping-dns-server"
+
+            # DNS-specific debug information
+            info_msg "DNS resolution issue suspected; gathering DNS configuration information..."
+            adb shell getprop | grep dns || true  # Display DNS settings
+
+        else
+            report_fail "network-ping-dns-server"
+
+            # General network debug information
+            info_msg "Ping to ${DNS_TEST_SERVER} failed too..."
+            info_msg "Network connectivity issue detected; gathering debug information..."
+            adb shell ip address || true
+            adb shell ifconfig || true
+        fi
+
+        # Exit with error to trigger YAML file handling
+        exit 100
+    fi
+}
+
 while getopts ':o:n:c:t:p:r:f:a:k:i:' opt; do
     case "${opt}" in
         o) TIMEOUT="${OPTARG}" ;;
@@ -87,53 +137,8 @@ if [ -e "${TEST_PATH}/testcases/vts/testcases/kernel/linux_kselftest/kselftest_c
     sed -i "/suspend/d" "${TEST_PATH}"/testcases/vts/testcases/kernel/linux_kselftest/kselftest_config.py
 fi
 
-if [ "X${INTERNET_ACCESS}" = "Xtrue" ]; then
-    if [ -n "${AP_SSID}" ] && [ -n "${AP_KEY}" ]; then
-        # try to connect to wifi with the feature provided by tradefed by default
-        # when AP_SSID and AP_KEY are specified for this tradefed test action explicitly
-        TEST_PARAMS="${TEST_PARAMS} --wifi-ssid ${AP_SSID} --wifi-psk ${AP_KEY}"
-    else
-        # try to connect to wifi with the external AdbJoinWifi apk from
-        # https://github.com/steinwurf/adb-join-wifi
-        # if AP_SSID and AP_KEY are not specified for this tradefed test action
-        adb_join_wifi "${AP_SSID}" "${AP_KEY}"
-    fi
-
-    # wait for a while till the wifi connecting operation finished
-    sleep 60
-
-    SERVER="www.google.com"
-    DNS_TEST_SERVER="8.8.8.8"
-
-    info_msg "device-${ANDROID_SERIAL}: Checking network connectivity by pinging ${SERVER}..."
-
-    # Try pinging www.google.com first
-    if adb shell 'ping -c 10 '"${SERVER}"'; echo exitcode: $?' | grep -q "exitcode: 0"; then
-        report_pass "network-available"
-    else
-        info_msg "Ping to ${SERVER} failed, testing DNS by pinging ${DNS_TEST_SERVER}..."
-        report_fail "network-available"
-
-        # If ping to $DNS_TEST_SERVER succeeds, it's likely a DNS issue
-        if adb shell 'ping -c 10 '"${DNS_TEST_SERVER}"'; echo exitcode: $?' | grep -q "exitcode: 0"; then
-            report_pass "network-ping-dns-server"
-
-            # DNS-specific debug information
-            info_msg "DNS resolution issue suspected; gathering DNS configuration information..."
-            adb shell getprop | grep dns || true  # Display DNS settings
-
-        else
-            report_fail "network-ping-dns-server"
-
-            # General network debug information
-            info_msg "Ping to ${DNS_TEST_SERVER} failed too..."
-            info_msg "Network connectivity issue detected; gathering debug information..."
-            adb shell ip address || true
-            adb shell ifconfig || true
-        fi
-
-        # Exit with error to trigger YAML file handling
-        exit 100
+if [ "X${INTERNET_ACCESS}" = "Xtrue" ] || [ "${INTERNET_ACCESS}" = "XTrue" ]; then
+    check_internet_access
 fi
 
 # Run tradefed test.
-- 
2.45.2

