2018-10-25 14:27:09 +02:00
import sys
import os
import time
2022-08-19 20:04:45 +02:00
import datetime
2022-08-19 20:57:58 +02:00
from xmlrpc . client import DateTime
2018-10-25 14:27:09 +02:00
import docker
from colorama import Fore , Style
2018-11-03 19:15:05 +02:00
import subprocess
2022-08-19 22:24:07 +02:00
import calendar
2018-10-25 14:27:09 +02:00
# Declare variables for service name and sleep time
test_name = sys . argv [ 1 ]
timeout = int ( sys . argv [ 2 ] )
2018-10-25 17:45:24 +02:00
test_path = " tests/compose/ " + test_name + " / "
compose_file = test_path + " docker-compose.yml "
2018-10-25 14:27:09 +02:00
client = docker . APIClient ( base_url = ' unix://var/run/docker.sock ' )
containers = [ ]
# Stop containers
def stop ( exit_code ) :
print_logs ( )
2018-11-03 19:34:47 +02:00
sys . stdout . flush ( )
2018-11-05 12:34:52 +02:00
print ( subprocess . check_output ( " docker-compose -f " + compose_file + " down " , shell = True ) . decode ( ) )
2018-10-25 14:27:09 +02:00
sys . exit ( exit_code )
2018-11-02 15:05:16 +02:00
2022-08-19 20:04:45 +02:00
def health_checks ( deadline ) :
2018-10-25 14:27:09 +02:00
exit_code = 0
#Iterating trough all containers dictionary
for container in client . containers ( all = True ) :
#Perform "docker container inspect" on container based on container ID and save output to a dictionary
container_inspect = client . inspect_container ( container [ ' Id ' ] ) #Dict
2018-11-02 15:05:16 +02:00
2018-10-25 14:27:09 +02:00
if " Health " in container_inspect [ ' State ' ] . keys ( ) :
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 )
if container_inspect [ ' State ' ] [ ' Health ' ] [ ' Status ' ] != " healthy " :
print ( Fore . RED + " Container " + container_inspect [ ' Name ' ] . replace ( " / " , " " ) + " is " + Fore . YELLOW + container_inspect [ ' State ' ] [ ' Health ' ] [ ' Status ' ]
+ Fore . RED + " , FailingStreak: " + Fore . YELLOW + str ( container_inspect [ ' State ' ] [ ' Health ' ] [ ' FailingStreak ' ] )
+ Fore . RED + " , Log: " + Fore . YELLOW + str ( container_inspect [ ' State ' ] [ ' Health ' ] [ ' Log ' ] ) + Style . RESET_ALL )
exit_code = 1
else :
if container_inspect [ ' State ' ] [ ' Status ' ] == " running " :
print ( Fore . GREEN + " Running status for " + container_inspect [ ' Name ' ] . replace ( " / " , " " ) + " : " + Fore . BLUE + container_inspect [ ' State ' ] [ ' Status ' ] + Style . RESET_ALL )
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 )
exit_code = 1
2018-11-02 15:05:16 +02:00
2018-10-25 14:27:09 +02:00
#Saving Id, Name and state to a new dictionary
containers_dict = { }
containers_dict [ ' Name ' ] = container_inspect [ ' Name ' ] . replace ( " / " , " " )
containers_dict [ ' Id ' ] = container_inspect [ ' Id ' ]
containers_dict [ ' State ' ] = container_inspect [ ' State ' ]
2018-11-02 15:05:16 +02:00
2018-10-25 14:27:09 +02:00
#Adding the generated dictionary to a list
containers . append ( containers_dict )
2018-11-02 15:05:16 +02:00
2022-08-19 20:04:45 +02:00
if exit_code == 0 :
return True
2022-08-19 22:32:53 +02:00
elif exit_code != 0 and deadline < datetime . datetime . now ( ) . timestamp ( ) :
2018-10-25 14:27:09 +02:00
stop ( exit_code )
2018-11-02 15:05:16 +02:00
2018-10-25 14:27:09 +02:00
def print_logs ( ) :
print ( " Printing 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 )
2018-11-03 19:34:47 +02:00
sys . stdout . flush ( )
2018-11-05 12:34:52 +02:00
print ( subprocess . check_output ( ' docker container logs ' + container [ ' Name ' ] , shell = True ) . decode ( ) )
2018-11-02 15:05:16 +02:00
#Iterating over hooks in test folder and running them
2018-10-25 14:27:09 +02:00
def hooks ( ) :
2018-11-05 12:34:52 +02:00
print ( Fore . LIGHTMAGENTA_EX + " Running hooks " + Style . RESET_ALL )
2018-10-25 17:45:24 +02:00
for test_file in sorted ( os . listdir ( test_path ) ) :
2018-11-05 12:34:52 +02:00
try :
if test_file . endswith ( " .py " ) :
sys . stdout . flush ( )
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 )
2018-11-02 15:05:16 +02:00
2018-10-25 16:35:53 +02:00
# Start up containers
2018-11-03 19:34:47 +02:00
sys . stdout . flush ( )
2022-08-19 20:57:58 +02:00
deadline = datetime . datetime . now ( ) + datetime . timedelta ( minutes = timeout )
2022-08-19 22:24:07 +02:00
deadline = calendar . timegm ( deadline . timetuple ( ) )
2018-11-05 12:34:52 +02:00
print ( subprocess . check_output ( " docker-compose -f " + compose_file + " up -d " , shell = True ) . decode ( ) )
2018-10-25 14:27:09 +02:00
print ( )
2022-08-19 20:04:45 +02:00
print ( Fore . LIGHTMAGENTA_EX + " Sleeping for 10s " + Style . RESET_ALL )
time . sleep ( 10 )
2018-10-25 14:27:09 +02:00
print ( )
2018-11-03 19:34:47 +02:00
sys . stdout . flush ( )
2018-11-05 12:34:52 +02:00
print ( subprocess . check_output ( " docker ps -a " , shell = True ) . decode ( ) )
2018-10-25 14:27:09 +02:00
print ( )
2022-08-19 20:04:45 +02:00
while not health_checks ( deadline ) :
print ( Fore . LIGHTMAGENTA_EX + " Sleeping for 10s " + Style . RESET_ALL )
sys . stdout . flush ( )
time . sleep ( 10 )
2018-10-25 14:27:09 +02:00
print ( )
hooks ( )
print ( )
stop ( 0 )