From 393154e08082c0bea33916b2ee80c25e403498c0 Mon Sep 17 00:00:00 2001 From: Sam Najian Date: Wed, 4 May 2022 17:10:16 +0200 Subject: [PATCH] Adjust used Docker container to run integration tests [MAILPOET-4251] --- mailpoet/.env.sample | 2 +- mailpoet/RoboFile.php | 64 ++++++------------- .../docker/codeception/docker-entrypoint.sh | 49 +++++++++----- .../docker-compose.override.macos-sample.yml | 7 +- mailpoet/tests/docker/docker-compose.yml | 38 +++++++++-- mailpoet/tests/integration/_bootstrap.php | 4 -- 6 files changed, 94 insertions(+), 70 deletions(-) diff --git a/mailpoet/.env.sample b/mailpoet/.env.sample index 1a7a2b7893..063ce06122 100644 --- a/mailpoet/.env.sample +++ b/mailpoet/.env.sample @@ -4,7 +4,7 @@ WP_TEST_MAILER_ENABLE_SENDING=true WP_TEST_ENABLE_NETWORK_TESTS=true # Optional: for multisite acceptance tests -WP_ROOT_MULTISITE=/var/www/wordpress +WP_ROOT_MULTISITE=/var/www/html WP_TEST_MULTISITE_SLUG= HTTP_HOST= # URL of your site (used for multisite env, equals to DOMAIN_CURRENT_SITE from wp-config.php) diff --git a/mailpoet/RoboFile.php b/mailpoet/RoboFile.php index 983c04ef8e..469a425e80 100644 --- a/mailpoet/RoboFile.php +++ b/mailpoet/RoboFile.php @@ -180,36 +180,13 @@ class RoboFile extends \Robo\Tasks { return $this->_exec($command); } - public function testIntegration(array $opts = ['file' => null, 'xml' => false, 'multisite' => false, 'debug' => false]) { - if (getenv('MAILPOET_DEV_SITE')) { - $run = $this->confirm("You are about to run tests on the development site. Your DB data will be erased. \nDo you want to proceed?", false); - if (!$run) { - return; - } - } - $command = 'vendor/bin/codecept run integration -vvv'; - - if ($opts['multisite']) { - $command = 'MULTISITE=true ' . $command; - } - - if ($opts['file']) { - $command .= ' -f ' . $opts['file']; - } - - if ($opts['xml']) { - $command .= ' --xml'; - } - - if ($opts['debug']) { - $command .= ' --debug'; - } - - return $this->_exec($command); + public function testIntegration(array $opts = ['file' => null, 'xml' => false, 'multisite' => false, 'debug' => false, 'skip-deps' => false]) { + return $this->runTestsInContainer(array_merge($opts, ['test_type' => 'integration'])); } - public function testMultisiteIntegration($opts = ['file' => null, 'xml' => false, 'multisite' => true]) { - return $this->testIntegration($opts); + public function testMultisiteIntegration($opts = ['file' => null, 'xml' => false, 'multisite' => true, 'skip-deps' => false]) { + return $this->runTestsInContainer(array_merge($opts, ['test_type' => 'integration'])); + } public function testCoverage($opts = ['file' => null, 'xml' => false]) { @@ -272,24 +249,11 @@ class RoboFile extends \Robo\Tasks { } public function testAcceptance($opts = ['file' => null, 'skip-deps' => false, 'timeout' => null]) { - return $this->taskExec( - 'COMPOSE_HTTP_TIMEOUT=200 docker-compose run ' . - ($opts['skip-deps'] ? '-e SKIP_DEPS=1 ' : '') . - ($opts['timeout'] ? '-e WAIT_TIMEOUT=' . (int)$opts['timeout'] . ' ' : '') . - 'codeception --steps --debug -vvv ' . - '-f ' . ($opts['file'] ? $opts['file'] : '') - )->dir(__DIR__ . '/tests/docker')->run(); + return $this->runTestsInContainer($opts); } public function testAcceptanceMultisite($opts = ['file' => null, 'skip-deps' => false, 'timeout' => null]) { - return $this->taskExec( - 'COMPOSE_HTTP_TIMEOUT=200 docker-compose run ' . - ($opts['skip-deps'] ? '-e SKIP_DEPS=1 ' : '') . - ($opts['timeout'] ? '-e WAIT_TIMEOUT=' . (int)$opts['timeout'] . ' ' : '') . - '-e MULTISITE=1 ' . - 'codeception --steps --debug -vvv ' . - '-f ' . ($opts['file'] ? $opts['file'] : '') - )->dir(__DIR__ . '/tests/docker')->run(); + return $this->runTestsInContainer(array_merge($opts, ['multisite' => true])); } public function deleteDocker() { @@ -1181,4 +1145,18 @@ class RoboFile extends \Robo\Tasks { 'platform' => new $platformClass, ], $configuration); } + + private function runTestsInContainer(array $opts) { + $testType = $opts['test_type'] ?? 'acceptance'; + + return $this->taskExec( + 'COMPOSE_HTTP_TIMEOUT=200 docker-compose run ' . + (isset($opts['skip-deps']) && $opts['skip-deps'] ? '-e SKIP_DEPS=1 ' : '') . + (isset($opts['timeout']) && $opts['timeout'] ? '-e WAIT_TIMEOUT=' . (int)$opts['timeout'] . ' ' : '') . + (isset($opts['multisite']) && $opts['multisite'] ? '-e MULTISITE=1 ' : '-e MULTISITE=0 ') . + "codeception_{$testType} --steps --debug -vvv " . + (isset($opts['xml']) && $opts['xml'] ? '--xml ' : '') . + '-f ' . (isset($opts['file']) && $opts['file'] ? $opts['file'] : '') + )->dir(__DIR__ . '/tests/docker')->run(); + } } diff --git a/mailpoet/tests/docker/codeception/docker-entrypoint.sh b/mailpoet/tests/docker/codeception/docker-entrypoint.sh index 37af0c41b4..95bedc0804 100755 --- a/mailpoet/tests/docker/codeception/docker-entrypoint.sh +++ b/mailpoet/tests/docker/codeception/docker-entrypoint.sh @@ -24,6 +24,16 @@ chown www-data:www-data wp-content/plugins chown www-data:www-data wp-content/uploads chmod 755 wp-content/plugins chmod -R 777 wp-content/uploads +chmod -R 777 /mailhog-data + +# deleting configs in case are set in previous run +wp config delete MULTISITE > /dev/null 2>&1 +wp config delete WP_ALLOW_MULTISITE > /dev/null 2>&1 +wp config delete SUBDOMAIN_INSTALL > /dev/null 2>&1 +wp config delete DOMAIN_CURRENT_SITE > /dev/null 2>&1 +wp config delete PATH_CURRENT_SITE > /dev/null 2>&1 +wp config delete SITE_ID_CURRENT_SITE > /dev/null 2>&1 +wp config delete BLOG_ID_CURRENT_SITE > /dev/null 2>&1 # disable automatic updates wp config set WP_AUTO_UPDATE_CORE false --raw @@ -33,13 +43,14 @@ mysqladmin --host=mysql --user=root --password=wordpress drop wordpress --force mysqladmin --host=mysql --user=root --password=wordpress create wordpress --force # install WordPress -WP_CORE_INSTALL_PARAMS="--url=test.local --title=tests --admin_user=admin --admin_email=test@test.com --admin_password=password --skip-email" +WP_CORE_INSTALL_PARAMS="--url=$HTTP_HOST --title=tests --admin_user=admin --admin_email=test@test.com --admin_password=password --skip-email" if [[ -z "$MULTISITE" || "$MULTISITE" -eq "0" ]]; then echo 'Installing WordPress (single site mode)' wp core install $WP_CORE_INSTALL_PARAMS else echo 'Installing WordPress (multisite mode)' wp core multisite-install $WP_CORE_INSTALL_PARAMS + wp site create --slug=$WP_TEST_MULTISITE_SLUG fi # Load Composer dependencies @@ -51,6 +62,8 @@ if [[ -z "${SKIP_DEPS}" ]]; then cd - >/dev/null fi +# extra plugins are not supported for integration tests yest +if [[ $TEST_TYPE == "acceptance" ]]; then # Install WooCommerce if [[ ! -d "/wp-core/wp-content/plugins/woocommerce" ]]; then cd /wp-core/wp-content/plugins @@ -105,6 +118,13 @@ if [[ ! -d "/wp-core/wp-content/plugins/woo-gutenberg-products-block" ]]; then unzip -q -o "$WOOCOMMERCE_BLOCKS_ZIP" -d /wp-core/wp-content/plugins/ fi +# activate all plugins which source code want to access in tests runtime +wp plugin activate woocommerce +wp plugin activate woocommerce-subscriptions +wp plugin activate woocommerce-memberships +wp plugin activate woo-gutenberg-products-block +fi # end of check for enabling extra plugins + # add configuration CONFIG='' CONFIG+="define('WP_DEBUG', true);\n" @@ -119,12 +139,6 @@ CONFIG+="if (!isset(\$_SERVER['SERVER_NAME'])) \$_SERVER['SERVER_NAME'] = '';\n" sed -i "s/define( *'WP_DEBUG', false *);/$CONFIG/" /wp-core/wp-config.php -# activate all plugins which source code want to access in tests runtime -wp plugin activate woocommerce -wp plugin activate woocommerce-subscriptions -wp plugin activate woocommerce-memberships -wp plugin activate woo-gutenberg-products-block - # activate theme wp theme activate twentytwentyone @@ -132,14 +146,18 @@ if [[ $CIRCLE_JOB == *"_oldest"* ]]; then wp theme activate twentynineteen fi -# print info about installed plugins -wp plugin get woocommerce -wp plugin get woocommerce-subscriptions -wp plugin get woocommerce-memberships -wp plugin get woo-gutenberg-products-block - +if [[ $TEST_TYPE == "acceptance" ]]; then + # print info about installed plugins + wp plugin get woocommerce + wp plugin get woocommerce-subscriptions + wp plugin get woocommerce-memberships + wp plugin get woo-gutenberg-products-block +fi # activate MailPoet wp plugin activate mailpoet/mailpoet.php +if [[ $MULTISITE == "1" ]]; then + wp plugin activate mailpoet/mailpoet.php --url=http://test.local/php7_multisite/ +fi if [[ $CIRCLE_JOB == *"_with_premium_"* ]]; then # Copy MailPoet Premium to plugin path @@ -150,12 +168,11 @@ fi cd /wp-core/wp-content/plugins/mailpoet # Remove Doctrine Annotations (no need since generated metadata are packed) -if [[ $CIRCLE_JOB ]]; then +if [[ $TEST_TYPE == "acceptance" ]] && [[ $CIRCLE_JOB ]]; then rm -rf ./vendor-prefixed/doctrine/annotations ./tools/vendor/composer.phar dump-autoload fi - -/project/vendor/bin/codecept run acceptance $@ +/project/vendor/bin/codecept run $TEST_TYPE $@ exitcode=$? exit $exitcode diff --git a/mailpoet/tests/docker/docker-compose.override.macos-sample.yml b/mailpoet/tests/docker/docker-compose.override.macos-sample.yml index de7c60f66a..4eb01b2242 100644 --- a/mailpoet/tests/docker/docker-compose.override.macos-sample.yml +++ b/mailpoet/tests/docker/docker-compose.override.macos-sample.yml @@ -9,7 +9,12 @@ services: chrome: image: seleniarm/standalone-chromium:4.0.0-20211213 - codeception: + codeception_acceptance: + volumes: + - nfs-mailpoet:/project + - nfs-mailpoet:/wp-core/wp-content/plugins/mailpoet + + codeception_integration: volumes: - nfs-mailpoet:/project - nfs-mailpoet:/wp-core/wp-content/plugins/mailpoet diff --git a/mailpoet/tests/docker/docker-compose.yml b/mailpoet/tests/docker/docker-compose.yml index 8de7332966..39f6662e22 100644 --- a/mailpoet/tests/docker/docker-compose.yml +++ b/mailpoet/tests/docker/docker-compose.yml @@ -1,10 +1,35 @@ version: '3.9' services: - codeception: + codeception_acceptance: image: mailpoet/wordpress:${CODECEPTION_IMAGE_VERSION:-8.0-cli_20220126.1} depends_on: - - mailhog + - smtp + - wordpress + - chrome + volumes: + - wp-core:/wp-core + - mailhog-data:/mailhog-data + - ../..:/project + - ../..:/wp-core/wp-content/plugins/mailpoet + - ./codeception/docker-entrypoint.sh:/docker-entrypoint.sh + - ../../../dev/php.ini:/usr/local/etc/php/conf.d/php_user.ini + - ../../../dev/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini + entrypoint: /docker-entrypoint.sh + environment: + WP_ROOT: /wp-core + WP_ROOT_MULTISITE: /wp-core + WP_TEST_MULTISITE_SLUG: php7_multisite + HTTP_HOST: test.local + WORDPRESS_DB_PASSWORD: wordpress + WORDPRESS_DB_USER: wordpress + WORDPRESS_DB_NAME: wordpress + WORDPRESS_TABLE_PREFIX: mp_ + TEST_TYPE: acceptance + codeception_integration: + image: mailpoet/wordpress:${CODECEPTION_IMAGE_VERSION:-8.0-cli_20220605.0} + depends_on: + - smtp - wordpress volumes: - wp-core:/wp-core @@ -15,12 +40,16 @@ services: entrypoint: /docker-entrypoint.sh environment: WP_ROOT: /wp-core + WP_ROOT_MULTISITE: /wp-core + WP_TEST_MULTISITE_SLUG: php7_multisite + HTTP_HOST: test.local WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_DB_USER: wordpress WORDPRESS_DB_NAME: wordpress WORDPRESS_TABLE_PREFIX: mp_ + TEST_TYPE: integration - mailhog: + smtp: image: mailhog/mailhog:v1.0.0 ports: - 1025:1025 @@ -35,8 +64,7 @@ services: wordpress: image: mailpoet/wordpress:${WORDPRESS_IMAGE_VERSION:-wp-5.9_php8.0_20220127.1} depends_on: - - chrome - - mailhog + - smtp - mysql volumes: - wp-core:/var/www/html diff --git a/mailpoet/tests/integration/_bootstrap.php b/mailpoet/tests/integration/_bootstrap.php index 08a4536f14..d4adc50441 100644 --- a/mailpoet/tests/integration/_bootstrap.php +++ b/mailpoet/tests/integration/_bootstrap.php @@ -23,10 +23,6 @@ if ((boolean)getenv('MULTISITE') === true) { } require_once($wpLoadFile); -if (!defined('MP_SETTINGS_TABLE')) { - die('MailPoet must be activated in the tests site (usually http://localhost:8003) before running the integration tests'); -} - $console = new \Codeception\Lib\Console\Output([]); $console->writeln('Loading WP core... (' . $wpLoadFile . ')');