Ensure HPOS is disabled if ENABLE_HPOS is set to '0' (#40528)

* Ensure HPOS is disabled if ENABLE_HPOS is set to '0'

* update spacing and changelog

* log ENABLE_HPOS value for GitHub action

* remove log ENABLE_HPOS value for GitHub action

* updates to HPOS using REST API

* update spacing

* code tidy up

* workflow and global-setup updates after review

* update workflow for api tests

* workflow updates

* delete .github/workflows/pr-build-and-e2e-tests-non-hpos.yml

* update workflow

* update api setup for HPOS

* updates to workflows for api references and setup updates

* add in ENABLE_HPOS check

* update API tests BASE_URL

* updates after review

* update error text

* remove extraneous variable names
This commit is contained in:
nigeljamesstevenson 2023-10-11 08:03:24 +01:00 committed by GitHub
parent c64ae36956
commit 026c3ea20d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 87 additions and 24 deletions

View File

@ -1,4 +1,4 @@
name: Run tests against PR in an environment with COT enabled
name: Run tests against PR in an environment with HPOS disabled
on:
pull_request:
workflow_dispatch:
@ -10,8 +10,8 @@ concurrency:
permissions: {}
jobs:
cot-e2e-tests-run:
name: Runs E2E tests with COT enabled.
non-hpos-e2e-tests-run:
name: Runs E2E tests with HPOS disabled.
runs-on: ubuntu-20.04
permissions:
contents: read
@ -24,11 +24,9 @@ jobs:
- name: Setup WooCommerce Monorepo
uses: ./.github/actions/setup-woocommerce-monorepo
- name: Load docker images and start containers with COT enabled.
- name: Load docker images and start containers
working-directory: plugins/woocommerce
env:
ENABLE_HPOS: 1
run: pnpm env:test:cot --filter=woocommerce
run: pnpm env:test --filter=woocommerce
- name: Download and install Chromium browser.
working-directory: plugins/woocommerce
@ -39,6 +37,7 @@ jobs:
id: run_playwright_e2e_tests
env:
USE_WP_ENV: 1
ENABLE_HPOS: 0
working-directory: plugins/woocommerce
run: pnpm exec playwright test --config=tests/e2e-pw/playwright.config.js
@ -66,8 +65,8 @@ jobs:
if-no-files-found: ignore
retention-days: 5
cot-api-tests-run:
name: Runs API tests with COT enabled.
non-hpos-api-tests-run:
name: Runs API tests with HPOS disabled.
runs-on: ubuntu-20.04
permissions:
contents: read
@ -80,11 +79,9 @@ jobs:
- name: Setup WooCommerce Monorepo
uses: ./.github/actions/setup-woocommerce-monorepo
- name: Load docker images and start containers with COT enabled.
- name: Load docker images and start containers
working-directory: plugins/woocommerce
env:
ENABLE_HPOS: 1
run: pnpm env:test:cot --filter=woocommerce
run: pnpm env:test --filter=woocommerce
- name: Run Playwright API tests.
id: run_playwright_api_tests
@ -93,6 +90,7 @@ jobs:
BASE_URL: http://localhost:8086
USER_KEY: admin
USER_SECRET: password
ENABLE_HPOS: 0
run: pnpm exec playwright test --config=tests/api-core-tests/playwright.config.js
- name: Generate Playwright API Test report.
@ -117,4 +115,4 @@ jobs:
${{ env.ALLURE_RESULTS_DIR }}
${{ env.ALLURE_REPORT_DIR }}
if-no-files-found: ignore
retention-days: 5
retention-days: 5

View File

@ -31,7 +31,6 @@ jobs:
- name: Load docker images and start containers.
working-directory: plugins/woocommerce
env:
ENABLE_HPOS: 0
WP_ENV_PHP_VERSION: 7.4
run: pnpm run env:test
@ -265,4 +264,4 @@ jobs:
-f pr_number=$PR_NUMBER \
-f commit_sha=$COMMIT_SHA \
-f s3_root=public \
--repo woocommerce/woocommerce-test-reports
--repo woocommerce/woocommerce-test-reports

View File

@ -0,0 +1,4 @@
Significance: patch
Type: dev
Ensure HPOS is disabled when ENABLE_HPOS is undefined or set to '0'

View File

@ -1,8 +1,10 @@
const { GITHUB_TOKEN, UPDATE_WC } = process.env;
const { ENABLE_HPOS, GITHUB_TOKEN, UPDATE_WC } = process.env;
const { downloadZip, deleteZip } = require( './utils/plugin-utils' );
const axios = require( 'axios' ).default;
const playwrightConfig = require('./playwright.config');
module.exports = async ( config ) => {
// If API_BASE_URL is configured and doesn't include localhost, running on daily host
if (
process.env.API_BASE_URL &&
@ -197,5 +199,29 @@ module.exports = async ( config ) => {
} else {
console.log( 'No DB update needed' );
}
} else {
// running on localhost using wp-env so ensure HPOS is set if ENABLE_HPOS env variable is passed
if (ENABLE_HPOS) {
let hposConfigured = false;
const value = ENABLE_HPOS === '0' ? 'no' : 'yes';
try {
const auth = { username: playwrightConfig.userKey, password: playwrightConfig.userSecret };
const hposResponse = await axios.post( playwrightConfig.use.baseURL + '/wp-json/wc/v3/settings/advanced/woocommerce_custom_orders_table_enabled', { value }, { auth } );
if ( hposResponse.data.value === value ) {
console.log( `HPOS Switched ${ value === 'yes' ? 'on' : 'off' } successfully` );
hposConfigured = true;
}
} catch( error ) {
console.log( 'HPOS setup failed.');
console.log( error );
process.exit( 1 );
}
if ( ! hposConfigured ) {
console.error(
'Cannot proceed to api tests, HPOS configuration failed. Please check if the correct ENABLE_HPOS value was used and the test site has been setup correctly.'
);
process.exit( 1 );
}
}
}
};

View File

@ -10,6 +10,8 @@ const userSecret = USER_SECRET ?? 'password';
const base64auth = btoa( `${ userKey }:${ userSecret }` );
const config = {
userKey,
userSecret,
timeout: DEFAULT_TIMEOUT_OVERRIDE
? Number( DEFAULT_TIMEOUT_OVERRIDE )
: 90 * 1000,

View File

@ -1,6 +1,5 @@
#!/usr/bin/env bash
ENABLE_HPOS="${ENABLE_HPOS:-0}"
ENABLE_TRACKING="${ENABLE_TRACKING:-0}"
echo -e 'Activate twentynineteen theme \n'
@ -29,12 +28,6 @@ wp-env run tests-cli wp option update blogname 'WooCommerce Core E2E Test Suite'
echo -e 'Preparing Test Files \n'
wp-env run tests-cli sudo cp /var/www/html/wp-content/plugins/woocommerce/tests/legacy/unit-tests/importer/sample.csv /var/www/sample.csv
if [ $ENABLE_HPOS == 1 ]; then
echo -e 'Enable High-Performance Order Tables\n'
wp-env run tests-cli wp option update woocommerce_feature_custom_order_tables_enabled 'yes'
wp-env run tests-cli wp option update woocommerce_custom_orders_table_enabled 'yes'
fi
if [ $ENABLE_TRACKING == 1 ]; then
echo -e 'Enable tracking\n'
wp-env run tests-cli wp option update woocommerce_allow_tracking 'yes'

View File

@ -2,6 +2,8 @@ const { chromium, expect } = require( '@playwright/test' );
const { admin, customer } = require( './test-data/data' );
const fs = require( 'fs' );
const { site } = require( './utils' );
const wcApi = require( '@woocommerce/woocommerce-rest-api' ).default;
const { ENABLE_HPOS } = process.env;
module.exports = async ( config ) => {
const { stateDir, baseURL, userAgent } = config.projects[ 0 ].use;
@ -39,6 +41,7 @@ module.exports = async ( config ) => {
let adminLoggedIn = false;
let customerLoggedIn = false;
let customerKeyConfigured = false;
let hposConfigured = false;
// Specify user agent when running against an external test site to avoid getting HTTP 406 NOT ACCEPTABLE errors.
const contextOptions = { baseURL, userAgent };
@ -178,6 +181,44 @@ module.exports = async ( config ) => {
process.exit( 1 );
}
// While we're here, let's set HPOS according to the passed in ENABLE_HPOS env variable
// (if a value for ENABLE_HPOS was set)
// This was always being set to 'yes' after login in wp-env so this step ensures the
// correct value is set before we begin our tests
if (ENABLE_HPOS) {
const hposSettingRetries = 5;
const api = new wcApi( {
url: baseURL,
consumerKey: process.env.CONSUMER_KEY,
consumerSecret: process.env.CONSUMER_SECRET,
version: 'wc/v3',
} );
const value = ENABLE_HPOS === '0' ? 'no' : 'yes';
for (let i = 0; i < hposSettingRetries; i++) {
try {
console.log( `Trying to switch ${ value === 'yes' ? 'on' : 'off' } HPOS...` );
const response = await api.post( 'settings/advanced/woocommerce_custom_orders_table_enabled', { value } );
if ( response.data.value === value ) {
console.log( `HPOS Switched ${ value === 'yes' ? 'on' : 'off' } successfully` );
hposConfigured = true;
break;
}
} catch (e) {
console.log( `HPOS setup failed. Retrying... ${ i }/${ hposSettingRetries }` );
console.log(e);
}
}
if ( ! hposConfigured ) {
console.error(
'Cannot proceed e2e test, HPOS configuration failed. Please check if the correct ENABLE_HPOS value was used and the test site has been setup correctly.'
);
process.exit( 1 );
}
}
await adminContext.close();
await customerContext.close();
await browser.close();