Skip to content

Commit 4634974

Browse files
committed
Add RemoteComputeHelperTest
1 parent 5486d53 commit 4634974

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNull;
21+
import static org.junit.Assert.assertSame;
22+
import static org.junit.Assert.assertTrue;
23+
24+
import com.google.gcloud.compute.ComputeOptions;
25+
import com.google.gcloud.compute.testing.RemoteComputeHelper.ComputeHelperException;
26+
27+
import org.junit.Test;
28+
29+
import java.io.ByteArrayInputStream;
30+
import java.io.IOException;
31+
import java.io.InputStream;
32+
import java.util.regex.Pattern;
33+
34+
public class RemoteComputeHelperTest {
35+
36+
private static final String PROJECT_ID = "project-id";
37+
private static final String JSON_KEY = "{\n"
38+
+ " \"private_key_id\": \"somekeyid\",\n"
39+
+ " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS"
40+
+ "kAgEAAoIBAQC+K2hSuFpAdrJI\\nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHg"
41+
+ "aR\\n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\\nQP/9dJfIkIDJ9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H8ruWJINllbQfcHVCX47ndLZwqv3oVFKh6pAS/vVI4dpOepP8++7y1u\\ncoOvtreXCX6XqfrWDtKIvv0vjl"
54+
+ "HBhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nkndj5uNl5SiuVxHFhcZJO+XWf6ofLUregtevZakGMn8EE1uVa"
55+
+ "2AY7eafmoU/nZPT\\n00YB0TBATdCbn/nBSuKDESkhSg9s2GEKQZG5hBmL5uCMfo09z3SfxZIhJdlerreP\\nJ7gSi"
56+
+ "dI12N+EZxYd4xIJh/HFDgp7RRO87f+WJkofMQKBgGTnClK1VMaCRbJZPriw\\nEfeFCoOX75MxKwXs6xgrw4W//AYG"
57+
+ "GUjDt83lD6AZP6tws7gJ2IwY/qP7+lyhjEqN\\nHtfPZRGFkGZsdaksdlaksd323423d+15/UvrlRSFPNj1tWQmNKk"
58+
+ "XyRDW4IG1Oa2p\\nrALStNBx5Y9t0/LQnFI4w3aG\\n-----END PRIVATE KEY-----\\n\",\n"
59+
+ " \"client_email\": \"someclientid@developer.gserviceaccount.com\",\n"
60+
+ " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n"
61+
+ " \"type\": \"service_account\"\n"
62+
+ "}";
63+
private static final InputStream JSON_KEY_STREAM = new ByteArrayInputStream(JSON_KEY.getBytes());
64+
private static final String BASE_RESOURCE_NAME_REGEX = "test-[0-9a-f]{24}-";
65+
private static final Pattern BASE_RESOURCE_NAME_PATTERN =
66+
Pattern.compile(BASE_RESOURCE_NAME_REGEX);
67+
68+
@Test
69+
public void testBaseResourceName() {
70+
String baseResourceName = RemoteComputeHelper.baseResourceName();
71+
assertTrue(BASE_RESOURCE_NAME_PATTERN.matcher(baseResourceName).matches());
72+
}
73+
74+
@Test
75+
public void testCreateFromStream() {
76+
RemoteComputeHelper helper = RemoteComputeHelper.create(PROJECT_ID, JSON_KEY_STREAM);
77+
ComputeOptions options = helper.options();
78+
assertEquals(PROJECT_ID, options.projectId());
79+
assertEquals(60000, options.connectTimeout());
80+
assertEquals(60000, options.readTimeout());
81+
assertEquals(10, options.retryParams().retryMaxAttempts());
82+
assertEquals(6, options.retryParams().retryMinAttempts());
83+
assertEquals(30000, options.retryParams().maxRetryDelayMillis());
84+
assertEquals(120000, options.retryParams().totalRetryPeriodMillis());
85+
assertEquals(250, options.retryParams().initialRetryDelayMillis());
86+
}
87+
88+
@Test
89+
public void testComputeHelperException() {
90+
ComputeHelperException exception = new ComputeHelperException("message", null);
91+
assertEquals("message", exception.getMessage());
92+
assertNull(exception.getCause());
93+
IOException cause = new IOException("message");
94+
exception = ComputeHelperException.translate(cause);
95+
assertEquals("message", exception.getMessage());
96+
assertSame(cause, exception.getCause());
97+
}
98+
}

0 commit comments

Comments
 (0)