From 974ff817e47b8a59c5a1e1eb400a5ad8212527c5 Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Thu, 2 Apr 2020 15:41:46 -0700 Subject: [PATCH 1/3] test: reap old instances before system tests --- package.json | 10 ++-------- system-test/bigtable.ts | 37 ++++++++++++++++++++++--------------- tslint.json | 3 --- 3 files changed, 24 insertions(+), 26 deletions(-) delete mode 100644 tslint.json diff --git a/package.json b/package.json index 039c85fb5..15e26fe78 100644 --- a/package.json +++ b/package.json @@ -30,9 +30,9 @@ "docs": "jsdoc -c .jsdoc.js", "predocs-test": "npm run docs", "docs-test": "linkinator docs", - "fix": "gts fix && eslint '**/*.js' --fix", + "fix": "gts fix", "prelint": "cd samples; npm link ../; npm i", - "lint": "gts check && eslint samples/*.js", + "lint": "gts check", "prepare": "npm run compile", "samples-test": "cd samples/ && npm link ../ && npm install && npm test && cd ../", "snippet-test": "mocha samples/document-snippets/tests/*.js --timeout 600000", @@ -79,10 +79,6 @@ "@types/uuid": "^7.0.0", "c8": "^7.1.0", "codecov": "^3.6.5", - "eslint": "^6.8.0", - "eslint-config-prettier": "^6.10.0", - "eslint-plugin-node": "^11.0.0", - "eslint-plugin-prettier": "^3.1.2", "google-auth-library": "^6.0.0", "gts": "2.0.0-alpha.9", "jsdoc": "^3.6.3", @@ -96,8 +92,6 @@ "null-loader": "^3.0.0", "p-queue": "^6.0.2", "pack-n-play": "^1.0.0-2", - "power-assert": "^1.6.1", - "prettier": "^1.19.1", "proxyquire": "^2.0.0", "sinon": "^9.0.1", "ts-loader": "^6.2.1", diff --git a/system-test/bigtable.ts b/system-test/bigtable.ts index 898070f45..472d1f5c3 100644 --- a/system-test/bigtable.ts +++ b/system-test/bigtable.ts @@ -35,7 +35,28 @@ describe('Bigtable', () => { const APP_PROFILE = INSTANCE.appProfile(APP_PROFILE_ID); const CLUSTER_ID = generateId('cluster'); + async function reapInstances(prefix?: string) { + const [instances] = await bigtable.getInstances(); + let testInstances = instances; + if (prefix) { + testInstances = testInstances.filter(i => i.id.match(PREFIX)); + } + testInstances = testInstances.filter(i => { + const timeCreated = (i.metadata!.labels!.time_created as {}) as Date; + // Only delete stale resources. + const oneHourAgo = new Date(Date.now() - 3600000); + return !timeCreated || timeCreated <= oneHourAgo; + }); + const q = new Q({concurrency: 5}); + await Promise.all( + testInstances.map(instance => { + q.add(() => instance.delete()); + }) + ); + } + before(async () => { + await reapInstances(); const [, operation] = await INSTANCE.create({ clusters: [ { @@ -59,21 +80,7 @@ describe('Bigtable', () => { }); after(async () => { - const [instances] = await bigtable.getInstances(); - const testInstances = instances - .filter(i => i.id.match(PREFIX)) - .filter(i => { - const timeCreated = (i.metadata!.labels!.time_created as {}) as Date; - // Only delete stale resources. - const oneHourAgo = new Date(Date.now() - 3600000); - return !timeCreated || timeCreated <= oneHourAgo; - }); - const q = new Q({concurrency: 5}); - await Promise.all( - testInstances.map(instance => { - q.add(() => instance.delete()); - }) - ); + await reapInstances(PREFIX); }); describe('instances', () => { diff --git a/tslint.json b/tslint.json deleted file mode 100644 index 617dc975b..000000000 --- a/tslint.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "extends": "gts/tslint.json" -} From da39b994f98cca4c9be0c438af6aee22cdbf7757 Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Thu, 2 Apr 2020 15:51:37 -0700 Subject: [PATCH 2/3] use the prefix --- system-test/bigtable.ts | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/system-test/bigtable.ts b/system-test/bigtable.ts index 472d1f5c3..d82f8647a 100644 --- a/system-test/bigtable.ts +++ b/system-test/bigtable.ts @@ -35,18 +35,16 @@ describe('Bigtable', () => { const APP_PROFILE = INSTANCE.appProfile(APP_PROFILE_ID); const CLUSTER_ID = generateId('cluster'); - async function reapInstances(prefix?: string) { + async function reapInstances() { const [instances] = await bigtable.getInstances(); - let testInstances = instances; - if (prefix) { - testInstances = testInstances.filter(i => i.id.match(PREFIX)); - } - testInstances = testInstances.filter(i => { - const timeCreated = (i.metadata!.labels!.time_created as {}) as Date; - // Only delete stale resources. - const oneHourAgo = new Date(Date.now() - 3600000); - return !timeCreated || timeCreated <= oneHourAgo; - }); + const testInstances = instances + .filter(i => i.id.match(PREFIX)) + .filter(i => { + const timeCreated = (i.metadata!.labels!.time_created as {}) as Date; + // Only delete stale resources. + const oneHourAgo = new Date(Date.now() - 3600000); + return !timeCreated || timeCreated <= oneHourAgo; + }); const q = new Q({concurrency: 5}); await Promise.all( testInstances.map(instance => { @@ -80,7 +78,7 @@ describe('Bigtable', () => { }); after(async () => { - await reapInstances(PREFIX); + await INSTANCE.delete().catch(console.error); }); describe('instances', () => { From e68a0aa892e4704b5ce07ed6f54527b47efc46d0 Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Thu, 2 Apr 2020 17:09:44 -0700 Subject: [PATCH 3/3] ugh --- samples/test/app-profile.js | 4 +-- samples/test/cluster.js | 4 +-- samples/test/family.js | 6 ++-- samples/test/filters.js | 12 +++---- samples/test/instance.js | 8 ++--- samples/test/instances.test.js | 4 +-- samples/test/reads.js | 21 ++++------- samples/test/row.js | 6 ++-- samples/test/table.js | 12 +++---- samples/test/util.js | 66 ++++++++++++++++++++++++++++++++++ samples/test/write.js | 14 +++----- system-test/install.ts | 4 +-- test/index.ts | 4 +-- test/table.ts | 11 +++--- 14 files changed, 111 insertions(+), 65 deletions(-) create mode 100644 samples/test/util.js diff --git a/samples/test/app-profile.js b/samples/test/app-profile.js index 809c119d1..4d85d382e 100644 --- a/samples/test/app-profile.js +++ b/samples/test/app-profile.js @@ -19,8 +19,8 @@ const {describe, it, before, after} = require('mocha'); const Bigtable = require('@google-cloud/bigtable'); const bigtable = new Bigtable(); -const INSTANCE_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules -const CLUSTER_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const INSTANCE_ID = `gcloud-tests-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const CLUSTER_ID = `gcloud-tests-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules const APP_PROFILE_ID = 'my-app-profile'; const appProfileSnippets = require('./app-profile.js'); diff --git a/samples/test/cluster.js b/samples/test/cluster.js index 82f10f0c6..49654fa70 100644 --- a/samples/test/cluster.js +++ b/samples/test/cluster.js @@ -19,8 +19,8 @@ const {describe, it, before, after} = require('mocha'); const Bigtable = require('@google-cloud/bigtable'); const bigtable = new Bigtable(); -const INSTANCE_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules -const CLUSTER_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const INSTANCE_ID = `gcloud-tests-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const CLUSTER_ID = `gcloud-tests-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules const clusterSnippets = require('./cluster.js'); diff --git a/samples/test/family.js b/samples/test/family.js index 32e178e20..6e1597360 100644 --- a/samples/test/family.js +++ b/samples/test/family.js @@ -19,9 +19,9 @@ const {describe, it, before, after} = require('mocha'); const Bigtable = require('@google-cloud/bigtable'); const bigtable = new Bigtable(); -const INSTANCE_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules -const CLUSTER_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules -const TABLE_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const INSTANCE_ID = `gcloud-tests-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const CLUSTER_ID = `gcloud-tests-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const TABLE_ID = `gcloud-tests-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules const FAMILY_ID = `sample-family-${uuid.v4()}`.substr(0, 10); // Bigtable naming rules const familySnippets = require('./family.js'); diff --git a/samples/test/filters.js b/samples/test/filters.js index 9ad672109..7d5a58a99 100644 --- a/samples/test/filters.js +++ b/samples/test/filters.js @@ -19,23 +19,23 @@ const snapshot = require('snap-shot-it'); const {assert} = require('chai'); const {describe, it, before, after} = require('mocha'); const cp = require('child_process'); -const {Bigtable} = require('@google-cloud/bigtable'); +const {obtainTestInstance} = require('./util'); const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); - -const INSTANCE_ID = 'nodejs-bigtable-samples-keepme'; -const TABLE_ID = `mobile-time-series-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const runId = uuid.v4().split('-')[0]; +const TABLE_ID = `mobile-time-series-${runId}`; describe('filters', async () => { - const bigtable = Bigtable(); - const instance = bigtable.instance(INSTANCE_ID); let table; + let INSTANCE_ID; const TIMESTAMP = new Date(2019, 5, 1); TIMESTAMP.setUTCHours(0); const TIMESTAMP_OLDER = new Date(2019, 4, 30); TIMESTAMP_OLDER.setUTCHours(0); before(async () => { + const instance = await obtainTestInstance(); + INSTANCE_ID = instance.id; table = instance.table(TABLE_ID); await table.create().catch(console.error); diff --git a/samples/test/instance.js b/samples/test/instance.js index ced62132d..b977273ab 100644 --- a/samples/test/instance.js +++ b/samples/test/instance.js @@ -19,10 +19,10 @@ const {describe, it, after} = require('mocha'); const Bigtable = require('@google-cloud/bigtable'); const bigtable = new Bigtable(); -const INSTANCE_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules -const CLUSTER_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules -// const APP_PROFILE_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules -const TABLE_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const INSTANCE_ID = `gcloud-tests-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const CLUSTER_ID = `gcloud-tests-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +// const APP_PROFILE_ID = `gcloud-tests-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const TABLE_ID = `gcloud-tests-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules const instanceSnippets = require('./instance.js'); diff --git a/samples/test/instances.test.js b/samples/test/instances.test.js index c440bdea5..3da04bdab 100644 --- a/samples/test/instances.test.js +++ b/samples/test/instances.test.js @@ -23,8 +23,8 @@ const Bigtable = require('@google-cloud/bigtable'); const exec = cmd => execSync(cmd, {encoding: 'utf8'}); const bigtable = new Bigtable(); -const clusterId = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules -const instanceId = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const clusterId = `gcloud-tests-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const instanceId = `gcloud-tests-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules const instance = bigtable.instance(instanceId); describe('instances', () => { diff --git a/samples/test/reads.js b/samples/test/reads.js index 91199ce6d..ad75e19e0 100644 --- a/samples/test/reads.js +++ b/samples/test/reads.js @@ -16,27 +16,24 @@ const uuid = require('uuid'); const snapshot = require('snap-shot-it'); -const {describe, it, before, after} = require('mocha'); +const {describe, it, before} = require('mocha'); const cp = require('child_process'); -const {Bigtable} = require('@google-cloud/bigtable'); +const {obtainTestInstance} = require('./util'); const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); - -const INSTANCE_ID = 'nodejs-bigtable-samples-keepme'; const TABLE_ID = `mobile-time-series-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules describe('reads', async () => { - const bigtable = Bigtable(); - const instance = bigtable.instance(INSTANCE_ID); + const instance = await obtainTestInstance(); + const INSTANCE_ID = instance.id; let table; const TIMESTAMP = new Date(2019, 5, 1); TIMESTAMP.setUTCHours(0); before(async () => { table = instance.table(TABLE_ID); - - await table.create().catch(console.error); - await table.createFamily('stats_summary').catch(console.error); + await table.create(); + await table.createFamily('stats_summary'); const rowsToInsert = [ { @@ -136,11 +133,7 @@ describe('reads', async () => { }, ]; - await table.insert(rowsToInsert).catch(console.error); - }); - - after(async () => { - await table.delete().catch(console.error); + await table.insert(rowsToInsert); }); it('should read one row', async () => { diff --git a/samples/test/row.js b/samples/test/row.js index 53d5718bb..f96d3e2f7 100644 --- a/samples/test/row.js +++ b/samples/test/row.js @@ -19,9 +19,9 @@ const {describe, it, before, after} = require('mocha'); const Bigtable = require('@google-cloud/bigtable'); const bigtable = new Bigtable(); -const INSTANCE_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules -const CLUSTER_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules -const TABLE_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const INSTANCE_ID = `gcloud-tests-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const CLUSTER_ID = `gcloud-tests-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const TABLE_ID = `gcloud-tests-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules const rowSnippets = require('./row.js'); diff --git a/samples/test/table.js b/samples/test/table.js index add5c790e..98437c0cb 100644 --- a/samples/test/table.js +++ b/samples/test/table.js @@ -19,9 +19,9 @@ const {describe, it, before, after} = require('mocha'); const Bigtable = require('@google-cloud/bigtable'); const bigtable = new Bigtable(); -const INSTANCE_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules -const CLUSTER_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules -const TABLE_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const INSTANCE_ID = `gcloud-tests-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const CLUSTER_ID = `gcloud-tests-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const TABLE_ID = `gcloud-tests-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules const tableSnippets = require('./table.js'); @@ -46,11 +46,7 @@ describe.skip('Table Snippets', () => { }); after(async () => { - try { - await instance.delete(); - } catch (err) { - // Handle the error. - } + await instance.delete().catch(console.error); }); it('should create a table', () => { diff --git a/samples/test/util.js b/samples/test/util.js new file mode 100644 index 000000000..5e70df564 --- /dev/null +++ b/samples/test/util.js @@ -0,0 +1,66 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +const uuid = require('uuid'); +const {Bigtable} = require('@google-cloud/bigtable'); +const {after} = require('mocha'); + +const runId = uuid.v4().split('-')[0]; +const instanceId = `gcloud-tests-${runId}`; +const clusterId = `gcloud-tests-${runId}`; +const bigtable = new Bigtable(); +let instance; + +let obtainPromise; + +/** + * Only create a single cluster and instance, but let multiple tests await + * the result. + */ +async function obtainTestInstance() { + if (!obtainPromise) { + obtainPromise = createTestInstance(); + } + return obtainPromise; +} + +/** + * Create a testing cluster and the corresponding instance. + */ +async function createTestInstance() { + instance = bigtable.instance(instanceId); + const [, operation] = await instance.create({ + clusters: [ + { + id: clusterId, + location: 'us-central1-c', + nodes: 3, + }, + ], + labels: { + time_created: Date.now(), + }, + }); + await operation.promise(); + return instance; +} + +/** + * Delete the instance in a global hook. + */ +after(async () => { + await instance.delete(); +}); + +module.exports = {obtainTestInstance}; diff --git a/samples/test/write.js b/samples/test/write.js index b9ec9ff65..99ee2eeb7 100644 --- a/samples/test/write.js +++ b/samples/test/write.js @@ -15,19 +15,17 @@ 'use strict'; const {assert} = require('chai'); -const {describe, it, before, after} = require('mocha'); +const {describe, it, before} = require('mocha'); const cp = require('child_process'); const uuid = require('uuid'); -const Bigtable = require('@google-cloud/bigtable'); +const {obtainTestInstance} = require('./util'); const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); - -const INSTANCE_ID = 'nodejs-bigtable-samples-keepme'; const TABLE_ID = `mobile-time-series-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules describe('writes', async () => { - const bigtable = Bigtable(); - const instance = bigtable.instance(INSTANCE_ID); + const instance = await obtainTestInstance(); + const INSTANCE_ID = instance.id; let table; before(async () => { @@ -37,10 +35,6 @@ describe('writes', async () => { await table.createFamily('stats_summary').catch(console.error); }); - after(async () => { - await table.delete().catch(console.error); - }); - it('should do a simple write', async () => { const stdout = execSync(`node writeSimple ${INSTANCE_ID} ${TABLE_ID}`); assert.match(stdout, /Successfully wrote row .*/); diff --git a/system-test/install.ts b/system-test/install.ts index c4d80e9c0..4c1ba3eb7 100644 --- a/system-test/install.ts +++ b/system-test/install.ts @@ -21,7 +21,7 @@ import {readFileSync} from 'fs'; import {describe, it} from 'mocha'; describe('typescript consumer tests', () => { - it('should have correct type signature for typescript users', async function() { + it('should have correct type signature for typescript users', async function () { this.timeout(300000); const options = { packageDir: process.cwd(), // path to your module. @@ -35,7 +35,7 @@ describe('typescript consumer tests', () => { await packNTest(options); // will throw upon error. }); - it('should have correct type signature for javascript users', async function() { + it('should have correct type signature for javascript users', async function () { this.timeout(300000); const options = { packageDir: process.cwd(), // path to your module. diff --git a/test/index.ts b/test/index.ts index 77cdd8638..95193f466 100644 --- a/test/index.ts +++ b/test/index.ts @@ -633,7 +633,7 @@ describe('Bigtable', () => { [CONFIG.method]: noop, }; // eslint-disable-next-line @typescript-eslint/no-explicit-any - (fakeV2 as any)[CONFIG.client] = function(options: any) { + (fakeV2 as any)[CONFIG.client] = function (options: any) { assert.strictEqual(options, bigtable.options[CONFIG.client]); return fakeClient; }; @@ -644,7 +644,7 @@ describe('Bigtable', () => { it('should use the cached client', done => { // eslint-disable-next-line @typescript-eslint/no-explicit-any - (fakeV2 as any)[CONFIG.client] = function() { + (fakeV2 as any)[CONFIG.client] = function () { done(new Error('Should not re-instantiate a GAX client.')); }; bigtable.request(CONFIG); diff --git a/test/table.ts b/test/table.ts index 3e04c1822..775ae2abb 100644 --- a/test/table.ts +++ b/test/table.ts @@ -71,7 +71,7 @@ FakeRow.formatChunks_ = sinon.spy(chunks => { }); const FakeChunkTransformer = createFake(ChunkTransformer); -FakeChunkTransformer.prototype._transform = function( +FakeChunkTransformer.prototype._transform = function ( rows: Row[], enc: {}, next: Function @@ -808,7 +808,7 @@ describe('Bigtable/Table', () => { sinon.stub(table, 'row').callsFake(() => { return {} as Row; }); - FakeChunkTransformer.prototype._transform = function( + FakeChunkTransformer.prototype._transform = function ( chunks: Array<{}>, enc: {}, next: Function @@ -990,7 +990,7 @@ describe('Bigtable/Table', () => { let setTimeoutSpy: sinon.SinonSpy; beforeEach(() => { - FakeChunkTransformer.prototype._transform = function( + FakeChunkTransformer.prototype._transform = function ( rows: Row[], enc: {}, next: Function @@ -1005,10 +1005,7 @@ describe('Bigtable/Table', () => { }; callCreateReadStream = (options: {}, verify: Function) => { - table - .createReadStream(options) - .on('end', verify) - .resume(); // The stream starts paused unless it has a `.data()` + table.createReadStream(options).on('end', verify).resume(); // The stream starts paused unless it has a `.data()` // callback. };