Skip to content

Commit 8c99921

Browse files
authored
Merge pull request #413 from wp-cli/fix/docker-sqlite
Make the Docker workers run on older PHP versions and without a host database
2 parents bd002b1 + 5729a87 commit 8c99921

6 files changed

Lines changed: 129 additions & 45 deletions

File tree

‎.readme-partials/USING.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ WP_CLI_TEST_DOCKER=auto WP_CLI_TEST_PARALLEL=4 composer behat
278278

279279
Docker is not used on Windows, when collecting code coverage, when Docker is not available, when the image cannot be built or when the database server cannot be identified.
280280

281-
Each worker gets its own network, temporary directory and database server, of the same type and version as the one configured through the `WP_CLI_TEST_DB*` variables (or set the image through `WP_CLI_TEST_DOCKER_DB`, e.g. `mysql:8.0`). Nothing is shared between workers, so they can run any scenario. The database servers keep their data in memory. Scenarios selected by path or name run in a single container.
281+
Each worker gets its own network, temporary directory and database server, of the same type and version as the one configured through the `WP_CLI_TEST_DB*` variables (or set the image through `WP_CLI_TEST_DOCKER_DB`, e.g. `mysql:8.0`, so that the host needs neither a database server nor a client). Nothing is shared between workers, so they can run any scenario. The database servers keep their data in memory. Scenarios selected by path or name run in a single container.
282282

283283
The containers run the same PHP version as the host, unless `WP_CLI_TEST_DOCKER_PHP` sets another one:
284284
```bash

‎bin/run-behat-tests‎

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,50 @@ is_numeric() {
2727
esac
2828
}
2929

30+
# With WP_CLI_TEST_DOCKER set, the scenarios run in throwaway containers instead, every worker in
31+
# its own, with a database server of the same type and version, so they share nothing.
32+
# "auto" runs them without Docker where it cannot be used, "1" (or "yes", "true", "on") fails instead.
33+
case "${WP_CLI_TEST_DOCKER-}" in
34+
""|0|no|false|off) DOCKER="";;
35+
auto) DOCKER="auto";;
36+
1|yes|true|on) DOCKER="force";;
37+
*)
38+
echo "Warning: WP_CLI_TEST_DOCKER is neither \"auto\" nor a yes or no, running the scenarios without Docker." >&2
39+
DOCKER="";;
40+
esac
41+
42+
without_docker() {
43+
if [ "${DOCKER}" = "force" ]; then
44+
echo "Error: $1 Set WP_CLI_TEST_DOCKER to \"auto\" to run the scenarios without Docker in that case." >&2
45+
exit 1
46+
fi
47+
echo "$1 Running the scenarios without Docker." >&2
48+
DOCKER=""
49+
}
50+
51+
case "$(uname -s)" in
52+
MINGW*|MSYS*|CYGWIN*)
53+
if [ -n "${DOCKER}" ]; then
54+
without_docker "The scenarios cannot run in Docker containers on Windows."
55+
fi;;
56+
esac
57+
58+
if [ -n "${DOCKER}" ]; then
59+
if ! command -v docker > /dev/null 2>&1 || ! docker info > /dev/null 2>&1; then
60+
without_docker "Docker is not available."
61+
elif [[ "${WP_CLI_TEST_COVERAGE}" == "true" ]]; then
62+
without_docker "Code coverage cannot be collected in Docker containers."
63+
fi
64+
fi
65+
66+
export WP_CLI_TEST_DOCKER="${DOCKER}"
67+
3068
# If DB type is already set to SQLite, there's nothing to do.
69+
# Neither is there when the containers bring their own database server of a given image.
3170
if [ "${WP_CLI_TEST_DBTYPE-}" = "sqlite" ]; then
3271
echo "WP_CLI_TEST_DBTYPE is set to 'sqlite', skipping database check."
72+
elif [ -n "${DOCKER}" ] && [ -n "${WP_CLI_TEST_DOCKER_DB-}" ]; then
73+
echo "The database servers run in Docker containers of ${WP_CLI_TEST_DOCKER_DB}, skipping database check."
3374
else
3475
# Check for database client and connectivity.
3576
DB_CLIENT=""
@@ -90,6 +131,9 @@ else
90131
fi
91132
fi
92133

134+
# The version as requested, before it is resolved below, for a run that starts over without Docker.
135+
export WP_CLI_TEST_REQUESTED_WP_VERSION="${WP_VERSION-}"
136+
93137
# When installing from an arbitrary archive, there is no version to resolve against
94138
# WordPress.org. Default to "trunk" so that no @require-wp tags are filtered out, as
95139
# such an archive is usually a development build. Set WP_VERSION explicitly alongside
@@ -276,44 +320,10 @@ done
276320
# to be allowed to create, as set up by "composer prepare-tests".
277321
PARALLEL="${WP_CLI_TEST_PARALLEL-}"
278322

279-
# With WP_CLI_TEST_DOCKER set, the scenarios run in throwaway containers instead, every worker in
280-
# its own, with a database server of the same type and version, so they share nothing.
281-
# "auto" runs them without Docker where it cannot be used, "1" (or "yes", "true", "on") fails instead.
282-
case "${WP_CLI_TEST_DOCKER-}" in
283-
""|0|no|false|off) DOCKER="";;
284-
auto) DOCKER="auto";;
285-
1|yes|true|on) DOCKER="force";;
286-
*)
287-
echo "Warning: WP_CLI_TEST_DOCKER is neither \"auto\" nor a yes or no, running the scenarios without Docker." >&2
288-
DOCKER="";;
289-
esac
290-
291-
without_docker() {
292-
if [ "${DOCKER}" = "force" ]; then
293-
echo "Error: $1 Set WP_CLI_TEST_DOCKER to \"auto\" to run the scenarios without Docker in that case." >&2
294-
exit 1
295-
fi
296-
echo "$1 Running the scenarios without Docker." >&2
297-
DOCKER=""
298-
}
299-
300323
case "$(uname -s)" in
301-
MINGW*|MSYS*|CYGWIN*)
302-
PARALLEL=""
303-
if [ -n "${DOCKER}" ]; then
304-
without_docker "The scenarios cannot run in Docker containers on Windows."
305-
fi;;
324+
MINGW*|MSYS*|CYGWIN*) PARALLEL="";;
306325
esac
307326

308-
if [ -n "${DOCKER}" ]; then
309-
if ! command -v docker > /dev/null 2>&1 || ! docker info > /dev/null 2>&1; then
310-
without_docker "Docker is not available."
311-
elif [[ "${WP_CLI_TEST_COVERAGE}" == "true" ]]; then
312-
without_docker "Code coverage cannot be collected in Docker containers."
313-
fi
314-
fi
315-
316-
export WP_CLI_TEST_DOCKER="${DOCKER}"
317327

318328
# A rerun after a parallel run covers exactly the scenarios that failed in it, which only the
319329
# parallel runner knows, whether or not WP_CLI_TEST_PARALLEL is set for the rerun as well.

‎tests/tests/TestBehatTags.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,33 @@ public function test_behat_tags_db_version(): void {
349349
$this->assertSame( $expected, $output );
350350
}
351351

352+
public function test_behat_tags_docker_db_image(): void {
353+
if ( Utils\is_windows() ) {
354+
$this->markTestSkipped( 'The scenarios do not run in Docker containers on Windows.' );
355+
}
356+
357+
file_put_contents( $this->temp_dir . DIRECTORY_SEPARATOR . 'features' . DIRECTORY_SEPARATOR . 'db.feature', '@require-mysql @require-mariadb-10.6 @require-mariadb-11.4 @less-than-mariadb-10.5 @less-than-mariadb-11' );
358+
359+
$base_expecteds = array( '~@github-api', '~@broken' );
360+
if ( 'sqlite' !== getenv( 'WP_CLI_TEST_OBJECT_CACHE' ) ) {
361+
$base_expecteds[] = '~@require-object-cache';
362+
} else {
363+
$base_expecteds[] = '~@skip-object-cache';
364+
}
365+
$expecteds = array( '~@require-mysql', '~@require-sqlite', '~@skip-mariadb', '~@require-mariadb-11.4', '~@less-than-mariadb-10.5' );
366+
$expected = '--tags=' . implode( '&&', array_merge( $base_expecteds, $expecteds ) );
367+
$output = $this->run_with_env(
368+
array(
369+
'GITHUB_TOKEN' => null,
370+
'WP_VERSION' => null,
371+
'WP_CLI_TEST_DBTYPE' => 'mysql',
372+
'WP_CLI_TEST_DOCKER' => 'force',
373+
'WP_CLI_TEST_DOCKER_DB' => 'mariadb:10.11',
374+
)
375+
);
376+
$this->assertSame( $expected, $output );
377+
}
378+
352379
public function test_behat_tags_os(): void {
353380
$env_github_token = getenv( 'GITHUB_TOKEN' );
354381
$db_type = getenv( 'WP_CLI_TEST_DBTYPE' );

‎utils/behat-parallel.php‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,28 @@ function wp_cli_tests_without_docker( $reason ) {
7171
fwrite( STDERR, "{$reason} Running the scenarios without Docker.\n" );
7272
}
7373

74+
/**
75+
* Start the run over without Docker and exit with its exit code.
76+
*
77+
* The database check and the scenario selection were made for the containers,
78+
* so run-behat-tests has to make them again for the host.
79+
*
80+
* @param array<string> $user_args
81+
* @param array<string>|null $rerun_paths Scenarios of a rerun, which are put back for it.
82+
*/
83+
function wp_cli_tests_restart_without_docker( array $user_args, $rerun_paths ) {
84+
if ( null !== $rerun_paths ) {
85+
file_put_contents( wp_cli_tests_failures_file(), implode( "\n", $rerun_paths ) . "\n" );
86+
$user_args[] = '--rerun';
87+
}
88+
89+
$wp_version = getenv( 'WP_CLI_TEST_REQUESTED_WP_VERSION' );
90+
putenv( false === $wp_version || '' === $wp_version ? 'WP_VERSION' : "WP_VERSION={$wp_version}" );
91+
putenv( 'WP_CLI_TEST_DOCKER=0' );
92+
passthru( escapeshellarg( dirname( __DIR__ ) . '/bin/run-behat-tests' ) . ' ' . implode( ' ', array_map( 'escapeshellarg', $user_args ) ), $exit_code );
93+
exit( $exit_code );
94+
}
95+
7496
/**
7597
* Run Behat in the current process' environment and exit with its exit code.
7698
*
@@ -567,15 +589,10 @@ function wp_cli_tests_docker_worker_command( $name, $php_image, $db_image, array
567589

568590
if ( null === $php_image ) {
569591
wp_cli_tests_without_docker( 'The containers could not be prepared.' );
592+
wp_cli_tests_restart_without_docker( $user_args, $rerun_paths );
570593
} elseif ( null === $db_image && 'sqlite' !== getenv( 'WP_CLI_TEST_DBTYPE' ) ) {
571594
wp_cli_tests_without_docker( 'The type and version of the database server could not be identified, set WP_CLI_TEST_DOCKER_DB to its Docker image.' );
572-
}
573-
574-
if ( null === $php_image || ( null === $db_image && 'sqlite' !== getenv( 'WP_CLI_TEST_DBTYPE' ) ) ) {
575-
$docker = false;
576-
if ( $workers < 2 || null !== $rerun_paths || $selects_scenarios ) {
577-
wp_cli_tests_run_behat_and_exit( $all_args );
578-
}
595+
wp_cli_tests_restart_without_docker( $user_args, $rerun_paths );
579596
}
580597
}
581598

‎utils/behat-tags.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ function version_tags(
4848
}
4949

5050
function get_db_type_and_version() {
51+
// Docker workers run their own database server of the given image, so it decides.
52+
$docker_db = getenv( 'WP_CLI_TEST_DOCKER_DB' );
53+
if ( getenv( 'WP_CLI_TEST_DOCKER' ) && is_string( $docker_db ) && preg_match( '@^(?:[^/]+/)*(mysql|mariadb)(?::([0-9]+(?:\.[0-9]+)*))?@', $docker_db, $matches ) ) {
54+
return array(
55+
'type' => $matches[1],
56+
'version' => isset( $matches[2] ) ? $matches[2] : '',
57+
);
58+
}
59+
5160
// Detect which client binary is available.
5261
$client_binary = null;
5362
$output = array();

‎utils/docker/Dockerfile‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,34 @@ RUN install_packages() { \
1010
libfreetype6-dev libjpeg-dev libpng-dev libzip-dev; \
1111
} \
1212
&& ( install_packages || { \
13-
sed -i -e 's|deb.debian.org|archive.debian.org|g' -e '/-updates/d' /etc/apt/sources.list \
13+
sed -i -e 's|deb.debian.org|archive.debian.org|g' -e 's|security.debian.org|archive.debian.org|g' -e '/-updates/d' /etc/apt/sources.list \
1414
&& echo 'Acquire::Check-Valid-Until "false";' > /etc/apt/apt.conf.d/99archive \
1515
&& install_packages; \
1616
} ) \
17-
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
17+
&& ( docker-php-ext-configure gd --with-freetype --with-jpeg \
18+
|| docker-php-ext-configure gd --with-freetype-dir=/usr --with-jpeg-dir=/usr ) \
1819
&& docker-php-ext-install exif gd mysqli pdo_mysql zip \
1920
&& ( php -m | grep -qi '^zend opcache$' || docker-php-ext-install opcache ) \
2021
&& rm -rf /var/lib/apt/lists/*
2122

23+
# The SQLite integration plugin needs SQLite 3.37 or later, which older Debian releases do not ship.
24+
# PHP links to the library, so a newer build in /usr/local/lib takes the place of the system one,
25+
# with the same options as Debian's, as PHP uses the functions they enable.
26+
ARG SQLITE_VERSION=3540100
27+
ARG SQLITE_YEAR=2026
28+
ARG SQLITE_SHA3_256=b6ced585c750916b1052667239a12c25a7ff34e7f06e1e6e01ecb0427657d9c4
29+
RUN if dpkg --compare-versions "$(dpkg-query -W -f '${Version}' libsqlite3-0)" lt 3.37; then \
30+
cd /tmp \
31+
&& curl -fsSL -o sqlite.tar.gz "https://www.sqlite.org/${SQLITE_YEAR}/sqlite-autoconf-${SQLITE_VERSION}.tar.gz" \
32+
&& [ "$(openssl dgst -sha3-256 -r sqlite.tar.gz | cut -d' ' -f1)" = "${SQLITE_SHA3_256}" ] \
33+
&& tar xzf sqlite.tar.gz \
34+
&& cd "sqlite-autoconf-${SQLITE_VERSION}" \
35+
&& CFLAGS="-O2 -DSQLITE_ENABLE_COLUMN_METADATA=1 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_UNLOCK_NOTIFY -DSQLITE_SECURE_DELETE" \
36+
./configure --prefix=/usr/local --disable-static > /dev/null \
37+
&& make -j"$(nproc)" > /dev/null \
38+
&& make install > /dev/null \
39+
&& ldconfig \
40+
&& cd /tmp && rm -rf sqlite.tar.gz "sqlite-autoconf-${SQLITE_VERSION}"; \
41+
fi
42+
2243
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

0 commit comments

Comments
 (0)