summaryrefslogtreecommitdiff
path: root/test/python/dockerpy/tests/common.py
blob: f83f4076fd3c2e6623394dad3cefce80b13daee4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
import pathlib
import subprocess
import sys
import time

from docker import APIClient

from . import constant

alpineDict = {
    "name": "docker.io/library/alpine:latest",
    "shortName": "alpine",
    "tarballName": "alpine.tar"
}


def get_client():
    client = APIClient(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, exist_ok=True)
        client.pull(constant.ALPINE)
        image = client.get_image(constant.ALPINE)
        tarball = open(alpineImage, mode="wb")
        for frame in image:
            tarball.write(frame)
        tarball.close()
    else:
        subprocess.run(
            [podman(), "load", "-i", alpineImage],
            shell=False,
            stdin=subprocess.DEVNULL,
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
            check=True,
        )


def flush_image_cache(TestCase):
    for f in pathlib.Path(constant.ImageCacheDir).glob("*"):
        f.unlink(f)


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)