|
1 | 1 | import logging |
2 | 2 | import os |
| 3 | +import shutil |
3 | 4 | import subprocess |
| 5 | +import tempfile |
4 | 6 |
|
5 | 7 | import pytest |
6 | 8 |
|
7 | 9 | from colin.core.colin import _set_logging |
| 10 | +from colin.core.target import ImageTarget, OstreeTarget, DockerfileTarget |
8 | 11 |
|
9 | 12 | _set_logging(level=logging.DEBUG) |
10 | 13 |
|
11 | 14 | BASH_IMAGE = "colin-test-bash" |
12 | 15 | LS_IMAGE = "colin-test-ls" |
13 | | -BUSYBOX_IMAGE = "docker.io/library/busybox" |
14 | | -LABELS_IMAGE = "colin-labels:latest" |
| 16 | +BUSYBOX_IMAGE = "busybox" |
| 17 | +LABELS_IMAGE = "colin-labels" |
15 | 18 | IMAGES = { |
16 | 19 | BASH_IMAGE: { |
17 | 20 | "dockerfile_path": "Dockerfile-bash" |
|
25 | 28 | } |
26 | 29 |
|
27 | 30 |
|
28 | | -def build_images(): |
29 | | - """ build container images we need for testing """ |
30 | | - this_dir = os.path.abspath(os.path.dirname(__file__)) |
31 | | - data_dir = os.path.join(this_dir, os.path.pardir, "data") |
32 | | - for image_name, image_data in IMAGES.items(): |
33 | | - cmd_create = ["podman", "build", "-t", image_name, "-f", |
34 | | - image_data["dockerfile_path"], data_dir] |
| 31 | +def build_image_if_not_exists(image_name): |
| 32 | + try: |
| 33 | + subprocess.check_call(["podman", "image", "inspect", image_name], |
| 34 | + stdout=subprocess.PIPE) |
| 35 | + except subprocess.CalledProcessError: |
| 36 | + this_dir = os.path.abspath(os.path.dirname(__file__)) |
| 37 | + data_dir = os.path.join(this_dir, os.path.pardir, "data") |
| 38 | + |
| 39 | + dockerfile_path = IMAGES[image_name]["dockerfile_path"] |
| 40 | + cmd_create = ["podman", "build", "-t", image_name, "-f", dockerfile_path, data_dir] |
35 | 41 | output = subprocess.check_output(cmd_create) |
36 | 42 | assert output |
37 | 43 |
|
38 | 44 |
|
39 | | -@pytest.fixture(autouse=True, scope='session') |
40 | | -def setup_test_session(): |
41 | | - """ set up environment before testing """ |
42 | | - for image_name in IMAGES: |
43 | | - try: |
44 | | - subprocess.check_call(["podman", "image", "inspect", image_name], |
45 | | - stdout=subprocess.PIPE) |
46 | | - except subprocess.CalledProcessError: |
47 | | - break |
48 | | - # executed if break was not reached |
49 | | - else: |
50 | | - # all images were found, we're good |
51 | | - return |
52 | | - # the loop ended with break, create images |
53 | | - build_images() |
| 45 | +def pull_image_if_not_exists(image_name): |
| 46 | + try: |
| 47 | + subprocess.check_call(["podman", "image", "inspect", image_name], |
| 48 | + stdout=subprocess.PIPE) |
| 49 | + except subprocess.CalledProcessError: |
| 50 | + subprocess.check_call(["podman", "pull", image_name], |
| 51 | + stdout=subprocess.PIPE) |
| 52 | + |
| 53 | + |
| 54 | +def convert_image_to_ostree(image_name): |
| 55 | + # /tmp is tmpfs and ostree can't do its magic there |
| 56 | + tmpdir_path = tempfile.mkdtemp(prefix="pytest-", dir="/var/tmp") |
| 57 | + ostree_path = os.path.join(tmpdir_path, "os3") |
| 58 | + os.makedirs(ostree_path) |
| 59 | + skopeo_target = get_skopeo_path(image_name=image_name, ostree_path=ostree_path) |
| 60 | + |
| 61 | + subprocess.check_call(["ostree", "init", "--mode", "bare-user-only", |
| 62 | + "--repo", ostree_path]) |
| 63 | + |
| 64 | + cmd = ["podman", "push", image_name, skopeo_target] |
| 65 | + subprocess.check_call(cmd) |
| 66 | + return ostree_path |
| 67 | + |
| 68 | + |
| 69 | +def get_target(name, type): |
| 70 | + if type == "image": |
| 71 | + |
| 72 | + target = ImageTarget(target=name, |
| 73 | + pull=False) |
| 74 | + yield target |
| 75 | + target.clean_up() |
| 76 | + |
| 77 | + elif type == "ostree": |
| 78 | + |
| 79 | + ostree_path = convert_image_to_ostree(name) |
| 80 | + skopeo_target = get_skopeo_path(image_name=name, |
| 81 | + ostree_path=ostree_path) |
| 82 | + |
| 83 | + ostree_target = OstreeTarget(target=skopeo_target) |
| 84 | + yield ostree_target |
| 85 | + ostree_target.clean_up() |
| 86 | + shutil.rmtree(ostree_path) |
| 87 | + |
| 88 | + elif type == "dockerfile": |
| 89 | + |
| 90 | + this_dir = os.path.abspath(os.path.dirname(__file__)) |
| 91 | + data_dir = os.path.join(this_dir, os.path.pardir, "data") |
| 92 | + dockerfile_path = os.path.join(data_dir, IMAGES[name]["dockerfile_path"]) |
| 93 | + |
| 94 | + yield DockerfileTarget(target=dockerfile_path) |
| 95 | + |
| 96 | + |
| 97 | +def get_skopeo_path(image_name, ostree_path): |
| 98 | + return "ostree:%s@%s" % (image_name, ostree_path) |
| 99 | + |
| 100 | + |
| 101 | +@pytest.fixture(scope="session") |
| 102 | +def label_image(): |
| 103 | + build_image_if_not_exists(LABELS_IMAGE) |
| 104 | + |
| 105 | + |
| 106 | +@pytest.fixture(scope="session", |
| 107 | + params=["image", "ostree", "dockerfile"]) |
| 108 | +def target_label(request, label_image): |
| 109 | + for t in get_target(name=LABELS_IMAGE, |
| 110 | + type=request.param): |
| 111 | + yield t |
| 112 | + |
| 113 | + |
| 114 | +@pytest.fixture(scope="session", |
| 115 | + params=["image", "ostree", "dockerfile"]) |
| 116 | +def target_label_image_and_dockerfile(request, label_image): |
| 117 | + for t in get_target(name=LABELS_IMAGE, |
| 118 | + type=request.param): |
| 119 | + yield t |
| 120 | + |
| 121 | + |
| 122 | +@pytest.fixture(scope="session") |
| 123 | +def target_bash_image(): |
| 124 | + build_image_if_not_exists(BASH_IMAGE) |
| 125 | + |
| 126 | + |
| 127 | +@pytest.fixture(scope="session", |
| 128 | + params=["image", "ostree"]) |
| 129 | +def target_bash(request, target_bash_image): |
| 130 | + for t in get_target(name=BASH_IMAGE, |
| 131 | + type=request.param): |
| 132 | + yield t |
| 133 | + |
| 134 | + |
| 135 | +@pytest.fixture(scope="session") |
| 136 | +def target_ls_image(): |
| 137 | + build_image_if_not_exists(LS_IMAGE) |
| 138 | + |
| 139 | + |
| 140 | +@pytest.fixture(scope="session", |
| 141 | + params=["image", "ostree"]) |
| 142 | +def target_ls(request, target_ls_image): |
| 143 | + for t in get_target(name=LS_IMAGE, |
| 144 | + type=request.param): |
| 145 | + yield t |
| 146 | + |
| 147 | + |
| 148 | +@pytest.fixture(scope="session", |
| 149 | + params=[LS_IMAGE, BASH_IMAGE]) |
| 150 | +def target_help_file(request, target_ls, target_bash): |
| 151 | + if request.param == LS_IMAGE: |
| 152 | + return target_ls, False |
| 153 | + if request.param == BASH_IMAGE: |
| 154 | + return target_bash, True |
| 155 | + |
| 156 | + |
| 157 | +@pytest.fixture(scope="session") |
| 158 | +def target_busybox_image(): |
| 159 | + pull_image_if_not_exists(image_name=BUSYBOX_IMAGE) |
| 160 | + |
| 161 | + |
| 162 | +@pytest.fixture(scope="session", |
| 163 | + params=["image", "ostree"]) |
| 164 | +def target_busybox(request, target_busybox_image): |
| 165 | + for t in get_target(name=BUSYBOX_IMAGE, |
| 166 | + type=request.param): |
| 167 | + yield t |
0 commit comments