diff --git a/monitoring/snippets/package.json b/monitoring/snippets/package.json index 6facf3c6d3..67486ca515 100644 --- a/monitoring/snippets/package.json +++ b/monitoring/snippets/package.json @@ -11,7 +11,7 @@ "node": ">=8" }, "scripts": { - "test": "mocha system-test --timeout 600000" + "test": "mocha --timeout 600000" }, "dependencies": { "@google-cloud/monitoring": "^0.7.1", @@ -19,7 +19,6 @@ }, "devDependencies": { "chai": "^4.2.0", - "execa": "^1.0.0", "mocha": "^6.0.0", "p-retry": "^4.0.0", "uuid": "^3.3.2" diff --git a/monitoring/snippets/system-test/.eslintrc.yml b/monitoring/snippets/test/.eslintrc.yml similarity index 100% rename from monitoring/snippets/system-test/.eslintrc.yml rename to monitoring/snippets/test/.eslintrc.yml diff --git a/monitoring/snippets/system-test/alerts.test.js b/monitoring/snippets/test/alerts.test.js similarity index 79% rename from monitoring/snippets/system-test/alerts.test.js rename to monitoring/snippets/test/alerts.test.js index 1810b86a8f..6ba1d3e3de 100644 --- a/monitoring/snippets/system-test/alerts.test.js +++ b/monitoring/snippets/test/alerts.test.js @@ -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]}`; @@ -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 ); diff --git a/monitoring/snippets/system-test/metrics.test.js b/monitoring/snippets/test/metrics.test.js similarity index 62% rename from monitoring/snippets/system-test/metrics.test.js rename to monitoring/snippets/test/metrics.test.js index f80e4f3c37..3c87e7dab9 100644 --- a/monitoring/snippets/system-test/metrics.test.js +++ b/monitoring/snippets/test/metrics.test.js @@ -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`; @@ -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 () => { @@ -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, @@ -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 () => { @@ -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)); }); }); }); @@ -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); }); }); @@ -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.'); }); }); @@ -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'); } }); }); diff --git a/monitoring/snippets/system-test/quickstart.test.js b/monitoring/snippets/test/quickstart.test.js similarity index 83% rename from monitoring/snippets/system-test/quickstart.test.js rename to monitoring/snippets/test/quickstart.test.js index fe570fb97c..8048b0d48c 100644 --- a/monitoring/snippets/system-test/quickstart.test.js +++ b/monitoring/snippets/test/quickstart.test.js @@ -16,9 +16,11 @@ '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. @@ -26,8 +28,8 @@ describe('quickstart', () => { // 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, diff --git a/monitoring/snippets/system-test/uptime.test.js b/monitoring/snippets/test/uptime.test.js similarity index 67% rename from monitoring/snippets/system-test/uptime.test.js rename to monitoring/snippets/test/uptime.test.js index a7505a03c9..12aa73e08d 100644 --- a/monitoring/snippets/system-test/uptime.test.js +++ b/monitoring/snippets/test/uptime.test.js @@ -16,12 +16,13 @@ 'use strict'; const {assert} = require('chai'); -const execa = require('execa'); +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); const cmd = 'node uptime.js'; const projectId = process.env.GCLOUD_PROJECT; const hostname = 'mydomain.com'; -const exec = async cmd => (await execa.shell(cmd)).stdout; function getResourceObjects(output) { const regex = new RegExp(/^\s*Resource: (.*)$/gm); @@ -35,38 +36,38 @@ function getResourceObjects(output) { describe('uptime', () => { it('should list uptime-check ips', async () => { - const output = await exec(`${cmd} list-ips`); + const output = execSync(`${cmd} list-ips`); assert.match(output, /USA/); }); let id; it('should create an uptime check', async () => { - const output = await exec(`${cmd} create ${hostname}`); + const output = execSync(`${cmd} create ${hostname}`); const matches = output.match( new RegExp(`ID: projects/${projectId}/uptimeCheckConfigs/(.+)`) ); id = matches[1]; assert.match(output, /Uptime check created:/); const resources = getResourceObjects(output); - assert.strictEqual(resources[0]['type'], 'uptime_url'); - assert.strictEqual(resources[0]['labels']['host'], hostname); + assert.include(resources[0]['type'], 'uptime_url'); + assert.include(resources[0]['labels']['host'], hostname); assert.match(output, /Display Name: My Uptime Check/); }); it('should get an uptime check', async () => { - const output = await exec(`${cmd} get ${id}`); + const output = execSync(`${cmd} get ${id}`); assert.match( output, new RegExp(`Retrieving projects/${projectId}/uptimeCheckConfigs/${id}`) ); const resources = getResourceObjects(output); - assert.strictEqual(resources[0]['type'], 'uptime_url'); - assert.strictEqual(resources[0]['labels']['host'], hostname); + assert.include(resources[0]['type'], 'uptime_url'); + assert.include(resources[0]['labels']['host'], hostname); }); it('should list uptime checks', async () => { - const output = await exec(`${cmd} list`); + const output = execSync(`${cmd} list`); const resources = getResourceObjects(output); const resourceCount = resources.filter( resource => @@ -80,32 +81,26 @@ describe('uptime', () => { it('should update an uptime check', async () => { const newDisplayName = 'My New Display'; const path = '/'; - const output = await exec( - `${cmd} update ${id} "${newDisplayName}" ${path}` - ); - assert.match( + const output = execSync(`${cmd} update ${id} "${newDisplayName}" ${path}`); + assert.include( output, - new RegExp( - `Updating projects/${projectId}/uptimeCheckConfigs/${id} to ${newDisplayName}` - ) + `Updating projects/${projectId}/uptimeCheckConfigs/${id} to ${newDisplayName}` ); - assert.match( + assert.include( output, - new RegExp( - `projects/${projectId}/uptimeCheckConfigs/${id} config updated.` - ) + `projects/${projectId}/uptimeCheckConfigs/${id} config updated.` ); }); it('should delete an uptime check', async () => { - const output = await exec(`${cmd} delete ${id}`); - assert.match( + const output = execSync(`${cmd} delete ${id}`); + assert.include( output, - new RegExp(`Deleting projects/${projectId}/uptimeCheckConfigs/${id}`) + `Deleting projects/${projectId}/uptimeCheckConfigs/${id}` ); - assert.match( + assert.include( output, - new RegExp(`projects/${projectId}/uptimeCheckConfigs/${id} deleted.`) + `projects/${projectId}/uptimeCheckConfigs/${id} deleted.` ); }); });