@ -3,6 +3,7 @@ import os
import time
import time
import docker
import docker
from colorama import Fore , Style
from colorama import Fore , Style
import subprocess
# Declare variables for service name and sleep time
# Declare variables for service name and sleep time
test_name = sys . argv [ 1 ]
test_name = sys . argv [ 1 ]
@ -17,21 +18,22 @@ containers = []
# Stop containers
# Stop containers
def stop ( exit_code ) :
def stop ( exit_code ) :
print_logs ( )
print_logs ( )
os . system ( " docker-compose -f " + compose_file + " -p $ { DOCKER_ORG:-mailu} down " )
sys . stdout . flush ( )
print ( subprocess . check_output ( " docker-compose -f " + compose_file + " down " , shell = True ) . decode ( ) )
sys . exit ( exit_code )
sys . exit ( exit_code )
# Sleep for a defined amount of time
# Sleep for a defined amount of time
def sleep ( ) :
def sleep ( ) :
print ( Fore . LIGHTMAGENTA_EX + " Sleeping for " + str ( timeout ) + " m " + Style . RESET_ALL )
print ( Fore . LIGHTMAGENTA_EX + " Sleeping for " + str ( timeout ) + " m " + Style . RESET_ALL )
time . sleep ( timeout * 60 )
time . sleep ( timeout * 60 )
def health_checks ( ) :
def health_checks ( ) :
exit_code = 0
exit_code = 0
#Iterating trough all containers dictionary
#Iterating trough all containers dictionary
for container in client . containers ( all = True ) :
for container in client . containers ( all = True ) :
#Perform "docker container inspect" on container based on container ID and save output to a dictionary
#Perform "docker container inspect" on container based on container ID and save output to a dictionary
container_inspect = client . inspect_container ( container [ ' Id ' ] ) #Dict
container_inspect = client . inspect_container ( container [ ' Id ' ] ) #Dict
if " Health " in container_inspect [ ' State ' ] . keys ( ) :
if " Health " in container_inspect [ ' State ' ] . keys ( ) :
if container_inspect [ ' State ' ] [ ' Health ' ] [ ' Status ' ] == " healthy " :
if container_inspect [ ' State ' ] [ ' Health ' ] [ ' Status ' ] == " healthy " :
print ( Fore . GREEN + " Health status for " + container_inspect [ ' Name ' ] . replace ( " / " , " " ) + " : " + Fore . CYAN + container_inspect [ ' State ' ] [ ' Health ' ] [ ' Status ' ] + Style . RESET_ALL )
print ( Fore . GREEN + " Health status for " + container_inspect [ ' Name ' ] . replace ( " / " , " " ) + " : " + Fore . CYAN + container_inspect [ ' State ' ] [ ' Health ' ] [ ' Status ' ] + Style . RESET_ALL )
@ -46,41 +48,50 @@ def health_checks():
if container_inspect [ ' State ' ] [ ' Status ' ] != " running " :
if container_inspect [ ' State ' ] [ ' Status ' ] != " running " :
print ( Fore . RED + " Container " + container_inspect [ ' Name ' ] . replace ( " / " , " " ) + " state is: " + Fore . YELLOW + container_inspect [ ' State ' ] [ ' Status ' ] + Style . RESET_ALL )
print ( Fore . RED + " Container " + container_inspect [ ' Name ' ] . replace ( " / " , " " ) + " state is: " + Fore . YELLOW + container_inspect [ ' State ' ] [ ' Status ' ] + Style . RESET_ALL )
exit_code = 1
exit_code = 1
#Saving Id, Name and state to a new dictionary
#Saving Id, Name and state to a new dictionary
containers_dict = { }
containers_dict = { }
containers_dict [ ' Name ' ] = container_inspect [ ' Name ' ] . replace ( " / " , " " )
containers_dict [ ' Name ' ] = container_inspect [ ' Name ' ] . replace ( " / " , " " )
containers_dict [ ' Id ' ] = container_inspect [ ' Id ' ]
containers_dict [ ' Id ' ] = container_inspect [ ' Id ' ]
containers_dict [ ' State ' ] = container_inspect [ ' State ' ]
containers_dict [ ' State ' ] = container_inspect [ ' State ' ]
#Adding the generated dictionary to a list
#Adding the generated dictionary to a list
containers . append ( containers_dict )
containers . append ( containers_dict )
if exit_code != 0 :
if exit_code != 0 :
stop ( exit_code )
stop ( exit_code )
def print_logs ( ) :
def print_logs ( ) :
print ( " Printing logs ... " )
print ( " Printing logs ... " )
#Iterating through docker container inspect list and print logs
#Iterating through docker container inspect list and print logs
for container in containers :
for container in containers :
print ( Fore . LIGHTMAGENTA_EX + " Printing logs for: " + Fore . GREEN + container [ ' Name ' ] + Style . RESET_ALL )
print ( Fore . LIGHTMAGENTA_EX + " Printing logs for: " + Fore . GREEN + container [ ' Name ' ] + Style . RESET_ALL )
os . system ( ' docker container logs ' + container [ ' Name ' ] )
sys . stdout . flush ( )
print ( subprocess . check_output ( ' docker container logs ' + container [ ' Name ' ] , shell = True ) . decode ( ) )
#Iterating over hooks in test folder and running them
#Iterating over hooks in test folder and running them
def hooks ( ) :
def hooks ( ) :
print ( " Running hooks " )
print ( Fore . LIGHTMAGENTA_EX + " Running hooks " + Style . RESET_ALL )
for test_file in sorted ( os . listdir ( test_path ) ) :
for test_file in sorted ( os . listdir ( test_path ) ) :
if test_file . endswith ( " .py " ) :
try :
os . system ( " python3 " + test_path + test_file )
if test_file . endswith ( " .py " ) :
elif test_file . endswith ( " .sh " ) :
sys . stdout . flush ( )
os . system ( " ./ " + test_path + test_file )
print ( subprocess . check_output ( " python3 " + test_path + test_file , shell = True ) . decode ( ) )
elif test_file . endswith ( " .sh " ) :
sys . stdout . flush ( )
print ( subprocess . check_output ( " ./ " + test_path + test_file , shell = True ) . decode ( ) )
except subprocess . CalledProcessError as e :
sys . stderr . write ( " [ERROR]: output = %s , error code = %s \n " % ( e . output . decode ( ) , e . returncode ) )
stop ( 1 )
# Start up containers
# Start up containers
os . system ( " docker-compose -f " + compose_file + " -p $ { DOCKER_ORG:-mailu} up -d " )
sys . stdout . flush ( )
print ( subprocess . check_output ( " docker-compose -f " + compose_file + " up -d " , shell = True ) . decode ( ) )
print ( )
print ( )
sleep ( )
sleep ( )
print ( )
print ( )
os . system ( " docker ps -a " )
sys . stdout . flush ( )
print ( subprocess . check_output ( " docker ps -a " , shell = True ) . decode ( ) )
print ( )
print ( )
health_checks ( )
health_checks ( )
print ( )
print ( )