Skip to content

Commit

Permalink
Add a sudo option
Browse files Browse the repository at this point in the history
  • Loading branch information
philpep committed May 21, 2015
1 parent e648054 commit 97ed08c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
5 changes: 5 additions & 0 deletions testinfra/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ def __repr__(self):

class BaseBackend(object):

def __init__(self, *args, **kwargs):
if args or kwargs:
raise RuntimeError("Unexpected arguments %s %s" % (args, kwargs))
super(BaseBackend, self).__init__()

def quote(self, command, *args):
if args:
return command % tuple(pipes.quote(a) for a in args)
Expand Down
6 changes: 6 additions & 0 deletions testinfra/backend/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@

class LocalBackend(base.BaseBackend):

def __init__(self, sudo=False, *args, **kwargs):
self.sudo = sudo
super(LocalBackend, self).__init__(*args, **kwargs)

def run(self, command, *args, **kwargs):
if self.sudo:
command = "sudo " + command
command = self.quote(command, *args)
logger.info("RUN %s", command)
p = subprocess.Popen(
Expand Down
5 changes: 4 additions & 1 deletion testinfra/backend/paramiko.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@

class ParamikoBakend(base.BaseBackend):

def __init__(self, hostspec, ssh_config=None, *args, **kwargs):
def __init__(self, hostspec, ssh_config=None, sudo=False, *args, **kwargs):
self.host, self.user, self.port = self.parse_hostspec(hostspec)
self.ssh_config = ssh_config
self.sudo = sudo
self._client = None
super(ParamikoBakend, self).__init__(*args, **kwargs)

Expand Down Expand Up @@ -73,6 +74,8 @@ def client(self):
return self._client

def run(self, command, *args, **kwargs):
if self.sudo:
command = "sudo " + command
command = self.quote(command, *args)
chan = self.client.get_transport().open_session()
logger.info("RUN %s", command)
Expand Down
5 changes: 4 additions & 1 deletion testinfra/backend/salt.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@

class SaltBackend(base.BaseBackend):

def __init__(self, host, *args, **kwargs):
def __init__(self, host, sudo=False, *args, **kwargs):
self.host = host
self._client = None
self.sudo = sudo
super(SaltBackend, self).__init__(*args, **kwargs)

@property
Expand All @@ -43,6 +44,8 @@ def client(self):
return self._client

def run(self, command, *args):
if self.sudo:
command = "sudo " + command
command = self.quote(command, *args)
out = self.client.cmd(
self.host, 'cmd.run_all', [command],
Expand Down
16 changes: 12 additions & 4 deletions testinfra/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@

@pytest.fixture(scope="module")
def testinfra_backend(pytestconfig, _testinfra_host):
kwargs = {}
if pytestconfig.option.ssh_config is not None:
kwargs["ssh_config"] = pytestconfig.option.ssh_config
if pytestconfig.option.sudo is not None:
kwargs["sudo"] = pytestconfig.option.sudo
if _testinfra_host is not None:
backend_type = pytestconfig.option.connection or "paramiko"
kwargs = {}
if pytestconfig.option.ssh_config is not None:
kwargs["ssh_config"] = pytestconfig.option.ssh_config
testinfra.set_backend(
backend_type,
_testinfra_host,
**kwargs)
else:
testinfra.set_backend("local")
testinfra.set_backend("local", **kwargs)


def pytest_addoption(parser):
Expand All @@ -66,6 +68,12 @@ def pytest_addoption(parser):
dest="ssh_config",
help="SSH config file",
)
group._addoption(
"--sudo",
action="store_true",
dest="sudo",
help="Use sudo",
)
group._addoption(
"--nagios",
action="store_true",
Expand Down

0 comments on commit 97ed08c

Please sign in to comment.