Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tests): don't leave emulator process running #4687

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ jobs:
env:
TREZOR_UPGRADE_TEST: core
PYTEST_TIMEOUT: 400
TREZOR_PYTEST_LOGS_DIR: ${{ github.workspace }}/tests/
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -381,6 +382,13 @@ jobs:
- uses: ./.github/actions/environment
- run: nix-shell --run "tests/download_emulators.sh"
- run: nix-shell --run "poetry run pytest tests/upgrade_tests"
- uses: actions/upload-artifact@v4
with:
name: core-test-upgrade-${{ matrix.model }}-${{ matrix.asan }}
path: |
tests/trezor.log
retention-days: 7
if: always()


# Persistence tests - UI.
Expand All @@ -399,6 +407,7 @@ jobs:
env:
TREZOR_PROFILING: ${{ matrix.asan == 'noasan' && '1' || '0' }}
PYTEST_TIMEOUT: 400
TREZOR_PYTEST_LOGS_DIR: ${{ github.workspace }}/tests/
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -421,6 +430,13 @@ jobs:
if: always()
continue-on-error: true
- uses: ./.github/actions/upload-coverage
- uses: actions/upload-artifact@v4
with:
name: core-test-persistence-${{ matrix.model }}-${{ matrix.asan }}
path: |
tests/trezor.log
retention-days: 7
if: always()

core_hwi_test:
name: HWI tests
Expand Down
13 changes: 13 additions & 0 deletions python/src/trezorlib/_internal/emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.

import atexit
import logging
import os
import subprocess
Expand All @@ -27,6 +28,15 @@
LOG = logging.getLogger(__name__)

EMULATOR_WAIT_TIME = 60
_RUNNING_PIDS = set()


def _cleanup_pids():
for process in _RUNNING_PIDS:
process.kill()


atexit.register(_cleanup_pids)


def _rm_f(path: Path) -> None:
Expand Down Expand Up @@ -130,6 +140,7 @@ def wait_until_ready(self, timeout: float = EMULATOR_WAIT_TIME) -> None:
def wait(self, timeout: Optional[float] = None) -> int:
assert self.process is not None, "Emulator not started"
ret = self.process.wait(timeout=timeout)
_RUNNING_PIDS.remove(self.process)
self.process = None
self.stop()
return ret
Expand Down Expand Up @@ -164,6 +175,7 @@ def start(self) -> None:
return

self.process = self.launch_process()
_RUNNING_PIDS.add(self.process)
try:
self.wait_until_ready()
except TimeoutError:
Expand Down Expand Up @@ -197,6 +209,7 @@ def stop(self) -> None:
except subprocess.TimeoutExpired:
LOG.info("Emulator seems stuck. Sending kill signal.")
self.process.kill()
_RUNNING_PIDS.remove(self.process)

_rm_f(self.profile_dir / "trezor.pid")
_rm_f(self.profile_dir / "trezor.port")
Expand Down
3 changes: 2 additions & 1 deletion tests/emulators.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def __exit__(self, exc_type, exc_value, traceback) -> None:
if logs_dir is not None:
src = Path(self.profile_dir.name) / "trezor.log"
dst = Path(logs_dir) / f"trezor-{self.worker_id}.log"
shutil.move(src, dst)
with open(src, "rb") as src, open(dst, "ab") as dst:
shutil.copyfileobj(src, dst)

self.profile_dir.cleanup()
Loading