Skip to content

Commit

Permalink
chore: Isolate e2e tests and reset themn for each run
Browse files Browse the repository at this point in the history
Fixes #7959
Fixes #8010
  • Loading branch information
tofumatt committed Jul 20, 2018
1 parent 201be4b commit 946ad7c
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 37 deletions.
47 changes: 13 additions & 34 deletions bin/install-docker.sh
Original file line number Diff line number Diff line change
@@ -1,66 +1,45 @@
#!/bin/bash

# Exit if any command fails
# Exit if any command fails.
set -e

# Include useful functions
# Include useful functions.
. "$(dirname "$0")/includes.sh"

# Check that Docker is installed
# Check that Docker is installed.
if ! command_exists "docker"; then
echo -e $(error_message "Docker doesn't seem to be installed. Please head on over to the Docker site to download it: $(action_format "https://www.docker.com/community-edition#/download")")
exit 1
fi

# Check that Docker is running
# Check that Docker is running.
if ! docker info >/dev/null 2>&1; then
echo -e $(error_message "Docker isn't running. Please check that you've started your Docker app, and see it in your system tray.")
exit 1
fi

# Stop existing containers
# Stop existing containers.
echo -e $(status_message "Stopping Docker containers...")
docker-compose down --remove-orphans >/dev/null 2>&1

# Download image updates
# Download image updates.
echo -e $(status_message "Downloading Docker image updates...")
docker-compose pull

# Launch the containers
# Launch the containers.
echo -e $(status_message "Starting Docker containers...")
docker-compose up -d >/dev/null

HOST_PORT=$(docker-compose port wordpress 80 | awk -F : '{printf $2}')
# Set up WordPress Development site.
. "$(dirname "$0")/install-wordpress.sh"

# Wait until the docker containers are setup properely
echo -en $(status_message "Attempting to connect to wordpress...")
until $(curl -L http://localhost:$HOST_PORT -so - 2>&1 | grep -q "WordPress"); do
echo -n '.'
sleep 5
done
echo ''
# Set up WordPress site used for end-to-end (e2e) tests.
. "$(dirname "$0")/install-wordpress.sh" --e2e_tests

# Install WordPress
echo -e $(status_message "Installing WordPress...")
docker-compose run --rm -u 33 cli core install --url=localhost:$HOST_PORT --title=Gutenberg --admin_user=admin --admin_password=password --admin_email=test@test.com >/dev/null
# Check for WordPress updates, just in case the WordPress image isn't up to date.
docker-compose run --rm -u 33 cli core update >/dev/null

# If the 'wordpress' volume wasn't during the down/up earlier, but the post port has changed, we need to update it.
CURRENT_URL=$(docker-compose run -T --rm cli option get siteurl)
if [ "$CURRENT_URL" != "http://localhost:$HOST_PORT" ]; then
docker-compose run --rm cli option update home "http://localhost:$HOST_PORT" >/dev/null
docker-compose run --rm cli option update siteurl "http://localhost:$HOST_PORT" >/dev/null
fi

# Activate Gutenberg
echo -e $(status_message "Activating Gutenberg...")
docker-compose run --rm cli plugin activate gutenberg >/dev/null

# Install the PHPUnit test scaffolding
# Install the PHPUnit test scaffolding.
echo -e $(status_message "Installing PHPUnit test scaffolding...")
docker-compose run --rm wordpress_phpunit /app/bin/install-wp-tests.sh wordpress_test root example mysql latest false >/dev/null

# Install Composer
# Install Composer. This is only used to run WordPress Coding Standards checks.
echo -e $(status_message "Installing and updating Composer modules...")
docker-compose run --rm composer install
54 changes: 54 additions & 0 deletions bin/install-wordpress.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash

# Exit if any command fails.
set -e

# Gutenberg script includes.
. "$(dirname "$0")/includes.sh"

CLI='cli'
CONTAINER='wordpress'
CONTAINER_SUFFIX=''
SITE_TITLE='Gutenberg Dev'

if [ "$1" == '--e2e_tests' ]; then
CONTAINER_SUFFIX='_e2e_test'
CLI="$CLI$CONTAINER_SUFFIX"
SITE_TITLE='Gutenberg Testing'
fi

# Get the host port for the development WordPress container.
HOST_PORT=$(docker-compose port $CONTAINER$CONTAINER_SUFFIX 80 | awk -F : '{printf $2}')

# Wait until the Docker containers are running and the WordPress site is
# responding to requests.
echo -en $(status_message "Attempting to connect to WordPress in ($CONTAINER$CONTAINER_SUFFIX)...")
until $(curl -L http://localhost:$HOST_PORT -so - 2>&1 | grep -q "WordPress"); do
echo -n '.'
sleep 5
done
echo ''

# Install WordPress
echo -e $(status_message "Installing WordPress ($SITE_TITLE)...")
docker-compose run --rm $CLI core install --title="$SITE_TITLE" --admin_user=admin --admin_password=password --admin_email=test@test.com --skip-email --url=http://localhost:$HOST_PORT >/dev/null
# Check for WordPress updates, just in case the WordPress image isn't up to date.
docker-compose run --rm $CLI core update >/dev/null

# If the 'wordpress' volume wasn't during the down/up earlier, but the post port has changed, we need to update it.
CURRENT_URL=$(docker-compose run -T --rm $CLI option get siteurl)
if [ "$CURRENT_URL" != "http://localhost:$HOST_PORT" ]; then
docker-compose run --rm $CLI option update home "http://localhost:$HOST_PORT" >/dev/null
docker-compose run --rm $CLI option update siteurl "http://localhost:$HOST_PORT" >/dev/null
fi

# Reset the database so no posts/comments dirty up tests, if any exist.
POSTS=$(docker-compose run --rm $CLI post list --format=ids)
if [ "$POSTS" != '' ]; then
echo -e $(status_message "Resetting test database...")
docker-compose run --rm $CLI post delete $POSTS --force --quiet
fi

# Activate Gutenberg
echo -e $(status_message "Activating Gutenberg ($SITE_TITLE)...")
docker-compose run --rm $CLI plugin activate gutenberg >/dev/null
10 changes: 10 additions & 0 deletions bin/reset-e2e-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# Exit if any command fails.
set -e

# Include useful functions.
. "$(dirname "$0")/includes.sh"

# Set up WordPress site used for end-to-end (e2e) tests.
. "$(dirname "$0")/install-wordpress.sh" --e2e_tests
27 changes: 27 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,33 @@ services:
volumes:
- .:/app

wordpress_e2e_test:
image: wordpress
ports:
- 8889:80
environment:
WORDPRESS_DB_NAME: wordpress_e2e_test
WORDPRESS_DB_PASSWORD: example
ABSPATH: /usr/src/wordpress/
volumes:
- wordpress_e2e_test:/var/www/html
- .:/var/www/html/wp-content/plugins/gutenberg
- ./test/e2e/test-plugins:/var/www/html/wp-content/plugins/gutenberg-test-plugins
- ./test/e2e/test-mu-plugins:/var/www/html/wp-content/mu-plugins

mysql_e2e_test:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: wordpress_e2e_test

cli_e2e_test:
image: wordpress:cli
volumes:
- wordpress_e2e_test:/var/www/html
- .:/var/www/html/wp-content/plugins/gutenberg

volumes:
testsuite:
wordpress:
wordpress_e2e_test:
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
"publish:dev": "npm run build:packages && lerna publish --npm-tag next",
"publish:prod": "npm run build:packages && lerna publish",
"test": "npm run lint && npm run test-unit",
"pretest-e2e": "./bin/reset-e2e-tests.sh",
"test-e2e": "wp-scripts test-unit-js --config test/e2e/jest.config.json --runInBand",
"test-e2e:watch": "npm run test-e2e -- --watch",
"test-php": "npm run lint-php && npm run test-unit-php",
Expand Down
24 changes: 22 additions & 2 deletions test/e2e/support/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
*/
import puppeteer from 'puppeteer';

const { PUPPETEER_HEADLESS, PUPPETEER_SLOWMO } = process.env;
/**
* Node dependencies
*/
import { visitAdmin } from './utils';

const { PUPPETEER_HEADLESS, PUPPETEER_SLOWMO, PUPPETEER_TIMEOUT } = process.env;

// The Jest timeout is increased because these tests are a bit slow
jest.setTimeout( 100000 );
jest.setTimeout( PUPPETEER_TIMEOUT || 100000 );

beforeAll( async () => {
global.browser = await puppeteer.launch( {
Expand All @@ -16,5 +21,20 @@ beforeAll( async () => {
} );

afterAll( async () => {
page.on( 'dialog', ( dialog ) => {
dialog.accept();
} );

await visitAdmin( 'edit.php' );

const bulkSelector = await page.$( '#bulk-action-selector-top' );
if ( bulkSelector ) {
await page.waitForSelector( '#cb-select-all-1' );
await page.click( '#cb-select-all-1' );
await page.select( '#bulk-action-selector-top', 'trash' );
await page.click( '#doaction' );
await page.waitForNavigation();
}

await browser.close();
} );
2 changes: 1 addition & 1 deletion test/e2e/support/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { URL } from 'url';
import { times } from 'lodash';

const {
WP_BASE_URL = 'http://localhost:8888',
WP_BASE_URL = 'http://localhost:8889',
WP_USERNAME = 'admin',
WP_PASSWORD = 'password',
} = process.env;
Expand Down

0 comments on commit 946ad7c

Please sign in to comment.