Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

test: reap old instances before system tests #687

Merged
merged 3 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions samples/test/app-profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions samples/test/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
6 changes: 3 additions & 3 deletions samples/test/family.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
12 changes: 6 additions & 6 deletions samples/test/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions samples/test/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
4 changes: 2 additions & 2 deletions samples/test/instances.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
21 changes: 7 additions & 14 deletions samples/test/reads.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand Down Expand Up @@ -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 () => {
Expand Down
6 changes: 3 additions & 3 deletions samples/test/row.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
12 changes: 4 additions & 8 deletions samples/test/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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', () => {
Expand Down
66 changes: 66 additions & 0 deletions samples/test/util.js
Original file line number Diff line number Diff line change
@@ -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};
14 changes: 4 additions & 10 deletions samples/test/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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 .*/);
Expand Down
35 changes: 20 additions & 15 deletions system-test/bigtable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,26 @@ describe('Bigtable', () => {
const APP_PROFILE = INSTANCE.appProfile(APP_PROFILE_ID);
const CLUSTER_ID = generateId('cluster');

async function reapInstances() {
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());
})
);
}

before(async () => {
await reapInstances();
const [, operation] = await INSTANCE.create({
clusters: [
{
Expand All @@ -59,21 +78,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 INSTANCE.delete().catch(console.error);
});

describe('instances', () => {
Expand Down
4 changes: 2 additions & 2 deletions system-test/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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);
Expand Down
Loading