如何为此编写测试用例?

时间:2019-11-29 06:47:52

标签: python mocking python-unittest

import paramiko
import os
from subprocess import PIPE, Popen, check_output
import config
import logging
logging = logging.getLogger(__name__)


class RemoteServer:
    """
    This class used to connect to remove server
    """
    TIMEOUT = 10

    def __init__(self, host_name, user_name, pass_word=None):
        ''' user to connect remote host_name and its return connection object'''
        self.host_name = host_name
        self.user_name = user_name
        self.pass_word = pass_word
        self.client = paramiko.SSHClient()
        try:
            self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            if pass_word is not None:
                self.client.connect(
                    hostname=self.host_name, username=self.user_name, port=22, password=self.pass_word)
            else:
                self.client.connect(hostname=self.host_name,
                                    username=self.user_name, port=22)
        except paramiko.AuthenticationException as exception:
            logging.error("Exception : %s ", str(exception))

    def close(self):
        '''Close the ssh connection'''
        try:
            if self.client is not None:
                self.client.close()
                self.client = None
        except Exception as exception:
            logging.error("Exception : %s ", str(exception))

    def execute(self, command):
        '''Executing commands in remote machine'''
        if command is not None:
            try:
                stdin, stdout, stderr = self.client.exec_command(command)
                logging.info('Command : %s,Output : %s,Error : %s',
                             command, stdout, stderr)
                return {
                    'out': stdout.readlines(),
                    'err': stderr.readlines(),
                    'exit_status': stdout.channel.recv_exit_status()
                }
            except Exception as exception:
                logging.error("Exception : %s ",  str(exception))

0 个答案:

没有答案