Skip to content

Commit 91fc21a

Browse files
committed
Add RemoteLoggingHelper and integration tests
1 parent e3321a7 commit 91fc21a

6 files changed

Lines changed: 703 additions & 0 deletions

File tree

TESTING.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,31 @@ You can test against an in-memory local DNS by following these steps:
8888

8989
This method will block until the server thread has been terminated.
9090

91+
### Testing code that uses Logging
92+
93+
Currently, there isn't an emulator for Stackdriver Logging, so an alternative is to create a test
94+
project. `RemoteLoggingHelper` contains convenience methods to make setting up the test project
95+
easier. To use this class, follow the steps below:
96+
97+
1. Create a test Google Cloud project.
98+
99+
2. Download a [JSON service account credentials file][create-service-account] from the Google
100+
Developer's Console.
101+
102+
3. Create a `RemoteLoggingHelper` object using your project ID and JSON key. Here is an example that
103+
uses the `RemoteLoggingHelper` to create a metric.
104+
```java
105+
RemoteLoggingHelper loggingHelper =
106+
RemoteLoggingHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
107+
Logging logging = loggingHelper.options().service();
108+
// Pick a name for the resource with low probability of clashing
109+
String metricName = RemoteLoggingHelper.formatForTest("test-metric");
110+
MetricInfo metricInfo = MetricInfo.of(name, "logName:syslog");
111+
Metric metric = logging.create(metricInfo);
112+
```
113+
114+
4. Run your tests.
115+
91116
### Testing code that uses Storage
92117

93118
Currently, there isn't an emulator for Google Cloud Storage, so an alternative is to create a test project. `RemoteStorageHelper` contains convenience methods to make setting up and cleaning up the test project easier. To use this class, follow the steps below:

gcloud-java-logging/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
<site.installationModule>gcloud-java-logging</site.installationModule>
1818
</properties>
1919
<dependencies>
20+
<dependency>
21+
<groupId>io.netty</groupId>
22+
<artifactId>netty-tcnative-boringssl-static</artifactId>
23+
<version>1.1.33.Fork17</version>
24+
</dependency>
2025
<dependency>
2126
<groupId>${project.groupId}</groupId>
2227
<artifactId>gcloud-java-core</artifactId>
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.logging.testing;
18+
19+
import com.google.cloud.AuthCredentials;
20+
import com.google.cloud.RetryParams;
21+
import com.google.cloud.logging.LoggingOptions;
22+
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.util.UUID;
26+
import java.util.logging.Level;
27+
import java.util.logging.Logger;
28+
29+
/**
30+
* Utility to create a remote logging configuration for testing. Logging options can be obtained via
31+
* the {@link #options()} method. Returned options have custom {@link LoggingOptions#retryParams()}:
32+
* {@link RetryParams#maxRetryDelayMillis()} is {@code 30000},
33+
* {@link RetryParams#totalRetryPeriodMillis()} is {@code 120000} and
34+
* {@link RetryParams#initialRetryDelayMillis()} is {@code 250}.
35+
* {@link LoggingOptions#initialTimeout()} is set to 60000, {@link LoggingOptions#maxTimeout()} is
36+
* set to {@code 240000} and {@link LoggingOptions#timeoutMultiplier()} is set to {@code 1.5}.
37+
*/
38+
public class RemoteLoggingHelper {
39+
40+
private static final Logger log = Logger.getLogger(RemoteLoggingHelper.class.getName());
41+
private final LoggingOptions options;
42+
43+
private RemoteLoggingHelper(LoggingOptions options) {
44+
this.options = options;
45+
}
46+
47+
/**
48+
* Returns a {@link LoggingOptions} object to be used for testing.
49+
*/
50+
public LoggingOptions options() {
51+
return options;
52+
}
53+
54+
/**
55+
* Creates a {@code RemoteLoggingHelper} object for the given project id and JSON key input
56+
* stream.
57+
*
58+
* @param projectId id of the project to be used for running the tests
59+
* @param keyStream input stream for a JSON key
60+
* @return A {@code RemoteLoggingHelper} object for the provided options
61+
* @throws com.google.cloud.logging.testing.RemoteLoggingHelper.LoggingHelperException if
62+
* {@code keyStream} is not a valid JSON key stream
63+
*/
64+
public static RemoteLoggingHelper create(String projectId, InputStream keyStream)
65+
throws LoggingHelperException {
66+
try {
67+
LoggingOptions storageOptions = LoggingOptions.builder()
68+
.authCredentials(AuthCredentials.createForJson(keyStream))
69+
.projectId(projectId)
70+
.retryParams(retryParams())
71+
.initialTimeout(60000)
72+
.maxTimeout(120000)
73+
.timeoutMultiplier(1.5)
74+
.build();
75+
return new RemoteLoggingHelper(storageOptions);
76+
} catch (IOException ex) {
77+
if (log.isLoggable(Level.WARNING)) {
78+
log.log(Level.WARNING, ex.getMessage());
79+
}
80+
throw LoggingHelperException.translate(ex);
81+
}
82+
}
83+
84+
/**
85+
* Creates a {@code RemoteLoggingHelper} object using default project id and authentication
86+
* credentials.
87+
*/
88+
public static RemoteLoggingHelper create() throws LoggingHelperException {
89+
LoggingOptions loggingOptions = LoggingOptions.builder()
90+
.retryParams(retryParams())
91+
.initialTimeout(60000)
92+
.maxTimeout(240000)
93+
.timeoutMultiplier(1.5)
94+
.build();
95+
return new RemoteLoggingHelper(loggingOptions);
96+
}
97+
98+
/**
99+
* Formats a resource name for testing purpose. This method appends a random UUID to the provided
100+
* name.
101+
*/
102+
public static String formatForTest(String name) {
103+
return name + "-" + UUID.randomUUID().toString();
104+
}
105+
106+
private static RetryParams retryParams() {
107+
return RetryParams.builder()
108+
.maxRetryDelayMillis(30000)
109+
.totalRetryPeriodMillis(120000)
110+
.initialRetryDelayMillis(250)
111+
.build();
112+
}
113+
114+
public static class LoggingHelperException extends RuntimeException {
115+
116+
private static final long serialVersionUID = 2617749404172557196L;
117+
118+
public LoggingHelperException(String message, Throwable cause) {
119+
super(message, cause);
120+
}
121+
122+
public static LoggingHelperException translate(Exception ex) {
123+
return new LoggingHelperException(ex.getMessage(), ex);
124+
}
125+
}
126+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* A testing helper for Stackdriver Logging.
19+
*
20+
* <p>A simple usage example:
21+
*
22+
* <p>Before the test:
23+
* <pre> {@code
24+
* RemoteLoggingHelper helper = RemoteLoggingHelper.create();
25+
* Logging logging = helper.options().service();
26+
* } </pre>
27+
*
28+
* <p>Format resource names to avoid name clashes:
29+
* <pre> {@code
30+
* String metricName = RemoteLoggingHelper.formatForTest("test-metric");
31+
* } </pre>
32+
*
33+
* @see <a href="https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md#testing-code-that-uses-logging">
34+
* gcloud-java tools for testing</a>
35+
*/
36+
package com.google.cloud.logging.testing;

0 commit comments

Comments
 (0)