forked from GoogleCloudPlatform/nodejs-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsinks.test.js
More file actions
126 lines (109 loc) · 3.41 KB
/
sinks.test.js
File metadata and controls
126 lines (109 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2015-2016, Google, Inc.
// 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.
'use strict';
require(`../../system-test/_setup`);
const Logging = require(`@google-cloud/logging`);
const Storage = require(`@google-cloud/storage`);
const uuid = require(`uuid`);
const program = require(`../sinks`);
const logging = Logging();
const storage = Storage();
const bucketName = `nodejs-docs-samples-test-${uuid.v4()}`;
const sinkName = `nodejs-docs-samples-test-${uuid.v4()}`;
const filter = `severity > WARNING`;
test.before(async (t) => {
await storage.createBucket(bucketName);
});
test.after.always(async (t) => {
try {
await logging.sink(sinkName).delete();
} catch (err) {} // ignore error
try {
await storage.bucket(bucketName).delete();
} catch (err) {} // ignore error
});
test.beforeEach(stubConsole);
test.afterEach.always(restoreConsole);
test.cb.serial(`should create a new sink`, (t) => {
program.createSink(sinkName, bucketName, filter, (err, sink, apiResponse) => {
t.ifError(err);
t.truthy(sink);
t.is(sink.name, sinkName);
t.not(apiResponse, undefined);
t.end();
});
});
test.cb.serial(`should get the metadata for a sink`, (t) => {
const expected = {
name: sinkName,
destination: `storage.googleapis.com/${bucketName}`,
filter: filter,
outputVersionFormat: `V2`,
writerIdentity: `serviceAccount:cloud-logs@system.gserviceaccount.com`
};
program.getSinkMetadata(sinkName, (err, metadata) => {
t.ifError(err);
for (let key in expected) {
t.is(metadata[key], expected[key]);
}
t.end();
});
});
test.serial(`should list sinks`, async (t) => {
await tryTest(async () => {
await new Promise((resolve, reject) => {
program.listSinks((err, sinks) => {
try {
t.ifError(err);
t.true(Array.isArray(sinks));
t.true(sinks.some((sink) => sink.name === sinkName));
resolve();
} catch (err) {
reject(err);
}
});
});
}).start();
});
test.cb.serial(`should update metdata for a sink`, (t) => {
const newFilter = `severity > ALERT`;
const expected = {
name: sinkName,
destination: `storage.googleapis.com/${bucketName}`,
filter: newFilter,
outputVersionFormat: `V2`,
writerIdentity: `serviceAccount:cloud-logs@system.gserviceaccount.com`
};
program.updateSink(sinkName, newFilter, (err, apiResponse) => {
t.ifError(err);
t.not(apiResponse, undefined);
program.getSinkMetadata(sinkName, (err, metadata) => {
t.ifError(err);
for (let key in expected) {
t.is(metadata[key], expected[key]);
}
t.end();
});
});
});
test.cb.serial(`should delete a sink`, (t) => {
program.deleteSink(sinkName, (err, apiResponse) => {
t.ifError(err);
t.not(apiResponse, undefined);
program.getSinkMetadata(sinkName, (err) => {
t.truthy(err);
t.is(err.code, 404);
t.end();
});
});
});