#! /usr/bin/env python3 # # SPDX-License-Identifier: GPL-2.0-or-later # # TASST - Test A Simple Socket Transport # # test/tasst/pasta.py - Helpers for seeting up pasta instances # # Copyright Red Hat # Author: David Gibson import contextlib import os from typing import Iterator import tunbridge @contextlib.contextmanager def pasta(host: tunbridge.Site, guest: tunbridge.Site, *opts: str) \ -> Iterator[tunbridge.site.SiteProcess]: if tunbridge.unshare.parent(guest) is not host: raise ValueError("pasta guest must be a namespace under host site") # This implies guest is a namespace site assert isinstance(guest, tunbridge.unshare.NsenterSite) exe = os.environ['PASTA'] with host.tempdir() as piddir: pidfile = os.path.join(piddir, 'pasta.pid') cmd = [exe, '-f', '-P', pidfile] + list(opts) + [f'{guest.pid}'] with host.bg(*cmd, stop=True) as pasta: # Wait for the PID file to be written pidstr = None while not pidstr: pidstr = host.readfile(pidfile, check=False) pid = int(pidstr) print(f'pasta started, host: {host}, guest: {guest}, pid: {pid}') yield pasta