diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/common/snapshot.js | 66 | ||||
-rw-r--r-- | test/fixtures/snapshot/dns-lookup.js | 39 | ||||
-rw-r--r-- | test/fixtures/snapshot/dns-resolve.js | 59 | ||||
-rw-r--r-- | test/internet/test-snapshot-dns-lookup.js | 34 | ||||
-rw-r--r-- | test/internet/test-snapshot-dns-resolve.js | 36 | ||||
-rw-r--r-- | test/parallel/test-snapshot-dns-lookup-localhost-promise.js | 35 | ||||
-rw-r--r-- | test/parallel/test-snapshot-dns-lookup-localhost.js | 35 | ||||
-rw-r--r-- | test/parallel/test-snapshot-dns-resolve-localhost-promise.js | 34 | ||||
-rw-r--r-- | test/parallel/test-snapshot-dns-resolve-localhost.js | 34 |
9 files changed, 372 insertions, 0 deletions
diff --git a/test/common/snapshot.js b/test/common/snapshot.js new file mode 100644 index 0000000000..97367c0565 --- /dev/null +++ b/test/common/snapshot.js @@ -0,0 +1,66 @@ +'use strict'; + +const tmpdir = require('../common/tmpdir'); +const { spawnSync } = require('child_process'); +const path = require('path'); +const fs = require('fs'); +const assert = require('assert'); + +function buildSnapshot(entry, env) { + const child = spawnSync(process.execPath, [ + '--snapshot-blob', + path.join(tmpdir.path, 'snapshot.blob'), + '--build-snapshot', + entry, + ], { + cwd: tmpdir.path, + env: { + ...process.env, + ...env, + }, + }); + + const stderr = child.stderr.toString(); + const stdout = child.stdout.toString(); + console.log('[stderr]'); + console.log(stderr); + console.log('[stdout]'); + console.log(stdout); + + assert.strictEqual(child.status, 0); + + const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob')); + assert(stats.isFile()); + + return { child, stderr, stdout }; +} + +function runWithSnapshot(entry, env) { + const args = ['--snapshot-blob', path.join(tmpdir.path, 'snapshot.blob')]; + if (entry !== undefined) { + args.push(entry); + } + const child = spawnSync(process.execPath, args, { + cwd: tmpdir.path, + env: { + ...process.env, + ...env, + } + }); + + const stderr = child.stderr.toString(); + const stdout = child.stdout.toString(); + console.log('[stderr]'); + console.log(stderr); + console.log('[stdout]'); + console.log(stdout); + + assert.strictEqual(child.status, 0); + + return { child, stderr, stdout }; +} + +module.exports = { + buildSnapshot, + runWithSnapshot, +}; diff --git a/test/fixtures/snapshot/dns-lookup.js b/test/fixtures/snapshot/dns-lookup.js new file mode 100644 index 0000000000..773b508f7c --- /dev/null +++ b/test/fixtures/snapshot/dns-lookup.js @@ -0,0 +1,39 @@ +'use strict'; +const dns = require('dns'); +const assert = require('assert'); + +assert(process.env.NODE_TEST_HOST); + +const { + setDeserializeMainFunction, +} = require('v8').startupSnapshot; + +function onError(err) { + console.error('error:', err); +} + +function onLookup(address, family) { + console.log(`address: ${JSON.stringify(address)}`); + console.log(`family: ${JSON.stringify(family)}`); +} + +function query() { + const host = process.env.NODE_TEST_HOST; + if (process.env.NODE_TEST_PROMISE === 'true') { + dns.promises.lookup(host, { family: 4 }).then( + ({address, family}) => onLookup(address, family), + onError); + } else { + dns.lookup(host, { family: 4 }, (err, address, family) => { + if (err) { + onError(err); + } else { + onLookup(address, family); + } + }); + } +} + +query(); + +setDeserializeMainFunction(query); diff --git a/test/fixtures/snapshot/dns-resolve.js b/test/fixtures/snapshot/dns-resolve.js new file mode 100644 index 0000000000..6a776f2970 --- /dev/null +++ b/test/fixtures/snapshot/dns-resolve.js @@ -0,0 +1,59 @@ +'use strict'; +const dns = require('dns'); +const assert = require('assert'); + +assert(process.env.NODE_TEST_HOST); + +const { + setDeserializeMainFunction, +} = require('v8').startupSnapshot; + +function onError(err) { + console.error('error:', err); +} + +function onResolve(addresses) { + console.log(`addresses: ${JSON.stringify(addresses)}`); +} + +function onReverse(hostnames) { + console.log(`hostnames: ${JSON.stringify(hostnames)}`); +} + +function query() { + if (process.env.NODE_TEST_DNS) { + dns.setServers([process.env.NODE_TEST_DNS]) + } + + const host = process.env.NODE_TEST_HOST; + if (process.env.NODE_TEST_PROMISE === 'true') { + dns.promises.resolve4(host).then(onResolve, onError); + } else { + dns.resolve4(host, (err, addresses) => { + if (err) { + onError(err); + } else { + onResolve(addresses); + } + }); + } + + const ip = process.env.NODE_TEST_IP; + if (ip) { + if (process.env.NODE_TEST_PROMISE === 'true') { + dns.promises.reverse(ip).then(onReverse, onError); + } else { + dns.reverse(ip, (err, hostnames) => { + if (err) { + onError(err); + } else { + onReverse(hostnames); + } + }); + } + } +} + +query(); + +setDeserializeMainFunction(query); diff --git a/test/internet/test-snapshot-dns-lookup.js b/test/internet/test-snapshot-dns-lookup.js new file mode 100644 index 0000000000..842e73e056 --- /dev/null +++ b/test/internet/test-snapshot-dns-lookup.js @@ -0,0 +1,34 @@ +'use strict'; + +// This tests support for the dns module in snapshot. + +require('../common'); +const assert = require('assert'); +const tmpdir = require('../common/tmpdir'); +const fixtures = require('../common/fixtures'); +const { buildSnapshot, runWithSnapshot } = require('../common/snapshot'); +const { + addresses: { INET4_HOST }, +} = require('../common/internet'); + +const entry = fixtures.path('snapshot', 'dns-lookup.js'); +const env = { + NODE_TEST_HOST: INET4_HOST, + NODE_TEST_PROMISE: 'false', +}; + +tmpdir.refresh(); +function checkOutput(stderr, stdout) { + assert(stdout.match(stdout, /address: "\d+\.\d+\.\d+\.\d+"/)); + assert(stdout.match(stdout, /family: 4/)); + assert.strictEqual(stdout.trim().split('\n').length, 2); +} +{ + const { stderr, stdout } = buildSnapshot(entry, env); + checkOutput(stderr, stdout); +} + +{ + const { stderr, stdout } = runWithSnapshot(entry, env); + checkOutput(stderr, stdout); +} diff --git a/test/internet/test-snapshot-dns-resolve.js b/test/internet/test-snapshot-dns-resolve.js new file mode 100644 index 0000000000..6efea9b4f7 --- /dev/null +++ b/test/internet/test-snapshot-dns-resolve.js @@ -0,0 +1,36 @@ +'use strict'; + +// This tests support for the dns module in snapshot. + +require('../common'); +const assert = require('assert'); +const tmpdir = require('../common/tmpdir'); +const fixtures = require('../common/fixtures'); +const { buildSnapshot, runWithSnapshot } = require('../common/snapshot'); +const { + addresses: { DNS4_SERVER, INET4_IP, INET4_HOST }, +} = require('../common/internet'); + +const entry = fixtures.path('snapshot', 'dns-resolve.js'); +const env = { + NODE_TEST_IP: INET4_IP, + NODE_TEST_HOST: INET4_HOST, + NODE_TEST_DNS: DNS4_SERVER, + NODE_TEST_PROMISE: 'false', +}; + +tmpdir.refresh(); +function checkOutput(stderr, stdout) { + assert(stdout.includes('hostnames: [')); + assert(stdout.includes('addresses: [')); + assert.strictEqual(stdout.trim().split('\n').length, 2); +} +{ + const { stderr, stdout } = buildSnapshot(entry, env); + checkOutput(stderr, stdout); +} + +{ + const { stderr, stdout } = runWithSnapshot(entry, env); + checkOutput(stderr, stdout); +} diff --git a/test/parallel/test-snapshot-dns-lookup-localhost-promise.js b/test/parallel/test-snapshot-dns-lookup-localhost-promise.js new file mode 100644 index 0000000000..d19488a716 --- /dev/null +++ b/test/parallel/test-snapshot-dns-lookup-localhost-promise.js @@ -0,0 +1,35 @@ +'use strict'; + +// This tests support for the dns module in snapshot. + +require('../common'); +const assert = require('assert'); +const tmpdir = require('../common/tmpdir'); +const fixtures = require('../common/fixtures'); +const { buildSnapshot, runWithSnapshot } = require('../common/snapshot'); + +const entry = fixtures.path('snapshot', 'dns-lookup.js'); +const env = { + NODE_TEST_HOST: 'localhost', + NODE_TEST_PROMISE: 'true', +}; + +tmpdir.refresh(); +function checkOutput(stderr, stdout) { + // We allow failures as it's not always possible to resolve localhost. + // Functional tests are done in test/internet instead. + if (!stderr.startsWith('error:')) { + assert(stdout.match(stdout, /address: "\d+\.\d+\.\d+\.\d+"/)); + assert(stdout.match(stdout, /family: 4/)); + assert.strictEqual(stdout.trim().split('\n').length, 2); + } +} +{ + const { stderr, stdout } = buildSnapshot(entry, env); + checkOutput(stderr, stdout); +} + +{ + const { stderr, stdout } = runWithSnapshot(entry, env); + checkOutput(stderr, stdout); +} diff --git a/test/parallel/test-snapshot-dns-lookup-localhost.js b/test/parallel/test-snapshot-dns-lookup-localhost.js new file mode 100644 index 0000000000..af00480bcc --- /dev/null +++ b/test/parallel/test-snapshot-dns-lookup-localhost.js @@ -0,0 +1,35 @@ +'use strict'; + +// This tests support for the dns module in snapshot. + +require('../common'); +const assert = require('assert'); +const tmpdir = require('../common/tmpdir'); +const fixtures = require('../common/fixtures'); +const { buildSnapshot, runWithSnapshot } = require('../common/snapshot'); + +const entry = fixtures.path('snapshot', 'dns-lookup.js'); +const env = { + NODE_TEST_HOST: 'localhost', + NODE_TEST_PROMISE: 'false', +}; + +tmpdir.refresh(); +function checkOutput(stderr, stdout) { + // We allow failures as it's not always possible to resolve localhost. + // Functional tests are done in test/internet instead. + if (!stderr.startsWith('error:')) { + assert(stdout.match(stdout, /address: "\d+\.\d+\.\d+\.\d+"/)); + assert(stdout.match(stdout, /family: 4/)); + assert.strictEqual(stdout.trim().split('\n').length, 2); + } +} +{ + const { stderr, stdout } = buildSnapshot(entry, env); + checkOutput(stderr, stdout); +} + +{ + const { stderr, stdout } = runWithSnapshot(entry, env); + checkOutput(stderr, stdout); +} diff --git a/test/parallel/test-snapshot-dns-resolve-localhost-promise.js b/test/parallel/test-snapshot-dns-resolve-localhost-promise.js new file mode 100644 index 0000000000..cca86007f1 --- /dev/null +++ b/test/parallel/test-snapshot-dns-resolve-localhost-promise.js @@ -0,0 +1,34 @@ +'use strict'; + +// This tests support for the dns module in snapshot. + +require('../common'); +const assert = require('assert'); +const tmpdir = require('../common/tmpdir'); +const fixtures = require('../common/fixtures'); +const { buildSnapshot, runWithSnapshot } = require('../common/snapshot'); + +const entry = fixtures.path('snapshot', 'dns-resolve.js'); +const env = { + NODE_TEST_HOST: 'localhost', + NODE_TEST_PROMISE: 'true', +}; + +tmpdir.refresh(); +function checkOutput(stderr, stdout) { + // We allow failures as it's not always possible to resolve localhost. + // Functional tests are done in test/internet instead. + if (!stderr.startsWith('error:')) { + assert(stdout.includes('addresses: [')); + assert.strictEqual(stdout.trim().split('\n').length, 1); + } +} +{ + const { stderr, stdout } = buildSnapshot(entry, env); + checkOutput(stderr, stdout); +} + +{ + const { stderr, stdout } = runWithSnapshot(entry, env); + checkOutput(stderr, stdout); +} diff --git a/test/parallel/test-snapshot-dns-resolve-localhost.js b/test/parallel/test-snapshot-dns-resolve-localhost.js new file mode 100644 index 0000000000..af4576bcc3 --- /dev/null +++ b/test/parallel/test-snapshot-dns-resolve-localhost.js @@ -0,0 +1,34 @@ +'use strict'; + +// This tests support for the dns module in snapshot. + +require('../common'); +const assert = require('assert'); +const tmpdir = require('../common/tmpdir'); +const fixtures = require('../common/fixtures'); +const { buildSnapshot, runWithSnapshot } = require('../common/snapshot'); + +const entry = fixtures.path('snapshot', 'dns-resolve.js'); +const env = { + NODE_TEST_HOST: 'localhost', + NODE_TEST_PROMISE: 'false', +}; + +tmpdir.refresh(); +function checkOutput(stderr, stdout) { + // We allow failures as it's not always possible to resolve localhost. + // Functional tests are done in test/internet instead. + if (!stderr.startsWith('error:')) { + assert(stdout.includes('addresses: [')); + assert.strictEqual(stdout.trim().split('\n').length, 1); + } +} +{ + const { stderr, stdout } = buildSnapshot(entry, env); + checkOutput(stderr, stdout); +} + +{ + const { stderr, stdout } = runWithSnapshot(entry, env); + checkOutput(stderr, stdout); +} |