Esempio n. 1
0
def test_utils_ssh_connect_exception(mock_connect, mock_sleep, mock_time):
    """Test exception raised connecting to ssh."""
    mock_connect.side_effect = paramiko.ssh_exception.SSHException('ERROR!')
    mock_sleep.return_value = None
    mock_time.side_effect = [0, 0, 2]

    with pytest.raises(IpaSSHException) as error:
        ipa_utils.get_ssh_client(LOCALHOST, 'tests/data/ida_test', timeout=1)

    assert str(error.value) == 'Attempt to establish SSH connection failed.'
    assert mock_connect.call_count > 0
Esempio n. 2
0
def test_utils_get_ssh_connection(mock_connect, mock_exec_cmd):
    """Test successful ssh connection."""
    stdin = stdout = stderr = MagicMock()
    stderr.read.return_value = b''

    mock_connect.return_value = None
    mock_exec_cmd.return_value = (stdin, stdout, stderr)

    ipa_utils.get_ssh_client(LOCALHOST, 'tests/data/ida_test')
    assert mock_connect.call_count == 1
    assert mock_exec_cmd.call_count == 1

    # Clear cache for subsequent tests.
    ipa_utils.clear_cache()
Esempio n. 3
0
def test_utils_clear_cache(mock_exec_cmd):
    """Test img_proof utils client cache and clear specific ip."""
    client = MagicMock()
    ipa_utils.CLIENT_CACHE[LOCALHOST] = client

    val = ipa_utils.get_ssh_client(LOCALHOST, 'tests/data/ida_test')
    assert client == val

    # Test clear specfic IP
    ipa_utils.clear_cache(LOCALHOST)
    with pytest.raises(KeyError):
        ipa_utils.CLIENT_CACHE[LOCALHOST]

    ipa_utils.CLIENT_CACHE[LOCALHOST] = client

    # Test clear all cache
    ipa_utils.clear_cache()
    with pytest.raises(KeyError):
        ipa_utils.CLIENT_CACHE[LOCALHOST]
Esempio n. 4
0
 def _get_ssh_client(self):
     """Return a new or existing SSH client for given ip."""
     return ipa_utils.get_ssh_client(self.instance_ip,
                                     self.ssh_private_key_file,
                                     self.ssh_user,
                                     timeout=self.timeout)