Repository navigation
Expand file tree
/
Copy pathBaseSystemTest.java
More file actions
369 lines (348 loc) · 15.1 KB
/
Copy pathBaseSystemTest.java
File metadata and controls
369 lines (348 loc) · 15.1 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Copyright 2016 Google LLC
*
* 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.
*/
package com.google.cloud.logging;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import com.google.api.gax.paging.Page;
import com.google.cloud.MonitoredResource;
import com.google.cloud.MonitoredResourceDescriptor;
import com.google.cloud.logging.Logging.EntryListOption;
import com.google.cloud.logging.Logging.SortingField;
import com.google.cloud.logging.Logging.SortingOrder;
import com.google.cloud.logging.Payload.JsonPayload;
import com.google.cloud.logging.Payload.StringPayload;
import com.google.cloud.logging.SinkInfo.Destination.DatasetDestination;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterators;
import com.google.common.collect.Sets;
import com.google.logging.v2.LogName;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.logging.v2.ProjectLogName;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.Timeout;
/**
* A base class for system tests. This class can be extended to run system tests in different
* environments (e.g. local emulator or remote Logging service).
*/
public abstract class BaseSystemTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Rule
public Timeout globalTimeout = Timeout.seconds(300);
/**
* Returns the Logging service used to issue requests. This service can be such that it interacts
* with the remote Logging service (for integration tests) or with an emulator (for local
* testing).
*/
protected abstract Logging logging();
/**
* Formats a resource name for testing purpose. For instance, for tests against the remote
* service, it is recommended to append to the name a random or time-based seed to prevent name
* clashes.
*/
protected abstract String formatForTest(String resourceName);
/**
* Creates an equality expression for logging filter.
*
* @see <a href="https://cloud.google.com/logging/docs/view/advanced_filters">Advanced Logs
* Filters Documentation</a>
*/
static <V> String createEqualityFilter(String name, V value) {
return name + " = " + "\"" + value.toString() + "\"";
}
@Test
public void testCreateGetUpdateAndDeleteSink() {
String name = formatForTest("test-create-get-update-sink");
SinkInfo sinkInfo = SinkInfo.newBuilder(name, DatasetDestination.of("dataset"))
.setFilter("severity>=ERROR")
.setVersionFormat(SinkInfo.VersionFormat.V2)
.build();
Sink sink = logging().create(sinkInfo);
assertEquals(name, sink.getName());
assertEquals(SinkInfo.VersionFormat.V2, sink.getVersionFormat());
assertEquals("severity>=ERROR", sink.getFilter());
DatasetDestination datasetDestination = sink.getDestination();
assertEquals(logging().getOptions().getProjectId(), datasetDestination.getProject());
assertEquals("dataset", datasetDestination.getDataset());
assertEquals(sink, logging().getSink(name));
sink = sink.toBuilder()
.setFilter("severity<=ERROR")
.build()
.update();
assertEquals(name, sink.getName());
assertEquals(SinkInfo.VersionFormat.V2, sink.getVersionFormat());
assertEquals("severity<=ERROR", sink.getFilter());
assertTrue(sink.delete());
assertFalse(sink.delete());
}
@Test
public void testUpdateNonExistingSink() {
String name = formatForTest("test-update-non-existing-sink");
SinkInfo sinkInfo = SinkInfo.newBuilder(name, DatasetDestination.of("dataset"))
.setFilter("severity>=ERROR")
.setVersionFormat(SinkInfo.VersionFormat.V2)
.build();
assertNull(logging().getSink(name));
thrown.expect(LoggingException.class);
thrown.expectMessage("NOT_FOUND");
logging().update(sinkInfo);
}
@Test
public void testListSinks() throws InterruptedException {
String firstName = formatForTest("test-list-sinks-1");
String secondName = formatForTest("test-list-sinks-2");
Sink firstSink = logging().create(SinkInfo.of(firstName, DatasetDestination.of("dataset")));
Sink secondSink = logging().create(SinkInfo.of(secondName, DatasetDestination.of("dataset")));
Logging.ListOption[] options = {Logging.ListOption.pageSize(1)};
Page<Sink> sinkPage = logging().listSinks(options);
Set<Sink> sinks = Sets.newHashSet(sinkPage.iterateAll());
while (!sinks.contains(firstSink) || !sinks.contains(secondSink)) {
Thread.sleep(500);
sinks = Sets.newHashSet(logging().listSinks(options).iterateAll());
}
firstSink.delete();
secondSink.delete();
}
@Test
public void testListMonitoredResourceDescriptors() {
Iterator<MonitoredResourceDescriptor> iterator =
logging().listMonitoredResourceDescriptors(Logging.ListOption.pageSize(100)).iterateAll().iterator();
int count = 0;
while (iterator.hasNext()) {
assertNotNull(iterator.next().getType());
count += 1;
}
assertTrue(count > 0);
}
@Test
public void testCreateGetUpdateAndDeleteMetric() {
String name = formatForTest("test-create-get-update-metric");
MetricInfo metricInfo = MetricInfo.newBuilder(name, "severity>=ERROR")
.setDescription("description")
.build();
Metric metric = logging().create(metricInfo);
assertEquals(name, metric.getName());
assertEquals("severity>=ERROR", metric.getFilter());
assertEquals("description", metric.getDescription());
assertEquals(metric, logging().getMetric(name));
metric = metric.toBuilder()
.setDescription("newDescription")
.setFilter("severity>=WARNING")
.build()
.update();
assertEquals(name, metric.getName());
assertEquals("severity>=WARNING", metric.getFilter());
assertEquals("newDescription", metric.getDescription());
assertTrue(metric.delete());
assertFalse(metric.delete());
}
@Test
public void testUpdateNonExistingMetric() {
String name = formatForTest("test-update-non-existing-metric");
MetricInfo metricInfo = MetricInfo.newBuilder(name, "severity>=ERROR")
.setDescription("description")
.build();
assertNull(logging().getMetric(name));
Metric metric = logging().update(metricInfo);
assertEquals(name, metric.getName());
assertEquals("severity>=ERROR", metric.getFilter());
assertEquals("description", metric.getDescription());
assertTrue(metric.delete());
}
@Test
public void testListMetrics() throws InterruptedException {
String firstName = formatForTest("test-list-metrics-1");
String secondName = formatForTest("test-list-metrics-2");
Metric firstMetric = logging().create(MetricInfo.of(firstName, "severity>=ERROR"));
Metric secondMetric = logging().create(MetricInfo.of(secondName, "severity>=ERROR"));
Logging.ListOption[] options = {Logging.ListOption.pageSize(1)};
Page<Metric> metricPage = logging().listMetrics(options);
Set<Metric> metrics = Sets.newHashSet(metricPage.iterateAll());
while (!metrics.contains(firstMetric) || !metrics.contains(secondMetric)) {
Thread.sleep(500);
metrics = Sets.newHashSet(logging().listMetrics(options).iterateAll());
}
firstMetric.delete();
secondMetric.delete();
}
@Test
public void testWriteAndListLogEntries() throws InterruptedException {
String logId = formatForTest("test-write-log-entries-log");
LoggingOptions loggingOptions = logging().getOptions();
LogName logName = ProjectLogName.of(loggingOptions.getProjectId(), logId);
StringPayload firstPayload = StringPayload.of("stringPayload");
LogEntry firstEntry = LogEntry.newBuilder(firstPayload)
.addLabel("key1", "value1")
.setLogName(logId)
.setHttpRequest(HttpRequest.newBuilder().setStatus(500).build())
.setResource(MonitoredResource.newBuilder("global").build())
.build();
JsonPayload secondPayload =
JsonPayload.of(ImmutableMap.<String, Object>of("jsonKey", "jsonValue"));
LogEntry secondEntry = LogEntry.newBuilder(secondPayload)
.addLabel("key2", "value2")
.setLogName(logId)
.setOperation(Operation.of("operationId", "operationProducer"))
.setResource(MonitoredResource.newBuilder("cloudsql_database").build())
.build();
logging().write(ImmutableList.of(firstEntry));
logging().write(ImmutableList.of(secondEntry));
String filter = createEqualityFilter("logName", logName);
EntryListOption[] options = {EntryListOption.filter(filter), EntryListOption.pageSize(1)};
Page<LogEntry> page = logging().listLogEntries(options);
while (Iterators.size(page.iterateAll().iterator()) < 2) {
Thread.sleep(500);
page = logging().listLogEntries(options);
}
Iterator<LogEntry> iterator = page.iterateAll().iterator();
assertTrue(iterator.hasNext());
LogEntry entry = iterator.next();
assertEquals(firstPayload, entry.getPayload());
assertEquals(logId, entry.getLogName());
assertEquals(ImmutableMap.of("key1", "value1"), entry.getLabels());
assertEquals("global", entry.getResource().getType());
assertEquals(HttpRequest.newBuilder().setStatus(500).build(), entry.getHttpRequest());
assertEquals(Severity.DEFAULT, entry.getSeverity());
assertNull(entry.getOperation());
assertNotNull(entry.getInsertId());
assertNotNull(entry.getTimestamp());
assertTrue(iterator.hasNext());
entry = iterator.next();
assertEquals(secondPayload, entry.getPayload());
assertEquals(logId, entry.getLogName());
assertEquals(ImmutableMap.of("key2", "value2"), entry.getLabels());
assertEquals("cloudsql_database", entry.getResource().getType());
assertEquals(Operation.of("operationId", "operationProducer"), entry.getOperation());
assertEquals(Severity.DEFAULT, entry.getSeverity());
assertNull(entry.getHttpRequest());
assertNotNull(entry.getInsertId());
assertNotNull(entry.getTimestamp());
options = new EntryListOption[]{EntryListOption.filter(filter),
EntryListOption.sortOrder(SortingField.TIMESTAMP, SortingOrder.DESCENDING)};
page = logging().listLogEntries(options);
while (Iterators.size(page.iterateAll().iterator()) < 2) {
Thread.sleep(500);
page = logging().listLogEntries(options);
}
iterator = page.iterateAll().iterator();
Long lastTimestamp = iterator.next().getTimestamp();
while (iterator.hasNext()) {
assertTrue(iterator.next().getTimestamp() <= lastTimestamp);
}
int deleteAttempts = 0;
int allowedDeleteAttempts = 5;
boolean deleted = false;
while (!deleted && deleteAttempts < allowedDeleteAttempts) {
Thread.sleep(1000);
deleted = logging().deleteLog(logId);
deleteAttempts++;
}
assertTrue(deleted);
}
@Test
public void testDeleteNonExistingLog() {
String logId = formatForTest("test-delete-non-existing-log");
assertFalse(logging().deleteLog(logId));
}
@Test
public void testLoggingHandler() throws InterruptedException {
String logId = formatForTest("test-logging-handler");
LoggingOptions options = logging().getOptions();
LogName logName = ProjectLogName.of(options.getProjectId(), logId);
LoggingHandler handler = new LoggingHandler(logId, options);
handler.setLevel(Level.INFO);
Logger logger = Logger.getLogger(getClass().getName());
logger.addHandler(handler);
logger.setLevel(Level.INFO);
logger.info("Message");
String filter = createEqualityFilter("logName", logName);
Iterator<LogEntry> iterator =
logging().listLogEntries(EntryListOption.filter(filter)).iterateAll().iterator();
while (!iterator.hasNext()) {
Thread.sleep(500L);
iterator =
logging().listLogEntries(EntryListOption.filter(filter)).iterateAll().iterator();
}
assertTrue(iterator.hasNext());
LogEntry entry = iterator.next();
assertTrue(entry.getPayload() instanceof StringPayload);
assertTrue(entry.<StringPayload>getPayload().getData().contains("Message"));
assertEquals(logId, entry.getLogName());
assertEquals(ImmutableMap.of("levelName", "INFO",
"levelValue", String.valueOf(Level.INFO.intValue())), entry.getLabels());
assertEquals("global", entry.getResource().getType());
assertEquals(ImmutableMap.of("project_id", options.getProjectId()),
entry.getResource().getLabels());
assertNull(entry.getHttpRequest());
assertEquals(Severity.INFO, entry.getSeverity());
assertNull(entry.getOperation());
assertNotNull(entry.getInsertId());
assertNotNull(entry.getTimestamp());
assertFalse(iterator.hasNext());
logger.removeHandler(handler);
logging().deleteLog(logId);
}
@Test
public void testSyncLoggingHandler() throws InterruptedException {
String logId = formatForTest("test-sync-logging-handler");
LoggingOptions options = logging().getOptions();
LogName logName = ProjectLogName.of(options.getProjectId(), logId);
MonitoredResource resource = MonitoredResource.of("gce_instance",
ImmutableMap.of("project_id", options.getProjectId(),
"instance_id", "instance",
"zone", "us-central1-a"));
LoggingHandler handler = new LoggingHandler(logId, options, resource);
handler.setLevel(Level.WARNING);
handler.setSynchronicity(Synchronicity.SYNC);
Logger logger = Logger.getLogger(getClass().getName());
logger.addHandler(handler);
logger.setLevel(Level.WARNING);
logger.warning("Message");
String filter = createEqualityFilter("logName", logName);
Iterator<LogEntry> iterator =
logging().listLogEntries(EntryListOption.filter(filter)).iterateAll().iterator();
while (!iterator.hasNext()) {
Thread.sleep(500L);
iterator =
logging().listLogEntries(EntryListOption.filter(filter)).iterateAll().iterator();
}
assertTrue(iterator.hasNext());
LogEntry entry = iterator.next();
assertTrue(entry.getPayload() instanceof StringPayload);
assertTrue(entry.<StringPayload>getPayload().getData().contains("Message"));
assertEquals(logId, entry.getLogName());
assertEquals(ImmutableMap.of("levelName", "WARNING",
"levelValue", String.valueOf(Level.WARNING.intValue())), entry.getLabels());
assertEquals(resource, entry.getResource());
assertNull(entry.getHttpRequest());
assertEquals(Severity.WARNING, entry.getSeverity());
assertNull(entry.getOperation());
assertNotNull(entry.getInsertId());
assertNotNull(entry.getTimestamp());
assertFalse(iterator.hasNext());
logger.removeHandler(handler);
logging().deleteLog(logId);
}
}