summaryrefslogtreecommitdiffstats
path: root/src/cephadm/tests/build/test_cephadm_build.py
blob: 1465c2c5efea761683fd834a595cafc08ebd0972 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# tests for building cephadm into a zipapp using build.py
#
# these should not be run automatically as they require the use of podman,
# which should not be assumed to exist on a typical test node

import json
import os
import pathlib
import pytest
import subprocess
import sys


CONTAINERS = {
    'centos-8': {
        'name': 'cephadm-build-test:centos8-py36',
        'base_image': 'quay.io/centos/centos:stream8',
        'script': 'dnf install -y python36',
    },
    'centos-9': {
        'name': 'cephadm-build-test:centos9-py3',
        'base_image': 'quay.io/centos/centos:stream9',
        'script': 'dnf install -y python3',
    },
    'centos-8-plusdeps': {
        'name': 'cephadm-build-test:centos8-py36-deps',
        'base_image': 'quay.io/centos/centos:stream8',
        'script': 'dnf install -y python36 python3-jinja2 python3-pyyaml',
    },
    'centos-9-plusdeps': {
        'name': 'cephadm-build-test:centos9-py3-deps',
        'base_image': 'quay.io/centos/centos:stream9',
        'script': 'dnf install -y python3 python3-jinja2 python3-pyyaml',
    },
    'ubuntu-20.04': {
        'name': 'cephadm-build-test:ubuntu-20-04-py3',
        'base_image': 'docker.io/library/ubuntu:20.04',
        'script': 'apt update && apt install -y python3-venv',
    },
    'ubuntu-22.04': {
        'name': 'cephadm-build-test:ubuntu-22-04-py3',
        'base_image': 'docker.io/library/ubuntu:22.04',
        'script': 'apt update && apt install -y python3-venv',
    },
}

BUILD_PY = 'src/cephadm/build.py'


def _print(*args):
    """Print with a highlight prefix."""
    print('----->', *args)
    sys.stdout.flush()


def container_cmd(image, cmd, ceph_dir, out_dir):
    return [
        'podman',
        'run',
        '--rm',
        f'--volume={ceph_dir}:/ceph:ro',
        f'--volume={out_dir}:/out',
        image,
    ] + list(cmd)


def run_container_cmd(image, cmd, ceph_dir, out_dir):
    full_cmd = container_cmd(image, cmd, ceph_dir, out_dir)
    _print("CMD", full_cmd)
    return subprocess.run(full_cmd)


def build_container(src_image, dst_image, build_script, workdir):
    cfile = pathlib.Path(workdir) / 'Dockerfile'
    with open(cfile, 'w') as fh:
        fh.write(f'FROM {src_image}\n')
        fh.write(f'RUN {build_script}\n')
    cmd = ['podman', 'build', '-t', str(dst_image), '-f', str(cfile)]
    _print("BUILD CMD", cmd)
    subprocess.run(cmd, check=True)


def build_in(alias, ceph_dir, out_dir, args):
    ctr = CONTAINERS[alias]
    build_container(ctr['base_image'], ctr['name'], ctr['script'], out_dir)
    cmd = ['/ceph/' + BUILD_PY] + list(args or []) + ['/out/cephadm']
    return run_container_cmd(ctr['name'], cmd, ceph_dir, out_dir)


@pytest.fixture
def source_dir():
    return pathlib.Path(__file__).parents[4].absolute()


@pytest.mark.parametrize(
    'env',
    [
        'centos-8',
        'centos-9',
        'ubuntu-20.04',
        'ubuntu-22.04',
    ],
)
def test_cephadm_build(env, source_dir, tmp_path):
    build_in(env, source_dir, tmp_path, [])
    binary = tmp_path / 'cephadm'
    assert binary.is_file()
    res = subprocess.run(
        [sys.executable, str(binary), 'version'],
        stdout=subprocess.PIPE,
    )
    out = res.stdout.decode('utf8')
    assert 'version' in out
    assert 'UNKNOWN' in out
    assert res.returncode != 0
    res = subprocess.run(
        [sys.executable, str(binary), 'version', '--verbose'],
        stdout=subprocess.PIPE,
    )
    data = json.loads(res.stdout)
    assert isinstance(data, dict)
    assert 'bundled_packages' in data
    assert all(v['package_source'] == 'pip' for v in data['bundled_packages'])
    assert all(
        v['name'] in ('Jinja2', 'MarkupSafe', 'PyYAML')
        for v in data['bundled_packages']
    )
    assert all('requirements_entry' in v for v in data['bundled_packages'])
    assert 'zip_root_entries' in data
    zre = data['zip_root_entries']
    assert any(e.startswith('Jinja2') for e in zre)
    assert any(e.startswith('MarkupSafe') for e in zre)
    assert any(e.startswith('jinja2') for e in zre)
    assert any(e.startswith('markupsafe') for e in zre)
    assert any(e.startswith('cephadmlib') for e in zre)
    assert any(e.startswith('_cephadmmeta') for e in zre)


@pytest.mark.parametrize(
    'env',
    [
        'centos-8-plusdeps',
        'centos-9-plusdeps',
        'centos-9',
    ],
)
def test_cephadm_build_from_rpms(env, source_dir, tmp_path):
    res = build_in(
        env,
        source_dir,
        tmp_path,
        ['-Brpm', '-SCEPH_GIT_VER=0', '-SCEPH_GIT_NICE_VER=foobar'],
    )
    if 'plusdeps' not in env:
        assert res.returncode != 0
        return
    binary = tmp_path / 'cephadm'
    if 'centos-8' in env and sys.version_info[:2] >= (3, 10):
        # The version of markupsafe in centos 8 is incompatible with
        # python>=3.10 due to changes in the stdlib therefore we can't execute
        # the cephadm binary, so we quit the test early.
        return
    assert binary.is_file()
    res = subprocess.run(
        [sys.executable, str(binary), 'version'],
        stdout=subprocess.PIPE,
    )
    out = res.stdout.decode('utf8')
    assert 'version' in out
    assert 'foobar' in out
    assert res.returncode == 0
    res = subprocess.run(
        [sys.executable, str(binary), 'version', '--verbose'],
        stdout=subprocess.PIPE,
    )
    data = json.loads(res.stdout)
    assert isinstance(data, dict)
    assert 'bundled_packages' in data
    assert all(v['package_source'] == 'rpm' for v in data['bundled_packages'])
    assert all(
        v['name'] in ('Jinja2', 'MarkupSafe', 'PyYAML')
        for v in data['bundled_packages']
    )
    assert all('requirements_entry' in v for v in data['bundled_packages'])
    assert 'zip_root_entries' in data
    zre = data['zip_root_entries']
    assert any(e.startswith('Jinja2') for e in zre)
    assert any(e.startswith('MarkupSafe') for e in zre)
    assert any(e.startswith('jinja2') for e in zre)
    assert any(e.startswith('markupsafe') for e in zre)
    assert any(e.startswith('cephadmlib') for e in zre)
    assert any(e.startswith('_cephadmmeta') for e in zre)