Skip to content

Commit

Permalink
tests: verify ssh driver setup based on different ssh version
Browse files Browse the repository at this point in the history
Signed-off-by: Larry Shen <[email protected]>
  • Loading branch information
atline committed Oct 10, 2023
1 parent 61d23ed commit f94bc01
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions tests/test_sshdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@

@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)
instance_mock.communicate = mocker.MagicMock(return_value=(b"", b""))
instance_mock.returncode = 0
SSHDriver(target, "ssh")
s = target.get_driver("SSHDriver")
return s
def _ssh_driver_mocked_and_activated(ssh_info="OpenSSH_8.9p1"):
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)
instance_mock.communicate = mocker.MagicMock(return_value=(b"", b""))
instance_mock.returncode = 0
run = mocker.patch('subprocess.run')
run_mock = mocker.MagicMock()
run.return_value = run_mock
run_mock.stderr = ssh_info
SSHDriver(target, "ssh")
s = target.get_driver("SSHDriver")
return s
return _ssh_driver_mocked_and_activated

def test_create_fail_missing_resource(target):
with pytest.raises(NoResourceFoundError):
Expand All @@ -39,19 +45,28 @@ def test_create(target, mocker):
instance_mock.wait = mocker.MagicMock(return_value=0)
instance_mock.communicate = mocker.MagicMock(return_value=(b"", b""))
instance_mock.returncode = 0
version_check = mocker.patch('labgrid.driver.sshdriver.SSHDriver._ssh_version_check')
version_check.return_value = True
s = SSHDriver(target, "ssh")
assert isinstance(s, SSHDriver)

def test_run_check(ssh_driver_mocked_and_activated, mocker):
s = ssh_driver_mocked_and_activated
@pytest.mark.parametrize(
"ssh_info",
[
"OpenSSH_8.2p1 Ubuntu-4ubuntu0.9, OpenSSL 1.1.1f 31 Mar 2020",
"OpenSSH_8.9p1 Ubuntu-3ubuntu0.4, OpenSSL 3.0.2f 15 Mar 2022"
],
)
def test_run_check(ssh_driver_mocked_and_activated, mocker, ssh_info):
s = ssh_driver_mocked_and_activated(ssh_info)
s._run = mocker.MagicMock(return_value=(['success'], [], 0))
res = s.run_check("test")
assert res == ['success']
res = s.run("test")
assert res == (['success'], [], 0)

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

0 comments on commit f94bc01

Please sign in to comment.