IOError:[Errno 2]没有这样的文件或目录-在Linux上,使用绝对路径

时间:2019-02-04 19:29:08

标签: python pytest

我正在努力寻找解决方案。我正在使用绝对存在于文件系统中的绝对路径。当在同一脚本中使用相同的确切路径时,

if host.file(logPath).exists:
    print("Exists: " + logPath)

我明白了

Exists: /var/opt/jws/jws3.0/v7/testinfra_node1/logs/catalina.out

但是尝试时:

with open(logPath, "rt") as log:

我得到:

>               with open(logPath, "rt") as log:
E               IOError: [Errno 2] No such file or directory: '/var/opt/jws/jws3.0/v7/testinfra_node1/logs/catalina.out'

这是完整的代码(这是用于测试JWT安装的testinfra脚本):

import pytest
import testinfra
import time
import os

@pytest.mark.parametrize("jws_version", [("3.0")])

def test_server_recycle(host, jws_version):
    instances = host.check_output("cat /var/opt/jws/jws" + jws_version + "/init/init_instances | grep -oP '(\/.*?\/)((?:[^\/]|\\\\/)+?)(?:(?<!\\\\)\s|$)'")
last = ""
for last in iter(instances.splitlines()):
    pass
last = last.strip().encode('ascii','ignore')
print(last)

instanceName = ""
for instanceName in iter(last.split("/")):
    pass
print(instanceName)

binPath = last + "/bin/"
logDir = last + "/logs/"
logPath = os.path.join(logDir, "catalina.out")
print(logPath)

runningAt0 = isInstanceRunning(host, instanceName)
if host.file(logPath).exists:
    print("Exists: " + logPath)

if (runningAt0):
    with host.sudo():
        host.run(os.path.join(binPath, "shutdown.sh"))    
        with open(logPath, "rt") as log:
            result = waitForEntry(log, "INFO  org.apache.catalina.core.StandardService- Stopping service Catalina","ERROR")
        assert not result.eq("ERROR")
        assert not isInstanceRunning(host, instanceName)
        host.run(os.path.join(binPath, "/bin/startup.sh"))
        with open(logPath, "rt") as log:
            result = waitForEntry(log, "INFO  org.apache.catalina.startup.Catalina- Server startup in","ERROR")
        assert not result.eq("ERROR")
        assert isInstanceRunning(host, instanceName)
else:
    with host.sudo():
        host.run(os.path.join(binPath, "startup.sh"))
        with open(logPath, "rt") as log:
            result = waitForEntry(log, "INFO  org.apache.catalina.core.StandardService- Stopping service Catalina","ERROR")
        assert not result.eq("ERROR")
        assert isInstanceRunning(host, instanceName)
        host.run(os.path.join(binPath, "shutdown.sh"))
        with open(logPath, "rt") as log:
            result = waitForEntry(log, "INFO  org.apache.catalina.startup.Catalina- Server startup in","ERROR")
        assert not result.eq("ERROR")
        assert not isInstanceRunning(host, instanceName)

def isInstanceRunning(host, instanceName):
    processes = host.check_output("ps auwwx | grep catalina.startup.Bootstrap")
    if  "-Dtomcat.inst.name=" + instanceName in processes:
        return True
    else:
        return False

def waitForEntry(file, entry1, entry2):
    while 1:
        file.seek(0,2)
        line = file.readline()
        if entry1 in line:
            return entry1
        else:
            if entry2 in line:
                return entry2
            else:
                time.sleep(0.1)

代替

host.file(logPath).exists

我也在尝试

print(host.check_output("cat " + logPath))

它可以很好地打印文件内容。

有关如何解决此问题的任何想法? 提前非常感谢!

编辑: 这是我执行脚本的方式:

  

py.test -v --host = user @ host tomcat_test_recycle.py --sudo

1 个答案:

答案 0 :(得分:3)

testinfra软件包似乎可以将测试远程服务器的状态集成到pytest中。因此

host.file(logPath).exists

host.check_output("cat " + logPath)

正在检查远程服务器上的存在和内容,而

open(logPath, "rb") 

正在本地计算机上运行。该文件不在本地计算机上,而在host上。