diff options
author | Tom Rini <trini@konsulko.com> | 2021-07-05 15:29:44 -0400 |
---|---|---|
committer | Tom Rini <trini@konsulko.com> | 2021-07-05 15:29:44 -0400 |
commit | 1311dd37ecf476be041d0452d4ee38619aadd5de (patch) | |
tree | 5c48d348930efa23ec452673f2afafb9581e8eb9 /test/py/u_boot_utils.py | |
parent | 6194b45a83bde42cd2f404123823e5b326702001 (diff) | |
parent | b1c2102db1774686282474aee3c2dd06df92f175 (diff) |
Merge branch '2021-07-01-update-CI-containers'
- General test.py improvements
- Rewrite the squashfs tests
- Update our CI container to Ubuntu 20.04 "focal" base.
- Make some changes to the Azure yaml so that we can have more tests run
there.
Diffstat (limited to 'test/py/u_boot_utils.py')
-rw-r--r-- | test/py/u_boot_utils.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/test/py/u_boot_utils.py b/test/py/u_boot_utils.py index 939d82eec12..e816c7fbb6a 100644 --- a/test/py/u_boot_utils.py +++ b/test/py/u_boot_utils.py @@ -8,6 +8,7 @@ import inspect import os import os.path import pytest +import signal import sys import time import re @@ -339,3 +340,38 @@ def crc32(u_boot_console, address, count): assert m, 'CRC32 operation failed.' return m.group(1) + +def waitpid(pid, timeout=60, kill=False): + """Wait a process to terminate by its PID + + This is an alternative to a os.waitpid(pid, 0) call that works on + processes that aren't children of the python process. + + Args: + pid: PID of a running process. + timeout: Time in seconds to wait. + kill: Whether to forcibly kill the process after timeout. + + Returns: + True, if the process ended on its own. + False, if the process was killed by this function. + + Raises: + TimeoutError, if the process is still running after timeout. + """ + try: + for _ in range(timeout): + os.kill(pid, 0) + time.sleep(1) + + if kill: + os.kill(pid, signal.SIGKILL) + return False + + except ProcessLookupError: + return True + + raise TimeoutError( + "Process with PID {} did not terminate after {} seconds." + .format(pid, timeout) + ) |