From 78308ba489320f5895656a327d6643db774aaf44 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Tue, 24 Jan 2023 09:28:13 -0800 Subject: [PATCH] [Manual] [Backport 1.x] Automates chromedriver version selection for tests (#2990) (#3128) (#3294) * Automates chromedriver version selection for tests (#2990) Signed-off-by: Miki (cherry picked from commit a26fb43cc3e4072a45e32c40ba59e37ae65cb83b) Signed-off-by: Josh Romero * Make `upgrade_chromedriver.js` work on Node v10 Signed-off-by: Miki Signed-off-by: Josh Romero Signed-off-by: Miki Co-authored-by: Miki (cherry picked from commit 3dab4c013217845137628f3a0579d91a873ab268) Signed-off-by: github-actions[bot] # Conflicts: # CHANGELOG.md Co-authored-by: github-actions[bot] --- .github/workflows/pr_check_workflow.yml | 22 ++--- .gitignore | 1 + scripts/upgrade_chromedriver.js | 123 ++++++++++++++++++++++++ 3 files changed, 135 insertions(+), 11 deletions(-) create mode 100644 scripts/upgrade_chromedriver.js diff --git a/.github/workflows/pr_check_workflow.yml b/.github/workflows/pr_check_workflow.yml index 6f883459ddef..0e096200b9ab 100644 --- a/.github/workflows/pr_check_workflow.yml +++ b/.github/workflows/pr_check_workflow.yml @@ -45,15 +45,15 @@ jobs: id: job_successful run: cat job_successful 2>/dev/null || echo 'false' - - name: Get the previous linter results + - name: Get the previous linter results id: linter_results run: cat linter_results 2>/dev/null || echo 'default' - - name: Get the previous unit tests results + - name: Get the previous unit tests results id: unit_tests_results run: cat unit_tests_results 2>/dev/null || echo 'default' - - name: Get the previous integration tests results + - name: Get the previous integration tests results id: integration_tests_results run: cat integration_tests_results 2>/dev/null || echo 'default' @@ -99,13 +99,13 @@ jobs: - name: Run integration tests if: steps.integration_tests_results.outputs.integration_tests_results != 'success' id: integration-tests - run: node scripts/jest_integration --ci --colors --max-old-space-size=5120 + run: node scripts/jest_integration --ci --colors --max-old-space-size=5120 # Set cache if linter, unit tests, and integration tests were successful then the job will be marked successful # Sets individual results to empower re-runs of the same build without re-running successful steps. - - if: | - (steps.linter.outcome == 'success' || steps.linter.outcome == 'skipped') && - (steps.unit-tests.outcome == 'success' || steps.unit-tests.outcome == 'skipped') && + - if: | + (steps.linter.outcome == 'success' || steps.linter.outcome == 'skipped') && + (steps.unit-tests.outcome == 'success' || steps.unit-tests.outcome == 'skipped') && (steps.integration-tests.outcome == 'success' || steps.integration-tests.outcome == 'skipped') run: echo "::set-output name=job_successful::true" > job_successful - if: steps.linter.outcome == 'success' || steps.linter.outcome == 'skipped' @@ -173,7 +173,7 @@ jobs: needs: [ build-lint-test-linux ] runs-on: ubuntu-latest name: Run functional tests - strategy: + strategy: matrix: group: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] steps: @@ -191,7 +191,7 @@ jobs: restore-keys: | ${{ github.run_id }}-${{ github.job }}-${{ matrix.group }}-${{ github.sha }} - - name: Get the cached tests results + - name: Get the cached tests results id: ftr_tests_results run: cat ftr_tests_results 2>/dev/null || echo 'default' @@ -231,7 +231,7 @@ jobs: # github virtual env is the latest chrome - name: Setup chromedriver if: steps.ftr_tests_results.outputs.ftr_tests_results != 'success' - run: yarn add --dev chromedriver@106.0.0 + run: node scripts/upgrade_chromedriver.js - name: Run bootstrap if: steps.ftr_tests_results.outputs.ftr_tests_results != 'success' @@ -249,7 +249,7 @@ jobs: CI_PARALLEL_PROCESS_NUMBER: ciGroup${{ matrix.group }} JOB: ci${{ matrix.group }} CACHE_DIR: ciGroup${{ matrix.group }} - + - if: steps.ftr-tests.outcome == 'success' || steps.ftr-tests.outcome == 'skipped' run: echo "::set-output name=ftr_tests_results::success" > ftr_tests_results diff --git a/.gitignore b/.gitignore index eaf84fdad060..8f252da0826f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ .ackrc /.opensearch /.chromium +/package.json.bak .DS_Store .node_binaries .native_modules diff --git a/scripts/upgrade_chromedriver.js b/scripts/upgrade_chromedriver.js new file mode 100644 index 000000000000..49e1d23bfa8c --- /dev/null +++ b/scripts/upgrade_chromedriver.js @@ -0,0 +1,123 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * Upgrades the chromedriver dev-dependency to the one supported by the version of Google Chrome + * installed on the machine. + * + * Usage: node scripts/upgrade_chromedriver.js [--install] + */ + +/* eslint no-restricted-syntax: 0 */ +const { execSync, spawnSync } = require('child_process'); +const { createReadStream, createWriteStream, unlinkSync, renameSync, existsSync } = require('fs'); +const { createInterface } = require('readline'); + +if (!process.argv.includes(__filename)) { + console.error('Usage: node scripts/upgrade_chromedriver.js [--install]'); + process.exit(1); +} + +const versionCheckCommands = []; + +switch (process.platform) { + case 'win32': + versionCheckCommands.push( + 'powershell "(Get-Item \\"$Env:Programfiles/Google/Chrome/Application/chrome.exe\\").VersionInfo.FileVersion"' + ); + break; + + case 'darwin': + versionCheckCommands.push( + '/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --version' + ); + break; + + default: + versionCheckCommands.push( + ...[ + '/usr/bin', + '/usr/local/bin', + '/usr/sbin', + '/usr/local/sbin', + '/opt/bin', + '/usr/bin/X11', + '/usr/X11R6/bin', + ].flatMap((loc) => + [ + 'google-chrome --version', + 'google-chrome-stable --version', + 'chromium --version', + 'chromium-browser --version', + ].map((cmd) => `${loc}/${cmd}`) + ) + ); +} + +let versionCheckOutput; +versionCheckCommands.some((cmd) => { + try { + console.log(cmd); + versionCheckOutput = (execSync(cmd, { encoding: 'utf8' }) || '').trim(); + return true; + } catch (e) { + console.log('Failed to get version using', cmd); + } +}); + +// Versions 90+ +const versionCheckOutputTokens = (versionCheckOutput || '').match(/(?:^|\s)(9\d|\d{3})\./); +const majorVersion = Array.isArray(versionCheckOutputTokens) && versionCheckOutputTokens[1]; + +if (majorVersion) { + if (process.argv.includes('--install')) { + console.log(`Installing chromedriver@^${majorVersion}`); + + spawnSync(`yarn add --dev chromedriver@^${majorVersion}`, { + stdio: 'inherit', + cwd: process.cwd(), + shell: true, + }); + } else { + console.log(`Upgrading to chromedriver@^${majorVersion}`); + + let upgraded = false; + const writeStream = createWriteStream('package.json.upgrading-chromedriver', { flags: 'w' }); + const rl = createInterface({ + input: createReadStream('package.json'), + crlfDelay: Infinity, + }); + rl.on('line', (line) => { + if (line.includes('"chromedriver": "')) { + line = line.replace( + /"chromedriver":\s*"[~^]?\d[\d.]*\d"/, + `"chromedriver": "^${majorVersion}"` + ); + upgraded = true; + } + writeStream.write(line + '\n', 'utf8'); + }); + rl.on('close', () => { + writeStream.end(); + if (upgraded) { + // Remove any previous backups + if (existsSync('package.json.bak')) unlinkSync('package.json.bak'); + + renameSync('package.json', 'package.json.bak'); + renameSync('package.json.upgrading-chromedriver', 'package.json'); + + console.log(`Backed up package.json and updated chromedriver to ${majorVersion}`); + } else { + unlinkSync('package.json.upgrading-chromedriver'); + console.error( + `Failed to update chromedriver to ${majorVersion}. Try adding the \`--install\` switch.` + ); + } + }); + } +} else { + console.debug(versionCheckOutput); + console.error(`Failed to extract the version of the installed Google Chrome.`); +}