0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Add e2e test for update check script (#16840)

refs https://github.com/TryGhost/Team/issues/3234

Added an e2e for the update check script to detect potential breakages
in the script due to uninitialised dependencies in the isolated
execution environment
This commit is contained in:
Michael Barrett 2023-05-25 14:36:36 +01:00 committed by GitHub
parent 488aa983d0
commit 8fc4e0fdcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 9 deletions

View file

@ -1,4 +1,4 @@
const {parentPort} = require('worker_threads');
const {parentPort, workerData} = require('worker_threads');
const postParentPortMessage = (message) => {
if (parentPort) {
@ -46,7 +46,9 @@ if (parentPort) {
// Finished INIT
await updateCheck({
rethrowErrors: true
rethrowErrors: true,
forceUpdate: workerData.forceUpdate,
updateCheckUrl: workerData.updateCheckUrl
});
postParentPortMessage(`Ran update check`);

View file

@ -14,14 +14,22 @@ const UpdateCheckService = require('@tryghost/update-check-service');
* Initializes and triggers update check
* @param {Object} [options]
* @param {Boolean} [options.rethrowErrors] - if true, errors will be thrown instead of logged
* @param {Boolean} [options.forceUpdate] - if true, the update check will be triggered regardless of the environment or scheudle, defaults to config if no value provided
* @param {String} [options.updateCheckUrl] - the url to check for updates against, defaults to config if no value provided
* @returns {Promise<any>}
*/
module.exports = async ({rethrowErrors = false} = {}) => {
const allowedCheckEnvironments = ['development', 'production'];
module.exports = async ({
rethrowErrors = false,
forceUpdate = config.get('updateCheck:forceUpdate'),
updateCheckUrl = config.get('updateCheck:url')
} = {}) => {
if (!forceUpdate) {
const allowedCheckEnvironments = ['development', 'production'];
// CASE: The check will not happen if your NODE_ENV is not in the allowed defined environments.
if (_.indexOf(allowedCheckEnvironments, process.env.NODE_ENV) === -1) {
return;
// CASE: The check will not happen if your NODE_ENV is not in the allowed defined environments
if (_.indexOf(allowedCheckEnvironments, process.env.NODE_ENV) === -1) {
return;
}
}
const {GhostMailer} = require('./services/mail');
@ -47,11 +55,11 @@ module.exports = async ({rethrowErrors = false} = {}) => {
mail: config.get('mail'),
env: config.get('env'),
databaseType: databaseInfo.getEngine(),
checkEndpoint: config.get('updateCheck:url'),
checkEndpoint: updateCheckUrl,
isPrivacyDisabled: config.isPrivacyDisabled('useUpdateCheck'),
notificationGroups: config.get('notificationGroups'),
siteUrl: urlUtils.urlFor('home', true),
forceUpdate: config.get('updateCheck:forceUpdate'),
forceUpdate,
ghostVersion: ghostVersion.original,
rethrowErrors
},

View file

@ -0,0 +1,56 @@
const assert = require('assert');
const http = require('http');
const path = require('path');
const models = require('../../../core/server/models');
models.init();
const jobService = require('../../../core/server/services/jobs/job-service');
const JOB_NAME = 'update-check';
const JOB_PATH = path.resolve(__dirname, '../../../core/server/run-update-check.js');
describe('Run Update Check', function () {
let mockUpdateServer;
afterEach(function () {
if (mockUpdateServer) {
mockUpdateServer.close();
}
});
it('successfully executes the update checker', async function () {
let mockUpdateServerRequestCount = 0;
// Initialise mock update server - We use a mock server here instead of
// nock because the update-check job will be executed in a separate
// process which will prevent nock from intercepting HTTP requests
mockUpdateServer = http.createServer((req, res) => {
mockUpdateServerRequestCount += 1;
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({hello: 'world'}));
});
mockUpdateServer.listen(0); // Listen on random port
const mockUpdateServerPort = mockUpdateServer.address().port;
// Trigger the update-check job and wait for it to finish
await jobService.addJob({
name: JOB_NAME,
job: JOB_PATH,
data: {
forceUpdate: true,
updateCheckUrl: `http://127.0.0.1:${mockUpdateServerPort}`
}
});
await jobService.awaitCompletion(JOB_NAME);
// Assert that the mock update server received a request (which means the update-check job ran successfully)
assert.equal(mockUpdateServerRequestCount, 1, 'Expected mock server to receive 1 request');
});
});