Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Allow node patch versions to be higher on runtime #1677

Merged
merged 1 commit into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/setup_node_env/node_version_validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ var pkg = require('../../package.json');
var currentVersion = (process && process.version) || null;
var rawRequiredVersion = (pkg && pkg.engines && pkg.engines.node) || null;
var requiredVersion = rawRequiredVersion ? 'v' + rawRequiredVersion : rawRequiredVersion;
var isVersionValid = !!currentVersion && !!requiredVersion && currentVersion === requiredVersion;
var currentVersionMajorMinorPatch = currentVersion.match(/^v(\d+)\.(\d+)\.(\d+)/);
var requiredVersionMajorMinorPatch = requiredVersion.match(/^v(\d+)\.(\d+)\.(\d+)/);
var isVersionValid =
currentVersionMajorMinorPatch[1] === requiredVersionMajorMinorPatch[1] &&
currentVersionMajorMinorPatch[2] === requiredVersionMajorMinorPatch[2] &&
parseInt(currentVersionMajorMinorPatch[3], 10) >= parseInt(requiredVersionMajorMinorPatch[3], 10);

// Validates current the NodeJS version compatibility when OpenSearch Dashboards starts.
if (!isVersionValid) {
var errorMessage =
'OpenSearch Dashboards does not support the current Node.js version ' +
currentVersion +
'. Please use Node.js ' +
requiredVersion +
'.';
`OpenSearch Dashboards was built with ${requiredVersion} and does not support the current Node.js version ${currentVersion}. ` +
`Please use Node.js ${requiredVersion} or a higher patch version.`;

// Actions to apply when validation fails: error report + exit.
console.error(errorMessage);
Expand Down
80 changes: 53 additions & 27 deletions src/setup_node_env/node_version_validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,64 @@ var exec = require('child_process').exec;
var pkg = require('../../package.json');

var REQUIRED_NODE_JS_VERSION = 'v' + pkg.engines.node;
var INVALID_NODE_JS_VERSION = 'v0.10.0';

describe('NodeVersionValidator', function () {
it('should run the script WITH error', function (done) {
var processVersionOverwrite =
"Object.defineProperty(process, 'version', { value: '" +
INVALID_NODE_JS_VERSION +
"', writable: true });";
var command =
'node -e "' + processVersionOverwrite + "require('./node_version_validator.js')\"";

exec(command, { cwd: __dirname }, function (error, stdout, stderr) {
expect(error.code).toBe(1);
expect(stderr).toBeDefined();
expect(stderr).not.toHaveLength(0);
done();
});
it('should run the script WITHOUT error when the version is the same', function (done) {
testValidateNodeVersion(done, REQUIRED_NODE_JS_VERSION);
});

it('should run the script WITHOUT error when only the patch version is higher', function (done) {
testValidateNodeVersion(done, requiredNodeVersionWithDiff(0, 0, +1));
});

it('should run the script WITH error if the patch version is lower', function (done) {
testValidateNodeVersion(done, requiredNodeVersionWithDiff(0, 0, -1), true);
});

it('should run the script WITH error if the major version is higher', function (done) {
testValidateNodeVersion(done, requiredNodeVersionWithDiff(+1, 0, 0), true);
});

it('should run the script WITH error if the major version is lower', function (done) {
testValidateNodeVersion(done, requiredNodeVersionWithDiff(-1, 0, 0), true);
});

it('should run the script WITH error if the minor version is higher', function (done) {
testValidateNodeVersion(done, requiredNodeVersionWithDiff(0, +1, 0), true);
});

it('should run the script WITH error if the minor version is lower', function (done) {
testValidateNodeVersion(done, requiredNodeVersionWithDiff(0, -1, 0), true);
});
});

function requiredNodeVersionWithDiff(majorDiff, minorDiff, patchDiff) {
var matches = REQUIRED_NODE_JS_VERSION.match(/^v(\d+)\.(\d+)\.(\d+)/);
var major = parseInt(matches[1]) + majorDiff;
var minor = parseInt(matches[2]) + minorDiff;
var patch = parseInt(matches[3]) + patchDiff;

it('should run the script WITHOUT error', function (done) {
var processVersionOverwrite =
"Object.defineProperty(process, 'version', { value: '" +
REQUIRED_NODE_JS_VERSION +
"', writable: true });";
var command =
'node -e "' + processVersionOverwrite + "require('./node_version_validator.js')\"";
return `v${major}.${minor}.${patch}`;
}

exec(command, { cwd: __dirname }, function (error, stdout, stderr) {
function testValidateNodeVersion(done, versionToTest, expectError = false) {
var processVersionOverwrite = `Object.defineProperty(process, 'version', { value: '${versionToTest}', writable: true });`;
var command = `node -e "${processVersionOverwrite}require('./node_version_validator.js')"`;

exec(command, { cwd: __dirname }, function (error, _stdout, stderr) {
expect(stderr).toBeDefined();
if (expectError) {
expect(error.code).toBe(1);

var speficicErrorMessage =
`OpenSearch Dashboards was built with ${REQUIRED_NODE_JS_VERSION} and does not support the current Node.js version ${versionToTest}. ` +
`Please use Node.js ${REQUIRED_NODE_JS_VERSION} or a higher patch version.\n`;

expect(stderr).toStrictEqual(speficicErrorMessage);
} else {
expect(error).toBeNull();
expect(stderr).toBeDefined();
expect(stderr).toHaveLength(0);
done();
});
}
done();
});
});
}