|
| 1 | +"""A simple python script to test the e3dc module with various python versions in docker.""" |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import docker |
| 8 | + |
| 9 | + |
| 10 | +class Testcontainers: |
| 11 | + """A class to run commands in a python container.""" |
| 12 | + |
| 13 | + container = "" |
| 14 | + version = "" |
| 15 | + |
| 16 | + def __init__( |
| 17 | + self, |
| 18 | + ipAddress, |
| 19 | + username, |
| 20 | + password, |
| 21 | + key, |
| 22 | + configuration, |
| 23 | + version="3.8", |
| 24 | + ): |
| 25 | + """The init method for Testcontainers.""" |
| 26 | + client = docker.from_env() |
| 27 | + self.version = version |
| 28 | + image_name = "python:" + version |
| 29 | + container_name = "python-e3dc-" + version |
| 30 | + client.images.pull(image_name) |
| 31 | + try: |
| 32 | + client.containers.get(container_name).remove(force=True) |
| 33 | + except docker.errors.NotFound: |
| 34 | + pass |
| 35 | + self.container = client.containers.run( |
| 36 | + image_name, |
| 37 | + name=container_name, |
| 38 | + stdin_open=True, |
| 39 | + remove=True, |
| 40 | + detach=True, |
| 41 | + volumes=[str(Path(__file__).resolve().parents[1]) + ":/pye3dc"], |
| 42 | + working_dir="/pye3dc", |
| 43 | + environment={ |
| 44 | + "IPADDRESS": ipAddress, |
| 45 | + "USERNAME": username, |
| 46 | + "PASSWORD": password, |
| 47 | + "KEY": key, |
| 48 | + "CONFIG": configuration, |
| 49 | + }, |
| 50 | + ) |
| 51 | + |
| 52 | + def exec_cmd_stream(self, command): |
| 53 | + """Execute a command and stream output.""" |
| 54 | + _, stream = self.container.exec_run(cmd=command, stream=True) |
| 55 | + for data in stream: |
| 56 | + print(data.decode(), end="") |
| 57 | + |
| 58 | + def exec_cmd(self, command): |
| 59 | + """Execute a command and validate return code.""" |
| 60 | + result, output = self.container.exec_run(cmd=command) |
| 61 | + print(output.decode()) |
| 62 | + if result != 0: |
| 63 | + exit(1) |
| 64 | + |
| 65 | + def remove(self): |
| 66 | + """Remove the test container.""" |
| 67 | + self.container.remove(force=True) |
| 68 | + |
| 69 | + |
| 70 | +parser = argparse.ArgumentParser(description="E3DC testcontainers") |
| 71 | +parser.add_argument( |
| 72 | + "-l", |
| 73 | + "--list", |
| 74 | + help="list of Python versions to test with", |
| 75 | + default='["3.8", "3.9", "3.10", "3.11", "3.12"]', |
| 76 | +) |
| 77 | +parser.add_argument("-c", "--config", help="config of E3DC", default="{}") |
| 78 | +requiredNamed = parser.add_argument_group("required named arguments") |
| 79 | +requiredNamed.add_argument( |
| 80 | + "-i", "--ipaddress", help="IP address of E3DC", required=True |
| 81 | +) |
| 82 | +requiredNamed.add_argument("-u", "--username", help="username of E3DC", required=True) |
| 83 | +requiredNamed.add_argument("-p", "--password", help="password of E3DC", required=True) |
| 84 | +requiredNamed.add_argument("-k", "--key", help="key of E3DC", required=True) |
| 85 | +args = vars(parser.parse_args()) |
| 86 | + |
| 87 | +for version in json.loads(args["list"]): |
| 88 | + print("Starting test on Python " + version + ":") |
| 89 | + testcontainers = Testcontainers( |
| 90 | + ipAddress=args["ipaddress"], |
| 91 | + username=args["username"], |
| 92 | + password=args["password"], |
| 93 | + key=args["key"], |
| 94 | + configuration=args["config"], |
| 95 | + version=version, |
| 96 | + ) |
| 97 | + testcontainers.exec_cmd("pip install .") |
| 98 | + testcontainers.exec_cmd( |
| 99 | + "sh -c 'python tools/tests.py -i $IPADDRESS -u $USERNAME -p $PASSWORD -k $KEY -c $CONFIG'" |
| 100 | + ) |
| 101 | + testcontainers.remove() |
| 102 | + print() |
0 commit comments