diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2020-06-15 13:11:24 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-15 13:11:24 -0400 |
commit | 2716234af3bf4e11b8017c67a770985ea8382baf (patch) | |
tree | a80cc5ecbce9302ad02bc10d935b8dfb93a8f184 /test/test_dockerpy/common.py | |
parent | b005465cb0ba78fea4563afefc34dac3df6fbd3d (diff) | |
parent | 4a3f3b5c02e43d64c68425de4306e310b4ef9ed6 (diff) | |
download | podman-2716234af3bf4e11b8017c67a770985ea8382baf.tar.gz podman-2716234af3bf4e11b8017c67a770985ea8382baf.tar.bz2 podman-2716234af3bf4e11b8017c67a770985ea8382baf.zip |
Merge pull request #6603 from sujil02/python-test
Adds more docker py test
Diffstat (limited to 'test/test_dockerpy/common.py')
-rw-r--r-- | test/test_dockerpy/common.py | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/test/test_dockerpy/common.py b/test/test_dockerpy/common.py new file mode 100644 index 000000000..975b13dc6 --- /dev/null +++ b/test/test_dockerpy/common.py @@ -0,0 +1,85 @@ +import docker +import subprocess +import os +import sys +import time +from docker import Client +from . import constant + +alpineDict = { + "name": "docker.io/library/alpine:latest", + "shortName": "alpine", + "tarballName": "alpine.tar"} + +def get_client(): + client = docker.Client(base_url="http://localhost:8080",timeout=15) + return client + +client = get_client() + +def podman(): + binary = os.getenv("PODMAN_BINARY") + if binary is None: + binary = "bin/podman" + return binary + +def restore_image_from_cache(TestClass): + alpineImage = os.path.join(constant.ImageCacheDir , alpineDict["tarballName"]) + if not os.path.exists(alpineImage): + os.makedirs(constant.ImageCacheDir) + client.pull(constant.ALPINE) + response = client.get_image(constant.ALPINE) + image_tar = open(alpineImage,mode="wb") + image_tar.write(response.data) + image_tar.close() + else : + TestClass.podman = subprocess.run( + [ + podman(), "load", "-i", alpineImage + ], + shell=False, + stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + +def run_top_container(): + c = client.create_container(image=constant.ALPINE,command='/bin/sleep 5',name=constant.TOP) + client.start(container=c.get("Id")) + return c.get("Id") + +def enable_sock(TestClass): + TestClass.podman = subprocess.Popen( + [ + podman(), "system", "service", "tcp:localhost:8080", + "--log-level=debug", "--time=0" + ], + shell=False, + stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + time.sleep(2) + +def terminate_connection(TestClass): + TestClass.podman.terminate() + stdout, stderr = TestClass.podman.communicate(timeout=0.5) + if stdout: + print("\nService Stdout:\n" + stdout.decode('utf-8')) + if stderr: + print("\nService Stderr:\n" + stderr.decode('utf-8')) + + if TestClass.podman.returncode > 0: + sys.stderr.write("podman exited with error code {}\n".format( + TestClass.podman.returncode)) + sys.exit(2) + +def remove_all_containers(): + containers = client.containers(quiet=True) + for c in containers: + client.remove_container(container=c.get("Id"), force=True) + +def remove_all_images(): + allImages = client.images() + for image in allImages: + client.remove_image(image,force=True) |