From ac208206c060c03bd8923241c37c149b73915ecd Mon Sep 17 00:00:00 2001 From: Liam Defty Date: Wed, 15 Apr 2020 20:15:21 +0100 Subject: [PATCH] 0.4.0 - Performance improvements --- .gitignore | 1 + Dockerfile | 7 +++-- Dockerfile.template | 29 +++++++++++++++++++ README.md | 2 +- bin/createConfig.js | 5 ++-- bin/start.js | 29 +++++++++++++------ bin/utils/exec.js | 1 + bin/utils/run.js | 5 +++- entrypoint.sh | 68 +++++++++++++++++---------------------------- package.json | 2 +- 10 files changed, 92 insertions(+), 57 deletions(-) create mode 100644 Dockerfile.template diff --git a/.gitignore b/.gitignore index aaf32cb..a761caa 100644 --- a/.gitignore +++ b/.gitignore @@ -34,6 +34,7 @@ Desktop.ini $RECYCLE.BIN/ # Project Specific +Dockerfile docker-compose.yml debug.log config.json diff --git a/Dockerfile b/Dockerfile index 21bc0d4..40d0598 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,5 @@ FROM php:7.3-apache -# install the PHP extensions we need RUN apt-get update && apt-get install -y wget libpng-dev libjpeg-dev gnupg default-mysql-client nano less && rm -rf /var/lib/apt/lists/* \ && docker-php-ext-configure gd \ && docker-php-ext-install gd mysqli @@ -13,7 +12,11 @@ RUN curl -o /bin/wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/pha && chmod +x /bin/wp \ && wp --info --allow-root -ENV WP_VERSION latest +RUN set -ex; \ + curl -o wordpress.tar.gz https://wordpress.org/wordpress-5.3.2.tar.gz; \ + tar -xzf wordpress.tar.gz -C /usr/src/; \ + rm wordpress.tar.gz; \ + chown -R www-data:www-data /usr/src/wordpress COPY entrypoint.sh /entrypoint.sh diff --git a/Dockerfile.template b/Dockerfile.template new file mode 100644 index 0000000..7e75967 --- /dev/null +++ b/Dockerfile.template @@ -0,0 +1,29 @@ +FROM php:7.3-apache + +RUN apt-get update && apt-get install -y wget libpng-dev libjpeg-dev gnupg default-mysql-client nano less && rm -rf /var/lib/apt/lists/* \ + && docker-php-ext-configure gd \ + && docker-php-ext-install gd mysqli + +RUN a2enmod rewrite + +VOLUME /var/www/html + +RUN curl -o /bin/wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar \ + && chmod +x /bin/wp \ + && wp --info --allow-root + +RUN set -ex; \ + curl -o wordpress.tar.gz https://wordpress.org/wordpress-WP_VERSION.tar.gz; \ + tar -xzf wordpress.tar.gz -C /usr/src/; \ + rm wordpress.tar.gz; \ + chown -R www-data:www-data /usr/src/wordpress + +COPY entrypoint.sh /entrypoint.sh + +RUN chmod +x /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] + +EXPOSE 80 + +CMD ["apache2-foreground"] diff --git a/README.md b/README.md index f777cd2..af99ec4 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ This may take a while as it builds and starts a new docker container. However, y > Having trouble? Use File sharing to allow local directories on the Mac to be shared with Linux containers. See more https://docs.docker.com/docker-for-mac/#file-sharing -Once it is running there is no need to re-start it every time you run cypress. WP Cypress will restore the database to its initial state between each suite of integration tests to ensure a clean slate between tests. +Once it is running there is no need to re-start it every time you run cypress. WP Cypress will start and restore the database to it's initial state between each suite of integration tests to ensure a clean slate between tests. You can add environment variables to the `cypress.json` configuration file. You can use this to specify the version of WordPress and which plugins/themes to install. All plugins will be activated and the first theme in the list will be activated. If this file is changed, you will need to re-run `wp-cypress start` to see the changes take effect. Composer is recommended to manage plugins and themes to if they do not exist in your project directory. diff --git a/bin/createConfig.js b/bin/createConfig.js index 029d181..769979e 100644 --- a/bin/createConfig.js +++ b/bin/createConfig.js @@ -25,6 +25,9 @@ const createConfig = (userConfig, dir) => { }); } + shell.cp(`${dir}/Dockerfile.template`, `${dir}/Dockerfile`); + shell.sed('-i', 'WP_VERSION', userConfig.version || 'latest', `${dir}/Dockerfile`); + const dockerComposeFile = shell.ShellString(` version: '3.7' services: @@ -34,8 +37,6 @@ services: build: . ports: - 80:80 - environment: - WP_VERSION: ${userConfig.version || 'latest'} volumes: - wp:/var/www/html ${volumes.map((x) => ` - ${x}`).join('')} diff --git a/bin/start.js b/bin/start.js index 811202a..d72a0d7 100644 --- a/bin/start.js +++ b/bin/start.js @@ -3,9 +3,8 @@ const shell = require('shelljs'); const retryCommand = require('./utils/retryCommand'); const createConfig = require('./createConfig'); -const { exec, wpcli } = require('./utils/exec'); +const { exec, cli, wpcli } = require('./utils/exec'); const run = require('./utils/run'); -const configureWordPress = require('./configureWordPress'); const start = async (userConfig, packageDir, logFile) => { const configFile = fs.createWriteStream(`${packageDir}/config.json`); @@ -18,19 +17,33 @@ const start = async (userConfig, packageDir, logFile) => { 'docker-compose down --volumes && docker-compose build && docker-compose up -d', logFile, ), - 'Creating Test Environment', - 'Test Environment running on port 80', + 'Creating test container', + 'Test container created', logFile, ); await run( - async () => retryCommand(() => wpcli('core is-installed', logFile), 2000, 30), - 'Downloading & Installing WordPress', - 'WordPress Installed', + async () => retryCommand(() => cli('mysqladmin ping -h"db"', logFile), 2000, 30), + 'Waiting for database connection', + 'Database connected', logFile, ); - await configureWordPress(configFile.path, logFile); + await run( + async () => wpcli(`core config \ + --dbhost=db \ + --dbname=wordpress \ + --dbuser=root \ + --dbpass='' \ + --locale=en_US \ + --extra-php < new Promise((resolve) => { module.exports = { exec, + cli: (command, logFile) => exec(`docker-compose exec -T wp ${command}`, logFile), wpcli: (command, logFile) => exec(`docker-compose exec -T wp wp --allow-root ${command}`, logFile), }; diff --git a/bin/utils/run.js b/bin/utils/run.js index 62f250e..ab95112 100644 --- a/bin/utils/run.js +++ b/bin/utils/run.js @@ -2,7 +2,10 @@ const ora = require('ora'); const shell = require('shelljs'); const run = async (command, start, succeed = false, logFile = false) => { - const spinner = ora(start).start(); + const spinner = ora({ + text: start, + prefixText: 'wp-cypress', + }).start(); const { code, stdout, stderr } = await command(); diff --git a/entrypoint.sh b/entrypoint.sh index 1452f60..c11663d 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,41 +1,25 @@ #!/bin/bash set -eu -if [ -f wp-config.php ]; then - rm wp-config.php -fi -: ${WP_LOCALE:=${WP_LOCALE:-en_US}} -: ${WP_ADMIN_EMAIL:=${WP_ADMIN_EMAIL:-admin@example.com}} -: ${WP_DB_HOST:=db} -: ${WP_DB_USER:=${MYSQL_ENV_MYSQL_USER:-root}} -: ${WP_DB_PASSWORD:=''} -: ${WP_DB_NAME:=${MYSQL_ENV_MYSQL_DATABASE:-wordpress}} +user="$(id -u)" +group="$(id -g)" -wp core --allow-root download \ - --version=${WP_VERSION} \ - --force --debug +if [ "$(id -u)" = '0' ] && [ "$(stat -c '%u:%g' .)" = '0:0' ]; then + chown "$user:$group" . +fi -c=1 -until mysqladmin ping -h"$WP_DB_HOST" --silent &> /dev/null -do - c=$((c + 1)) - if [ $c -eq 60 ] - then - break - fi - sleep 2 -done +sourceTarArgs=( + --create + --file - + --directory /usr/src/wordpress + --owner "$user" --group "$group" +) +targetTarArgs=( + --extract + --file - +) -# Generate the wp-config file for debugging. -wp core --allow-root config \ - --dbhost="$WP_DB_HOST" \ - --dbname="$WP_DB_NAME" \ - --dbuser="$WP_DB_USER" \ - --dbpass="$WP_DB_PASSWORD" \ - --locale="$WP_LOCALE" \ - --extra-php < .htaccess < /dev/null +do + c=$((c + 1)) + if [ $c -eq 60 ] + then + break + fi + sleep 2 +done exec "$@" diff --git a/package.json b/package.json index 51845ce..15a9b82 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bigbite/wp-cypress", - "version": "0.3.2", + "version": "0.4.0", "description": "WordPress end to end testing with Cypress.io", "main": "lib/index.js", "repository": "https://github.com/bigbite/wp-cypress",