Skip to content
New issue

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

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

Already on GitHub? # to your account

[cypress] Database Unix Sockets for System Tests #44092

Merged
merged 6 commits into from
Oct 24, 2024
Merged
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
47 changes: 39 additions & 8 deletions tests/System/plugins/db.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,20 @@ function queryTestDB(joomlaQuery, config) {
// Do we use PostgreSQL?
if (config.env.db_type === 'pgsql' || config.env.db_type === 'PostgreSQL (PDO)') {
if (postgresConnectionPool === null) {
let hostOrUnixPath = config.env.db_host;

/* Verify if the connection is a Unix socket by checking for the "unix:/" prefix.
* PostgreSQL JS driver does not support this prefix, so it must be removed.
* We standardise the use of this prefix with the PHP driver by handling it here.
*/
if (hostOrUnixPath.startsWith('unix:/')) {
// e.g. 'unix:/var/run/postgresql' -> '/var/run/postgresql'
hostOrUnixPath = hostOrUnixPath.replace('unix:', '');
}

// Initialisation on the first call
postgresConnectionPool = new Pool({
host: config.env.db_host,
host: hostOrUnixPath,
port: config.env.db_port,
database: config.env.db_name,
user: config.env.db_user,
Expand Down Expand Up @@ -82,13 +93,33 @@ function queryTestDB(joomlaQuery, config) {
// Return a promise which runs the query for MariaDB / MySQL
return new Promise((resolve, reject) => {
// Create the connection and connect
const connection = mysql.createConnection({
host: config.env.db_host,
port: config.env.db_port,
user: config.env.db_user,
password: config.env.db_password,
database: config.env.db_name,
});
let connectionConfig;
/* Verify if the connection is a Unix socket by checking for the "unix:/" prefix.
* MariaDB and MySQL JS drivers do not support this prefix, so it must be removed.
* We standardise the use of this prefix with the PHP driver by handling it here.
*/
if (config.env.db_host.startsWith('unix:/')) {
// If the host is a Unix socket, extract the socket path
connectionConfig = {
// e.g. 'unix:/var/run/mysqld/mysqld.sock' -> '/var/run/mysqld/mysqld.sock'
socketPath: config.env.db_host.replace('unix:', ''),
user: config.env.db_user,
password: config.env.db_password,
database: config.env.db_name,
};
} else {
// Otherwise, use regular TCP host connection settings
connectionConfig = {
host: config.env.db_host,
port: config.env.db_port,
user: config.env.db_user,
password: config.env.db_password,
database: config.env.db_name,
};
}

// Create the MySQL/MariaDB connection
const connection = mysql.createConnection(connectionConfig);

// Perform the query
connection.query(query, (error, results) => {
Expand Down