Skip to content

Commit dbcbdc2

Browse files
authored
Merge pull request #408 from wp-cli/perf/docker-workers
Run parallel workers in throwaway Docker containers
2 parents 5ec9b93 + 282b410 commit dbcbdc2

4 files changed

Lines changed: 521 additions & 15 deletions

File tree

‎.readme-partials/USING.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,28 @@ Every worker but the first gets its own temporary directory, and with it its own
265265
266266
Scenarios that rely on resources shared across the system, such as a fixed port or a fixed path in the system's temporary directory, are all run by the first worker, one after the other. `composer behat-rerun` re-runs exactly the scenarios that failed in the last parallel run.
267267

268+
Set `WP_CLI_TEST_DOCKER` to run the scenarios in throwaway Docker containers instead, every worker in its own:
269+
```bash
270+
WP_CLI_TEST_DOCKER=auto WP_CLI_TEST_PARALLEL=4 composer behat
271+
```
272+
273+
| `WP_CLI_TEST_DOCKER` | Effect |
274+
|-------------------------------------|--------------------------------------------------------------------------------------------------|
275+
| not set (default), `0`, `no`, `off` | The scenarios run without Docker. |
276+
| `auto` | The scenarios run in containers where possible, and without Docker otherwise, which is reported. |
277+
| `1`, `yes`, `on` | The scenarios run in containers, and the run fails where that is not possible. |
278+
279+
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.
280+
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.
282+
283+
The containers run the same PHP version as the host, unless `WP_CLI_TEST_DOCKER_PHP` sets another one:
284+
```bash
285+
WP_CLI_TEST_DOCKER=1 WP_CLI_TEST_DOCKER_PHP=7.4 WP_CLI_TEST_PARALLEL=4 composer behat
286+
```
287+
288+
The project's Composer dependencies are then installed for that PHP version in a separate directory, the way CI installs them without a lock file, and refreshed once a day. Dependencies that are linked to local checkouts, as in `wp-cli/wp-cli-dev`, come from their published versions in that case, which is reported.
289+
268290
### Linting the feature files
269291
270292
`composer lint-gherkin` checks `features/` with

‎bin/run-behat-tests‎

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,45 @@ done
276276
# to be allowed to create, as set up by "composer prepare-tests".
277277
PARALLEL="${WP_CLI_TEST_PARALLEL-}"
278278

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+
300+
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;;
306+
esac
307+
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}"
317+
279318
# A rerun after a parallel run covers exactly the scenarios that failed in it, which only the
280319
# parallel runner knows, whether or not WP_CLI_TEST_PARALLEL is set for the rerun as well.
281320
for arg in "$@"; do
@@ -284,10 +323,10 @@ for arg in "$@"; do
284323
fi
285324
done
286325

287-
case "$(uname -s)" in
288-
MINGW*|MSYS*|CYGWIN*)
289-
PARALLEL="";;
290-
esac
326+
if [ -n "${DOCKER}" ]; then
327+
exec php "$WP_CLI_TESTS_ROOT"/utils/behat-parallel.php "${PARALLEL:-1}" --snippets-for="WP_CLI\Tests\Context\FeatureContext" "${FORMAT_ARGS[@]}" "$BEHAT_TAGS" --strict "${BEHAT_EXTRA_ARGS[@]}" -- "$@"
328+
fi
329+
291330
if [ -n "${PARALLEL}" ] && [ "${PARALLEL}" != "1" ]; then
292331
if ! is_numeric "${PARALLEL}"; then
293332
echo "Warning: WP_CLI_TEST_PARALLEL is not a number of workers, running the scenarios one after the other." >&2

0 commit comments

Comments
 (0)