Skip to content

Commit c87933c

Browse files
swissspidyclaude
andauthored
Give the Docker workers the database host, port and socket (#429)
The GitHub Actions workflows set MYSQL_HOST, MYSQL_TCP_PORT and WP_CLI_TEST_DBSOCKET for the database server of the runner, and scenarios use them through {MYSQL_HOST}, {MYSQL_PORT} and {DB_SOCKET}. The Docker workers passed on none of them, and had no socket to connect through, so scenarios such as those of wp-cli/config-command that connect through the socket or with the mysql client failed as soon as they ran in containers. Set all three for the worker's own database container, whose socket is now in a directory of the host that both containers mount. Claude-Session: https://claude.ai/code/session_01UWTd5pgZgbJQL9QCPAUWvf Co-authored-by: Claude <noreply@anthropic.com>
1 parent cc95af5 commit c87933c

1 file changed

Lines changed: 34 additions & 8 deletions

File tree

‎utils/behat-parallel.php‎

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
*/
3333
const WP_CLI_TESTS_SHARED_RESOURCES = '#localhost:\d+|127\.0\.0\.1:\d+|built-in web server|launch in the background|\bwp server\b|_scaffold\b|install-wp-tests|/tmp/#';
3434

35+
/**
36+
* Directory in which the database container of a Docker worker puts its socket, in both of its containers.
37+
*/
38+
const WP_CLI_TESTS_DOCKER_SOCKET_DIR = '/run/wp-cli-tests-mysqld';
39+
3540
/**
3641
* Get the file that holds the scenarios which failed in the last parallel run.
3742
*
@@ -592,23 +597,40 @@ function wp_cli_tests_docker_worker_command( $name, $php_image, $db_image, array
592597
$network = '';
593598
$label = ' --label ' . escapeshellarg( 'wp-cli-tests.owner=' . wp_cli_tests_docker_owner() );
594599

600+
$env = [
601+
'HOME' => '/tmp/home',
602+
'WP_CLI_TEST_DBHOST' => '127.0.0.1:3306',
603+
'WP_CLI_TEST_DBROOTUSER' => 'root',
604+
'WP_CLI_TEST_DBROOTPASS' => 'root',
605+
];
606+
595607
if ( null !== $db_image ) {
608+
// The server's socket is shared with the worker through a directory of the host that both mount,
609+
// so that scenarios can connect through the socket as well as over TCP.
610+
$socket_dir = sys_get_temp_dir() . "/{$name}-mysqld";
611+
// The server takes the directory over, so it is left as it is when it is there from an earlier run.
612+
if ( ! is_dir( $socket_dir ) ) {
613+
mkdir( $socket_dir, 0777, true );
614+
chmod( $socket_dir, 0777 );
615+
}
616+
$socket = WP_CLI_TESTS_DOCKER_SOCKET_DIR . '/mysqld.sock';
617+
596618
exec(
597619
'docker run -d --rm --name ' . escapeshellarg( "{$name}-db" ) . $label
598620
. ' --tmpfs /var/lib/mysql'
621+
. ' -v ' . escapeshellarg( $socket_dir . ':' . WP_CLI_TESTS_DOCKER_SOCKET_DIR )
599622
. ' -e MYSQL_ROOT_PASSWORD=root -e MYSQL_USER=wp_cli_test -e MYSQL_PASSWORD=password1'
600-
. ' ' . escapeshellarg( $db_image ) . ' ' . wp_cli_tests_docker_db_options( $db_image ) . ' 2>&1'
623+
. ' ' . escapeshellarg( $db_image ) . ' ' . wp_cli_tests_docker_db_options( $db_image )
624+
. ' --socket=' . escapeshellarg( $socket ) . ' 2>&1'
601625
);
602626
// Share the database container's network, so the server is at 127.0.0.1 and nothing else is.
603627
$network = ' --network ' . escapeshellarg( "container:{$name}-db" );
604-
}
605628

606-
$env = [
607-
'HOME' => '/tmp/home',
608-
'WP_CLI_TEST_DBHOST' => '127.0.0.1:3306',
609-
'WP_CLI_TEST_DBROOTUSER' => 'root',
610-
'WP_CLI_TEST_DBROOTPASS' => 'root',
611-
];
629+
// The settings the GitHub Actions workflows pass for the server of the runner, adapted to the container's.
630+
$env['MYSQL_HOST'] = '127.0.0.1';
631+
$env['MYSQL_TCP_PORT'] = '3306';
632+
$env['WP_CLI_TEST_DBSOCKET'] = $socket;
633+
}
612634

613635
foreach ( getenv() as $key => $value ) {
614636
if ( preg_match( '/^(WP_VERSION|NO_COLOR|CI|GITHUB_TOKEN|WP_CLI_TEST_(DBTYPE|OBJECT_CACHE|CORE_ZIP|QUIET|LOG_RUN_TIMES|PHP_TUNING|WP_VERSION_CACHE_TTL))$/', $key ) ) {
@@ -629,6 +651,10 @@ function wp_cli_tests_docker_worker_command( $name, $php_image, $db_image, array
629651
}
630652
}
631653

654+
if ( null !== $db_image ) {
655+
$command .= ' -v ' . escapeshellarg( sys_get_temp_dir() . "/{$name}-mysqld:" . WP_CLI_TESTS_DOCKER_SOCKET_DIR );
656+
}
657+
632658
foreach ( $env as $key => $value ) {
633659
$command .= ' -e ' . escapeshellarg( "{$key}={$value}" );
634660
}

0 commit comments

Comments
 (0)