Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prepare-tests info #209

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 60 additions & 16 deletions bin/install-package-tests
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
is_numeric() {
case $1 in
''|*[!0-9]*) return 1;; # returns 1 if not numeric
*) return 0;; # returns 0 if numeric
*) return 0;; # returns 0 if numeric
esac
}
# Promt color vars.
C_RED="\033[31m"
C_BLUE="\033[34m"
NO_FORMAT="\033[0m"

HOST=localhost
PORT=""
Expand All @@ -28,65 +32,103 @@ if [ -n "${WP_CLI_TEST_DBHOST}" ]; then
if [ -n "${PORT}" ]; then
# If the port is not numeric, then we assume it is a socket path.
if is_numeric "${PORT}"; then
echo "Connecting to custom host: ${C_BLUE}${HOST}${NO_FORMAT} on port ${C_BLUE}${PORT}${NO_FORMAT}"
HOST_STRING="${HOST_STRING} --port=${PORT} --protocol=tcp"
else
echo "Connecting to custom host: ${C_BLUE}${HOST}${NO_FORMAT} on socket ${C_BLUE}${PORT}${NO_FORMAT}"
HOST_STRING="${HOST_STRING} --socket=${PORT} --protocol=socket"
fi
else
echo "Connecting to custom host: ${C_BLUE}${HOST}${NO_FORMAT}"
fi
else
echo "Connecting to default host: ${C_BLUE}${HOST}${NO_FORMAT}"
fi

USER=root
if [ -n "${WP_CLI_TEST_DBROOTUSER}" ]; then
USER="${WP_CLI_TEST_DBROOTUSER}"
echo "Connecting with custom root user: ${C_BLUE}${WP_CLI_TEST_DBROOTUSER}${NO_FORMAT}"
USER="${WP_CLI_TEST_DBROOTUSER}"
else
echo "Connecting with default root user: ${C_BLUE}${USER}${NO_FORMAT}"
fi

PASSWORD_STRING=""
if [ -n "${WP_CLI_TEST_DBROOTPASS}" ]; then
PASSWORD_STRING="-p${WP_CLI_TEST_DBROOTPASS}"
echo "Connecting with custom root password: ${C_BLUE}${WP_CLI_TEST_DBROOTPASS}${NO_FORMAT}"
PASSWORD_STRING="-p${WP_CLI_TEST_DBROOTPASS}"
else
echo "Connecting with default root password: ${C_BLUE}empty${NO_FORMAT}"
fi

TEST_DB=wp_cli_test
if [ -n "${WP_CLI_TEST_DBNAME}" ]; then
TEST_DB="${WP_CLI_TEST_DBNAME}"
echo "Using custom test database: ${C_BLUE}${WP_CLI_TEST_DBNAME}${NO_FORMAT}"
TEST_DB="${WP_CLI_TEST_DBNAME}"
else
echo "Using default test database: ${C_BLUE}${TEST_DB}${NO_FORMAT}"
fi

TEST_USER=wp_cli_test
if [ -n "${WP_CLI_TEST_DBUSER}" ]; then
TEST_USER="${WP_CLI_TEST_DBUSER}"
echo "Using custom test user: ${C_BLUE}${WP_CLI_TEST_DBUSER}${NO_FORMAT}"
TEST_USER="${WP_CLI_TEST_DBUSER}"
else
echo "Using default test user: ${C_BLUE}${TEST_USER}${NO_FORMAT}"
fi

TEST_PASSWORD=password1
if [ -n "${WP_CLI_TEST_DBPASS}" ]; then
TEST_PASSWORD="${WP_CLI_TEST_DBPASS}"
echo "Using custom test password: ${C_BLUE}${WP_CLI_TEST_DBPASS}${NO_FORMAT}"
TEST_PASSWORD="${WP_CLI_TEST_DBPASS}"
else
echo "Using default test password: ${C_BLUE}${TEST_PASSWORD}${NO_FORMAT}"
fi

echo 'Checking if MySQL is ready...'

if [ -z "$PS1" ]; then
# These vars are because github actions gave problems in the past.
MYSQL_TRIES=36
MYSQL_WAIT=5
else
MYSQL_TRIES=1
MYSQL_WAIT=0
fi

while ! mysql ${HOST_STRING} --user="${USER}" "${PASSWORD_STRING}" --execute="SHOW DATABASES;" | grep 'information_schema' >/dev/null;
do
echo 'Waiting for MySQL...'
sleep 5
i=$((i+1))
if [ $i -gt 36 ]; then
echo 'MySQL failed to start. Aborting.'
exit 1
fi
i=$((i+1))
if [ "${MYSQL_TRIES}" -gt 1 ]; then
echo "Waiting for MySQL(${i}/${MYSQL_TRIES})..."
sleep ${MYSQL_WAIT}
fi

if [ $i -ge $MYSQL_TRIES ]; then
echo "${C_RED}MySQL failed to start. Aborting.${NO_FORMAT}"
echo "Cannot connect to mysql server. For all available variables, check the documentation at:"
echo " ${C_BLUE}https://github.com/wp-cli/wp-cli-tests?tab=readme-ov-file#the-database-credentials${NO_FORMAT}"
exit 1
fi
done

# Prepare the database for running the tests with a MySQL version 8.0 or higher.
install_mysql_db_8_0_plus() {
set -ex
set -ex # print all the commands.
mysql -e "CREATE DATABASE IF NOT EXISTS \`${TEST_DB}\`;" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
mysql -e "CREATE USER IF NOT EXISTS \`${TEST_USER}\`@'%' IDENTIFIED WITH mysql_native_password BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
mysql -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
mysql -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
{ set +ex; } 2> /dev/null # stop printing the commands
}

# Prepare the database for running the tests with a MySQL version lower than 8.0.
install_mysql_db_lower_than_8_0() {
set -ex
set -ex # print all the commands.
mysql -e "CREATE DATABASE IF NOT EXISTS \`${TEST_DB}\`;" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
mysql -e "GRANT ALL ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
mysql -e "GRANT ALL ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
{ set +ex; } 2> /dev/null # stop printing the commands
}

VERSION_STRING=$(mysql -e "SELECT VERSION()" --skip-column-names ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}")
Expand All @@ -102,9 +144,11 @@ esac

echo "Detected ${TYPE} at version ${MAJOR}.${MINOR}"


if [ "${TYPE}" != "MariaDB" ] && [ "${MAJOR}" -ge 8 ]; then
install_mysql_db_8_0_plus
else
install_mysql_db_lower_than_8_0
fi

echo "Succesfully prepared the database for running tests."
echo "This command does not have to be run again."
37 changes: 36 additions & 1 deletion src/Context/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Behat\Testwork\Hook\Scope\AfterSuiteScope;
use Behat\Testwork\Hook\Scope\BeforeSuiteScope;
use RuntimeException;
use WP_CLI;
use WP_CLI\Process;
use WP_CLI\Utils;

Expand Down Expand Up @@ -639,6 +640,7 @@ public function __construct() {

$this->variables['CORE_CONFIG_SETTINGS'] = Utils\assoc_args_to_str( self::$db_settings );

$this->test_connection();
$this->drop_db();
$this->set_cache_dir();
}
Expand Down Expand Up @@ -842,6 +844,8 @@ private function set_cache_dir() {
* @param string $sql_cmd Command to run.
* @param array $assoc_args Optional. Associative array of options. Default empty.
* @param bool $add_database Optional. Whether to add dbname to the $sql_cmd. Default false.
*
* return array.
*/
private static function run_sql( $sql_cmd, $assoc_args = [], $add_database = false ) {
$default_assoc_args = [
Expand All @@ -852,11 +856,22 @@ private static function run_sql( $sql_cmd, $assoc_args = [], $add_database = fal
if ( $add_database ) {
$sql_cmd .= ' ' . escapeshellarg( self::$db_settings['dbname'] );
}
$send_to_shell = true;
if ( isset( $assoc_args['send_to_shell'] ) ) {
$send_to_shell = (bool) $assoc_args['send_to_shell'];
unset( $assoc_args['send_to_shell'] );
}

$start_time = microtime( true );
Utils\run_mysql_command( $sql_cmd, array_merge( $assoc_args, $default_assoc_args ) );
$result = Utils\run_mysql_command( $sql_cmd, array_merge( $assoc_args, $default_assoc_args ), null, $send_to_shell );
if ( self::$log_run_times ) {
self::log_proc_method_run_time( 'run_sql ' . $sql_cmd, $start_time );
}
return [
'stdout' => $result[0],
'stderr' => $result[1],
'exit_code' => $result[2],
];
}

public function create_db() {
Expand All @@ -868,6 +883,26 @@ public function create_db() {
self::run_sql( 'mysql --no-defaults', [ 'execute' => "CREATE DATABASE IF NOT EXISTS $dbname" ] );
}

/**
* Test if the database connection is working.
*/
public function test_connection() {
$sql_result = self::run_sql(
'mysql --no-defaults',
[
'execute' => 'SELECT 1',
'send_to_shell' => false,
]
);
if ( ! empty( $sql_result['stderr'] ) ) {
# WP_CLI output functions are suppressed in behat context.
echo 'There was an error connecting to the database:' . \PHP_EOL;
echo ' ' . trim( $sql_result['stderr'] ) . \PHP_EOL;
echo 'run `composer prepare-tests` to connect to the database.' . \PHP_EOL;
die( 1 );
}
}

public function drop_db() {
if ( 'sqlite' === self::$db_type ) {
return;
Expand Down
Loading