15
15
16
16
'use strict' ;
17
17
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' ) ;
23
21
const uuid = require ( 'uuid' ) ;
22
+ const path = require ( 'path' ) ;
23
+ const fs = require ( 'fs' ) ;
24
24
25
25
const client = new monitoring . AlertPolicyServiceClient ( ) ;
26
26
const channelClient = new monitoring . NotificationChannelServiceClient ( ) ;
27
- const cwd = path . join ( __dirname , `..` ) ;
28
27
const projectId = process . env . GCLOUD_PROJECT ;
28
+ const cmd = 'node alerts' ;
29
+ const exec = async cmd => ( await execa . shell ( cmd ) ) . stdout ;
29
30
30
31
let policyOneName , policyTwoName , channelName ;
31
-
32
32
const testPrefix = `gcloud-test-${ uuid . v4 ( ) . split ( '-' ) [ 0 ] } ` ;
33
33
34
- before ( tools . checkCredentials ) ;
35
- before ( async ( ) => {
36
- try {
37
- tools . checkCredentials ;
34
+ describe ( 'alerts' , ( ) => {
35
+ before ( async ( ) => {
36
+ await reapPolicies ( ) ;
38
37
let results = await client . createAlertPolicy ( {
39
38
name : client . projectPath ( projectId ) ,
40
39
alertPolicy : {
@@ -48,7 +47,8 @@ before(async () => {
48
47
{
49
48
displayName : 'Condition 1' ,
50
49
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"' ,
52
52
aggregations : [
53
53
{
54
54
alignmentPeriod : {
@@ -79,7 +79,8 @@ before(async () => {
79
79
{
80
80
displayName : 'Condition 2' ,
81
81
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"' ,
83
84
aggregations : [
84
85
{
85
86
alignmentPeriod : {
@@ -112,108 +113,102 @@ before(async () => {
112
113
} ,
113
114
} ) ;
114
115
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 ( / ^ g c l o u d - t e s t - / ) )
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
+ }
117
140
}
118
- } ) ;
119
141
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
+ }
128
150
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
+ }
135
157
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
+ } ) ;
141
163
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 , / U p d a t e d p r o j e c t s / ) ;
167
+ assert . match ( stdout , new RegExp ( policyOneName ) ) ;
168
+ } ) ;
151
169
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 , / D i s a b l e d p r o j e c t s / ) ;
175
+ assert . notMatch ( stdout , new RegExp ( policyOneName ) ) ;
176
+ assert . match ( stdout , new RegExp ( policyTwoName ) ) ;
177
+ } ) ;
162
178
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 , / E n a b l e d p r o j e c t s / ) ;
184
+ assert . notMatch ( stdout , new RegExp ( policyOneName ) ) ;
185
+ assert . match ( stdout , new RegExp ( policyTwoName ) ) ;
186
+ } ) ;
173
187
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 , / P o l i c i e s : / ) ;
191
+ assert . match ( stdout , / f i r s t - p o l i c y / ) ;
192
+ assert . match ( stdout , / T e s t / ) ;
193
+ assert . match ( stdout , / s e c o n d / ) ;
194
+ } ) ;
185
195
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 , / S a v e d p o l i c i e s t o .\/ p o l i c i e s _ b a c k u p .j s o n / ) ;
199
+ assert . ok ( fs . existsSync ( path . join ( __dirname , '../policies_backup.json' ) ) ) ;
200
+ await client . deleteAlertPolicy ( { name : policyOneName } ) ;
201
+ } ) ;
202
202
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 = / p r o j e c t s \/ [ A - Z a - z 0 - 9 - ] + \/ a l e r t P o l i c i e s \/ ( [ \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 , / L o a d i n g p o l i c i e s f r o m .\/ p o l i c i e s _ b a c k u p .j s o n / ) ;
206
+ const matches = output . match (
207
+ / p r o j e c t s \/ [ A - Z a - z 0 - 9 - ] + \/ a l e r t P o l i c i e s \/ ( [ \d ] + ) / gi
208
+ ) ;
209
+ assert . ok ( Array . isArray ( matches ) ) ;
210
+ assert ( matches . length > 1 ) ;
211
+ policyOneName = matches [ 0 ] ;
212
+ policyTwoName = matches [ 1 ] ;
213
+ } ) ;
219
214
} ) ;
0 commit comments