Skip to content

Commit

Permalink
refactor: use execSync for tests (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Apr 10, 2019
1 parent 1ea9e50 commit d8ff54a
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 103 deletions.
3 changes: 1 addition & 2 deletions monitoring/snippets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
"node": ">=8"
},
"scripts": {
"test": "mocha system-test --timeout 600000"
"test": "mocha --timeout 600000"
},
"dependencies": {
"@google-cloud/monitoring": "^0.7.1",
"yargs": "^13.0.0"
},
"devDependencies": {
"chai": "^4.2.0",
"execa": "^1.0.0",
"mocha": "^6.0.0",
"p-retry": "^4.0.0",
"uuid": "^3.3.2"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@

const monitoring = require('@google-cloud/monitoring');
const {assert} = require('chai');
const execa = require('execa');
const cp = require('child_process');
const uuid = require('uuid');
const path = require('path');
const fs = require('fs');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const client = new monitoring.AlertPolicyServiceClient();
const channelClient = new monitoring.NotificationChannelServiceClient();
const projectId = process.env.GCLOUD_PROJECT;
const cmd = 'node alerts';
const exec = async cmd => (await execa.shell(cmd)).stdout;

let policyOneName, policyTwoName, channelName;
const testPrefix = `gcloud-test-${uuid.v4().split('-')[0]}`;
Expand Down Expand Up @@ -161,48 +162,48 @@ describe('alerts', () => {
await deleteChannels();
});

it('should replace notification channels', async () => {
const stdout = await exec(`${cmd} replace ${policyOneName} ${channelName}`);
assert.match(stdout, /Updated projects/);
assert.match(stdout, new RegExp(policyOneName));
it('should replace notification channels', () => {
const stdout = execSync(`${cmd} replace ${policyOneName} ${channelName}`);
assert.include(stdout, 'Updated projects');
assert.include(stdout, policyOneName);
});

it('should disable policies', async () => {
const stdout = await exec(
it('should disable policies', () => {
const stdout = execSync(
`${cmd} disable ${projectId} 'display_name.size < 28'`
);
assert.match(stdout, /Disabled projects/);
assert.notMatch(stdout, new RegExp(policyOneName));
assert.match(stdout, new RegExp(policyTwoName));
assert.include(stdout, 'Disabled projects');
assert.notInclude(stdout, policyOneName);
assert.include(stdout, policyTwoName);
});

it('should enable policies', async () => {
const stdout = await exec(
it('should enable policies', () => {
const stdout = execSync(
`${cmd} enable ${projectId} 'display_name.size < 28'`
);
assert.match(stdout, /Enabled projects/);
assert.notMatch(stdout, new RegExp(policyOneName));
assert.match(stdout, new RegExp(policyTwoName));
assert.include(stdout, 'Enabled projects');
assert.notInclude(stdout, policyOneName);
assert.include(stdout, policyTwoName);
});

it('should list policies', async () => {
const stdout = await exec(`${cmd} list ${projectId}`);
assert.match(stdout, /Policies:/);
assert.match(stdout, /first-policy/);
assert.match(stdout, /Test/);
assert.match(stdout, /second/);
it('should list policies', () => {
const stdout = execSync(`${cmd} list ${projectId}`);
assert.include(stdout, 'Policies:');
assert.include(stdout, 'first-policy');
assert.include(stdout, 'Test');
assert.include(stdout, 'second');
});

it('should backup all policies', async () => {
const output = await exec(`${cmd} backup ${projectId}`);
assert.match(output, /Saved policies to .\/policies_backup.json/);
const output = execSync(`${cmd} backup ${projectId}`);
assert.include(output, 'Saved policies to ./policies_backup.json');
assert.ok(fs.existsSync(path.join(__dirname, '../policies_backup.json')));
await client.deleteAlertPolicy({name: policyOneName});
});

it('should restore policies', async () => {
const output = await exec(`${cmd} restore ${projectId}`);
assert.match(output, /Loading policies from .\/policies_backup.json/);
it('should restore policies', () => {
const output = execSync(`${cmd} restore ${projectId}`);
assert.include(output, 'Loading policies from ./policies_backup.json');
const matches = output.match(
/projects\/[A-Za-z0-9-]+\/alertPolicies\/([\d]+)/gi
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@

const monitoring = require('@google-cloud/monitoring');
const {assert} = require('chai');
const execa = require('execa');
const cp = require('child_process');
const retry = require('p-retry');

const exec = async cmd => (await execa.shell(cmd)).stdout;
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const client = new monitoring.MetricServiceClient();
const cmd = `node metrics.js`;
const customMetricId = `custom.googleapis.com/stores/daily_sales`;
Expand All @@ -30,10 +31,10 @@ const projectId = process.env.GCLOUD_PROJECT;
const resourceId = `cloudsql_database`;

describe('metrics', () => {
it('should create a metric descriptors', async () => {
const output = await exec(`${cmd} create`);
assert.match(output, /Created custom Metric/);
assert.match(output, new RegExp(`Type: ${customMetricId}`));
it('should create a metric descriptors', () => {
const output = execSync(`${cmd} create`);
assert.include(output, 'Created custom Metric');
assert.include(output, `Type: ${customMetricId}`);
});

it('should list metric descriptors, including the new custom one', async () => {
Expand All @@ -42,9 +43,9 @@ describe('metrics', () => {
// https://github.com/googleapis/nodejs-monitoring/issues/190
await retry(
async () => {
const output = await exec(`${cmd} list`);
assert.match(output, new RegExp(customMetricId));
assert.match(output, new RegExp(computeMetricId));
const output = execSync(`${cmd} list`);
assert.include(output, customMetricId);
assert.include(output, computeMetricId);
},
{
retries: 10,
Expand All @@ -53,34 +54,32 @@ describe('metrics', () => {
);
});

it('should get a metric descriptor', async () => {
const output = await exec(`${cmd} get ${customMetricId}`);
assert.match(output, new RegExp(`Type: ${customMetricId}`));
it('should get a metric descriptor', () => {
const output = execSync(`${cmd} get ${customMetricId}`);
assert.include(output, `Type: ${customMetricId}`);
});

it('should write time series data', async () => {
const output = await exec(`${cmd} write`);
assert.match(output, /Done writing time series data./);
it('should write time series data', () => {
const output = execSync(`${cmd} write`);
assert.include(output, 'Done writing time series data.');
});

it('should delete a metric descriptor', async () => {
const output = await exec(`${cmd} delete ${customMetricId}`);
assert.match(output, new RegExp(`Deleted ${customMetricId}`));
it('should delete a metric descriptor', () => {
const output = execSync(`${cmd} delete ${customMetricId}`);
assert.include(output, `Deleted ${customMetricId}`);
});

it('should list monitored resource descriptors', async () => {
const output = await exec(`${cmd} list-resources`);
assert.match(
it('should list monitored resource descriptors', () => {
const output = execSync(`${cmd} list-resources`);
assert.include(
output,
new RegExp(
`projects/${projectId}/monitoredResourceDescriptors/${resourceId}`
)
`projects/${projectId}/monitoredResourceDescriptors/${resourceId}`
);
});

it('should get a monitored resource descriptor', async () => {
const output = await exec(`${cmd} get-resource ${resourceId}`);
assert.match(output, new RegExp(`Type: ${resourceId}`));
it('should get a monitored resource descriptor', () => {
const output = execSync(`${cmd} get-resource ${resourceId}`);
assert.include(output, `Type: ${resourceId}`);
});

it('should read time series data', async () => {
Expand All @@ -97,12 +96,12 @@ describe('metrics', () => {
},
},
});
const output = await exec(`${cmd} read '${filter}'`);
const output = execSync(`${cmd} read '${filter}'`);
//t.true(true); // Do not fail if there is simply no data to return.
timeSeries.forEach(data => {
assert.match(output, new RegExp(`${data.metric.labels.instance_name}:`));
assert.include(output, `${data.metric.labels.instance_name}:`);
data.points.forEach(point => {
assert.match(output, new RegExp(JSON.stringify(point.value)));
assert.include(output, JSON.stringify(point.value));
});
});
});
Expand All @@ -124,10 +123,10 @@ describe('metrics', () => {
// the metrics that match the filter
view: `HEADERS`,
});
const output = await exec(`${cmd} read-fields`);
assert.match(output, /Found data points for the following instances/);
const output = execSync(`${cmd} read-fields`);
assert.include(output, 'Found data points for the following instances');
timeSeries.forEach(data => {
assert.match(output, new RegExp(data.metric.labels.instance_name));
assert.include(output, data.metric.labels.instance_name);
});
});

Expand All @@ -152,12 +151,12 @@ describe('metrics', () => {
perSeriesAligner: `ALIGN_MEAN`,
},
});
const output = await exec(`${cmd} read-aggregate`);
assert.match(output, /CPU utilization:/);
const output = execSync(`${cmd} read-aggregate`);
assert.include(output, 'CPU utilization:');
timeSeries.forEach(data => {
assert.match(output, new RegExp(data.metric.labels.instance_name));
assert.match(output, / Now: 0./);
assert.match(output, / 10 min ago: 0./);
assert.include(output, data.metric.labels.instance_name);
assert.include(output, ' Now: 0.');
assert.include(output, ' 10 min ago: 0.');
});
});

Expand All @@ -183,14 +182,15 @@ describe('metrics', () => {
perSeriesAligner: `ALIGN_MEAN`,
},
});
const output = await exec(`${cmd} read-reduce`);
const output = execSync(`${cmd} read-reduce`);
// Special case: No output.
if (output === 'No data') {
assert.match(output, /No data/);
} else {
assert.match(output, /Average CPU utilization across all GCE instances:/);
assert.match(output, / {2}Last 10 min/);
assert.match(output, / {2}10-20 min ago/);
if (output.indexOf('No data') < 0) {
assert.include(
output,
'Average CPU utilization across all GCE instances:'
);
assert.include(output, 'Last 10 min');
assert.include(output, '10-20 min ago');
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@
'use strict';

const {assert} = require('chai');
const execa = require('execa');
const cp = require('child_process');
const retry = require('p-retry');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

describe('quickstart', () => {
it('should run the quickstart', async () => {
// The write in the quickstart appears to be very, very flaky.
// This should not be needed. The tracking bug is here:
// https://github.com/googleapis/nodejs-monitoring/issues/191
await retry(
async () => {
const result = await execa.shell('node quickstart');
assert.match(result.stdout, /Done writing time series data/);
const result = execSync('node quickstart');
assert.match(result, /Done writing time series data/);
},
{
retries: 10,
Expand Down
Loading

0 comments on commit d8ff54a

Please sign in to comment.