Replaces os.popen with subprocess.check_output

This commit is contained in:
Ionut Filip
2018-11-03 19:15:05 +02:00
parent 982e586e49
commit 9a7fc1416a
2 changed files with 11 additions and 8 deletions

View File

@@ -1,55 +0,0 @@
import string
import random
import smtplib
import imaplib
import time
def secret(length=16):
charset = string.ascii_uppercase + string.digits
return ''.join(
random.SystemRandom().choice(charset)
for _ in range(length)
)
#Generating secret message
secret_message = secret(16)
#Login to smt server and sending email with secret message
def send_email(msg):
print("Sending email ...")
server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.connect('localhost', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login("admin@mailu.io", "password")
server.sendmail("admin@mailu.io", "user@mailu.io", msg)
server.quit()
print("email sent with message " + msg)
#Login to imap server, read latest email and check for secret message
def read_email():
print("Receiving email ...")
server = imaplib.IMAP4_SSL('localhost')
server.login('user@mailu.io', 'password')
stat, count = server.select('inbox')
stat, data = server.fetch(count[0], '(UID BODY[TEXT])')
print("email received with message " + str(data[0][1]))
if secret_message in str(data[0][1]):
print("Success!")
else:
print("Failed! Something went wrong")
server.close()
server.logout()
send_email(secret_message)
print("Sleeping for 1m")
time.sleep(60)
read_email()

View File

@@ -3,6 +3,7 @@ import os
import time
import docker
from colorama import Fore, Style
import subprocess
# Declare variables for service name and sleep time
test_name=sys.argv[1]
@@ -17,7 +18,7 @@ containers = []
# Stop containers
def stop(exit_code):
print_logs()
print(os.popen("docker-compose -f " + compose_file + " down").read())
print(subprocess.check_output("docker-compose -f " + compose_file + " down", shell=True))
sys.exit(exit_code)
# Sleep for a defined amount of time
@@ -64,25 +65,25 @@ def print_logs():
#Iterating through docker container inspect list and print logs
for container in containers:
print(Fore.LIGHTMAGENTA_EX + "Printing logs for: " + Fore.GREEN + container['Name'] + Style.RESET_ALL)
print(os.popen('docker container logs ' + container['Name']).read())
print(subprocess.check_output('docker container logs ' + container['Name'], shell=True))
#Iterating over hooks in test folder and running them
def hooks():
print("Running hooks")
for test_file in sorted(os.listdir(test_path)):
if test_file.endswith(".py"):
print(os.popen("python3 " + test_path + test_file).read())
print(subprocess.check_output("python3 " + test_path + test_file, shell=True))
elif test_file.endswith(".sh"):
print(os.popen("./" + test_path + test_file).read())
print(subprocess.check_output("./" + test_path + test_file, shell=True))
print(os.popen("python3 tests/compose/email_test.py").read())
print(subprocess.check_output("python3 tests/email_test.py", shell=True))
# Start up containers
print(os.popen("docker-compose -f " + compose_file + " up -d ").read())
print(subprocess.check_output("docker-compose -f " + compose_file + " up -d", shell=True))
print()
sleep()
print()
print(os.popen("docker ps -a").read())
print(subprocess.check_output("docker ps -a", shell=True))
print()
health_checks()
print()