Skip to content

Commit 4418ecf

Browse files
refactor: modernize and fix the sample tests (#193)
1 parent 11d433a commit 4418ecf

File tree

8 files changed

+349
-405
lines changed

8 files changed

+349
-405
lines changed

monitoring/snippets/metrics.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ async function createMetricDescriptor(projectId) {
6868
descriptor.labels.forEach(label => {
6969
console.log(` ${label.key} (${label.valueType}) - ${label.description}`);
7070
});
71-
7271
// [END monitoring_create_metric]
7372
}
7473

monitoring/snippets/package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"name": "nodejs-docs-samples-monitoring",
3-
"version": "0.0.1",
43
"private": true,
54
"license": "Apache-2.0",
65
"author": "Google Inc.",
@@ -12,17 +11,17 @@
1211
"node": ">=8"
1312
},
1413
"scripts": {
15-
"test": "mocha system-test/*.js --timeout 600000"
14+
"test": "mocha system-test --timeout 600000"
1615
},
1716
"dependencies": {
1817
"@google-cloud/monitoring": "^0.6.0",
1918
"yargs": "^12.0.0"
2019
},
2120
"devDependencies": {
22-
"@google-cloud/nodejs-repo-tools": "^3.0.0",
21+
"chai": "^4.2.0",
22+
"execa": "^1.0.0",
2323
"mocha": "^5.0.0",
24-
"proxyquire": "^2.0.1",
25-
"sinon": "^7.0.0",
24+
"p-retry": "^3.0.0",
2625
"uuid": "^3.3.2"
2726
}
2827
}

monitoring/snippets/quickstart.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
// [START monitoring_quickstart]
1919
// Imports the Google Cloud client library
2020
const monitoring = require('@google-cloud/monitoring');
21-
async function quickStart() {
21+
22+
async function quickstart() {
2223
// Your Google Cloud Platform project ID
23-
const projectId = 'YOUR_PROJECT_ID';
24+
const projectId = process.env.GCLOUD_PROJECT || 'YOUR_PROJECT_ID';
2425

2526
// Creates a client
2627
const client = new monitoring.MetricServiceClient();
@@ -65,6 +66,6 @@ async function quickStart() {
6566
const [result] = await client.createTimeSeries(request);
6667
console.log(`Done writing time series data.`, result);
6768
}
68-
69-
quickStart().catch(console.error);
7069
// [END monitoring_quickstart]
70+
71+
quickstart().catch(console.error);
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
---
22
env:
33
mocha: true
4-
rules:
5-
node/no-unpublished-require: off

monitoring/snippets/system-test/alerts.test.js

Lines changed: 102 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,25 @@
1515

1616
'use strict';
1717

18-
const fs = require(`fs`);
19-
const monitoring = require(`@google-cloud/monitoring`);
20-
const path = require(`path`);
21-
const assert = require('assert');
22-
const tools = require(`@google-cloud/nodejs-repo-tools`);
18+
const monitoring = require('@google-cloud/monitoring');
19+
const {assert} = require('chai');
20+
const execa = require('execa');
2321
const uuid = require('uuid');
22+
const path = require('path');
23+
const fs = require('fs');
2424

2525
const client = new monitoring.AlertPolicyServiceClient();
2626
const channelClient = new monitoring.NotificationChannelServiceClient();
27-
const cwd = path.join(__dirname, `..`);
2827
const projectId = process.env.GCLOUD_PROJECT;
28+
const cmd = 'node alerts';
29+
const exec = async cmd => (await execa.shell(cmd)).stdout;
2930

3031
let policyOneName, policyTwoName, channelName;
31-
3232
const testPrefix = `gcloud-test-${uuid.v4().split('-')[0]}`;
3333

34-
before(tools.checkCredentials);
35-
before(async () => {
36-
try {
37-
tools.checkCredentials;
34+
describe('alerts', () => {
35+
before(async () => {
36+
await reapPolicies();
3837
let results = await client.createAlertPolicy({
3938
name: client.projectPath(projectId),
4039
alertPolicy: {
@@ -48,7 +47,8 @@ before(async () => {
4847
{
4948
displayName: 'Condition 1',
5049
conditionAbsent: {
51-
filter: `resource.type = "cloud_function" AND metric.type = "cloudfunctions.googleapis.com/function/execution_count"`,
50+
filter:
51+
'resource.type = "cloud_function" AND metric.type = "cloudfunctions.googleapis.com/function/execution_count"',
5252
aggregations: [
5353
{
5454
alignmentPeriod: {
@@ -79,7 +79,8 @@ before(async () => {
7979
{
8080
displayName: 'Condition 2',
8181
conditionAbsent: {
82-
filter: `resource.type = "cloud_function" AND metric.type = "cloudfunctions.googleapis.com/function/execution_count"`,
82+
filter:
83+
'resource.type = "cloud_function" AND metric.type = "cloudfunctions.googleapis.com/function/execution_count"',
8384
aggregations: [
8485
{
8586
alignmentPeriod: {
@@ -112,108 +113,102 @@ before(async () => {
112113
},
113114
});
114115
channelName = results[0].name;
115-
} catch (err) {
116-
// ignore error
116+
});
117+
118+
/**
119+
* Delete any policies created by a test that's older than 2 minutes.
120+
*/
121+
async function reapPolicies() {
122+
const [policies] = await client.listAlertPolicies({
123+
name: client.projectPath(projectId),
124+
});
125+
const crustyPolicies = policies
126+
.filter(p => p.displayName.match(/^gcloud-test-/))
127+
.filter(p => {
128+
const minutesOld =
129+
(Date.now() - p.creationRecord.mutateTime.seconds * 1000) / 1000 / 60;
130+
return minutesOld > 2;
131+
});
132+
// This is serial on purpose. When trying to delete all alert policies in
133+
// parallel, all of the promises return successful, but then only 2?
134+
// get deleted. Super, super bizarre.
135+
// https://github.com/googleapis/nodejs-monitoring/issues/192
136+
for (const p of crustyPolicies) {
137+
console.log(`\tReaping ${p.name}...`);
138+
await client.deleteAlertPolicy({name: p.name});
139+
}
117140
}
118-
});
119141

120-
async function deletePolicies() {
121-
await client.deleteAlertPolicy({
122-
name: policyOneName,
123-
});
124-
await client.deleteAlertPolicy({
125-
name: policyTwoName,
126-
});
127-
}
142+
async function deletePolicies() {
143+
await client.deleteAlertPolicy({
144+
name: policyOneName,
145+
});
146+
await client.deleteAlertPolicy({
147+
name: policyTwoName,
148+
});
149+
}
128150

129-
async function deleteChannels() {
130-
await channelClient.deleteNotificationChannel({
131-
name: channelName,
132-
force: true,
133-
});
134-
}
151+
async function deleteChannels() {
152+
await channelClient.deleteNotificationChannel({
153+
name: channelName,
154+
force: true,
155+
});
156+
}
135157

136-
after(async () => {
137-
await deletePolicies();
138-
// has to be done after policies are deleted
139-
await deleteChannels();
140-
});
158+
after(async () => {
159+
await deletePolicies();
160+
// has to be done after policies are deleted
161+
await deleteChannels();
162+
});
141163

142-
it(`should replace notification channels`, async () => {
143-
const results = await tools.spawnAsyncWithIO(
144-
`node`,
145-
[`alerts.js`, `replace`, policyOneName, channelName],
146-
cwd
147-
);
148-
assert.strictEqual(results.output.includes('Updated projects'), true);
149-
assert.strictEqual(results.output.includes(policyOneName), true);
150-
});
164+
it('should replace notification channels', async () => {
165+
const stdout = await exec(`${cmd} replace ${policyOneName} ${channelName}`);
166+
assert.match(stdout, /Updated projects/);
167+
assert.match(stdout, new RegExp(policyOneName));
168+
});
151169

152-
it(`should disable policies`, async () => {
153-
const results = await tools.spawnAsyncWithIO(
154-
`node`,
155-
[`alerts.js`, `disable`, projectId, `'display_name.size < 28'`],
156-
cwd
157-
);
158-
assert.strictEqual(results.output.includes('Disabled projects'), true);
159-
assert.strictEqual(results.output.includes(policyOneName), false);
160-
assert.strictEqual(results.output.includes(policyTwoName), true);
161-
});
170+
it('should disable policies', async () => {
171+
const stdout = await exec(
172+
`${cmd} disable ${projectId} 'display_name.size < 28'`
173+
);
174+
assert.match(stdout, /Disabled projects/);
175+
assert.notMatch(stdout, new RegExp(policyOneName));
176+
assert.match(stdout, new RegExp(policyTwoName));
177+
});
162178

163-
it(`should enable policies`, async () => {
164-
const results = await tools.spawnAsyncWithIO(
165-
`node`,
166-
[`alerts.js`, `enable`, projectId, `'display_name.size < 28'`],
167-
cwd
168-
);
169-
assert.strictEqual(results.output.includes('Enabled projects'), true);
170-
assert.strictEqual(results.output.includes(policyOneName), false);
171-
assert.strictEqual(results.output.includes(policyTwoName), true);
172-
});
179+
it('should enable policies', async () => {
180+
const stdout = await exec(
181+
`${cmd} enable ${projectId} 'display_name.size < 28'`
182+
);
183+
assert.match(stdout, /Enabled projects/);
184+
assert.notMatch(stdout, new RegExp(policyOneName));
185+
assert.match(stdout, new RegExp(policyTwoName));
186+
});
173187

174-
it(`should list policies`, async () => {
175-
const results = await tools.spawnAsyncWithIO(
176-
`node`,
177-
[`alerts.js`, `list`, projectId],
178-
cwd
179-
);
180-
assert.strictEqual(results.output.includes('Policies:'), true);
181-
assert.strictEqual(results.output.includes('first-policy'), true);
182-
assert.strictEqual(results.output.includes('Test'), true);
183-
assert.strictEqual(results.output.includes('second'), true);
184-
});
188+
it('should list policies', async () => {
189+
const stdout = await exec(`${cmd} list ${projectId}`);
190+
assert.match(stdout, /Policies:/);
191+
assert.match(stdout, /first-policy/);
192+
assert.match(stdout, /Test/);
193+
assert.match(stdout, /second/);
194+
});
185195

186-
it(`should backup all policies`, async () => {
187-
const results = await tools.spawnAsyncWithIO(
188-
`node`,
189-
[`alerts.js`, `backup`, projectId],
190-
cwd
191-
);
192-
assert.strictEqual(
193-
results.output.includes('Saved policies to ./policies_backup.json'),
194-
true
195-
);
196-
assert.strictEqual(
197-
fs.existsSync(path.join(cwd, `policies_backup.json`)),
198-
true
199-
);
200-
await client.deleteAlertPolicy({name: policyOneName});
201-
});
196+
it('should backup all policies', async () => {
197+
const output = await exec(`${cmd} backup ${projectId}`);
198+
assert.match(output, /Saved policies to .\/policies_backup.json/);
199+
assert.ok(fs.existsSync(path.join(__dirname, '../policies_backup.json')));
200+
await client.deleteAlertPolicy({name: policyOneName});
201+
});
202202

203-
it(`should restore policies`, async () => {
204-
const results = await tools.spawnAsyncWithIO(
205-
`node`,
206-
[`alerts.js`, `restore`, projectId],
207-
cwd
208-
);
209-
assert.strictEqual(
210-
results.output.includes('Loading policies from ./policies_backup.json'),
211-
true
212-
);
213-
const nameRegexp = /projects\/[A-Za-z0-9-]+\/alertPolicies\/([\d]+)/gi;
214-
const matches = results.output.match(nameRegexp);
215-
assert.strictEqual(Array.isArray(matches), true);
216-
assert(matches.length > 1);
217-
policyOneName = matches[0];
218-
policyTwoName = matches[1];
203+
it('should restore policies', async () => {
204+
const output = await exec(`${cmd} restore ${projectId}`);
205+
assert.match(output, /Loading policies from .\/policies_backup.json/);
206+
const matches = output.match(
207+
/projects\/[A-Za-z0-9-]+\/alertPolicies\/([\d]+)/gi
208+
);
209+
assert.ok(Array.isArray(matches));
210+
assert(matches.length > 1);
211+
policyOneName = matches[0];
212+
policyTwoName = matches[1];
213+
});
219214
});

0 commit comments

Comments
 (0)