This repository was archived by the owner on Feb 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathBaseSystemTest.java
More file actions
142 lines (123 loc) · 4.79 KB
/
BaseSystemTest.java
File metadata and controls
142 lines (123 loc) · 4.79 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
/*
* 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 com.google.api.gax.paging.Page;
import com.google.cloud.MonitoredResource;
import com.google.cloud.logging.testing.RemoteLoggingHelper;
import com.google.common.collect.Iterators;
import com.google.logging.v2.LogName;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Iterator;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
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 class BaseSystemTest {
@Rule public Timeout globalTimeout = Timeout.seconds(600);
private static DateFormat RFC_3339 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
protected static Logging logging;
@BeforeClass
public static void beforeClass() {
RemoteLoggingHelper helper = RemoteLoggingHelper.create();
logging = helper.getOptions().getService();
}
@AfterClass
public static void afterClass() throws Exception {
logging.close();
}
/**
* Creates an equality expression for logging filter.
*
* @see <a href= "https://cloud.google.com/logging/docs/view/advanced_filters">Advanced Logs
* Filters Documentation</a>
*/
protected static <V> String createEqualityFilter(String name, V value) {
return name + "=" + "\"" + value.toString() + "\"";
}
protected static boolean cleanupLog(String logName) throws InterruptedException {
int deleteAttempts = 0;
int allowedDeleteAttempts = 5;
boolean deleted = false;
while (!deleted && deleteAttempts < allowedDeleteAttempts) {
Thread.sleep(5000);
deleted = logging.deleteLog(logName);
deleteAttempts++;
}
return deleted;
}
/**
* Creates an equality expression for logging filter.
*
* @see <a href= "https://cloud.google.com/logging/docs/view/advanced_filters">Advanced Logs
* Filters Documentation</a>
*/
protected static String createTimestampFilter(int hoursAgo) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR, -1 * hoursAgo);
return "timestamp>=\"" + RFC_3339.format(calendar.getTime()) + "\"";
}
protected static String appendResourceTypeFilter(
String currentFilter, MonitoredResource[] monitoredResources) {
StringBuilder filter = new StringBuilder(currentFilter);
if (monitoredResources != null && monitoredResources.length > 0) {
if (monitoredResources.length == 1) {
filter.append(" AND resource.type=");
filter.append(monitoredResources[0].getType());
} else {
filter.append(" AND resource.type=(");
filter.append(monitoredResources[0].getType());
// OR between all monitored resources we search
for (int i = 1; i < monitoredResources.length; i++) {
filter.append(" OR ");
filter.append(monitoredResources[i].getType());
}
filter.append(")");
}
}
return filter.toString();
}
/** Helper to poll for logs until they are returned by the backend. */
protected static Iterator<LogEntry> waitForLogs(
LogName logName, MonitoredResource[] monitoredResources, int minLogs)
throws InterruptedException {
StringBuilder filter = new StringBuilder();
filter.append(createTimestampFilter(1));
filter.append(" AND ");
filter.append(createEqualityFilter("logName", logName));
String monitoredResourceFilter =
appendResourceTypeFilter(filter.toString(), monitoredResources);
Logging.EntryListOption[] options = {Logging.EntryListOption.filter(monitoredResourceFilter)};
return waitForLogs(options, minLogs);
}
protected static Iterator<LogEntry> waitForLogs(LogName logName) throws InterruptedException {
return waitForLogs(logName, null, 1);
}
protected static Iterator<LogEntry> waitForLogs(Logging.EntryListOption[] options, int minLogs)
throws InterruptedException {
Page<LogEntry> page = logging.listLogEntries(options);
while (Iterators.size(page.iterateAll().iterator()) < minLogs) {
Thread.sleep(500);
page = logging.listLogEntries(options);
}
return page.iterateAll().iterator();
}
}