Вызов функции python внутри ожидаемого кода скрипта, который находится в том же файле python - PullRequest
0 голосов
/ 29 апреля 2020

Я пишу сценарий testcase Python, в котором я подключаюсь к удаленному серверу

cmd ="/usr/bin/expect -c 'spawn ssh "+ ip +" \"/usr/bin/expect -c \'\\\'\'spawn some_os_command; expect \\\"continue connecting (yes/no)?\\\" { send yes\r}; expect \\\"password: \\\" { send \\\""+ pass +"\r\\\"};interact\'\\\'\'\" ; expect \\\"~]#\\\" { send echo -\r}; expect \"password: \" { send \""+ pass +"\r\"};expect \"~]#\" { send echo -\r};expect \"continue connecting (yes/no)?\" { send yes\r};interact'"

proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
_output, _error = proc.communicate()
_code = proc.returncode
return _output, _error, _code

Приведенный выше код проверяет подключение к удаленному серверу.

Теперь я должен вызвать приведенная ниже python функция

def checkLatencyTime(file_search, corr_check = False):
    cmdStr = ""
    return_code = 3
    _output, _error, _code = execute("ls -ltr " + file_search + " | grep -v tmp | tail -1")
    if _output:
        cmdStr = "cat " + _output.split()[8]
        _output, _error, _code = execute(cmdStr)
        if _output:
            epoch_value = 0
            m = re.match(r'\d+\.\d*', _output)
            if m:
                epoch_value = m.group()
            else:
                m = re.search(r'time\":(\d+\.\d*),', _output)
                if m:
                    epoch_value = m.group(1)
            if float(epoch_value) > 0:
                if (time.time() - float(epoch_value)) < 3600:
                    return_code = 0
    return _output, _error, return_code, cmdStr

Я пытаюсь вызвать эту функцию выше python вместо some_os_command внутри сценария ожидаемого . Весь этот код находится внутри одного и того же теста python file

...