diff --git a/packages/core/test/quarantine/test_config_variables.js b/packages/core/test/acceptance/config-variables.test.js similarity index 52% rename from packages/core/test/quarantine/test_config_variables.js rename to packages/core/test/acceptance/config-variables.test.js index ef95027e03..b9d0d00201 100644 --- a/packages/core/test/quarantine/test_config_variables.js +++ b/packages/core/test/acceptance/config-variables.test.js @@ -1,18 +1,35 @@ 'use strict'; -const { test } = require('tap'); +const { test, beforeEach, afterEach } = require('tap'); const runner = require('../..').runner.runner; const { SSMS } = require('../../lib/ssms'); +const createTestServer = require('../targets/simple'); -test('config variables', function (t) { +let server; +let port; +beforeEach(async () => { + server = await createTestServer(0); + port = server.info.port; +}); + +afterEach(() => { + server.stop(); +}); + +test('config variables', (t) => { const script = require('../scripts/config_variables.json'); + script.config.target = `http://127.0.0.1:${port}`; + runner(script).then(function (ee) { ee.on('done', function (nr) { const report = SSMS.legacyReport(nr).report(); t.ok(report.codes[200] > 0, 'there are 200s for /'); t.ok(report.codes[404] > 0, 'there are 404s for /will/404'); - t.end(); + ee.stop().then(() => { + t.end(); + }); }); + ee.run(); }); }); diff --git a/packages/core/test/acceptance/misc/ws.test.js b/packages/core/test/acceptance/misc/ws.test.js index 7b086923f3..67600b4eff 100644 --- a/packages/core/test/acceptance/misc/ws.test.js +++ b/packages/core/test/acceptance/misc/ws.test.js @@ -5,9 +5,9 @@ const createTestServer = require('../../targets/simple_ws'); let server; let port; beforeEach(async () => { - server = createTestServer().listen(0, function () { - port = server.address().port; - }); + const serverInfo = await createTestServer(); + port = serverInfo.port; + server = serverInfo.server; }); afterEach(() => { diff --git a/packages/core/test/acceptance/tls.test.js b/packages/core/test/acceptance/tls.test.js new file mode 100644 index 0000000000..d545f680ae --- /dev/null +++ b/packages/core/test/acceptance/tls.test.js @@ -0,0 +1,59 @@ +'use strict'; + +const { test, beforeEach, afterEach } = require('tap'); +const runner = require('../../lib/runner').runner; +const { updateGlobalObject } = require('../../index'); +const { SSMS } = require('../../lib/ssms'); +const createTestServer = require('../targets/simple_tls'); + +let server; +let port; +beforeEach(async () => { + await updateGlobalObject(); + const serverInfo = await createTestServer(); + port = serverInfo.port; + server = serverInfo.server; +}); + +afterEach(() => { + server.close(); +}); + +test('tls strict', function (t) { + const script = require('../scripts/tls-strict.json'); + script.config.target = `https://127.0.0.1:${port}`; + runner(script).then(function (ee) { + ee.on('done', function (nr) { + const report = SSMS.legacyReport(nr).report(); + console.log(report); + const rejected = report.errors.DEPTH_ZERO_SELF_SIGNED_CERT; + t.ok(rejected, 'requests to self-signed tls certs fail by default'); + + ee.stop().then(() => { + t.end(); + }); + }); + ee.run(); + }); +}); + +test('tls lax', function (t) { + const script = require('../scripts/tls-lax.json'); + script.config.target = `https://127.0.0.1:${port}`; + runner(script).then(function (ee) { + ee.on('done', function (nr) { + const report = SSMS.legacyReport(nr).report(); + const rejected = report.errors.DEPTH_ZERO_SELF_SIGNED_CERT; + const reason = + 'requests to self-signed tls certs pass ' + + 'when `rejectUnauthorized` is false'; + + t.ok(rejected == null, reason); + + ee.stop().then(() => { + t.end(); + }); + }); + ee.run(); + }); +}); diff --git a/packages/core/test/quarantine/ws/scripts/subprotocols.json b/packages/core/test/acceptance/ws/scripts/subprotocols.json similarity index 100% rename from packages/core/test/quarantine/ws/scripts/subprotocols.json rename to packages/core/test/acceptance/ws/scripts/subprotocols.json diff --git a/packages/core/test/quarantine/ws/scripts/extra_options.json b/packages/core/test/acceptance/ws/scripts/ws-tls.json similarity index 100% rename from packages/core/test/quarantine/ws/scripts/extra_options.json rename to packages/core/test/acceptance/ws/scripts/ws-tls.json diff --git a/packages/core/test/acceptance/ws/ws-subprotocols.test.js b/packages/core/test/acceptance/ws/ws-subprotocols.test.js new file mode 100644 index 0000000000..57cf202990 --- /dev/null +++ b/packages/core/test/acceptance/ws/ws-subprotocols.test.js @@ -0,0 +1,91 @@ +'use strict'; + +const { test, beforeEach, afterEach } = require('tap'); +const core = require('../../..'); +const vuserLauncher = core.runner.runner; +const { SSMS } = require('../../../lib/ssms'); +const createTestServer = require('../../targets/simple_ws'); + +let server; +let port; +beforeEach(async () => { + const serverInfo = await createTestServer(); + port = serverInfo.port; + server = serverInfo.server; +}); + +afterEach(() => { + server.close(); +}); + +test('Subprotocols - using a known subprotocol', function (t) { + const script = require('./scripts/subprotocols.json'); + script.config.target = `ws://127.0.0.1:${port}`; + vuserLauncher(script).then((ee) => { + ee.on('done', (nr) => { + const report = SSMS.legacyReport(nr).report(); + console.log(report); + t.equal( + Object.keys(report.errors).length, + 0, + 'Test with a subprotocol should complete with no errors' + ); + ee.stop().then(() => { + t.end(); + }); + }); + + ee.run(); + }); +}); + +test('Subprotocols - no subprotocol', function (t) { + const script = require('./scripts/subprotocols.json'); + script.config.target = `ws://127.0.0.1:${port}`; + delete script.config.ws; + + vuserLauncher(script).then((ee) => { + ee.on('done', (nr) => { + const report = SSMS.legacyReport(nr).report(); + t.equal( + Object.keys(report.errors).length, + 0, + 'Test with no subprotocol set should complete with no errors' + ); + ee.stop().then(() => { + t.end(); + }); + }); + + ee.run(); + }); +}); + +test('Subprotocols - unknown subprotocol', function (t) { + const script = require('./scripts/subprotocols.json'); + script.config.target = `ws://127.0.0.1:${port}`; + + script.config.ws = { + subprotocols: ['unsupportedByTheServer'] + }; + + vuserLauncher(script).then((ee) => { + ee.on('done', (nr) => { + const report = SSMS.legacyReport(nr).report(); + t.equal(Object.keys(report.errors).length, 1, 'Should have one error'); + + // FIXME: This test is coupled to the error message generated by the ws library + t.ok( + Object.keys(report.errors)[0].toLowerCase().indexOf('no subprotocol') > + -1, + 'The error should be of "no subprotocol" type' + ); + + ee.stop().then(() => { + t.end(); + }); + }); + + ee.run(); + }); +}); diff --git a/packages/core/test/acceptance/ws/ws-tls.test.js b/packages/core/test/acceptance/ws/ws-tls.test.js new file mode 100644 index 0000000000..81546652ab --- /dev/null +++ b/packages/core/test/acceptance/ws/ws-tls.test.js @@ -0,0 +1,56 @@ +'use strict'; + +const { test, beforeEach, afterEach } = require('tap'); +const core = require('../../..'); +const vuserLauncher = core.runner.runner; +const { SSMS } = require('../../../lib/ssms'); +const createTestServer = require('../../targets/ws_tls'); + +let server; +let port; +beforeEach(async () => { + const serverInfo = await createTestServer(); + port = serverInfo.port; + server = serverInfo.server; +}); + +afterEach(() => { + server.close(); +}); + +test('TLS - with rejectUnauthorized false', function (t) { + const script = require('./scripts/ws-tls.json'); + script.config.target = `wss://127.0.0.1:${port}`; + vuserLauncher(script).then(function (ee) { + ee.on('done', function (nr) { + const report = SSMS.legacyReport(nr).report(); + console.log(report); + t.ok(Object.keys(report.errors).length === 0, 'Test ran without errors'); + ee.stop().then(() => { + t.end(); + }); + }); + ee.run(); + }); +}); + +test('TLS - with rejectUnauthorized true', function (t) { + const script = require('./scripts/ws-tls.json'); + script.config.target = `wss://127.0.0.1:${port}`; + script.config.ws.rejectUnauthorized = true; + vuserLauncher(script).then(function (ee) { + ee.on('done', function (nr) { + const report = SSMS.legacyReport(nr).report(); + console.log(report); + t.equal( + Object.keys(report.errors).length, + 1, + `Test should run with one error. Got: ${Object.keys(report.errors)}` + ); + ee.stop().then(() => { + t.end(); + }); + }); + ee.run(); + }); +}); diff --git a/packages/core/test/quarantine/test_config_plugin_package.js b/packages/core/test/quarantine/test_config_plugin_package.js index 936e428fc4..16d357d33a 100644 --- a/packages/core/test/quarantine/test_config_plugin_package.js +++ b/packages/core/test/quarantine/test_config_plugin_package.js @@ -32,8 +32,10 @@ test('Normal artillery-plugin-*', function (t) { function runTest(t, scriptName) { const script = require(scriptName); + console.log(script); runner(script).then(function (ee) { ee.on('plugin_loaded', function (stats) { + console.log('hey'); t.ok(true); t.end(); }); diff --git a/packages/core/test/quarantine/test_environments.js b/packages/core/test/quarantine/test_environments.js index 283101ffae..9e952a2976 100644 --- a/packages/core/test/quarantine/test_environments.js +++ b/packages/core/test/quarantine/test_environments.js @@ -1,47 +1,70 @@ 'use strict'; -var { test } = require('tap'); -var runner = require('../../lib/runner').runner; -var createTarget = require('./lib/interfakify').create; -var url = require('url'); +const { test, beforeEach, afterEach } = require('tap'); +const runner = require('../../lib/runner').runner; +// const createTarget = require('./lib/interfakify').create; +const { updateGlobalObject } = require('../../index'); +const { SSMS } = require('../../lib/ssms'); +const url = require('url'); +const createTestServer = require('../targets/simple'); -test('environments - override target', function (t) { - var script = require('../scripts/hello_environments.json'); - runner(script, null, { environment: 'production' }).then(function (ee) { - ee.on('done', function (report) { - t.ok( - report.requestsCompleted === 0, - 'there should be no completed requests' - ); - t.ok( - report.errors.ETIMEDOUT && report.errors.ETIMEDOUT > 1, - 'there should ETIMEDOUT errors' - ); - t.end(); - }); - ee.run(); - }); +let server; +let port; +beforeEach(async () => { + await updateGlobalObject(); + server = await createTestServer(0); + port = server.info.port; }); +afterEach(() => { + server.stop(); +}); + +// test('environments - override target', function (t) { +// const script = require('../scripts/hello_environments.json'); +// runner(script, null, { environment: 'production' }).then(function (ee) { +// ee.on('done', function (nr) { +// const report = SSMS.legacyReport(nr).report(); +// console.log(report) +// t.ok( +// report.requestsCompleted === 0, +// 'there should be no completed requests' +// ); +// t.ok( +// report.errors.ETIMEDOUT && report.errors.ETIMEDOUT > 1, +// 'there should ETIMEDOUT errors' +// ); +// ee.stop().then(() => { +// t.end(); +// }); +// }); +// ee.run(); +// }); +// }); + test('environments - override target and phases', function (t) { - var startedAt; - var script = require('../scripts/hello_environments.json'); - var target = createTarget( - script.scenarios[0].flow, - script.config.environments.staging - ); - target.listen( - url.parse(script.config.environments.staging.target).port || 80 - ); + let startedAt; + const script = require('../scripts/hello_environments.json'); + script.config.environments.staging.target = `http://127.0.0.1:${port}`; + // var target = createTarget( + // script.scenarios[0].flow, + // script.config.environments.staging + // ); + // target.listen( + // url.parse(script.config.environments.staging.target).port || 80 + // ); runner(script, null, { environment: 'staging' }).then(function (ee) { - ee.on('done', function (report) { - var completedAt = process.hrtime(startedAt); - var delta = (completedAt[0] * 1e9 + completedAt[1]) / 1e6; + ee.on('done', function (nr) { + const report = SSMS.legacyReport(nr).report(); + console.log(report); + const completedAt = process.hrtime(startedAt); + const delta = (completedAt[0] * 1e9 + completedAt[1]) / 1e6; t.ok(report.codes[200], 'stats should not be empty'); t.ok(delta >= 20 * 1000, "should've run for 20 seconds"); - target.stop(); - t.end(); + ee.stop().then(() => { + t.end(); + }); }); startedAt = process.hrtime(); ee.run(); diff --git a/packages/core/test/quarantine/test_tls.js b/packages/core/test/quarantine/test_tls.js deleted file mode 100644 index 8eaf716198..0000000000 --- a/packages/core/test/quarantine/test_tls.js +++ /dev/null @@ -1,53 +0,0 @@ -'use strict'; - -var { test } = require('tap'); -var runner = require('../../lib/runner').runner; -var tls = require('tls'); -var fs = require('fs'); -var path = require('path'); - -var options = { - key: fs.readFileSync( - path.resolve(__dirname, '../targets/certs/private-key.pem') - ), - cert: fs.readFileSync( - path.resolve(__dirname, '../targets/certs/public-cert.pem') - ), - path: '/' -}; - -var server = tls.createServer(options, function (socket) { - socket.pipe(socket); -}); -server.listen(3002); - -test('tls strict', function (t) { - var script = require('../scripts/tls-strict.json'); - runner(script).then(function (ee) { - ee.on('done', function (report) { - var rejected = report.errors.DEPTH_ZERO_SELF_SIGNED_CERT; - t.ok(rejected, 'requests to self-signed tls certs fail by default'); - - t.end(); - }); - ee.run(); - }); -}); - -test('tls lax', function (t) { - var script = require('../scripts/tls-lax.json'); - runner(script).then(function (ee) { - ee.on('done', function (report) { - var rejected = report.errors.DEPTH_ZERO_SELF_SIGNED_CERT; - var reason = - 'requests to self-signed tls certs pass ' + - 'when `rejectUnauthorized` is false'; - - t.ok(rejected == null, reason); - - t.end(); - server.close(); - }); - ee.run(); - }); -}); diff --git a/packages/core/test/quarantine/ws/test_options.js b/packages/core/test/quarantine/ws/test_options.js deleted file mode 100644 index d2a17cf779..0000000000 --- a/packages/core/test/quarantine/ws/test_options.js +++ /dev/null @@ -1,113 +0,0 @@ -'use strict'; - -const { test } = require('tap'); -const core = require('../../..'); -const vuserLauncher = core.runner.runner; -const { SSMS } = require('../../../lib/ssms'); - -// -// If config.ws.rejectUnauthorized is not set, we will have an error. -// Otherwise the test will run fine. -// - -test('TLS options for WebSocket', function (t) { - const script = require('./scripts/extra_options.json'); - vuserLauncher(script).then(function (sessions) { - sessions.on('done', function (nr) { - const report = SSMS.legacyReport(nr).report(); - t.ok(Object.keys(report.errors).length === 0, 'Test ran without errors'); - - // Now remove TLS options and rerun - should have an error - delete script.config.ws; - vuserLauncher(script).then(function (sessions2) { - sessions2.on('done', function (nr2) { - const report2 = SSMS.legacyReport(nr2).report(); - t.equal( - Object.keys(report2.errors).length, - 1, - `Test should run with one error. Got: ${Object.keys( - report2.errors - )}` - ); - - sessions.stop().then(() => { - sessions2.stop().then(() => { - t.end(); - }); - }); - }); - sessions2.run(); - }); - }); - sessions.run(); - }); -}); - -test('Subprotocols - using a known subprotocol', function (t) { - const script = require('./scripts/subprotocols.json'); - vuserLauncher(script).then((sessions) => { - sessions.on('done', (nr) => { - const report = SSMS.legacyReport(nr).report(); - t.equal( - Object.keys(report.errors).length, - 0, - 'Test with a subprotocol should complete with no errors' - ); - sessions.stop().then(() => { - t.end(); - }); - }); - - sessions.run(); - }); -}); - -test('Subprotocols - no subprotocol', function (t) { - const script = require('./scripts/subprotocols.json'); - - delete script.config.ws; - - vuserLauncher(script).then((sessions) => { - sessions.on('done', (nr) => { - const report = SSMS.legacyReport(nr).report(); - t.equal( - Object.keys(report.errors).length, - 0, - 'Test with no subprotocol set should complete with no errors' - ); - sessions.stop().then(() => { - t.end(); - }); - }); - - sessions.run(); - }); -}); - -test('Subprotocols - unknown subprotocol', function (t) { - const script = require('./scripts/subprotocols.json'); - - script.config.ws = { - subprotocols: ['unsupportedByTheServer'] - }; - - vuserLauncher(script).then((sessions) => { - sessions.on('done', (nr) => { - const report = SSMS.legacyReport(nr).report(); - t.equal(Object.keys(report.errors).length, 1, 'Should have one error'); - - // FIXME: This test is coupled to the error message generated by the ws library - t.ok( - Object.keys(report.errors)[0].toLowerCase().indexOf('no subprotocol') > - -1, - 'The error should be of "no subprotocol" type' - ); - - sessions.stop().then(() => { - t.end(); - }); - }); - - sessions.run(); - }); -}); diff --git a/packages/core/test/targets/simple_tls.js b/packages/core/test/targets/simple_tls.js index cee67b684d..bd43515f99 100644 --- a/packages/core/test/targets/simple_tls.js +++ b/packages/core/test/targets/simple_tls.js @@ -15,7 +15,11 @@ const createTestServer = () => { res.end('hello\n'); }); - return server; + return new Promise((resolve, reject) => { + server.listen(0, function () { + resolve({ server, port: server.address().port }); + }); + }); }; module.exports = createTestServer; diff --git a/packages/core/test/targets/simple_ws.js b/packages/core/test/targets/simple_ws.js index fe600e26ff..800ebfc344 100644 --- a/packages/core/test/targets/simple_ws.js +++ b/packages/core/test/targets/simple_ws.js @@ -34,7 +34,11 @@ const createTestServer = (port, host = '127.0.0.1') => { } } - return server; + return new Promise((resolve, reject) => { + server.listen(0, function () { + resolve({ server, wss, port: server.address().port }); + }); + }); }; module.exports = createTestServer; diff --git a/packages/core/test/targets/ws_tls.js b/packages/core/test/targets/ws_tls.js index 3eacf85851..222e3e9526 100644 --- a/packages/core/test/targets/ws_tls.js +++ b/packages/core/test/targets/ws_tls.js @@ -33,7 +33,11 @@ const createTestServer = (port = 9443) => { }); }); - return app; + return new Promise((resolve, reject) => { + app.listen(0, function () { + resolve({ server: app, wss, port: app.address().port }); + }); + }); }; module.exports = createTestServer;