-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_php.py
More file actions
340 lines (284 loc) · 11.6 KB
/
test_php.py
File metadata and controls
340 lines (284 loc) · 11.6 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"""Tests for the PHP-cli, -apache and -fpm containers."""
## Maintainer: BCI team (#proj-bci)
try:
from typing import Literal
except ImportError:
from typing_extensions import Literal
import pytest
import requests
from pytest_container import DerivedContainer
from pytest_container import OciRuntimeBase
from pytest_container import container_and_marks_from_pytest_param
from pytest_container.container import ContainerData
from pytest_container.container import ImageFormat
from pytest_container.container import PortForwarding
from pytest_container.pod import Pod
from pytest_container.pod import PodData
from bci_tester.data import OS_VERSION
from bci_tester.data import PHP_8_APACHE
from bci_tester.data import PHP_8_CLI
from bci_tester.data import PHP_8_FPM
CONTAINER_IMAGES = [PHP_8_CLI, PHP_8_APACHE, PHP_8_FPM]
PHP_FLAVOR_T = Literal["apache", "fpm", "cli"]
CONTAINER_IMAGES_WITH_FLAVORS = [
pytest.param(*t, marks=t[0].marks)
for t in ((PHP_8_APACHE, "apache"), (PHP_8_FPM, "fpm"), (PHP_8_CLI, "cli"))
]
_PHP_MAJOR_VERSION = 8
_MEDIAWIKI_VERSION = "1.39.10"
_MEDIAWIKI_MAJOR_VERSION = ".".join(_MEDIAWIKI_VERSION.split(".")[:2])
MEDIAWIKI_APACHE_CONTAINER = DerivedContainer(
base=container_and_marks_from_pytest_param(PHP_8_APACHE)[0],
forwarded_ports=[PortForwarding(container_port=80)],
image_format=ImageFormat.DOCKER,
containerfile=f"""ENV MEDIAWIKI_VERSION={_MEDIAWIKI_VERSION}
ENV MEDIAWIKI_MAJOR_VERSION={_MEDIAWIKI_MAJOR_VERSION}"""
+ """
RUN set -e; zypper -n in $PHPIZE_DEPS oniguruma-devel libicu-devel gcc-c++ php8-sqlite php8-gd gzip && \
for ext in mbstring intl fileinfo iconv calendar ctype dom; do \
docker-php-ext-configure $ext; \
docker-php-ext-install $ext; \
done \
&& docker-php-source delete \
&& zypper -n rm oniguruma-devel libicu-devel gcc-c++ \
&& zypper -n clean && rm -rf /var/log/{zypp*,suseconnect*}
RUN set -euo pipefail; \
zypper -n in tar; \
curl -sfOL "https://releases.wikimedia.org/mediawiki/${MEDIAWIKI_MAJOR_VERSION}/mediawiki-${MEDIAWIKI_VERSION}.tar.gz"; \
tar -xf "mediawiki-${MEDIAWIKI_VERSION}.tar.gz"; \
rm "mediawiki-${MEDIAWIKI_VERSION}.tar.gz"; \
pushd "mediawiki-${MEDIAWIKI_VERSION}/"; mv * ..; popd; rmdir "mediawiki-${MEDIAWIKI_VERSION}"; \
php maintenance/install.php --dbname mediawiki.db --dbtype sqlite --pass insecureAndAtLeast10CharsLong --scriptpath="" --server="http://localhost" test-wiki geeko; \
chown --recursive wwwrun data; \
zypper -n clean; rm -rf /var/log/{zypp*,suseconnect*}
HEALTHCHECK --interval=10s --timeout=1s --retries=10 CMD curl -sf http://localhost
EXPOSE 80
""",
)
MEDIAWIKI_FPM_CONTAINER = DerivedContainer(
base=container_and_marks_from_pytest_param(PHP_8_FPM)[0],
containerfile=f"""ENV MEDIAWIKI_VERSION={_MEDIAWIKI_VERSION}
ENV MEDIAWIKI_MAJOR_VERSION={_MEDIAWIKI_MAJOR_VERSION}"""
+ r"""
RUN set -eux; \
zypper -n ref; \
zypper -n up; \
zypper -n in $PHPIZE_DEPS php8-pecl oniguruma-devel git \
librsvg \
ImageMagick \
python3; # Required for SyntaxHighlighting
# Install the PHP extensions we need
RUN set -eux; \
zypper -n in libicu-devel php8-mysql php8-sqlite; \
docker-php-ext-install \
calendar \
intl \
mbstring \
opcache \
iconv \
ctype \
fileinfo \
dom ; \
pecl install APCu-5.1.21; \
docker-php-ext-enable apcu; \
rm -r /tmp/pear
# set recommended PHP.ini settings
# see https://secure.php.net/manual/en/opcache.installation.php
RUN { \
echo 'opcache.memory_consumption=128'; \
echo 'opcache.interned_strings_buffer=8'; \
echo 'opcache.max_accelerated_files=4000'; \
echo 'opcache.revalidate_freq=60'; \
} > /etc/php8/conf.d/opcache-recommended.ini
# SQLite Directory Setup
RUN set -eux; \
mkdir -p data; \
chown -R wwwrun data
# pre-fetched keys from https://www.mediawiki.org/keys/keys.txt
COPY tests/files/mariadb-keys.asc /tmp/mariadb-keys.asc
# MediaWiki setup
RUN set -eux; \
zypper -n in dirmngr gzip; \
curl -sfSL "https://releases.wikimedia.org/mediawiki/${MEDIAWIKI_MAJOR_VERSION}/mediawiki-${MEDIAWIKI_VERSION}.tar.gz" -o mediawiki.tar.gz; \
curl -sfSL "https://releases.wikimedia.org/mediawiki/${MEDIAWIKI_MAJOR_VERSION}/mediawiki-${MEDIAWIKI_VERSION}.tar.gz.sig" -o mediawiki.tar.gz.sig; \
export GNUPGHOME="$(mktemp -d)"; \
# import gpg keys from https://www.mediawiki.org/keys/keys.txt
gpg --import /tmp/mariadb-keys.asc; \
gpg --batch --verify mediawiki.tar.gz.sig mediawiki.tar.gz; \
tar -x --strip-components=1 -f mediawiki.tar.gz; \
gpgconf --kill all; \
rm -r "$GNUPGHOME" mediawiki.tar.gz.sig mediawiki.tar.gz; \
chown -R wwwrun extensions skins cache images data; \
php maintenance/install.php --dbname mediawiki.db --dbtype sqlite --pass insecureAndAtLeast10CharsLong --scriptpath="" --server="http://localhost" test-wiki geeko && \
chown --recursive wwwrun data; \
zypper -n rm dirmngr gzip;
CMD ["php-fpm"]
""",
)
NGINX_FPM_PROXY = DerivedContainer(
base="registry.opensuse.org/opensuse/nginx",
containerfile="""COPY tests/files/nginx.conf /etc/nginx/
COPY tests/files/fastcgi_params /etc/nginx/
""",
)
MEDIAWIKI_FPM_POD = Pod(
containers=[MEDIAWIKI_FPM_CONTAINER, NGINX_FPM_PROXY],
forwarded_ports=[PortForwarding(container_port=80)],
)
def test_install_phpize_deps(auto_container_per_test: ContainerData):
"""Check that we can install whatever is in the environment variable
``PHPIZE_DEPS`` and that afterwards :command:`phpize` works.
"""
auto_container_per_test.connection.run_expect(
[0],
"zypper -n in $PHPIZE_DEPS",
)
auto_container_per_test.connection.run_expect([0], "touch config.m4")
auto_container_per_test.connection.run_expect([0], "phpize")
@pytest.mark.parametrize("extension", ["pcntl", "gd"])
def test_install_php_extension_via_script(
auto_container_per_test: ContainerData, extension: str
):
"""Verify that :command:`docker-php-ext-configuer $ex &&
docker-php-ext-install` works for the ``pcntl`` and ``gd`` extensions and
that both are present in the output of :command:`php -m` after running the
former command.
"""
auto_container_per_test.connection.run_expect(
[0], f"docker-php-ext-configure {extension}"
)
auto_container_per_test.connection.run_expect(
[0], f"docker-php-ext-install {extension}"
)
assert extension in auto_container_per_test.connection.check_output(
"php -m"
)
def test_install_multiple_extensions_via_script(
auto_container_per_test: ContainerData,
) -> None:
"""Try to install multiple extensions at the same time via
:command:`docker-php-ext-install $ext1 $ext2` and check that they have
actually been installed via :command:`zypper`.
"""
extensions = [
"calendar",
"intl",
"mbstring",
"opcache",
"iconv",
"ctype",
"fileinfo",
"dom",
]
auto_container_per_test.connection.run_expect(
[0], f"docker-php-ext-install {' '.join(extensions)}"
)
for ext in extensions:
assert auto_container_per_test.connection.package(
f"php{_PHP_MAJOR_VERSION}-{ext}"
).is_installed
@pytest.mark.parametrize("extension_name", ["gd"])
def test_zypper_install_php_extensions(
auto_container_per_test: ContainerData, extension_name: str
):
"""Test that the ``gd`` extension is not registered with php before
installing ``php$ver-gd``, and that it is after installing the package
``php$ver-gd``.
"""
assert (
extension_name
not in auto_container_per_test.connection.check_output("php -m")
)
auto_container_per_test.connection.run_expect(
[0], f"zypper -n in php{_PHP_MAJOR_VERSION}-{extension_name}"
)
assert extension_name in auto_container_per_test.connection.check_output(
"php -m"
)
@pytest.mark.parametrize(
"container_per_test,flavor",
CONTAINER_IMAGES_WITH_FLAVORS,
indirect=["container_per_test"],
)
def test_environment_variables(
container_per_test: ContainerData, flavor: PHP_FLAVOR_T
):
"""Sanity check of the environment variables:
- ``PHP_VERSION``
- ``COMPOSER_VERSION``
- ``PHP_INI_DIR``
and specific to php-apache variant:
- ``APACHE_CONFDIR``
- ``APACHE_ENVVARS``
"""
def get_env_var(env_var: str) -> str:
return container_per_test.connection.check_output(f"echo ${env_var}")
php_pkg_version = container_per_test.connection.package(
f"php{_PHP_MAJOR_VERSION}"
).version
assert php_pkg_version == get_env_var("PHP_VERSION")
assert container_per_test.connection.package(
"php-composer2"
).version == get_env_var("COMPOSER_VERSION")
php_ini_dir_path = get_env_var("PHP_INI_DIR")
php_ini_dir = container_per_test.connection.file(php_ini_dir_path)
assert php_ini_dir.exists and php_ini_dir.is_directory
if flavor == "apache":
apache_confdir = get_env_var("APACHE_CONFDIR")
assert container_per_test.connection.file(apache_confdir).is_directory
assert container_per_test.connection.file(
f"{apache_confdir}/httpd.conf"
).is_file
if OS_VERSION in ("15.3", "15.4", "15.5"):
apache_envvars = get_env_var("APACHE_ENVVARS")
assert container_per_test.connection.file(apache_envvars).is_file
assert container_per_test.connection.run_expect(
[0], f"source {apache_envvars}"
)
@pytest.mark.parametrize("container_image", [PHP_8_CLI])
def test_cli_entry_point(
container_image: DerivedContainer,
container_runtime: OciRuntimeBase,
host,
pytestconfig: pytest.Config,
) -> None:
"""Smoke test of the entrypoint of the php-cli variant: run the container
and verify that the arguments ``-r 'print_r(get_defined_constants());'`` are
forwarded to :command:`php`.
"""
container_image.prepare_container(container_runtime, pytestconfig.rootpath)
assert "PHP_BINARY" in host.check_output(
f"{container_runtime.runner_binary} run --rm "
f"{container_image.url or container_image.container_id} -r 'print_r(get_defined_constants());'",
)
@pytest.mark.parametrize(
"container_per_test",
[MEDIAWIKI_APACHE_CONTAINER],
indirect=["container_per_test"],
)
def test_mediawiki_php_apache(container_per_test: ContainerData) -> None:
"""Application test of the php-apache variant.
This test builds mediawiki deployed via mod_php. The test itself just checks
if the container is reachable using requests.
"""
resp = requests.get(
f"http://localhost:{container_per_test.forwarded_ports[0].host_port}",
timeout=30,
# we will get redirected to https://localhost, which will not resolve, so forbid that
allow_redirects=False,
)
resp.raise_for_status()
@pytest.mark.parametrize(
"pod_per_test", [MEDIAWIKI_FPM_POD], indirect=["pod_per_test"]
)
def test_mediawiki_fpm_build(pod_per_test: PodData) -> None:
"""Application test of the php-fpm variant.
This test builds mediawiki deployed via fpm with a nginx proxy in front of
it, both deployed via two containers in a podman pod. The test itself just
checks if the pod is reachable using requests.
"""
resp = requests.get(
f"http://localhost:{pod_per_test.forwarded_ports[0].host_port}",
timeout=30,
)
resp.raise_for_status()