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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
| | #! /usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-or-later
#
# Copyright Red Hat
# Author: David Gibson <david@gibson.dropbear.id.au>
"""
Test A Simple Socket Transport
nstool.py - Run commands in namespaces via 'nstool'
"""
import contextlib
import os
import subprocess
import tempfile
import exeter
from .snh import RealHost, SimNetHost
# FIXME: Can this be made more portable?
UNIX_PATH_MAX = 108
NSTOOL_BIN = 'test/nstool'
class NsTool(SimNetHost):
"""A bundle of Linux namespaces managed by nstool"""
def __init__(self, name, sockpath, parent=RealHost()):
if len(sockpath) > UNIX_PATH_MAX:
raise ValueError(
f'Unix domain socket path "{sockpath}" is too long'
)
super().__init__(name)
self.sockpath = sockpath
self.parent = parent
self._pid = None
def __enter__(self):
cmd = [f'{NSTOOL_BIN}', 'info', '-wp', f'{self.sockpath}']
pid = self.parent.output(*cmd, timeout=1)
self._pid = int(pid)
return self
def __exit__(self, *exc_details):
pass
# PID of the nstool hold process as seen by the parent snh
def pid(self):
return self._pid
# PID of the nstool hold process as seen by another snh which can
# see the nstool socket (important when using PID namespaces)
def relative_pid(self, relative_to):
cmd = [f'{NSTOOL_BIN}', 'info', '-p', f'{self.sockpath}']
relpid = relative_to.output(*cmd)
return int(relpid)
def hostify(self, *cmd, capable=False, **kwargs):
hostcmd = [f'{NSTOOL_BIN}', 'exec']
if capable:
hostcmd.append('--keep-caps')
hostcmd += [self.sockpath, '--']
hostcmd += list(cmd)
return hostcmd, kwargs
@contextlib.contextmanager
def unshare_snh(name, *opts, parent=RealHost(), capable=False):
# Create path for temporary nstool Unix socket
with tempfile.TemporaryDirectory() as tmpd:
sockpath = os.path.join(tmpd, name)
cmd = ['unshare'] + list(opts)
cmd += ['--', f'{NSTOOL_BIN}', 'hold', f'{sockpath}']
with parent.bg(*cmd, capable=capable) as holder:
try:
with NsTool(name, sockpath, parent=parent) as snh:
yield snh
finally:
try:
parent.fg(f'{NSTOOL_BIN}', 'stop', f'{sockpath}')
finally:
try:
holder.run(timeout=0.1)
holder.kill()
finally:
try:
os.remove(sockpath)
except FileNotFoundError:
pass
TEST_EXC = ValueError
def test_sockdir_cleanup(s):
def mess(sockpaths):
with s as snh:
ns = snh
while isinstance(ns, NsTool):
sockpaths.append(ns.sockpath)
ns = ns.parent
raise TEST_EXC
sockpaths = []
exeter.assert_raises(TEST_EXC, mess, sockpaths)
assert sockpaths
for path in sockpaths:
assert not os.path.exists(os.path.dirname(path))
@contextlib.contextmanager
def userns_snh():
with unshare_snh('usernetns', '-Ucn') as ns:
ns.ifup('lo')
yield ns
@exeter.test
def test_userns():
cmd = ['capsh', '--has-p=CAP_SETUID']
with RealHost() as realhost:
status = realhost.fg(*cmd, check=False)
assert status.returncode != 0
with userns_snh() as ns:
ns.fg(*cmd, capable=True)
@contextlib.contextmanager
def nested_snh():
with unshare_snh('userns', '-Uc') as userns:
with unshare_snh('netns', '-n', parent=userns, capable=True) as netns:
netns.ifup('lo')
yield netns
@contextlib.contextmanager
def pidns_snh():
with unshare_snh('pidns', '-Upfn') as ns:
ns.ifup('lo')
yield ns
@exeter.test
def test_relative_pid():
with pidns_snh() as snh:
# The holder is init (pid 1) within its own pidns
exeter.assert_eq(snh.relative_pid(snh), 1)
# General tests for all the nstool examples
for setup in [userns_snh, nested_snh, pidns_snh]:
# Common snh tests
SimNetHost.selftest_isolated(setup)
exeter.register_pipe(f'{setup.__qualname__}|test_sockdir_cleanup',
setup, test_sockdir_cleanup)
@contextlib.contextmanager
def connect_snh():
with tempfile.TemporaryDirectory() as tmpd:
sockpath = os.path.join(tmpd, 'nons')
holdcmd = [f'{NSTOOL_BIN}', 'hold', f'{sockpath}']
with subprocess.Popen(holdcmd) as holder:
try:
with NsTool("fakens", sockpath) as snh:
yield snh
finally:
holder.kill()
os.remove(sockpath)
SimNetHost.selftest(connect_snh)
|