summaryrefslogtreecommitdiff
path: root/tempest/tests/lib/common/utils/linux/test_remote_client.py
blob: df23e637cac0fabb508faded592570484ba1cbba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Copyright 2017 NEC Corporation.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from unittest import mock

from tempest.lib.common import ssh
from tempest.lib.common.utils.linux import remote_client
from tempest.lib import exceptions as lib_exc
from tempest.tests import base


class FakeServersClient(object):

    def get_console_output(self, server_id):
        return {"output": "fake_output"}


class TestRemoteClient(base.TestCase):

    @mock.patch.object(ssh.Client, 'exec_command', return_value='success')
    def test_exec_command(self, mock_ssh_exec_command):
        client = remote_client.RemoteClient('192.168.1.10', 'username')
        client.exec_command('ls')
        mock_ssh_exec_command.assert_called_once_with(
            'set -eu -o pipefail; PATH=$PATH:/sbin; ls')

    @mock.patch.object(ssh.Client, 'test_connection_auth')
    def test_validate_authentication(self, mock_test_connection_auth):
        client = remote_client.RemoteClient('192.168.1.10', 'username')
        client.validate_authentication()
        mock_test_connection_auth.assert_called_once_with()

    @mock.patch.object(remote_client.LOG, 'debug')
    @mock.patch.object(ssh.Client, 'exec_command')
    def test_debug_ssh_without_console(self, mock_exec_command, mock_debug):
        mock_exec_command.side_effect = lib_exc.SSHTimeout
        server = {'id': 'fake_id'}
        client = remote_client.RemoteClient('192.168.1.10', 'username',
                                            server=server)
        self.assertRaises(lib_exc.SSHTimeout, client.exec_command, 'ls')
        mock_debug.assert_called_with(
            'Caller: %s. Timeout trying to ssh to server %s',
            'TestRemoteClient:test_debug_ssh_without_console', server)

    @mock.patch.object(remote_client.LOG, 'debug')
    @mock.patch.object(ssh.Client, 'exec_command')
    def test_debug_ssh_with_console(self, mock_exec_command, mock_debug):
        mock_exec_command.side_effect = lib_exc.SSHTimeout
        server = {'id': 'fake_id'}
        client = remote_client.RemoteClient('192.168.1.10', 'username',
                                            server=server,
                                            servers_client=FakeServersClient())
        self.assertRaises(lib_exc.SSHTimeout, client.exec_command, 'ls')
        mock_debug.assert_called_with(
            'Console log for server %s: %s', server['id'], 'fake_output')