Reputation: 73
I am struggling to find the solution for this. I am using an absolute path that definitely exists in the file system. When same exact path is used in same script with
if host.file(logPath).exists:
print("Exists: " + logPath)
I get
Exists: /var/opt/jws/jws3.0/v7/testinfra_node1/logs/catalina.out
But when trying:
with open(logPath, "rt") as log:
I am getting:
> 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'
Here is the whole code (it is a testinfra script for testing JWT installation):
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)
Instead of
host.file(logPath).exists
I have been also trying
print(host.check_output("cat " + logPath))
and it prints the file contents nicely.
Any ideas on how to approach this? Many thanks in advance!
Edit: Here is how I execute the script:
py.test -v --host=user@host tomcat_test_recycle.py --sudo
Upvotes: 1
Views: 389
Reputation: 23206
The testinfra
package appears to integrate testing the state of remote servers into pytest
. Thus
host.file(logPath).exists
and
host.check_output("cat " + logPath)
are checking the existence and the contents on a remote server, whereas
open(logPath, "rb")
is acting on the local machine. The file is not on the local machine, it’s on host
.
Upvotes: 3