From 48f0a61f05cd2394e27b2cbcc6b01e1af1027977 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Bergstr=C3=B6m?= Date: Mon, 14 Sep 2015 15:19:06 +1000 Subject: [PATCH 1/3] test: introduce a broadcast helper Introduce a helper - very similar to `common.localhostIPv4` that allows test runner to override a broadcast address. This is required in certain scenarios - like FreeBSD jails - since networking acts a bit different. Refs: https://github.com/nodejs/node/issues/2472 --- test/common.js | 22 +++++++++++++++++++ .../test-dgram-broadcast-multi-process.js | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/test/common.js b/test/common.js index 9859db24cd78..a3c74d9587c5 100644 --- a/test/common.js +++ b/test/common.js @@ -68,6 +68,7 @@ exports.tmpDir = path.join(exports.testDir, exports.tmpDirName); var opensslCli = null; var inFreeBSDJail = null; var localhostIPv4 = null; +var broadcastIPv4 = null; Object.defineProperty(exports, 'inFreeBSDJail', { get: function() { @@ -107,6 +108,27 @@ Object.defineProperty(exports, 'localhostIPv4', { } }); +Object.defineProperty(exports, 'broadcastIPv4', { + + get: function() { + if (broadcastIPv4 !== null) return broadcastIPv4; + + if (exports.inFreeBSDJail) { + if (process.env.BROADCAST) { + broadcastIPv4 = process.env.BROADCAST; + } else { + // shorter error than above since you'd probably be seeing both + console.error('In a FreeBSD jail. Pass broadcast as \'BROADCAST=\' ' + + 'to avoid test failures.'); + } + } + + if (broadcastIPv4 === null) broadcastIPv4 = '255.255.255.255'; + + return broadcastIPv4; + } +}); + // opensslCli defined lazily to reduce overhead of spawnSync Object.defineProperty(exports, 'opensslCli', {get: function() { if (opensslCli !== null) return opensslCli; diff --git a/test/internet/test-dgram-broadcast-multi-process.js b/test/internet/test-dgram-broadcast-multi-process.js index 9df217710eaf..ca110ec0a913 100644 --- a/test/internet/test-dgram-broadcast-multi-process.js +++ b/test/internet/test-dgram-broadcast-multi-process.js @@ -6,7 +6,7 @@ var common = require('../common'), networkInterfaces = require('os').networkInterfaces(), Buffer = require('buffer').Buffer, fork = require('child_process').fork, - LOCAL_BROADCAST_HOST = '255.255.255.255', + LOCAL_BROADCAST_HOST = common.broadcastIPv4, TIMEOUT = common.platformTimeout(5000), messages = [ new Buffer('First message to send'), From b725f03b3141d702d625c16e024c9caea9c71255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Bergstr=C3=B6m?= Date: Mon, 14 Sep 2015 16:16:53 +1000 Subject: [PATCH 2/3] feature: refactor getting environment variables --- test/common.js | 48 ++++++++++++++++++------------------------------ 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/test/common.js b/test/common.js index a3c74d9587c5..55ef56fe50ae 100644 --- a/test/common.js +++ b/test/common.js @@ -51,6 +51,22 @@ function rmdirSync(p, originalEr) { } } +function getEnvironmentDefault(environ, defaultValue) { + let value = null; + if (exports.inFreeBSDJail) { + if (process.env[environ]) { + value = process.env[environ]; + } else { + console.error('In a FreeBSD jail. You might have to override the value ' + + 'by passing %s=\'value\' to avoid any test ' + + 'failures', environ); + } + } + + if (value === null) value = defaultValue; + return value; +} + exports.refreshTmpDir = function() { rimrafSync(exports.tmpDir); fs.mkdirSync(exports.tmpDir); @@ -88,43 +104,15 @@ Object.defineProperty(exports, 'inFreeBSDJail', { Object.defineProperty(exports, 'localhostIPv4', { get: function() { if (localhostIPv4 !== null) return localhostIPv4; - - if (exports.inFreeBSDJail) { - // Jailed network interfaces are a bit special - since we need to jump - // through loops, as well as this being an exception case, assume the - // user will provide this instead. - if (process.env.LOCALHOST) { - localhostIPv4 = process.env.LOCALHOST; - } else { - console.error('Looks like we\'re in a FreeBSD Jail. ' + - 'Please provide your default interface address ' + - 'as LOCALHOST or expect some tests to fail.'); - } - } - - if (localhostIPv4 === null) localhostIPv4 = '127.0.0.1'; - + localhostIPv4 = getEnvironmentDefault('LOCALHOST', '127.0.0.1'); return localhostIPv4; } }); Object.defineProperty(exports, 'broadcastIPv4', { - get: function() { if (broadcastIPv4 !== null) return broadcastIPv4; - - if (exports.inFreeBSDJail) { - if (process.env.BROADCAST) { - broadcastIPv4 = process.env.BROADCAST; - } else { - // shorter error than above since you'd probably be seeing both - console.error('In a FreeBSD jail. Pass broadcast as \'BROADCAST=\' ' + - 'to avoid test failures.'); - } - } - - if (broadcastIPv4 === null) broadcastIPv4 = '255.255.255.255'; - + broadcastIPv4 = getEnvironmentDefault('BROADCAST', '255.255.255.255'); return broadcastIPv4; } }); From c230032aa6991d57ef552f7fa26188c585e2f9e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Bergstr=C3=B6m?= Date: Tue, 15 Sep 2015 09:54:39 +1000 Subject: [PATCH 3/3] fix: move function below variable declaration --- test/common.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/test/common.js b/test/common.js index 55ef56fe50ae..e6a3dfa54c82 100644 --- a/test/common.js +++ b/test/common.js @@ -51,22 +51,6 @@ function rmdirSync(p, originalEr) { } } -function getEnvironmentDefault(environ, defaultValue) { - let value = null; - if (exports.inFreeBSDJail) { - if (process.env[environ]) { - value = process.env[environ]; - } else { - console.error('In a FreeBSD jail. You might have to override the value ' + - 'by passing %s=\'value\' to avoid any test ' + - 'failures', environ); - } - } - - if (value === null) value = defaultValue; - return value; -} - exports.refreshTmpDir = function() { rimrafSync(exports.tmpDir); fs.mkdirSync(exports.tmpDir); @@ -178,6 +162,22 @@ var util = require('util'); for (var i in util) exports[i] = util[i]; //for (var i in exports) global[i] = exports[i]; +function getEnvironmentDefault(environ, defaultValue) { + let value = null; + if (exports.inFreeBSDJail) { + if (process.env[environ]) { + value = process.env[environ]; + } else { + console.error('In a FreeBSD jail. You might have to override the value ' + + 'by passing %s=\'value\' to avoid any test ' + + 'failures', environ); + } + } + + if (value === null) value = defaultValue; + return value; +} + function protoCtrChain(o) { var result = []; for (; o; o = o.__proto__) { result.push(o.constructor); }