Skip to content

Commit

Permalink
Merge 9727023 into ff0fc3e
Browse files Browse the repository at this point in the history
  • Loading branch information
Emantor committed Mar 14, 2018
2 parents ff0fc3e + 9727023 commit b5c3bfa
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 74 deletions.
18 changes: 0 additions & 18 deletions labgrid/driver/bareboxdriver.py
Expand Up @@ -89,24 +89,6 @@ def run(self, cmd: str, *, step, timeout: int = 30):
else:
return None

@Driver.check_active
def run_check(self, cmd: str, timeout: int = 30):
"""
Runs the specified command on the shell and returns the output if successful,
raises ExecutionError otherwise.
Args:
cmd (str): command to run on the shell
timeout (int): optional, timeout in seconds
Returns:
List[str]: stdout of the executed command
"""
res = self.run(cmd, timeout=timeout)
if res[2] != 0:
raise ExecutionError(cmd)
return res[0]

@Driver.check_active
@step()
def reset(self):
Expand Down
33 changes: 33 additions & 0 deletions labgrid/driver/commandmixin.py
Expand Up @@ -22,3 +22,36 @@ def wait_for(self, cmd, pattern, timeout=30.0, sleepduration=1):
sleep(sleepduration)
if timeout.expired:
raise ExecutionError("Wait timeout expired")

@step(args=['cmd'], result=True)
def _run_check(self, cmd: str, timeout=30):
"""
Internal function which runs the specified command on the shell and
returns the output if successful, raises ExecutionError otherwise.
Args:
cmd (str): command to run on the shell
Returns:
List[str]: stdout of the executed command
"""
stdout, stderr, exitcode = self.run(cmd, timeout=timeout)
if exitcode != 0:
raise ExecutionError(cmd, stdout, stderr)
return stdout

@Driver.check_active
@step(args=['cmd'], result=True)
def run_check(self, cmd: str, timeout=30):
"""
External run_check function, only available if the driver is active.
Runs the supplied command and returns the stdout, raises an
ExecutionError otherwise.
Args:
cmd (str): command to run on the shell
Returns:
List[str]: stdout of the executed command
"""
return self._run_check(cmd,timeout)
2 changes: 2 additions & 0 deletions labgrid/driver/exception.py
Expand Up @@ -4,6 +4,8 @@
@attr.s(cmp=False)
class ExecutionError(Exception):
msg = attr.ib(validator=attr.validators.instance_of(str))
stdout = attr.ib(default=None, validator=attr.validators.optional(attr.validators.instance_of(list)))
stderr = attr.ib(default=None, validator=attr.validators.optional(attr.validators.instance_of(list)))


@attr.s(cmp=False)
Expand Down
18 changes: 0 additions & 18 deletions labgrid/driver/shelldriver.py
Expand Up @@ -121,24 +121,6 @@ def _await_login(self):
raise Exception("Password entry needed but no password set")
self._check_prompt()

@step(args=['cmd'], result=True)
def _run_check(self, cmd, timeout=30):
out, _, res = self._run(cmd, timeout=timeout)
if res != 0:
raise ExecutionError(cmd)
return out

@Driver.check_active
def run_check(self, cmd, timeout=30):
"""
Runs the specified cmd on the shell and returns the output if successful,
raises ExecutionError otherwise.
Arguments:
cmd - cmd to run on the shell
"""
return self._run_check(cmd, timeout=timeout)

@step()
def get_status(self):
"""Returns the status of the shell-driver.
Expand Down
15 changes: 0 additions & 15 deletions labgrid/driver/sshdriver.py
Expand Up @@ -137,21 +137,6 @@ def run(self, cmd):
stderr.pop()
return (stdout, stderr, sub.returncode)

@Driver.check_active
@step(args=['cmd'])
def run_check(self, cmd):
"""
Runs the specified cmd on the shell and returns the output if successful,
raises ExecutionError otherwise.
Arguments:
cmd - cmd to run on the shell
"""
res = self.run(cmd)
if res[2] != 0:
raise ExecutionError(cmd)
return res[0]

def get_status(self):
"""The SSHDriver is always connected, return 1"""
return 1
Expand Down
21 changes: 0 additions & 21 deletions labgrid/driver/ubootdriver.py
Expand Up @@ -105,27 +105,6 @@ def run(self, cmd):
"""
return self._run(cmd)

@step(args=['cmd'], result=True)
def _run_check(self, cmd):
res = self._run(cmd)
if res[2] != 0:
raise ExecutionError(cmd)
return res[0]

@Driver.check_active
def run_check(self, cmd):
"""
Runs the specified command on the shell and returns the output if successful,
raises ExecutionError otherwise.
Args:
cmd (str): command to run on the shell
Returns:
List[str]: stdout of the executed command
"""
return self._run_check

def get_status(self):
"""Retrieve status of the UBootDriver.
0 means inactive, 1 means active.
Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Expand Up @@ -9,12 +9,18 @@
from labgrid.driver import SerialDriver
from labgrid.protocol import CommandProtocol, ConsoleProtocol
from labgrid.resource import RawSerialPort, NetworkSerialPort
from labgrid.driver.fake import FakeConsoleDriver


@pytest.fixture(scope='function')
def target():
return Target('Test')

@pytest.fixture(scope='function')
def target_with_fakeconsole():
t = Target('dummy')
cp = FakeConsoleDriver(t, "console")
return t

@pytest.fixture(scope='function')
def serial_port(target):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_bareboxdriver.py
Expand Up @@ -6,6 +6,7 @@
from labgrid.driver.fake import FakeConsoleDriver
from labgrid.protocol import (CommandProtocol, ConsoleProtocol,
LinuxBootProtocol)
from labgrid.driver import ExecutionError


class TestBareboxDriver:
Expand All @@ -16,3 +17,19 @@ def test_create(self):
assert (isinstance(d, BareboxDriver))
assert (isinstance(d, CommandProtocol))
assert (isinstance(d, LinuxBootProtocol))

def test_barebox_run(self, target_with_fakeconsole, mocker):
t = target_with_fakeconsole
d = BareboxDriver(t, "barebox")
d = t.get_driver(BareboxDriver)
d.run = mocker.MagicMock(return_value=[['success'],[],0])
res = d.run_check("test")
assert res == ['success']

def test_barebox_run_error(self, target_with_fakeconsole, mocker):
t = target_with_fakeconsole
d = BareboxDriver(t, "barebox")
d = t.get_driver(BareboxDriver)
d.run = mocker.MagicMock(return_value=[['error'],[],1])
with pytest.raises(ExecutionError):
res = d.run_check("test")
29 changes: 28 additions & 1 deletion tests/test_shelldriver.py
@@ -1,6 +1,6 @@
import pytest

from labgrid.driver import ShellDriver
from labgrid.driver import ShellDriver, ExecutionError
from labgrid.exceptions import NoDriverFoundError


Expand All @@ -12,3 +12,30 @@ def test_instance(self, target, serial_driver):
def test_no_driver(self, target):
with pytest.raises(NoDriverFoundError):
ShellDriver(target, "shell", "", "", "")

def test_run(self, target_with_fakeconsole, mocker):
t = target_with_fakeconsole
d = ShellDriver(t, "shell", prompt='dummy', login_prompt='dummy', username='dummy')
d.on_activate = mocker.MagicMock()
d = t.get_driver('ShellDriver')
d.run = mocker.MagicMock(return_value=[['success'],[],0])
res = d.run_check("test")
assert res == ['success']

def test_run_error(self, target_with_fakeconsole, mocker):
t = target_with_fakeconsole
d = ShellDriver(t, "shell", prompt='dummy', login_prompt='dummy', username='dummy')
d.on_activate = mocker.MagicMock()
d = t.get_driver('ShellDriver')
d.run = mocker.MagicMock(return_value=[['error'],[],1])
with pytest.raises(ExecutionError):
res = d.run_check("test")

def test_run_with_timeout(self, target_with_fakeconsole, mocker):
t = target_with_fakeconsole
d = ShellDriver(t, "shell", prompt='dummy', login_prompt='dummy', username='dummy')
d.on_activate = mocker.MagicMock()
d = t.get_driver('ShellDriver')
d.run = mocker.MagicMock(return_value=[['success'],[],0])
res = d.run_check("test", timeout=30.0)
assert res == ['success']
28 changes: 27 additions & 1 deletion tests/test_sshdriver.py
@@ -1,9 +1,23 @@
import pytest

from labgrid.driver import SSHDriver
from labgrid.driver import SSHDriver, ExecutionError
from labgrid.exceptions import NoResourceFoundError
from labgrid.resource import NetworkService

@pytest.fixture(scope='function')
def ssh_driver_mocked_and_activated(target, mocker):
NetworkService(target, "service", "1.2.3.4", "root")
call = mocker.patch('subprocess.call')
call.return_value = 0
popen = mocker.patch('subprocess.Popen', autospec=True)
path = mocker.patch('os.path.exists')
path.return_value = True
instance_mock = mocker.MagicMock()
popen.return_value = instance_mock
instance_mock.wait = mocker.MagicMock(return_value=0)
SSHDriver(target, "ssh")
s = target.get_driver("SSHDriver")
return s

class TestSSHDriver:
def test_create_fail_missing_resource(self, target):
Expand All @@ -22,3 +36,15 @@ def test_create(self, target, mocker):
instance_mock.wait = mocker.MagicMock(return_value=0)
s = SSHDriver(target, "ssh")
assert (isinstance(s, SSHDriver))

def test_run_check(self, ssh_driver_mocked_and_activated, mocker):
s = ssh_driver_mocked_and_activated
s.run = mocker.MagicMock(return_value=[['success'],[],0])
res = s.run_check("test")
assert res == ['success']

def test_run_check_raise(self, ssh_driver_mocked_and_activated, mocker):
s = ssh_driver_mocked_and_activated
s.run = mocker.MagicMock(return_value=[['error'],[],1])
with pytest.raises(ExecutionError):
res = s.run_check("test")

0 comments on commit b5c3bfa

Please sign in to comment.