clearing/clr_test/run_test_docker_containers.py
Ivan Nikolaev-Axenov eb6a75a265 test v4
2024-08-05 12:10:19 +03:00

109 lines
4.7 KiB
Python

import os
import re
import sys
import time
import shutil
import subprocess
import logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
DATA_ROOT_DIR = os.environ['CLEARING_TEST_DIR']
CLR_TEST_DIR = os.environ['CLR_TEST_DIR']
RESULT_FILES_DIR = os.environ['RESULT_FILES_DIR']
def run_docker(folder_name):
logging.info(f"Running docker-compose for {folder_name}")
os.chdir("clr_test")
subprocess.call('docker rm -f $(docker ps --all -q --filter ancestor=clr_test-db)', shell=True)
subprocess.call('docker rm -f $(docker ps --all -q --filter ancestor=clr_test-clearing_system)', shell=True)
subprocess.call('docker rmi -f clr_test-db', shell=True)
subprocess.call('docker rmi -f clr_test-clearing_system', shell=True)
code = subprocess.call('docker compose up --abort-on-container-exit --exit-code-from clearing_system', shell=True)
logging.info(os.listdir(f"{CLR_TEST_DIR}/mnt/results"))
os.chdir("..")
if code != 0:
sys.exit(code)
return code
def change_date(folder_name):
pattern = r"((?<=clearing-tester\.mock-date-for-sdf=)|(?<=clearing-tester\.mock-date-for-execution=))(.+)"
with open(f"{DATA_ROOT_DIR}/{folder_name}/date.txt", 'r') as f:
date = f.read()
with open(f"{CLR_TEST_DIR}/clearing-all/clearing_testing_utils/clearing-tester/application.properties", 'r') as f:
file = f.read()
re.sub(pattern, date, file)
with open(f"{CLR_TEST_DIR}/clearing-all/clearing_testing_utils/clearing-tester/application.properties", 'w') as f:
f.write(file)
def remove_files_with_extensions(extension):
for root, dirs, files in os.walk(CLR_TEST_DIR):
for currentFile in files:
if currentFile.lower().endswith(extension):
logging.info(f"Removing file {os.path.join(root, currentFile)}, based on extension {extension}")
os.remove(os.path.join(root, currentFile))
def run_specific(index):
logging.info(f"$CI_PROJECT_DIR = {os.environ['CI_PROJECT_DIR']}")
folders = [i for i in sorted(os.listdir(DATA_ROOT_DIR)) if re.search("\\d{1,}.\\d{1,}.*", i)]
logging.info(f"Removing folder {CLR_TEST_DIR}/clearing-all/clearing_testing_utils/store/expected")
shutil.rmtree(f"{CLR_TEST_DIR}/clearing-all/clearing_testing_utils/store/expected")
logging.info(f"Removing folder {CLR_TEST_DIR}/clearing-all/clearing_testing_utils/store/input")
shutil.rmtree(f"{CLR_TEST_DIR}/clearing-all/clearing_testing_utils/store/input")
logging.info(f"Removing file {CLR_TEST_DIR}/db/export.sql")
os.remove(f"{CLR_TEST_DIR}/db/export.sql")
logging.info(f"Removing file {CLR_TEST_DIR}/db/test-data.sql")
os.remove(f"{CLR_TEST_DIR}/db/test-data.sql")
logging.info(f"Copying files from {DATA_ROOT_DIR}/{folders[index]}/db/ to {CLR_TEST_DIR}/db/")
shutil.copytree(f"{DATA_ROOT_DIR}/{folders[index]}/db/", f"{CLR_TEST_DIR}/db/", dirs_exist_ok=True)
logging.info(f"Copying files from {DATA_ROOT_DIR}/{folders[index]}/clearing_testing_utils/ to {CLR_TEST_DIR}/clearing-all/clearing_testing_utils/")
shutil.copytree(f"{DATA_ROOT_DIR}/{folders[index]}/clearing_testing_utils/", f"{CLR_TEST_DIR}/clearing-all/clearing_testing_utils/", dirs_exist_ok=True)
remove_files_with_extensions(".xml")
change_date(folders[index])
run_docker(folders[index])
def main():
folders = [i for i in sorted(os.listdir(DATA_ROOT_DIR)) if re.search("\\d{1,}.\\d{1,}.*", i)]
for folder in folders:
logging.info(f"Removing folder {CLR_TEST_DIR}/clearing-all/clearing_testing_utils/store/expected")
shutil.rmtree(f"{CLR_TEST_DIR}/clearing-all/clearing_testing_utils/store/expected")
logging.info(f"Removing folder {CLR_TEST_DIR}/clearing-all/clearing_testing_utils/store/input")
shutil.rmtree(f"{CLR_TEST_DIR}/clearing-all/clearing_testing_utils/store/input")
logging.info(f"Removing file {CLR_TEST_DIR}/db/export.sql")
os.remove(f"{CLR_TEST_DIR}/db/export.sql")
logging.info(f"Removing file {CLR_TEST_DIR}/db/test-data.sql")
os.remove(f"{CLR_TEST_DIR}/db/test-data.sql")
logging.info(f"Copying files from {DATA_ROOT_DIR}/{folder}/db/ to {CLR_TEST_DIR}/db/")
shutil.copytree(f"{DATA_ROOT_DIR}/{folder}/db/", f"{CLR_TEST_DIR}/db/", dirs_exist_ok=True)
logging.info(f"Copying files from {DATA_ROOT_DIR}/{folder}/clearing_testing_utils/ to {CLR_TEST_DIR}/clearing-all/clearing_testing_utils/")
shutil.copytree(f"{DATA_ROOT_DIR}/{folder}/clearing_testing_utils/", f"{CLR_TEST_DIR}/clearing-all/clearing_testing_utils/", dirs_exist_ok=True)
remove_files_with_extensions(".xml")
change_date(folder)
run_docker(folder)
if __name__=='__main__':
main()
# run_specific(6)