Skip to content

Commit

Permalink
Add RemoteLoggingHelper and integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed Jul 15, 2016
1 parent e3321a7 commit 91fc21a
Show file tree
Hide file tree
Showing 6 changed files with 703 additions and 0 deletions.
25 changes: 25 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,31 @@ You can test against an in-memory local DNS by following these steps:

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

### Testing code that uses Logging

Currently, there isn't an emulator for Stackdriver Logging, so an alternative is to create a test
project. `RemoteLoggingHelper` contains convenience methods to make setting up the test project
easier. To use this class, follow the steps below:

1. Create a test Google Cloud project.

2. Download a [JSON service account credentials file][create-service-account] from the Google
Developer's Console.

3. Create a `RemoteLoggingHelper` object using your project ID and JSON key. Here is an example that
uses the `RemoteLoggingHelper` to create a metric.
```java
RemoteLoggingHelper loggingHelper =
RemoteLoggingHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
Logging logging = loggingHelper.options().service();
// Pick a name for the resource with low probability of clashing
String metricName = RemoteLoggingHelper.formatForTest("test-metric");
MetricInfo metricInfo = MetricInfo.of(name, "logName:syslog");
Metric metric = logging.create(metricInfo);
```

4. Run your tests.

### Testing code that uses Storage

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:
Expand Down
5 changes: 5 additions & 0 deletions gcloud-java-logging/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<site.installationModule>gcloud-java-logging</site.installationModule>
</properties>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-tcnative-boringssl-static</artifactId>
<version>1.1.33.Fork17</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>gcloud-java-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* 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.testing;

import com.google.cloud.AuthCredentials;
import com.google.cloud.RetryParams;
import com.google.cloud.logging.LoggingOptions;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Utility to create a remote logging configuration for testing. Logging options can be obtained via
* the {@link #options()} method. Returned options have custom {@link LoggingOptions#retryParams()}:
* {@link RetryParams#maxRetryDelayMillis()} is {@code 30000},
* {@link RetryParams#totalRetryPeriodMillis()} is {@code 120000} and
* {@link RetryParams#initialRetryDelayMillis()} is {@code 250}.
* {@link LoggingOptions#initialTimeout()} is set to 60000, {@link LoggingOptions#maxTimeout()} is
* set to {@code 240000} and {@link LoggingOptions#timeoutMultiplier()} is set to {@code 1.5}.
*/
public class RemoteLoggingHelper {

private static final Logger log = Logger.getLogger(RemoteLoggingHelper.class.getName());
private final LoggingOptions options;

private RemoteLoggingHelper(LoggingOptions options) {
this.options = options;
}

/**
* Returns a {@link LoggingOptions} object to be used for testing.
*/
public LoggingOptions options() {
return options;
}

/**
* Creates a {@code RemoteLoggingHelper} object for the given project id and JSON key input
* stream.
*
* @param projectId id of the project to be used for running the tests
* @param keyStream input stream for a JSON key
* @return A {@code RemoteLoggingHelper} object for the provided options
* @throws com.google.cloud.logging.testing.RemoteLoggingHelper.LoggingHelperException if
* {@code keyStream} is not a valid JSON key stream
*/
public static RemoteLoggingHelper create(String projectId, InputStream keyStream)
throws LoggingHelperException {
try {
LoggingOptions storageOptions = LoggingOptions.builder()
.authCredentials(AuthCredentials.createForJson(keyStream))
.projectId(projectId)
.retryParams(retryParams())
.initialTimeout(60000)
.maxTimeout(120000)
.timeoutMultiplier(1.5)
.build();
return new RemoteLoggingHelper(storageOptions);
} catch (IOException ex) {
if (log.isLoggable(Level.WARNING)) {
log.log(Level.WARNING, ex.getMessage());
}
throw LoggingHelperException.translate(ex);
}
}

/**
* Creates a {@code RemoteLoggingHelper} object using default project id and authentication
* credentials.
*/
public static RemoteLoggingHelper create() throws LoggingHelperException {
LoggingOptions loggingOptions = LoggingOptions.builder()
.retryParams(retryParams())
.initialTimeout(60000)
.maxTimeout(240000)
.timeoutMultiplier(1.5)
.build();
return new RemoteLoggingHelper(loggingOptions);
}

/**
* Formats a resource name for testing purpose. This method appends a random UUID to the provided
* name.
*/
public static String formatForTest(String name) {
return name + "-" + UUID.randomUUID().toString();
}

private static RetryParams retryParams() {
return RetryParams.builder()
.maxRetryDelayMillis(30000)
.totalRetryPeriodMillis(120000)
.initialRetryDelayMillis(250)
.build();
}

public static class LoggingHelperException extends RuntimeException {

private static final long serialVersionUID = 2617749404172557196L;

public LoggingHelperException(String message, Throwable cause) {
super(message, cause);
}

public static LoggingHelperException translate(Exception ex) {
return new LoggingHelperException(ex.getMessage(), ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* 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.
*/

/**
* A testing helper for Stackdriver Logging.
*
* <p>A simple usage example:
*
* <p>Before the test:
* <pre> {@code
* RemoteLoggingHelper helper = RemoteLoggingHelper.create();
* Logging logging = helper.options().service();
* } </pre>
*
* <p>Format resource names to avoid name clashes:
* <pre> {@code
* String metricName = RemoteLoggingHelper.formatForTest("test-metric");
* } </pre>
*
* @see <a href="https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md#testing-code-that-uses-logging">
* gcloud-java tools for testing</a>
*/
package com.google.cloud.logging.testing;
Loading

0 comments on commit 91fc21a

Please sign in to comment.