|
| 1 | +"""The Job Tester 'compose' module. |
| 2 | +
|
| 3 | +This module is responsible for injecting a docker-compose file into the |
| 4 | +repository of the Data Manager Job repository under test. It also |
| 5 | +executes docker-compose and can remove the test directory. |
| 6 | +""" |
| 7 | +import os |
| 8 | +import shutil |
| 9 | +import subprocess |
| 10 | +from typing import Dict, Optional, Tuple |
| 11 | + |
| 12 | +_INSTANCE_DIRECTORY: str = '.instance-88888888-8888-8888-8888-888888888888' |
| 13 | + |
| 14 | +_COMPOSE_CONTENT: str = """--- |
| 15 | +version: '3.8' |
| 16 | +services: |
| 17 | + job: |
| 18 | + image: {image} |
| 19 | + command: {command} |
| 20 | + environment: |
| 21 | + - DM_INSTANCE_DIRECTORY={instance_directory} |
| 22 | + volumes: |
| 23 | + - {test_path}:{project_directory} |
| 24 | + deploy: |
| 25 | + resources: |
| 26 | + limits: |
| 27 | + cpus: 1 |
| 28 | + memory: 1G |
| 29 | +""" |
| 30 | + |
| 31 | +# A default, 30 minute timeout |
| 32 | +_DEFAULT_TEST_TIMEOUT: int = 30 * 60 |
| 33 | + |
| 34 | +# The docker-compose version (for the first test) |
| 35 | +_COMPOSE_VERSION: Optional[str] = None |
| 36 | + |
| 37 | + |
| 38 | +def _get_docker_compose_version() -> str: |
| 39 | + |
| 40 | + result: subprocess.CompletedProcess =\ |
| 41 | + subprocess.run(['docker-compose', 'version'], |
| 42 | + capture_output=True, timeout=4) |
| 43 | + |
| 44 | + # stdout will contain the version on the first line: - |
| 45 | + # "docker-compose version 1.29.2, build unknown" |
| 46 | + # Ignore the first 23 characters of the first line... |
| 47 | + return result.stdout.decode("utf-8").split('\n')[0][23:] |
| 48 | + |
| 49 | + |
| 50 | +def get_test_path(test_name: str) -> str: |
| 51 | + """Returns the path to the root directory for a given test. |
| 52 | + """ |
| 53 | + cwd: str = os.getcwd() |
| 54 | + return f'{cwd}/data-manager/jote/{test_name}' |
| 55 | + |
| 56 | + |
| 57 | +def create(test_name: str, |
| 58 | + image: str, |
| 59 | + project_directory: str, |
| 60 | + command: str) -> str: |
| 61 | + """Writes a docker-compose file |
| 62 | + and creates the test directory structure returning the |
| 63 | + full path to the test (project) directory. |
| 64 | + """ |
| 65 | + global _COMPOSE_VERSION |
| 66 | + |
| 67 | + print('# Creating test environment...') |
| 68 | + |
| 69 | + # Do we have the docker-compose version the user's installed? |
| 70 | + if not _COMPOSE_VERSION: |
| 71 | + _COMPOSE_VERSION = _get_docker_compose_version() |
| 72 | + print(f'# docker-compose ({_COMPOSE_VERSION})') |
| 73 | + |
| 74 | + # Make the test directory... |
| 75 | + test_path: str = get_test_path(test_name) |
| 76 | + project_path: str = f'{test_path}/project' |
| 77 | + inst_path: str = f'{project_path}/{_INSTANCE_DIRECTORY}' |
| 78 | + if not os.path.exists(inst_path): |
| 79 | + os.makedirs(inst_path) |
| 80 | + |
| 81 | + # Write the Docker compose content to a file to the test directory |
| 82 | + variables: Dict[str, str] = {'test_path': project_path, |
| 83 | + 'image': image, |
| 84 | + 'command': command, |
| 85 | + 'project_directory': project_directory, |
| 86 | + 'instance_directory': _INSTANCE_DIRECTORY} |
| 87 | + compose_content: str = _COMPOSE_CONTENT.format(**variables) |
| 88 | + compose_path: str = f'{test_path}/docker-compose.yml' |
| 89 | + with open(compose_path, 'wt') as compose_file: |
| 90 | + compose_file.write(compose_content) |
| 91 | + |
| 92 | + print('# Created') |
| 93 | + |
| 94 | + return project_path |
| 95 | + |
| 96 | + |
| 97 | +def run(test_name: str) -> Tuple[int, str, str]: |
| 98 | + """Runs the container for the test, expecting the docker-compose file |
| 99 | + written by the 'create()'. The container exit code is returned to the |
| 100 | + caller along with the stdout and stderr content. |
| 101 | + A non-zero exit code does not necessarily mean the test has failed. |
| 102 | + """ |
| 103 | + |
| 104 | + print('# Executing the test ("docker-compose up")...') |
| 105 | + |
| 106 | + cwd = os.getcwd() |
| 107 | + os.chdir(get_test_path(test_name)) |
| 108 | + |
| 109 | + timeout: int = _DEFAULT_TEST_TIMEOUT |
| 110 | + try: |
| 111 | + # Run the container |
| 112 | + # and then cleanup |
| 113 | + test: subprocess.CompletedProcess =\ |
| 114 | + subprocess.run(['docker-compose', 'up', |
| 115 | + '--exit-code-from', 'job', |
| 116 | + '--abort-on-container-exit'], |
| 117 | + capture_output=True, |
| 118 | + timeout=timeout) |
| 119 | + _ = subprocess.run(['docker-compose', 'down'], |
| 120 | + capture_output=True, |
| 121 | + timeout=120) |
| 122 | + finally: |
| 123 | + os.chdir(cwd) |
| 124 | + |
| 125 | + print(f'# Executed ({test.returncode})') |
| 126 | + |
| 127 | + return test.returncode,\ |
| 128 | + test.stdout.decode("utf-8"),\ |
| 129 | + test.stderr.decode("utf-8") |
| 130 | + |
| 131 | + |
| 132 | +def delete(test_name: str, quiet: bool = False) -> None: |
| 133 | + """Deletes a test directory created by 'crete()'. |
| 134 | + """ |
| 135 | + print(f'# Deleting the test...') |
| 136 | + |
| 137 | + test_path: str = get_test_path(test_name) |
| 138 | + if os.path.exists(test_path): |
| 139 | + shutil.rmtree(test_path) |
| 140 | + |
| 141 | + print('# Deleted') |
0 commit comments