salt/tests/unit/ssh/test_ssh.py
Benjamin Drung 3d6f658309 Skip SSHPasswordTests if ssh binary is not found
If the ssh binary is not found in path, SSHPasswordTests will fail:

ERROR: test_password_failure (unit.ssh.test_ssh.SSHPasswordTests)
[CPU:0.0%|MEM:54.9%]
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests/unit/ssh/test_ssh.py", line 42, in test_password_failure
    client = ssh.SSH(opts)
  File "salt/client/ssh/__init__.py", line 226, in __init__
    raise salt.exceptions.SaltSystemExit('No ssh binary found in path -- ssh must be '
salt.exceptions.SaltSystemExit: None

Either SSHPasswordTests needs to be converted into a real unit test
(i.e. mocking the salt call) or this tests needs to be skipped if ssh is
not available.

Signed-off-by: Benjamin Drung <benjamin.drung@profitbricks.com>
2018-03-07 16:36:06 +01:00

50 lines
1.9 KiB
Python

# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Daniel Wallace <dwallace@saltstack.com`
'''
# Import python libs
from __future__ import absolute_import, unicode_literals
import os
# Import Salt Testing libs
from tests.support.unit import skipIf
from tests.support.case import ShellCase
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, MagicMock
# Import Salt libs
import salt.config
import salt.utils.path
from salt.client import ssh
@skipIf(NO_MOCK, NO_MOCK_REASON)
@skipIf(not salt.utils.path.which('ssh'), "No ssh binary found in path")
class SSHPasswordTests(ShellCase):
def test_password_failure(self):
'''
Check password failures when trying to deploy keys
'''
opts = salt.config.client_config(self.get_config_file_path('master'))
opts['list_hosts'] = False
opts['argv'] = ['test.ping']
opts['selected_target_option'] = 'glob'
opts['tgt'] = 'localhost'
opts['arg'] = []
roster = os.path.join(self.get_config_dir(), 'roster')
handle_ssh_ret = [
{'localhost': {'retcode': 255, 'stderr': u'Permission denied (publickey).\r\n', 'stdout': ''}},
]
expected = {'localhost': 'Permission denied (publickey)'}
display_output = MagicMock()
with patch('salt.roster.get_roster_file', MagicMock(return_value=roster)), \
patch('salt.client.ssh.SSH.handle_ssh', MagicMock(return_value=handle_ssh_ret)), \
patch('salt.client.ssh.SSH.key_deploy', MagicMock(return_value=expected)), \
patch('salt.output.display_output', display_output):
client = ssh.SSH(opts)
ret = next(client.run_iter())
with self.assertRaises(SystemExit):
client.run()
display_output.assert_called_once_with(expected, 'nested', opts)
self.assertIs(ret, handle_ssh_ret[0])