From d22fa928f0f2d6ccdc21a5fc3eb52ffb3a6f5a37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ribari=C4=87?= <43731400+ribaricplusplus@users.noreply.github.com> Date: Fri, 12 Mar 2021 04:00:31 +0100 Subject: [PATCH] Fix Error: Could not process the 'wp-config.php' transformation (#29800) - Use separate tests database service - Configure database credentials in every service - Set wp-config.php to be writable Co-authored-by: Riad Benguella Co-authored-by: Noah Allen --- .../env/lib/build-docker-compose-config.js | 34 ++++++++++++++++--- packages/env/lib/commands/start.js | 6 ++++ packages/env/lib/config/db-env.js | 23 +++++++++++++ packages/env/lib/config/index.js | 2 ++ packages/env/lib/wordpress.js | 22 ++++++++++++ 5 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 packages/env/lib/config/db-env.js diff --git a/packages/env/lib/build-docker-compose-config.js b/packages/env/lib/build-docker-compose-config.js index 668836de88878e..b17cb7bb58212e 100644 --- a/packages/env/lib/build-docker-compose-config.js +++ b/packages/env/lib/build-docker-compose-config.js @@ -9,6 +9,7 @@ const path = require( 'path' ); * Internal dependencies */ const { hasSameCoreSource } = require( './wordpress' ); +const { dbEnv } = require( './config' ); /** * @typedef {import('./config').WPConfig} WPConfig @@ -176,26 +177,40 @@ module.exports = function buildDockerComposeConfig( config ) { image: 'mariadb', ports: [ '3306' ], environment: { - MYSQL_ALLOW_EMPTY_PASSWORD: 'yes', + MYSQL_ROOT_PASSWORD: + dbEnv.credentials.WORDPRESS_DB_PASSWORD, + MYSQL_DATABASE: dbEnv.development.WORDPRESS_DB_NAME, }, volumes: [ 'mysql:/var/lib/mysql' ], }, + 'tests-mysql': { + image: 'mariadb', + ports: [ '3306' ], + environment: { + MYSQL_ROOT_PASSWORD: + dbEnv.credentials.WORDPRESS_DB_PASSWORD, + MYSQL_DATABASE: dbEnv.tests.WORDPRESS_DB_NAME, + }, + volumes: [ 'mysql-test:/var/lib/mysql' ], + }, wordpress: { build: '.', depends_on: [ 'mysql' ], image: developmentWpImage, ports: [ developmentPorts ], environment: { - WORDPRESS_DB_NAME: 'wordpress', + ...dbEnv.credentials, + ...dbEnv.development, }, volumes: developmentMounts, }, 'tests-wordpress': { - depends_on: [ 'mysql' ], + depends_on: [ 'tests-mysql' ], image: testsWpImage, ports: [ testsPorts ], environment: { - WORDPRESS_DB_NAME: 'tests-wordpress', + ...dbEnv.credentials, + ...dbEnv.tests, }, volumes: testsMounts, }, @@ -204,12 +219,20 @@ module.exports = function buildDockerComposeConfig( config ) { image: developmentWpCliImage, volumes: developmentMounts, user: cliUser, + environment: { + ...dbEnv.credentials, + ...dbEnv.development, + }, }, 'tests-cli': { depends_on: [ 'tests-wordpress' ], image: testsWpCliImage, volumes: testsMounts, user: cliUser, + environment: { + ...dbEnv.credentials, + ...dbEnv.tests, + }, }, composer: { image: 'composer', @@ -228,6 +251,8 @@ module.exports = function buildDockerComposeConfig( config ) { LOCAL_DIR: 'html', WP_PHPUNIT__TESTS_CONFIG: '/var/www/html/phpunit-wp-config.php', + ...dbEnv.credentials, + ...dbEnv.tests, }, }, }, @@ -235,6 +260,7 @@ module.exports = function buildDockerComposeConfig( config ) { ...( ! config.coreSource && { wordpress: {} } ), ...( ! config.coreSource && { 'tests-wordpress': {} } ), mysql: {}, + 'mysql-test': {}, 'phpunit-uploads': {}, }, }; diff --git a/packages/env/lib/commands/start.js b/packages/env/lib/commands/start.js index 22fe075948c4a0..4f3a00cc927dd7 100644 --- a/packages/env/lib/commands/start.js +++ b/packages/env/lib/commands/start.js @@ -24,6 +24,7 @@ const downloadSources = require( '../download-sources' ); const { checkDatabaseConnection, makeContentDirectoriesWritable, + makeConfigWritable, configureWordPress, setupWordPressDirectories, } = require( '../wordpress' ); @@ -140,6 +141,11 @@ module.exports = async function start( { spinner, debug, update, xdebug } ) { : [], } ); + await Promise.all( [ + makeConfigWritable( 'development', config ), + makeConfigWritable( 'tests', config ), + ] ); + // Only run WordPress install/configuration when config has changed. if ( shouldConfigureWp ) { spinner.text = 'Configuring WordPress.'; diff --git a/packages/env/lib/config/db-env.js b/packages/env/lib/config/db-env.js new file mode 100644 index 00000000000000..6f06ff97315106 --- /dev/null +++ b/packages/env/lib/config/db-env.js @@ -0,0 +1,23 @@ +// Username and password used in all databases +const credentials = { + WORDPRESS_DB_USER: 'root', + WORDPRESS_DB_PASSWORD: 'password', +}; + +// Environment for test database +const tests = { + WORDPRESS_DB_NAME: 'tests-wordpress', + WORDPRESS_DB_HOST: 'tests-mysql', +}; + +// Environment for development database. DB host gets default value which is set +// elsewhere +const development = { + WORDPRESS_DB_NAME: 'wordpress', +}; + +module.exports = { + credentials, + tests, + development, +}; diff --git a/packages/env/lib/config/index.js b/packages/env/lib/config/index.js index 0dbbcc08a5b8ac..1a944f43501d0d 100644 --- a/packages/env/lib/config/index.js +++ b/packages/env/lib/config/index.js @@ -3,6 +3,7 @@ */ const readConfig = require( './config' ); const { ValidationError } = require( './validate-config' ); +const dbEnv = require( './db-env' ); /** * @typedef {import('./config').WPConfig} WPConfig @@ -13,4 +14,5 @@ const { ValidationError } = require( './validate-config' ); module.exports = { ValidationError, readConfig, + dbEnv, }; diff --git a/packages/env/lib/wordpress.js b/packages/env/lib/wordpress.js index 07705167ac1dc4..da632f9703b5b6 100644 --- a/packages/env/lib/wordpress.js +++ b/packages/env/lib/wordpress.js @@ -47,6 +47,27 @@ async function makeContentDirectoriesWritable( ); } +/** + * Makes wp-config.php owned by www-data so that WordPress can work with the + * file. + * + * @param {WPEnvironment} environment The environment to check. Either 'development' or 'tests'. + * @param {WPConfig} config The wp-env config object. + */ +async function makeConfigWritable( + environment, + { dockerComposeConfigPath, debug } +) { + await dockerCompose.exec( + environment === 'development' ? 'wordpress' : 'tests-wordpress', + 'chown www-data:www-data wp-config.php', + { + config: dockerComposeConfigPath, + log: debug, + } + ); +} + /** * Checks a WordPress database connection. An error is thrown if the test is * unsuccessful. @@ -260,6 +281,7 @@ async function copyCoreFiles( fromPath, toPath ) { module.exports = { hasSameCoreSource, makeContentDirectoriesWritable, + makeConfigWritable, checkDatabaseConnection, configureWordPress, resetDatabase,