|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import shlex |
| 5 | +import requests |
| 6 | +from enum import Enum |
| 7 | +import sys |
| 8 | +from typing import List |
| 9 | + |
| 10 | +from .common_tool_methods import (get_supported_postgres_release_versions, get_minor_version) |
| 11 | + |
| 12 | +POSTGRES_MATRIX_FILE = "postgres-matrix.yml" |
| 13 | +POSTGRES_MATRIX_WEB_ADDRESS = "https://raw.githubusercontent.com/citusdata/packaging/all-citus-unit-tests/postgres-matrix.yml" |
| 14 | + |
| 15 | + |
| 16 | +def run_command(command: str) -> int: |
| 17 | + with subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as process: |
| 18 | + for line in iter(process.stdout.readline, b''): # b'\n'-separated lines |
| 19 | + print(line.decode("utf-8"), end=" ") |
| 20 | + exitcode = process.wait() |
| 21 | + return exitcode |
| 22 | + |
| 23 | + |
| 24 | +class TestPlatform(Enum): |
| 25 | + el_7 = {"name": "el/7", "docker_image_name": "el-7"} |
| 26 | + el_8 = {"name": "el/8", "docker_image_name": "el-8"} |
| 27 | + centos_8 = {"name": "centos/8", "docker_image_name": "centos-8"} |
| 28 | + centos_7 = {"name": "centos/7", "docker_image_name": "centos-7"} |
| 29 | + ol_7 = {"name": "ol/7", "docker_image_name": "ol-7"} |
| 30 | + ol_8 = {"name": "ol/8", "docker_image_name": "ol-8"} |
| 31 | + debian_buster = {"name": "debian/buster", "docker_image_name": "debian-buster", } |
| 32 | + debian_bullseye = {"name": "debian/bullseye", "docker_image_name": "debian-bullseye"} |
| 33 | + debian_stretch = {"name": "debian/stretch", "docker_image_name": "debian-stretch"} |
| 34 | + ubuntu_bionic = {"name": "ubuntu/bionic", "docker_image_name": "ubuntu-bionic"} |
| 35 | + ubuntu_focal = {"name": "ubuntu/focal", "docker_image_name": "ubuntu-focal"} |
| 36 | + undefined = {"name": "undefined", "docker_image_name": "undefined"} |
| 37 | + |
| 38 | + |
| 39 | +def get_test_platform_for_os_release(os_release: str) -> TestPlatform: |
| 40 | + result = TestPlatform.undefined |
| 41 | + for tp in TestPlatform: |
| 42 | + if tp.value["name"] == os_release: |
| 43 | + result = tp |
| 44 | + return result |
| 45 | + |
| 46 | + |
| 47 | +def get_postgres_versions_from_matrix_file(project_version: str) -> List[str]: |
| 48 | + r = requests.get(POSTGRES_MATRIX_WEB_ADDRESS, allow_redirects=True) |
| 49 | + |
| 50 | + with open(POSTGRES_MATRIX_FILE, 'wb') as writer: |
| 51 | + writer.write(r.content) |
| 52 | + pg_versions = get_supported_postgres_release_versions(POSTGRES_MATRIX_FILE, project_version) |
| 53 | + |
| 54 | + return pg_versions |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + parser = argparse.ArgumentParser() |
| 59 | + parser.add_argument('--prj_ver', required=True) |
| 60 | + parser.add_argument('--pg_major_version') |
| 61 | + parser.add_argument("--os_release", choices=[t.value["name"] for t in TestPlatform]) |
| 62 | + |
| 63 | + args = parser.parse_args() |
| 64 | + test_platform = get_test_platform_for_os_release(args.os_release) |
| 65 | + minor_prj_ver = get_minor_version(args.prj_ver) |
| 66 | + |
| 67 | + platform = args.os_release |
| 68 | + |
| 69 | + postgres_versions = get_postgres_versions_from_matrix_file(args.prj_ver) |
| 70 | + |
| 71 | + print(f'This version of Citus supports following pg versions: {postgres_versions}') |
| 72 | + |
| 73 | + os.chdir("test-images") |
| 74 | + return_codes = {} |
| 75 | + |
| 76 | + if args.pg_major_version: |
| 77 | + postgres_versions = [p for p in postgres_versions if p == args.pg_major_version] |
| 78 | + |
| 79 | + |
| 80 | + if len(postgres_versions) == 0: |
| 81 | + raise ValueError("At least one supported postgres version is required") |
| 82 | + |
| 83 | + for postgres_version in postgres_versions: |
| 84 | + print(f'Testing package for following pg version: {postgres_version}') |
| 85 | + docker_image_name = f"test:{test_platform.value['docker_image_name']}-{postgres_version}" |
| 86 | + build_command = (f"docker build -t {docker_image_name} " |
| 87 | + f"-f {test_platform.value['docker_image_name']}/Dockerfile " |
| 88 | + f"--build-arg CITUS_VERSION={args.prj_ver} --build-arg PG_MAJOR={postgres_version} " |
| 89 | + f"--build-arg CITUS_MAJOR_VERSION={minor_prj_ver} .") |
| 90 | + print(build_command) |
| 91 | + return_build = run_command(build_command) |
| 92 | + return_run = run_command( |
| 93 | + f"docker run -e POSTGRES_VERSION={postgres_version} {docker_image_name} ") |
| 94 | + return_codes[f"{docker_image_name}-build"] = return_build |
| 95 | + return_codes[f"{docker_image_name}-run"] = return_run |
| 96 | + |
| 97 | + error_exists = False |
| 98 | + print("-----------------Summary Report------------------") |
| 99 | + for key, value in return_codes.items(): |
| 100 | + if value > 0: |
| 101 | + error_exists = True |
| 102 | + print(f"{key}: {'Success' if value == 0 else f'Fail. ErrorCode: {value}'}") |
| 103 | + summary_error = 'FAILED :(' if error_exists else 'SUCCESS :)' |
| 104 | + print(f'------------------------{summary_error}------------------------') |
| 105 | + |
| 106 | + if error_exists: |
| 107 | + sys.exit("Failed") |
0 commit comments