|
20 | 20 | # Uses the local maestro-runner binary: |
21 | 21 | # Android → --driver devicelab |
22 | 22 | # iOS → default driver |
| 23 | +# |
| 24 | +# In CI on Android, driver startup crashes are retried twice (3 runs total). |
23 | 25 |
|
24 | 26 | set -euo pipefail |
25 | 27 |
|
@@ -141,28 +143,66 @@ set_font_scale default |
141 | 143 | # maestro-runner exits non-zero when the tag filter matches zero flows. That's |
142 | 144 | # not a real failure for us (e.g. running a single flow that has no |
143 | 145 | # accessibility variant), and letting it propagate aborts later test suites. |
| 146 | +is_driver_startup_crash() { |
| 147 | + grep -Eqi 'failed to create driver|driver crashed on startup|DeviceLab driver crashed' "$1" |
| 148 | +} |
| 149 | + |
| 150 | +cleanup_devicelab_driver() { |
| 151 | + [ "$PLATFORM" != android ] && return 0 |
| 152 | + local sock="/tmp/devicelab-driver-${DEVICE_ID}.sock" |
| 153 | + adb -s "$DEVICE_ID" forward --remove "localfilesystem:$sock" 2>/dev/null || true |
| 154 | + adb -s "$DEVICE_ID" shell am force-stop dev.devicelab.driver.android.test 2>/dev/null || true |
| 155 | + sleep 3 |
| 156 | +} |
| 157 | + |
144 | 158 | run_maestro() { |
145 | | - local tmp rc |
146 | | - tmp=$(mktemp) |
147 | | - # `script` allocates a pseudo-TTY so the runner keeps |
148 | | - # ANSI colors when piped through `tee`. |
149 | | - # Global flags (--device, --driver, --env, tags) come before `test`. |
150 | | - if [[ "$OSTYPE" == darwin* ]]; then |
151 | | - # shellcheck disable=SC2086 |
152 | | - script -q /dev/null "$MAESTRO_BIN" --platform "$PLATFORM" --device "$DEVICE_ID" $DRIVER_ARGS $EXTRA "$@" test $FLOWS 2>&1 | tee "$tmp" |
| 159 | + local max_attempts rc tmp attempt=1 |
| 160 | + if [ -n "${MAESTRO_MAX_RETRIES:-}" ]; then |
| 161 | + max_attempts="$MAESTRO_MAX_RETRIES" |
| 162 | + elif [ -n "${CI:-}" ] && [ "$PLATFORM" = android ]; then |
| 163 | + max_attempts=3 # 1 initial run + 2 retries |
153 | 164 | else |
154 | | - local cmd |
155 | | - # shellcheck disable=SC2086 |
156 | | - cmd=$(printf '%q ' "$MAESTRO_BIN" --platform "$PLATFORM" --device "$DEVICE_ID" $DRIVER_ARGS $EXTRA "$@" test $FLOWS) |
157 | | - script -qc "$cmd" /dev/null 2>&1 | tee "$tmp" |
158 | | - fi |
159 | | - rc=${PIPESTATUS[0]} |
160 | | - if [ "$rc" -ne 0 ] && grep -Eqi "did not match any [Ff]lows|no flows matched" "$tmp"; then |
161 | | - echo "warn: no flows matched the tag filter — treating as success" >&2 |
162 | | - rc=0 |
| 165 | + max_attempts=1 |
163 | 166 | fi |
164 | | - rm -f "$tmp" |
165 | | - return "$rc" |
| 167 | + |
| 168 | + while [ "$attempt" -le "$max_attempts" ]; do |
| 169 | + tmp=$(mktemp) |
| 170 | + # `script` allocates a pseudo-TTY so the runner keeps |
| 171 | + # ANSI colors when piped through `tee`. |
| 172 | + # Global flags (--device, --driver, --env, tags) come before `test`. |
| 173 | + if [[ "$OSTYPE" == darwin* ]]; then |
| 174 | + # shellcheck disable=SC2086 |
| 175 | + script -q /dev/null "$MAESTRO_BIN" --platform "$PLATFORM" --device "$DEVICE_ID" $DRIVER_ARGS $EXTRA "$@" test $FLOWS 2>&1 | tee "$tmp" |
| 176 | + else |
| 177 | + local cmd |
| 178 | + # shellcheck disable=SC2086 |
| 179 | + cmd=$(printf '%q ' "$MAESTRO_BIN" --platform "$PLATFORM" --device "$DEVICE_ID" $DRIVER_ARGS $EXTRA "$@" test $FLOWS) |
| 180 | + script -qc "$cmd" /dev/null 2>&1 | tee "$tmp" |
| 181 | + fi |
| 182 | + rc=${PIPESTATUS[0]} |
| 183 | + |
| 184 | + if [ "$rc" -eq 0 ]; then |
| 185 | + rm -f "$tmp" |
| 186 | + return 0 |
| 187 | + fi |
| 188 | + |
| 189 | + if grep -Eqi "did not match any [Ff]lows|no flows matched" "$tmp"; then |
| 190 | + echo "warn: no flows matched the tag filter — treating as success" >&2 |
| 191 | + rm -f "$tmp" |
| 192 | + return 0 |
| 193 | + fi |
| 194 | + |
| 195 | + if [ "$attempt" -lt "$max_attempts" ] && is_driver_startup_crash "$tmp"; then |
| 196 | + echo "warn: driver crashed on startup (attempt $attempt/$max_attempts), retrying..." >&2 |
| 197 | + cleanup_devicelab_driver |
| 198 | + attempt=$((attempt + 1)) |
| 199 | + rm -f "$tmp" |
| 200 | + continue |
| 201 | + fi |
| 202 | + |
| 203 | + rm -f "$tmp" |
| 204 | + return "$rc" |
| 205 | + done |
166 | 206 | } |
167 | 207 |
|
168 | 208 | set +e |
|
0 commit comments