Skip to content

Commit c18730c

Browse files
committed
Add RemoteComputeHelper and integration tests
1 parent aed3535 commit c18730c

File tree

3 files changed

+526
-0
lines changed

3 files changed

+526
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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.gcloud.compute.testing;
18+
19+
import com.google.gcloud.AuthCredentials;
20+
import com.google.gcloud.RetryParams;
21+
import com.google.gcloud.compute.ComputeOptions;
22+
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.util.logging.Level;
26+
import java.util.logging.Logger;
27+
28+
/**
29+
* Utility to create a remote Compute configuration for testing. Compute options can be obtained
30+
* via the {@link #options()} method. Returned options have custom
31+
* {@link ComputeOptions#retryParams()}: {@link RetryParams#retryMaxAttempts()} is {@code 10},
32+
* {@link RetryParams#retryMinAttempts()} is {@code 6}, {@link RetryParams#maxRetryDelayMillis()} is
33+
* {@code 30000}, {@link RetryParams#totalRetryPeriodMillis()} is {@code 120000} and
34+
* {@link RetryParams#initialRetryDelayMillis()} is {@code 250}.
35+
* {@link ComputeOptions#connectTimeout()} and {@link ComputeOptions#readTimeout()} are both set to
36+
* {@code 60000}.
37+
*/
38+
public class RemoteComputeHelper {
39+
40+
private static final Logger log = Logger.getLogger(RemoteComputeHelper.class.getName());
41+
private final ComputeOptions options;
42+
43+
private RemoteComputeHelper(ComputeOptions options) {
44+
this.options = options;
45+
}
46+
47+
/**
48+
* Returns a {@link ComputeOptions} object to be used for testing.
49+
*/
50+
public ComputeOptions options() {
51+
return options;
52+
}
53+
54+
/**
55+
* Creates a {@code RemoteComputeHelper} 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 RemoteComputeHelper} object for the provided options
61+
* @throws ComputeHelperException if {@code keyStream} is not a valid JSON key stream
62+
*/
63+
public static RemoteComputeHelper create(String projectId, InputStream keyStream)
64+
throws ComputeHelperException {
65+
try {
66+
ComputeOptions computeOptions = ComputeOptions.builder()
67+
.authCredentials(AuthCredentials.createForJson(keyStream))
68+
.projectId(projectId)
69+
.retryParams(retryParams())
70+
.connectTimeout(60000)
71+
.readTimeout(60000)
72+
.build();
73+
return new RemoteComputeHelper(computeOptions);
74+
} catch (IOException ex) {
75+
if (log.isLoggable(Level.WARNING)) {
76+
log.log(Level.WARNING, ex.getMessage());
77+
}
78+
throw ComputeHelperException.translate(ex);
79+
}
80+
}
81+
82+
/**
83+
* Creates a {@code RemoteComputeHelper} object using default project id and authentication
84+
* credentials.
85+
*/
86+
public static RemoteComputeHelper create() {
87+
ComputeOptions computeOptions = ComputeOptions.builder()
88+
.retryParams(retryParams())
89+
.connectTimeout(60000)
90+
.readTimeout(60000)
91+
.build();
92+
return new RemoteComputeHelper(computeOptions);
93+
}
94+
95+
private static RetryParams retryParams() {
96+
return RetryParams.builder()
97+
.retryMaxAttempts(10)
98+
.retryMinAttempts(6)
99+
.maxRetryDelayMillis(30000)
100+
.totalRetryPeriodMillis(120000)
101+
.initialRetryDelayMillis(250)
102+
.build();
103+
}
104+
105+
public static class ComputeHelperException extends RuntimeException {
106+
107+
private static final long serialVersionUID = -5747977015007639912L;
108+
109+
public ComputeHelperException(String message, Throwable cause) {
110+
super(message, cause);
111+
}
112+
113+
public static ComputeHelperException translate(Exception ex) {
114+
return new ComputeHelperException(ex.getMessage(), ex);
115+
}
116+
}
117+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 Google Compute Engine.
19+
*
20+
* <p>A simple usage example:
21+
*
22+
* <p>Before the test:
23+
* <pre> {@code
24+
* RemoteComputeHelper computeHelper = RemoteComputeHelper.create();
25+
* Compute compute = computeHelper.options().service();
26+
* } </pre>
27+
*
28+
* @see <a href="https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md#testing-code-that-uses-compute">
29+
* gcloud-java tools for testing</a>
30+
*/
31+
package com.google.gcloud.compute.testing;

0 commit comments

Comments
 (0)