From 3b1fdc6166a3505e7c516d0f99b210dc5ab066f9 Mon Sep 17 00:00:00 2001 From: Ionut Filip Date: Thu, 25 Oct 2018 15:27:09 +0300 Subject: [PATCH] Migrate test script from shell to python - test.py needs to be called with 2 arguments : test_name and timeout - it will cd to test_name dir and use the test_name.yml from there - it will sleep for an amount of time equals to timeout in minutes - it will perform health checks for containers. If healtcheck isn't enabled will check for running state - it will run hooks inside the test_name dir ( .py and .sh) if there are any - printing logs in any case --- .travis.yml | 7 +- tests/compose/{ => core}/core.yml | 10 +- tests/compose/{ => fetchmail}/fetchmail.yml | 0 .../compose/{ => filters+dns}/filters+dns.yml | 0 tests/compose/{ => rainloop}/rainloop.yml | 0 tests/compose/{ => roundcube}/roundcube.yml | 0 tests/compose/test.py | 92 +++++++++++++++++++ tests/compose/{ => webdav}/webdav.yml | 0 tests/requirements.txt | 2 + 9 files changed, 109 insertions(+), 2 deletions(-) rename tests/compose/{ => core}/core.yml (85%) rename tests/compose/{ => fetchmail}/fetchmail.yml (100%) rename tests/compose/{ => filters+dns}/filters+dns.yml (100%) rename tests/compose/{ => rainloop}/rainloop.yml (100%) rename tests/compose/{ => roundcube}/roundcube.yml (100%) create mode 100755 tests/compose/test.py rename tests/compose/{ => webdav}/webdav.yml (100%) create mode 100644 tests/requirements.txt diff --git a/.travis.yml b/.travis.yml index c3a19529..107696aa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,12 +6,17 @@ addons: - docker-ce env: - VERSION=$TRAVIS_BRANCH +language: python +python: + - "3.6" +install: + - pip install -r tests/requirements.txt script: # Default to mailu for DOCKER_ORG - if [ -z "$DOCKER_ORG" ]; then export DOCKER_ORG="mailu"; fi - docker-compose -f tests/build.yml build - - tests/compose/test-script.sh + - python tests/compose/test.py deploy: provider: script diff --git a/tests/compose/core.yml b/tests/compose/core/core.yml similarity index 85% rename from tests/compose/core.yml rename to tests/compose/core/core.yml index 460a6908..d85388a4 100644 --- a/tests/compose/core.yml +++ b/tests/compose/core/core.yml @@ -66,7 +66,15 @@ services: - front # Optional services - + antispam: + image: mailu/rspamd:master + env_file: mailu.env + volumes: + - "/mailu/filter:/var/lib/rspamd" + - "/mailu/dkim:/dkim" + - "/mailu/overrides/rspamd:/etc/rspamd/override.d" + depends_on: + - front diff --git a/tests/compose/fetchmail.yml b/tests/compose/fetchmail/fetchmail.yml similarity index 100% rename from tests/compose/fetchmail.yml rename to tests/compose/fetchmail/fetchmail.yml diff --git a/tests/compose/filters+dns.yml b/tests/compose/filters+dns/filters+dns.yml similarity index 100% rename from tests/compose/filters+dns.yml rename to tests/compose/filters+dns/filters+dns.yml diff --git a/tests/compose/rainloop.yml b/tests/compose/rainloop/rainloop.yml similarity index 100% rename from tests/compose/rainloop.yml rename to tests/compose/rainloop/rainloop.yml diff --git a/tests/compose/roundcube.yml b/tests/compose/roundcube/roundcube.yml similarity index 100% rename from tests/compose/roundcube.yml rename to tests/compose/roundcube/roundcube.yml diff --git a/tests/compose/test.py b/tests/compose/test.py new file mode 100755 index 00000000..95670e30 --- /dev/null +++ b/tests/compose/test.py @@ -0,0 +1,92 @@ +import sys +import os +import time +import docker +from colorama import Fore, Style + +# Declare variables for service name and sleep time +test_name=sys.argv[1] +timeout=int(sys.argv[2]) + +client = docker.APIClient(base_url='unix://var/run/docker.sock') + +containers = [] + +# Start up containers +def start(): + os.system("cp mailu.env " + test_name + "/") + os.system("docker-compose -f " + test_name + "/" + test_name + ".yml -p $DOCKER_ORG up -d ") + +# Stop containers +def stop(exit_code): + print_logs() + os.system("docker-compose -f " + test_name + "/" + test_name + ".yml -p $DOCKER_ORG down") + os.system("rm " + test_name +"/mailu.env") + sys.exit(exit_code) + +# Sleep for a defined amount of time +def sleep(): + print(Fore.LIGHTMAGENTA_EX + "Sleeping for " + str(timeout) + "m" + Style.RESET_ALL) + time.sleep(timeout*60) + +def health_checks(): + 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 + + 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 + + #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'] + + #Adding the generated dictionary to a list + containers.append(containers_dict) + + if exit_code != 0: + stop(exit_code) + +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) + os.system('docker container logs ' + container['Name']) + +#Iterating over hooks in test folder and running them +def hooks(): + print("Running hooks") + for test_file in sorted(os.listdir(test_name + "/")): + if test_file.endswith(".py"): + os.system("python3 " + test_name + "/" + test_file) + elif test_file.endswith(".sh"): + os.system("./" + test_name + "/" + test_file) + +start() +print() +sleep() +print() +os.system("docker ps -a") +print() +health_checks() +print() +hooks() +print() +stop(0) diff --git a/tests/compose/webdav.yml b/tests/compose/webdav/webdav.yml similarity index 100% rename from tests/compose/webdav.yml rename to tests/compose/webdav/webdav.yml diff --git a/tests/requirements.txt b/tests/requirements.txt new file mode 100644 index 00000000..36006cfc --- /dev/null +++ b/tests/requirements.txt @@ -0,0 +1,2 @@ +docker +colorama