add loop breaker to test

This commit is contained in:
matt committed 2026-10-17 23:36:47 -07:00
1 parent 4c205bdfa0
commit 0b4bb35962
3 files changed
+139 -1

No files matched your search

+100
View File
@@ -0,0 +1,100 @@
pipeline {
agent any
environment {
DEBIAN_IP = 'debian_machine_ip'
SSH_CREDENTIALS_ID = 'jenkins_root_ssh'
REMOTE_SCRIPT = '/opt/ble-test/ble_api/api/ble.py'
}
parameters {
string(name: 'host_ip', defaultValue: '172.20.20.51', description: 'IP of the host that runs the BLE test')
string(name: 'test_count', defaultValue: '25', description: 'Number of test iterations')
string(name: 'BT_USB_ID', defaultValue: '0bda:c821', description: 'USB ID of the BLE dongle')
}
stages {
stage('Run BLE Test') {
steps {
// ----- 1. Compute a 6‑digit hash from the epoch seconds
script {
// epoch seconds at the moment the build starts
def epochSec = System.currentTimeMillis() / 1000
// keep only the last 6 decimal digits
def hash = String.format("%06d", epochSec % 1_000_000)
env.SCREEN_SESSION = "ble_test_${hash}"
}
// ----- 2. Retry SSH 3× in case of a transient hiccup
retry(3) {
// ----- 3. Timeout the whole test (adjust as needed)
timeout(time: 5, unit: 'MINUTES') {
sshagent([env.SSH_CREDENTIALS_ID]) {
sh """
ssh -o StrictHostKeyChecking=no \\
-o ServerAliveInterval=60 \\
-o ServerAliveCountMax=120 \\
root@${params.host_ip} <<'EOF'
# Ensure screen is present
if ! command -v screen > /dev/null; then
echo "screen not found - installing"
apt-get update && apt-get install -y screen
fi
# Start the test in a detached screen session
screen -dmS ${env.SCREEN_SESSION} bash -c '
echo "===== BLE test starting: \$(date)" > /tmp/ble_test.log
usbreset ${params.BT_USB_ID}
sleep 5
cd /opt/ble-test/ble_api
git pull
cd /opt/ble-test/ble_api/api
python3 -u ${REMOTE_SCRIPT} ${params.test_count} 2>&1 | tee -a /tmp/ble_test.log
echo "===== BLE test finished: \$(date)" >> /tmp/ble_test.log
'
# Give screen a moment to spin up
sleep 2
EOF
"""
}
// Small pause so the screen session has a chance to start
sh 'sleep 2'
// Verify that the test reported completion
sh """
ssh -o StrictHostKeyChecking=no \\
-o ServerAliveInterval=60 \\
-o ServerAliveCountMax=120 \\
root@${params.host_ip} <<'EOF'
if grep -q "BLE test finished" /tmp/ble_test.log; then
echo "Test completed successfully."
else
echo "Test did NOT finish - aborting build."
exit 1
fi
EOF
"""
} // end timeout
} // end retry
} // end steps
} // end stage
} // end stages
// ----- 4. Clean up the screen session on the remote host
post {
always {
sh """
ssh -o StrictHostKeyChecking=no \\
-o ServerAliveInterval=60 \\
-o ServerAliveCountMax=120 \\
root@${params.host_ip} <<'EOF'
if screen -list | grep -q ${env.SCREEN_SESSION}; then
screen -S ${env.SCREEN_SESSION} -X quit
fi
EOF
"""
}
}
}
+31
View File
@@ -0,0 +1,31 @@
pipeline {
agent any
environment {
DEBIAN_IP = 'debian_machine_ip'
SSH_CREDENTIALS_ID = 'jenkins_root_ssh'
}
stages {
stage('Run BLE Test') {
steps {
script {
sshagent([env.SSH_CREDENTIALS_ID]) {
sh """
ssh -o StrictHostKeyChecking=no root@${params.host_ip} << EOF
usbreset ${BT_USB_ID}
sleep 5
cd /opt/ble-test/ble_api
git pull
cd /opt/ble-test/ble_api/api
python3 -u ./ble.py ${params.test_count}
"""
}
}
}
}
}
}
+8 -1
View File
@@ -275,6 +275,7 @@ def test(test_count = 0, bt_usb_id = None) -> None:
return -1
log.info(f"Ensuring BLE active...")
test_loop_count = 0
reset_count = 0
found_macs = scan_macs()
log.info("Found these MACs on a scan:")
log.info(found_macs)
@@ -290,7 +291,12 @@ def test(test_count = 0, bt_usb_id = None) -> None:
log.info(f" Scanning MACs: {mac_list}")
test_list = mac_list
while not test_result:
log.info(f" Test run {test_count} loop {test_loop_count} start. Initializing run...")
if test_count > 20:
reset_adapter()
reset_count = reset_count + 1
test_loop_count = 0
log.warning("Too many test loops - resetting adapter")
log.info(f" Test run {test_count} loop {reset_count}.{test_loop_count} start. Initializing run...")
test_run_results = sensor_data_query(test_list)
test_loop_count = test_loop_count + 1
for test_run_result in test_run_results:
@@ -362,6 +368,7 @@ if __name__ == "__main__":
log.info(f"BLE USB ID Detected: {current_usb_id} - Resetting")
reset_adapter()
test_count = 0
reset_count = 0
test_results = []
log.info(f"Starting BLE Test for {count} iterations")
while test_count < count: