I have a specific NiFi Service Log that runs when my site is up. To get its contents, I just run:
docker service logs nifi
I'm trying to test the site with PyTest. Thanks to Selenium, I have been able to develop automated Python Tests for testing functionality. The functional tests were easy enough to write, but testing to see if certain entries in the logs are generated has been more difficult. My initial idea has been the following:
# function to test runs
time.sleep(2) # Give time for log entries to generate; they generate fairly quickly
cmd = 'docker service nifi -f | grep -e "Successfully sent" -e "Failed to process session" >> $PWD/nifi_logs/nifi1.log'
subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT)
time.sleep(1) # Give time for log to generate
with open("nifi_logs/nifi1.log", "r") as rfile:
contents = rfile.read()
assert contents != ""
I send grepped log entries to a LOG file, and then have the LOG file checked to see if it's empty. Several problems with this:
- Sometimes the LOG files will have entries, sometimes they won't. It's a toss-up, and very annoying.
- The printed out LOG files are unnecessary, I really just need to know if the 2 grepped expressions appeared in the Docker Log or not after my Test was executed.
- I'm not actually testing the contents of the file, just to see if it's empty or not.
And before you ask: I also tried os.system(), but I've read it's nearly deprecated, so not as good to use as subprocess; plus the 3 problems above still arise.
I know the functions I wrote are working properly, because if I run docker service nifi -f | grep -e "Successfully sent" -e "Failed to process session" >> $PWD/nifi_logs/nifi1.log in a separate terminal, I can see the 2 log entries printed when I run the function. I don't think it has to do with what I'm grepping, but whitespace could be an issue for subprocess?
Any recommendations? I really just need a way to look to see if the 2 strings ran within the last couple seconds, and if both have, then PyTest will say the test is a success.