ci: check server health before starting integration tests

This commit is contained in:
Christopher Willis-Ford 2023-09-18 11:14:15 -07:00
parent d20f498e7f
commit 75a430bb92
3 changed files with 58 additions and 2 deletions

View file

@ -145,7 +145,11 @@ jobs:
SLACK_WEBHOOK_MODS: ${{ secrets.SLACK_WEBHOOK_MODS }}
- name: integration tests
if: ${{ env.SCRATCH_SHOULD_DEPLOY == 'true' }}
run: JEST_JUNIT_OUTPUT_NAME=integration-jest-results.xml npm run test:integration:remote -- --reporters=jest-junit
run: |
# if the health test fails, there's no point in trying to run the integration tests
npm run test:health
# health test succeeded, so proceed with integration tests
JEST_JUNIT_OUTPUT_NAME=integration-jest-results.xml npm run test:integration -- --reporters=jest-junit
env:
ROOT_URL: ${{ secrets.ROOT_URL }}
@ -155,6 +159,7 @@ jobs:
CIRCLE_BUILD_NUM: ${{ github.run_id }} # TODO
SAUCE_ACCESS_KEY: ${{ secrets.SAUCE_ACCESS_KEY }}
SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}
SMOKE_REMOTE: "true" # use Sauce Labs
# test/integration/*
SMOKE_USERNAME: ${{ secrets.SMOKE_USERNAME }}

View file

@ -7,8 +7,8 @@
"test": "npm run test:lint && npm run build && npm run test:unit",
"test:lint": "eslint . --ext .js,.jsx,.json",
"test:lint:ci": "eslint . --ext .js,.jsx,.json --format junit -o ./test/results/lint-results.xml",
"test:health": "jest ./test/health/*.test.js",
"test:integration": "jest ./test/integration/*.test.js --reporters=default --maxWorkers=5",
"test:integration:remote": "SMOKE_REMOTE=true jest ./test/integration/*.test.js --reporters=default --maxWorkers=5",
"test:unit": "npm run test:unit:jest && npm run test:unit:tap",
"test:unit:jest": "npm run test:unit:jest:unit && npm run test:unit:jest:localization",
"test:unit:jest:unit": "jest ./test/unit/ --reporters=default",

View file

@ -0,0 +1,51 @@
/* eslint-disable no-console */
// this basic server health check is meant to be run before integration tests
// it should be run with the same environment variables as the integration tests
// and operate in the same way as the integration tests
const SeleniumHelper = require('../integration/selenium-helpers.js');
const rootUrl = process.env.ROOT_URL || (() => {
const ROOT_URL_DEFAULT = 'https://scratch.ly';
console.warn(`ROOT_URL not set, defaulting to ${ROOT_URL_DEFAULT}`);
return ROOT_URL_DEFAULT;
})();
jest.setTimeout(60000);
describe('www server health check', () => {
/** @type {import('selenium-webdriver').ThenableWebDriver} */
let driver;
/** @type {SeleniumHelper} */
let seleniumHelper;
beforeAll(() => {
seleniumHelper = new SeleniumHelper();
driver = seleniumHelper.buildDriver('www server health check');
});
afterAll(() => driver.quit());
test('server is healthy', async () => {
const healthUrl = new URL('health/', rootUrl);
await driver.get(healthUrl.toString());
// Note: driver.getPageSource() will return the pretty HTML form of the JSON
const pageText = await driver.executeScript('return document.body.innerText');
let healthObject;
let serverReturnedValidJson = false;
try {
healthObject = JSON.parse(pageText);
serverReturnedValidJson = true;
} catch (_e) {
// ignore
}
expect(serverReturnedValidJson).toBe(true);
expect(healthObject).toHaveProperty('healthy', true);
});
});