Skip to content

Commit 30acc8a

Browse files
fengxxphilpep
authored andcommitted
add chroot support
1 parent 53321ac commit 30acc8a

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

testinfra/backend/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"winrm": "testinfra.backend.winrm.WinRMBackend",
2828
"lxc": "testinfra.backend.lxc.LxcBackend",
2929
"openshift": "testinfra.backend.openshift.OpenShiftBackend",
30+
"chroot": "testinfra.backend.chroot.ChrootBackend",
3031
}
3132

3233

testinfra/backend/chroot.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
import os.path
13+
14+
from testinfra.backend import base
15+
16+
17+
class ChrootBackend(base.BaseBackend):
18+
"""Run commands in a chroot folder
19+
20+
Requires root access or sudo
21+
Can be invoked by --hosts=/path/to/chroot/ --connection=chroot --sudo
22+
"""
23+
24+
NAME = "chroot"
25+
26+
def __init__(self, name, *args, **kwargs):
27+
self.name = name
28+
super().__init__(self.name, *args, **kwargs)
29+
30+
def run(self, command, *args, **kwargs):
31+
if not os.path.exists(self.name) and os.path.isdir(self.name):
32+
raise RuntimeError(
33+
"chroot path {} not found or not a directory".format(self.name)
34+
)
35+
cmd = self.get_command(command, *args)
36+
out = self.run_local("chroot %s /bin/sh -c %s", self.name, cmd)
37+
out.command = self.encode(cmd)
38+
return out

0 commit comments

Comments
 (0)