1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
| | #! /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 <david@gibson.dropbear.id.au>
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
|