From 34b9ced14e6ff50d6089f1d26d6e0c59fedeec72 Mon Sep 17 00:00:00 2001 From: Tom Elliott Date: Tue, 10 Nov 2026 11:55:58 -0600 Subject: [PATCH] fix(test): run every test against _test, never the configured database npm test used whatever database config/local.js names. In a dev or production checkout that is the real database. Its after() then deleted AuditLog rows by name prefix, which could remove real audit rows. Every test record (hosts, users, the agent root Setting) also landed there. test:ui did the same through its server child. - tools/lib/test-db.js resolves the test database from the config: the configured name plus _test, always appended. It handles a url, a url with a top-level database, and the installer's host/port/database shape, and adds authSource when the url has credentials. It drops only a name that ends in _test and differs from the configured name. - test/lifecycle.test.js and tools/smoke/ui.js (test:ui and test:ui:prod) lift on that database. config/bootstrap.js refuses a lift on any other database before its index writes, and the suite and ui.js check the database the models really use before any test or browser action. - test:install drops fog_install_smoke_test (was fog_install_smoke), and never the configured database. - The prefix-based audit cleanup is gone: nothing in a _test database is real. tools/lib/database.js datastoreUrl is the one installer-shape URL builder. Co-Authored-By: Claude Opus 5.5 --- config/bootstrap.js | 13 ++++ docs/adr/0003-audit-trail.md | 14 ++-- test/integration/agent-admin.test.js | 2 - test/integration/agent-enroll.test.js | 21 ++---- test/integration/agent-mtls.test.js | 4 +- test/integration/audit.test.js | 10 +-- test/lifecycle.test.js | 100 +++++++++++-------------- test/migrations/2.test.js | 4 +- test/setup/test-db.test.js | 87 ++++++++++++++++++++++ tools/lib/database.js | 24 +++--- tools/lib/test-db.js | 103 ++++++++++++++++++++++++++ tools/setup/lib/schema.js | 7 +- tools/smoke/install.js | 11 ++- tools/smoke/ui.js | 55 ++++++++++++-- 14 files changed, 346 insertions(+), 109 deletions(-) create mode 100644 test/setup/test-db.test.js create mode 100644 tools/lib/test-db.js diff --git a/config/bootstrap.js b/config/bootstrap.js index 1679135..fcbf464 100644 --- a/config/bootstrap.js +++ b/config/bootstrap.js @@ -11,6 +11,19 @@ module.exports.bootstrap = async function() { + // A test run (test/lifecycle.test.js, tools/smoke/ui.js) sets + // custom.testDatabase to the database it must use (tools/lib/test-db.js). + // Report the database the models really use, read from the live + // connection, and refuse to lift on any other one. This runs before the + // index writes below. Nothing else sets custom.testDatabase. + if (sails.config.custom.testDatabase) { + const name = User.getDatastore().manager.databaseName; + console.log(`fog-node models database: ${name}`); + if (name !== sails.config.custom.testDatabase) { + throw new Error(`Refusing to lift: the models use the database "${name}", not the test database "${sails.config.custom.testDatabase}".`); + } + } + // The ?v= value on the production bundle links (views/layouts/layout.ejs). // A production lift rebuilds the bundles, so each lift gets a new value and // browsers fetch the new bundle despite production's one-year cache. diff --git a/docs/adr/0003-audit-trail.md b/docs/adr/0003-audit-trail.md index 21ce8e2..d341af8 100644 --- a/docs/adr/0003-audit-trail.md +++ b/docs/adr/0003-audit-trail.md @@ -105,12 +105,8 @@ enumeration (1.6 stores none either). - A full-page save that fails answers 302. `general/save` sets `req.audit.outcome` (`denied` for a refused group change, `failed` for any other error), and the seam uses it in place of the status. -- At the end of a run, the test suite (`test/lifecycle.test.js`) deletes the - `AuditLog` rows that meet both conditions: - - `createdAt` is at or after the start of the run. - - `createdBy` is `mochatestadmin` or starts with one of `PREFIXES`, or - `subjectLabel` starts with one of `PREFIXES`. - The one test that checks the `agent-` fallback host name also deletes - the rows of the hosts it made, by `subjectId`. A dev server that shares the - database keeps all its other rows. The rows of anonymous denied requests and the - `auth.token.rejected` rows of the suite have no name, so they stay. +- The test suite (`test/lifecycle.test.js`) runs on `_test`, never on the + configured database, and the suite empties that database before each run. It + stops with an error if the name does not end in `_test`. The suite deletes no + `AuditLog` rows itself, so a dev server that uses the configured database keeps + all its rows. diff --git a/test/integration/agent-admin.test.js b/test/integration/agent-admin.test.js index 3f7e492..0b27fd5 100644 --- a/test/integration/agent-admin.test.js +++ b/test/integration/agent-admin.test.js @@ -39,8 +39,6 @@ describe('Agent admin::', function () { agent_version: '1.0.0', os: 'linux', arch: 'amd64', - // A prefixed name, so the audit rows of the test are removable - // (test/lifecycle.test.js). hostname: uniq(), identity: {system_uuid: '', system_serial: '', board_serial: '', chassis_asset: '', macs}, csr_pem: c.pem diff --git a/test/integration/agent-enroll.test.js b/test/integration/agent-enroll.test.js index ef7a9b5..cd935f9 100644 --- a/test/integration/agent-enroll.test.js +++ b/test/integration/agent-enroll.test.js @@ -50,8 +50,7 @@ describe('Agent enroll::', function () { agent_version: 'v0.0.1', os: 'linux', arch: 'amd64', - // A prefixed name, so the audit rows of the test are removable - // (test/lifecycle.test.js). The fallback name test passes its own. + // The fallback name test passes its own. hostname: uniq(), identity: {system_uuid: '', system_serial: '', board_serial: '', chassis_asset: '', macs: []}, csr_pem: c.pem @@ -190,19 +189,11 @@ describe('Agent enroll::', function () { }); it('unknown-host: a hostname that fails the name rule or is over 15 characters becomes agent-', async () => { - const hostIds = []; - try { - for (const hostname of ['bad name!', 'x'.repeat(16), '']) { - const c = await newCsr(); - await enroll(c, {hostname, identity: identity({macs: [mac()]})}).expect(202); - const host = await hostOf((await rowOf(c)).host); - hostIds.push(host.id); - assert.strictEqual(host.name, 'agent-' + c.fingerprint.slice(0, 8), `hostname ${JSON.stringify(hostname)}`); - } - } finally { - // The fallback name has no suite prefix, so remove the audit rows of - // these hosts by their id (the lifecycle cleanup cannot attribute them). - await AuditLog.destroy({subjectType: 'host', subjectId: hostIds}); + for (const hostname of ['bad name!', 'x'.repeat(16), '']) { + const c = await newCsr(); + await enroll(c, {hostname, identity: identity({macs: [mac()]})}).expect(202); + const host = await hostOf((await rowOf(c)).host); + assert.strictEqual(host.name, 'agent-' + c.fingerprint.slice(0, 8), `hostname ${JSON.stringify(hostname)}`); } }); diff --git a/test/integration/agent-mtls.test.js b/test/integration/agent-mtls.test.js index 97527e9..f052ded 100644 --- a/test/integration/agent-mtls.test.js +++ b/test/integration/agent-mtls.test.js @@ -21,8 +21,8 @@ describe('Agent mTLS::', function () { const fixtureDir = support.useFixture(); const app = () => sails.hooks.http.app; const AJAX = ['X-Requested-With', 'XMLHttpRequest']; - // Every name this file creates starts with PREFIX, which starts with one of - // the PREFIXES in test/lifecycle.test.js, so the audit rows are removable. + // Every name this file creates starts with PREFIX, so the names never + // collide with another test file's names in the same run. const RUN = crypto.randomBytes(3).toString('hex'); const PREFIX = 'mcm' + RUN; let seq = 0; diff --git a/test/integration/audit.test.js b/test/integration/audit.test.js index c6ed8ab..c8d3da8 100644 --- a/test/integration/audit.test.js +++ b/test/integration/audit.test.js @@ -5,7 +5,6 @@ const path = require('path'); const supertest = require('supertest'); const agentCsr = require('../support/agent-csr'); const support = require('../support/pki'); -const {PREFIXES} = require('../lifecycle.test'); // The audit trail core (docs/adr/0003-audit-trail.md): the AuditLog model, the // record helper, the request seam (api/hooks/audit), the sign-in events and the @@ -14,9 +13,8 @@ const {PREFIXES} = require('../lifecycle.test'); // The seam writes after the response finishes, so a test that expects a row // polls for it. A test that expects NO row makes its request, then a sentinel // request that does write a row (a denied GET by the same actor), waits for the -// sentinel row, and only then asserts the absence. Every name a test creates -// starts with a lifecycle PREFIXES entry. The correlation id is not exposed to -// a client, so no test filters on it. +// sentinel row, and only then asserts the absence. The correlation id is not +// exposed to a client, so no test filters on it. describe('Audit trail::', function () { this.timeout(120000); @@ -26,9 +24,9 @@ describe('Audit trail::', function () { const AJAX = ['X-Requested-With', 'XMLHttpRequest']; const RUN = crypto.randomBytes(3).toString('hex'); // Users, roles and attempted sign-in names. - const PREFIX = PREFIXES[0] + '-aud' + RUN; + const PREFIX = 'mocha-aud' + RUN; // Hosts, tokens and agent hostnames: a host name is at most 15 characters. - const SHORT = PREFIXES[1] + RUN; + const SHORT = 'mc' + RUN; const PASSWORD = 'mocha-audit-pw'; let seq = 0; const uniq = () => PREFIX + '-' + (seq++); diff --git a/test/lifecycle.test.js b/test/lifecycle.test.js index 5f6f54a..959aa19 100644 --- a/test/lifecycle.test.js +++ b/test/lifecycle.test.js @@ -1,17 +1,12 @@ +const assert = require('assert'); const sails = require('sails'); const support = require('./support/pki'); const {freePort} = require('./support/port'); +const testDb = require('../tools/lib/test-db'); -// The names the suite creates. Every test names its users, hosts, tokens, -// attempted sign-in names and agent hostnames with one of the PREFIXES, and -// the suite admin below is the other actor. The `after` below removes the -// AuditLog rows that carry one of them and nothing else: the suite may run -// while a dev server on 1337 writes real audit rows to the same database -// (package.json custom-tests), and those rows are never touched. -const ADMIN = 'mochatestadmin'; -const PREFIXES = ['mocha', 'mc', 'mpa', 'mph', 'testmochauser']; -// When the run started: only a row at or after it is the suite's. -let startedAt; +// The suite never runs on the configured database (tools/lib/test-db.js). It +// lifts on the same name plus `_test`, emptied before each run. +let target; // The agent listener (api/hooks/agent-tls) needs a PKI and a free port. 8443 is // often taken (a dev server, test:ui, test:install), so the suite asks the OS @@ -24,59 +19,50 @@ let agentTlsPort; // fixture. RSA-4096 generation is slow, hence the long timeout. before(async function() { this.timeout(180000); - startedAt = Date.now(); pkiDir = await support.fixture(); agentTlsPort = await freePort(); }); -before(function(done) { +before(async function() { this.timeout(11000); - sails.lift({ - // These were PORT=1338 and sails_hooks__grunt=false in the npm script. - // That syntax fails in Windows cmd, so the options live here instead. - port: 1338, - hooks: {grunt: false}, - log: {level: 'debug'}, - custom: {pkiDir, agentTlsPort, agentTlsSans: support.SANS} - }, (err) => { - if (err) return done(err); - Role.findOrCreate( - {name: 'Mocha_Test_Admin'}, - { - name: 'Mocha_Test_Admin', - isAdmin: true, - permissions: {} - }, (err, role) => { - if (err) return done(err); - User.findOrCreate( - {username: 'mochatestadmin'}, - { - username: 'mochatestadmin', - password: 'mochatestadmin', - email: 'mochatest@admin.com', - roles: [role.id] - }, (err) => { - if (err) return done(err); - done(); - } - ); - } - ); + target = await testDb.prepare(); + await new Promise((resolve, reject) => { + sails.lift({ + // These were PORT=1338 and sails_hooks__grunt=false in the npm script. + // That syntax fails in Windows cmd, so the options live here instead. + port: 1338, + hooks: {grunt: false}, + log: {level: 'debug'}, + datastores: {[target.datastore]: target.overrides}, + // config/bootstrap.js refuses to lift on any other database than + // testDatabase, before its index writes. + custom: {pkiDir, agentTlsPort, agentTlsSans: support.SANS, testDatabase: target.database} + }, (err) => (err ? reject(err) : resolve())); }); + // The guard, before any test file runs: the database the models really + // write to, read from the live connection. A failed root hook stops mocha. + const name = User.getDatastore().manager.databaseName; + if (!name.endsWith(testDb.SUFFIX) || name === target.original) { + await new Promise((resolve) => sails.lower(() => resolve())); + throw new Error(`Refusing to run: the models use the database "${name}", which must end in ${testDb.SUFFIX} and differ from the configured database.`); + } + const role = await Role.findOrCreate( + {name: 'Mocha_Test_Admin'}, + {name: 'Mocha_Test_Admin', isAdmin: true, permissions: {}} + ); + await User.findOrCreate( + {username: 'mochatestadmin'}, + { + username: 'mochatestadmin', + password: 'mochatestadmin', + email: 'mochatest@admin.com', + roles: [role.id] + } + ); }); after(async () => { // The seam writes after the response finishes, so let the last write land. await new Promise((resolve) => setTimeout(resolve, 200)); - await AuditLog.destroy({and: [ - {createdAt: {'>=': startedAt}}, - {or: [ - {createdBy: {in: [ADMIN]}}, - ...PREFIXES.map((prefix) => ({createdBy: {startsWith: prefix}})), - ...PREFIXES.map((prefix) => ({subjectLabel: {startsWith: prefix}})) - ]} - ]}); - await Role.destroy({name: 'Mocha_Test_Admin'}); - await User.destroy({username: ADMIN}); await new Promise((resolve, reject) => sails.lower((err) => (err ? reject(err) : resolve()))); }); @@ -84,6 +70,10 @@ describe('Basic Tests ::', () => { it('Sails did not crash', () => { return true; }); -}); -module.exports = {ADMIN, PREFIXES}; + // The before() guard stops the run on a wrong database. This reports the + // database that the run used. + it('runs on the test database, not the configured one', () => { + assert.strictEqual(User.getDatastore().manager.databaseName, target.database); + }); +}); diff --git a/test/migrations/2.test.js b/test/migrations/2.test.js index 0ba6c86..89c05e6 100644 --- a/test/migrations/2.test.js +++ b/test/migrations/2.test.js @@ -1,5 +1,6 @@ const assert = require('assert'); const {MongoClient} = require('mongodb'); +const {datastoreUrl} = require('../../tools/lib/database'); const migration = require('../../tools/migrate/lib/migration'); const revision2 = require('../../migrations/2'); @@ -18,8 +19,7 @@ describe('Schema revision 2::', () => { const url = new URL(ds.url); return {url: ds.url, name: `${decodeURIComponent(url.pathname.slice(1)) || 'fog'}_migration_test`}; } - const auth = ds.user ? `${ds.user}:${ds.password || ''}@` : ''; - return {url: `mongodb://${auth}${ds.host}:${ds.port}/${ds.database}`, name: `${decodeURIComponent(ds.database)}_migration_test`}; + return {url: datastoreUrl(ds), name: `${decodeURIComponent(ds.database)}_migration_test`}; } const run = (from, to) => new Promise((resolve, reject) => { diff --git a/test/setup/test-db.test.js b/test/setup/test-db.test.js new file mode 100644 index 0000000..d5e9a99 --- /dev/null +++ b/test/setup/test-db.test.js @@ -0,0 +1,87 @@ +const assert = require('assert'); +const {resolve} = require('../../tools/lib/test-db'); +const {datastoreUrl} = require('../../tools/lib/database'); + +// The test database name (tools/lib/test-db.js). The URLs here are fake. +describe('Test database name::', () => { + it('appends _test to the url database and keeps host, port and query', () => { + const t = resolve({url: 'mongodb://127.0.0.1:27017/fog?replicaSet=rs0'}); + assert.strictEqual(t.original, 'fog'); + assert.strictEqual(t.database, 'fog_test'); + assert.strictEqual(t.url, 'mongodb://127.0.0.1:27017/fog_test?replicaSet=rs0'); + assert.deepStrictEqual(t.overrides, {url: t.url, database: 'fog_test'}); + }); + + // sails-mongo lets a top-level `database` win over the url, so a url-only + // override would leave the run on the configured database. + it('overrides the top-level database of a url datastore, and names the configured one by it', () => { + const same = resolve({url: 'mongodb://h:1/fog', database: 'fog'}); + assert.deepStrictEqual(same.overrides, {url: 'mongodb://h:1/fog_test', database: 'fog_test'}); + const other = resolve({url: 'mongodb://h:1/fog', database: 'fog_test'}); + assert.strictEqual(other.original, 'fog_test'); + assert.strictEqual(other.database, 'fog_test_test'); + assert.deepStrictEqual(other.overrides, {url: 'mongodb://h:1/fog_test_test', database: 'fog_test_test'}); + }); + + it('appends the suffix again to a name that already ends in _test', () => { + const t = resolve({url: 'mongodb://127.0.0.1:27017/fog_test'}); + assert.strictEqual(t.database, 'fog_test_test'); + assert.notStrictEqual(t.database, t.original); + }); + + it('never returns the configured name', () => { + for (const name of ['fog', 'fog_test', 'fog_mtls', '_test', 'a_test_test']) { + const url = resolve({url: `mongodb://h:1/${name}`}); + const parts = resolve({host: 'h', port: 1, database: name}); + for (const t of [url, parts]) { + assert.notStrictEqual(t.database, t.original); + assert.strictEqual(t.original, name); + assert.ok(t.database.endsWith('_test')); + } + } + }); + + it('adds authSource= when the url has credentials and no authSource', () => { + assert.strictEqual(resolve({url: 'mongodb://fakeuser:fakepass@h:1/fog'}).url, + 'mongodb://fakeuser:fakepass@h:1/fog_test?authSource=fog'); + assert.strictEqual(resolve({url: 'mongodb://fakeuser:fakepass@h:1/fog?tls=true'}).url, + 'mongodb://fakeuser:fakepass@h:1/fog_test?tls=true&authSource=fog'); + }); + + it('keeps an authSource that the url has, and adds none without credentials', () => { + assert.strictEqual(resolve({url: 'mongodb://fakeuser:fakepass@h:1/fog?authSource=admin'}).url, + 'mongodb://fakeuser:fakepass@h:1/fog_test?authSource=admin'); + assert.ok(!resolve({url: 'mongodb://h:1/fog'}).url.includes('authSource')); + }); + + it('builds the target from host, port and database (the installer shape)', () => { + const t = resolve({host: '10.0.0.5', port: 27017, database: 'fog'}); + assert.strictEqual(t.original, 'fog'); + assert.strictEqual(t.database, 'fog_test'); + assert.strictEqual(t.url, 'mongodb://10.0.0.5:27017/fog_test'); + assert.deepStrictEqual(t.overrides, {url: t.url, database: 'fog_test'}); + }); + + it('adds the credentials and authSource in the installer shape, and decodes the name', () => { + const t = resolve({host: 'h', port: 1, user: 'fakeuser', password: 'fake%40pass', database: 'my%20fog'}); + assert.strictEqual(t.original, 'my fog'); + assert.strictEqual(t.database, 'my fog_test'); + assert.strictEqual(t.url, 'mongodb://fakeuser:fake%40pass@h:1/my%20fog_test?authSource=my%20fog'); + assert.strictEqual(t.overrides.database, 'my%20fog_test'); + }); + + it('throws when there is no database name', () => { + assert.throws(() => resolve({url: 'mongodb://h:1'})); + assert.throws(() => resolve({url: 'mongodb://h:1/'})); + assert.throws(() => resolve({host: 'h'})); + }); + + // The installer-shape URL (tools/lib/database.js datastoreUrl), as + // sails-mongo builds it: no `user:@` without a password, no `:undefined` + // without a port. + it('builds the installer-shape url without an empty password or a missing port', () => { + assert.strictEqual(datastoreUrl({host: 'h', database: 'd', user: 'u'}), 'mongodb://u@h/d'); + assert.strictEqual(datastoreUrl({host: 'h', database: 'd'}), 'mongodb://h/d'); + assert.strictEqual(datastoreUrl({host: 'h', port: 1, database: 'd', user: 'u', password: 'p'}), 'mongodb://u:p@h:1/d'); + }); +}); diff --git a/tools/lib/database.js b/tools/lib/database.js index e75a896..33f916b 100644 --- a/tools/lib/database.js +++ b/tools/lib/database.js @@ -1,21 +1,25 @@ const MongoClient = require('mongodb').MongoClient; +// The URL of a datastore in the installer shape: {host, port, database, user, +// password} (tools/setup/index.js). The installer writes each value +// URL-encoded, and sails-mongo puts them in its URL as given +// (normalize-datastore-config), so this does the same, with the same rules: a +// port only when set, and credentials only when a username was provided. An +// empty auth part makes the driver attempt authentication with blank +// credentials, which fails against a MongoDB with no authentication. +function datastoreUrl(ds) { + const auth = ds.user ? `${ds.user}${ds.password ? ':' + ds.password : ''}@` : ''; + return `mongodb://${auth}${ds.host}${ds.port ? ':' + ds.port : ''}/${ds.database}`; +} + module.exports = { + datastoreUrl, // `datastore` is a fogdb datastore object, as config/local.js holds it: // {url} as `npm run setup:dev` writes it, or {host, port, database, user, // password} as the installer writes it (tools/setup/index.js). The callback // gets the MongoClient; client.db() is the database. connect: (datastore, next) => { - let dbUri = datastore.url; - if (!dbUri) { - // The installer writes each value URL-encoded, and sails-mongo puts - // them in its URL as given (normalize-datastore-config), so this does - // the same. Credentials only when a username was provided: an empty - // auth part makes the driver attempt authentication with blank - // credentials, which fails against a MongoDB with no authentication. - let auth = datastore.user ? `${datastore.user}:${datastore.password || ''}@` : ''; - dbUri = `mongodb://${auth}${datastore.host}:${datastore.port}/${datastore.database}`; - } + let dbUri = datastore.url || datastoreUrl(datastore); let opts = { useUnifiedTopology: true, useNewUrlParser: true, diff --git a/tools/lib/test-db.js b/tools/lib/test-db.js new file mode 100644 index 0000000..8a5eee0 --- /dev/null +++ b/tools/lib/test-db.js @@ -0,0 +1,103 @@ +/** + * The test database, shared by the mocha suite (test/lifecycle.test.js) and + * the UI smoke test (tools/smoke/ui.js). + * + * A test run never uses the configured database. It uses the configured name + * plus the suffix `_test`, always: `fog` gives `fog_test`, and `fog_test` + * gives `fog_test_test`. It empties that database first. Never print a URL: + * it can hold credentials. + */ +const {datastoreUrl} = require('./database'); + +const SUFFIX = '_test'; + +// The datastore the models use, and its config. sails.config is not available +// before a lift, so read the files Sails merges: config/local.js over +// config/datastores.js. +function configured() { + const models = require('../../config/models.js').models || {}; + const name = models.datastore || 'default'; + const local = require('../../config/local.js').datastores || {}; + const base = require('../../config/datastores.js').datastores || {}; + return {name, config: Object.assign({}, base[name], local[name])}; +} + +// Adds authSource= to the query when the URL has +// credentials and no authSource. The user lives in the configured database, +// and a login checked against the test database would fail. +function withAuthSource(query, authDatabase, hasCredentials) { + if (!hasCredentials || /(^|[?&])authSource=/i.test(query || '')) return query || ''; + return (query ? query + '&' : '?') + 'authSource=' + authDatabase; +} + +// Resolves the test database from a datastore config. The config is either +// `url` (npm run setup:dev) or `host`, `port` and `database` with an optional +// `user` and `password` (npm run setup, tools/setup/index.js), where the +// installer has already URI-encoded the parts. The same two shapes are in +// test/migrations/2.test.js. +// +// Returns {original, database, url, overrides}: the decoded configured name, +// the decoded test name, the test URL, and the datastore keys to override. +// `database` is overridden in both shapes, because sails-mongo lets a +// top-level `database` win over the URL (normalize-datastore-config). For the +// same reason, a url with a top-level `database` names the configured +// database by the top-level value. +function resolve(ds) { + if (ds.url) { + const match = /^(mongodb(?:\+srv)?:\/\/)([^/?]*)\/([^/?]*)(\?.*)?$/.exec(ds.url); + const configuredName = ds.database || (match && match[3]); + if (!match || !configuredName) throw new Error('The configured datastore url has no database name, so the test run cannot name a test database.'); + const original = decodeURIComponent(configuredName); + const database = original + SUFFIX; + const url = match[1] + match[2] + '/' + encodeURIComponent(database) + + withAuthSource(match[4], configuredName, match[2].includes('@')); + return {original, database, url, overrides: {url, database: encodeURIComponent(database)}}; + } + if (!ds.host || !ds.database) throw new Error('The configured datastore has no url and no host and database, so the test run cannot name a test database.'); + const original = decodeURIComponent(ds.database); + const database = original + SUFFIX; + const url = datastoreUrl(Object.assign({}, ds, {database: encodeURIComponent(database)})) + + withAuthSource('', ds.database, Boolean(ds.user)); + return {original, database, url, overrides: {url, database: encodeURIComponent(database)}}; +} + +// Resolves the test database, checks the name, and drops it. Returns +// {datastore, original, database, url, overrides}: the datastore name the +// models use is `datastore`. +async function prepare() { + const {MongoClient} = require('mongodb'); + const target = configured(); + const test = resolve(target.config); + // The guard. The drop below runs only on a name that ends in _test and + // differs from the configured name. + if (test.database === test.original || !test.database.endsWith(SUFFIX)) { + throw new Error(`Refusing to run: the test database "${test.database}" must end in ${SUFFIX} and differ from the configured database.`); + } + // A wrong host or port fails in 5 s, not the driver default of 30 s, as in + // tools/lib/database.js. + const client = await MongoClient.connect(test.url, {useUnifiedTopology: true, serverSelectionTimeoutMS: 5000}); + try { + await client.db(test.database).dropDatabase(); + } finally { + await client.close(); + } + return {datastore: target.name, original: test.original, database: test.database, url: test.url, overrides: test.overrides}; +} + +// The configured database name, or null when this checkout has no datastore +// config yet (a fresh clone). tools/smoke/install.js checks its own throwaway +// name against it before a drop. +function configuredName() { + let target; + try { + target = configured(); + } catch (err) { + // Only a missing config file means "no config yet". A broken one stops + // the run instead of letting a drop through. + if (err.code === 'MODULE_NOT_FOUND') { return null; } + throw err; + } + return resolve(target.config).original; +} + +module.exports = {SUFFIX, resolve, prepare, configuredName}; diff --git a/tools/setup/lib/schema.js b/tools/setup/lib/schema.js index 71c49d2..1e9cd5e 100644 --- a/tools/setup/lib/schema.js +++ b/tools/setup/lib/schema.js @@ -1,5 +1,6 @@ const config = require('../../lib/config'); const sails = require('sails'); +const _ = require('@sailshq/lodash'); var adminRole = { name: 'Administrator', isAdmin: true, @@ -16,8 +17,12 @@ var schema = { }; module.exports = { - generate: (adminPassword, adminEmail, next) => { + // `overrides` (optional) is merged over a copy of the config, for example + // the datastore of the UI smoke test (tools/smoke/ui.js). It comes after + // `next` so that the two setup callers (dev.js, index.js) stay as they are. + generate: (adminPassword, adminEmail, next, overrides) => { let cfg = config.getMergedSettings(); + if (overrides) cfg = _.merge({}, cfg, overrides); cfg.log = { level: 'error', }; diff --git a/tools/smoke/install.js b/tools/smoke/install.js index 8b837bd..5948e25 100644 --- a/tools/smoke/install.js +++ b/tools/smoke/install.js @@ -17,7 +17,7 @@ * * Usage: npm run test:install * It needs MongoDB and Redis. It installs into the database - * fog_install_smoke, which it drops first, and an image directory under the + * fog_install_smoke_test, which it drops first, and an image directory under the * OS temp directory. It saves config/local.js and config/models.js first * and puts them back at the end, so a dev checkout keeps its config. * @@ -31,11 +31,14 @@ const fs = require('fs'); const os = require('os'); const path = require('path'); const server = require('./lib/server'); +const testDb = require('../lib/test-db'); const root = server.root; const port = 1340; const base = `http://127.0.0.1:${port}`; -const dbName = 'fog_install_smoke'; +// A throwaway name that ends in _test, as every test database does +// (tools/lib/test-db.js). dropDatabase refuses it if it is the configured one. +const dbName = 'fog_install_smoke' + testDb.SUFFIX; const dbHost = process.env.FOG_SMOKE_DB_HOST || '127.0.0.1'; const configs = ['local.js', 'models.js'].map((f) => path.join(root, 'config', f)); const work = fs.mkdtempSync(path.join(os.tmpdir(), 'fog-install-smoke-')); @@ -54,6 +57,10 @@ const keepCookies = (res) => { const cookieHeader = () => [...jar].map(([k, v]) => `${k}=${v}`).join('; '); async function dropDatabase() { + // The guard: never drop the database this checkout is configured to use. + if (dbName === testDb.configuredName()) { + throw new Error(`Refusing to drop "${dbName}": it is the configured database.`); + } const {MongoClient} = require('mongodb'); const client = await MongoClient.connect(`mongodb://${dbHost}:27017`, {useUnifiedTopology: true}); try { diff --git a/tools/smoke/ui.js b/tools/smoke/ui.js index f51796f..d321b0f 100644 --- a/tools/smoke/ui.js +++ b/tools/smoke/ui.js @@ -38,19 +38,25 @@ * created {host, storageGroup}: the names the round trips created; a * name is undefined when its round trip failed * - * Environment: - * FOG_UI_PASSWORD the Administrator password (default: fogadmin1, the - * `npm run setup:dev` default) + * Database: the run never uses the configured database. It empties the + * database `_test` (tools/lib/test-db.js), seeds the + * Administrator account there with a password that exists only in memory + * (the same seed as `npm run setup:dev`), and lifts on it. Redis sessions are + * shared with a dev server, but session ids are random, so they do not collide. */ +const crypto = require('crypto'); const fs = require('fs'); const path = require('path'); const {chromium} = require('playwright'); const server = require('./lib/server'); +const testDb = require('../lib/test-db'); +const schema = require('../setup/lib/schema'); const root = server.root; const port = 1339; const base = `http://127.0.0.1:${port}`; -const password = process.env.FOG_UI_PASSWORD || 'fogadmin1'; +// A new password for each run. It only ever guards the test database. +const password = crypto.randomBytes(12).toString('hex'); // --prod lifts with the production asset build: concatenated and minified // bundles (issue #115 broke that build, and nothing noticed). const prod = process.argv.includes('--prod'); @@ -219,12 +225,45 @@ const CHECKS = fs.readdirSync(checksDir).filter((f) => f.endsWith('.js')).sort() }); async function main() { + const target = await testDb.prepare(); + const datastores = {[target.datastore]: target.overrides}; + // Seed the Administrator into the test database, then close that Sails. + // custom.testDatabase makes config/bootstrap.js refuse any other database. + await new Promise((resolve, reject) => { + schema.generate(password, 'admin@example.com', (err) => (err ? reject(err) : resolve()), + {datastores, custom: {testDatabase: target.database}}); + }); + await new Promise((resolve) => require('sails').lower(() => resolve())); + // The datastore overrides reach the server as sails_datastores____. + const datastoreEnv = {}; + for (const [key, value] of Object.entries(target.overrides)) { + datastoreEnv[`sails_datastores__${target.datastore}__${key}`] = value; + } // Production refuses to lift without socket origins. `npm run setup` // writes them, but `npm run setup:dev` does not, so the test sets the one // origin that its browser uses. const child = server.start({ port, prod, log: serverLog, - env: {'sails_sockets__onlyAllowOrigins': JSON.stringify([base])} + env: { + 'sails_sockets__onlyAllowOrigins': JSON.stringify([base]), + 'sails_custom__testDatabase': target.database, + ...datastoreEnv + } + }); + // With custom.testDatabase set, config/bootstrap.js prints the database the + // models use, and refuses to lift on another one. Keep that line, so the + // check below does not depend on the 200 lines that serverLog keeps. + let reported; + let childOut = ''; + child.stdout.on('data', function watchDatabase(buf) { + childOut += buf.toString(); + const match = /fog-node models database: (.+?)\r?\n/.exec(childOut); + if (match) { + reported = match[1]; + child.stdout.off('data', watchDatabase); + } else { + childOut = childOut.slice(-500); + } }); let browser; let failed = 0; @@ -247,6 +286,10 @@ async function main() { }; try { await server.waitForReady(child, base); + // No browser action unless the server reported the test database. + if (reported !== target.database) { + throw new Error(`the server uses the database "${reported}", not the test database "${target.database}"`); + } browser = await chromium.launch(); const page = await browser.newPage(); const loginErrors = await collectErrors(page, () => login(page)); @@ -280,6 +323,8 @@ async function main() { process.exit(1); } console.log('\nAll UI checks passed.'); + // The seed's Sails app and its Redis client keep the process alive. + process.exit(0); } main();