From 3b358b82564f73aa3a7212683cb628805aa92636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Heiko=20L=C3=BCbbe?= Date: Thu, 24 Oct 2024 07:34:55 +0200 Subject: [PATCH] [cypress] Database Unix Sockets for System Tests (#44092) * [cypress] Enable database usage with Unix Sockets Check if db_host is a Unix socket by verifying the "unix:/" prefix. JavaScript drivers does not handle this prefix, it needs to be stripped. This approach maintains compatibility with the PHP drivers and allows to have Cypress.config.mjs working for installation step (PHP driver) and custom database commands (JavaScript driver). * Fixing lint errors * Rewrote a comment and used C-style formatting. * Rewrote comments and used C-style formatting. --- tests/System/plugins/db.mjs | 47 ++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/tests/System/plugins/db.mjs b/tests/System/plugins/db.mjs index e24a176ae80e7..6f1dcd5d56ad3 100644 --- a/tests/System/plugins/db.mjs +++ b/tests/System/plugins/db.mjs @@ -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, @@ -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) => {