Skip to content

Commit 5b36928

Browse files
committed
---
yaml --- r: 6343 b: refs/heads/tswast-patch-1 c: f24b2bf h: refs/heads/master i: 6341: 9544233 6339: ae99293 6335: a353f43
1 parent 0c5d8d8 commit 5b36928

2 files changed

Lines changed: 157 additions & 1 deletion

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5757
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5858
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5959
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
60-
refs/heads/tswast-patch-1: 3c71bf2adf08dbd0f7983edd2fde45eef964f14c
60+
refs/heads/tswast-patch-1: f24b2bf3808a74aff13c5cb8223b5d3b649f7646
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* Copyright 2015 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.storage;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertTrue;
21+
22+
import com.google.common.collect.ImmutableList;
23+
import com.google.gcloud.storage.testing.RemoteGcsHelper;
24+
25+
import java.io.ByteArrayInputStream;
26+
import java.io.InputStream;
27+
import java.nio.file.Files;
28+
import java.nio.file.Paths;
29+
import java.util.Iterator;
30+
import java.util.List;
31+
import java.util.UUID;
32+
import java.util.concurrent.ExecutionException;
33+
import java.util.concurrent.TimeUnit;
34+
35+
import org.easymock.EasyMock;
36+
import org.junit.BeforeClass;
37+
import org.junit.Rule;
38+
import org.junit.Test;
39+
import org.junit.rules.ExpectedException;
40+
41+
public class RemoteGcsHelperTest {
42+
43+
private static final String BUCKET_NAME = "bucket-name";
44+
private static final String PROJECT_ID = "project-id";
45+
private static final String JSON_KEY = "{\n"
46+
+ " \"private_key_id\": \"somekeyid\",\n"
47+
+ " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS"
48+
+ "kAgEAAoIBAQC+K2hSuFpAdrJI\\nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHg"
49+
+ "aR\\n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\\nQP/9dJfIkIDJ9Fw9N4"
50+
+ "Bhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nknddadwkwewcVxHFhcZJO+XWf6ofLUXpRwiTZakGMn8EE1uVa2"
51+
+ "LgczOjwWHGi99MFjxSer5m9\\n1tCa3/KEGKiS/YL71JvjwX3mb+cewlkcmweBKZHM2JPTk0ZednFSpVZMtycjkbLa"
52+
+ "\\ndYOS8V85AgMBewECggEBAKksaldajfDZDV6nGqbFjMiizAKJolr/M3OQw16K6o3/\\n0S31xIe3sSlgW0+UbYlF"
53+
+ "4U8KifhManD1apVSC3csafaspP4RZUHFhtBywLO9pR5c\\nr6S5aLp+gPWFyIp1pfXbWGvc5VY/v9x7ya1VEa6rXvL"
54+
+ "sKupSeWAW4tMj3eo/64ge\\nsdaceaLYw52KeB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GZsdaksdlaksd323423d+15/UvrlRSFPNj1tWQmNKk"
66+
+ "XyRDW4IG1Oa2p\\nrALStNBx5Y9t0/LQnFI4w3aG\\n-----END PRIVATE KEY-----\\n\",\n"
67+
+ " \"client_email\": \"[email protected]\",\n"
68+
+ " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n"
69+
+ " \"type\": \"service_account\"\n"
70+
+ "}";
71+
private static final InputStream JSON_KEY_STREAM = new ByteArrayInputStream(JSON_KEY.getBytes());
72+
private static final List<BlobInfo> BLOB_LIST = ImmutableList.of(
73+
BlobInfo.builder(BUCKET_NAME, "n1").build(),
74+
BlobInfo.builder(BUCKET_NAME, "n2").build());
75+
private static final ListResult<BlobInfo> BLOB_LIST_RESULT = new ListResult<BlobInfo>() {
76+
77+
@Override
78+
public String nextPageCursor() {
79+
return "listResult";
80+
}
81+
82+
@Override
83+
public ListResult<BlobInfo> nextPage() {
84+
return null;
85+
}
86+
87+
@Override
88+
public Iterator<BlobInfo> iterator() {
89+
return BLOB_LIST.iterator();
90+
}
91+
};
92+
private static String KEY_PATH = "/does/not/exist/key." + UUID.randomUUID().toString() + ".json";
93+
94+
@Rule
95+
public ExpectedException thrown = ExpectedException.none();
96+
97+
@BeforeClass
98+
public static void beforeClass() {
99+
while (Files.exists(Paths.get(JSON_KEY))) {
100+
KEY_PATH = "/does/not/exist/key." + UUID.randomUUID().toString() + ".json";
101+
}
102+
}
103+
104+
@Test
105+
public void testForceDelete() throws InterruptedException, ExecutionException {
106+
Storage storageMock = EasyMock.createMock(Storage.class);
107+
EasyMock.expect(storageMock.list(BUCKET_NAME)).andReturn(BLOB_LIST_RESULT);
108+
for (BlobInfo info : BLOB_LIST) {
109+
EasyMock.expect(storageMock.delete(BUCKET_NAME, info.name())).andReturn(true);
110+
}
111+
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andReturn(true);
112+
EasyMock.replay(storageMock);
113+
assertTrue(RemoteGcsHelper.forceDelete(storageMock, BUCKET_NAME, 5, TimeUnit.SECONDS));
114+
EasyMock.verify(storageMock);
115+
}
116+
117+
@Test
118+
public void testForceDeleteTimeout() throws InterruptedException, ExecutionException {
119+
Storage storageMock = EasyMock.createMock(Storage.class);
120+
EasyMock.expect(storageMock.list(BUCKET_NAME)).andReturn(BLOB_LIST_RESULT);
121+
for (BlobInfo info : BLOB_LIST) {
122+
EasyMock.expect(storageMock.delete(BUCKET_NAME, info.name())).andReturn(true);
123+
}
124+
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andReturn(false);
125+
EasyMock.replay(storageMock);
126+
assertTrue(!RemoteGcsHelper.forceDelete(storageMock, BUCKET_NAME, 50, TimeUnit.MICROSECONDS));
127+
}
128+
129+
@Test
130+
public void testCreateFromStream() {
131+
RemoteGcsHelper helper = RemoteGcsHelper.create(PROJECT_ID, JSON_KEY_STREAM);
132+
StorageOptions options = helper.options();
133+
assertEquals(PROJECT_ID, options.projectId());
134+
assertEquals(60000, options.connectTimeout());
135+
assertEquals(60000, options.readTimeout());
136+
assertEquals(10, options.retryParams().getRetryMaxAttempts());
137+
assertEquals(6, options.retryParams().getRetryMinAttempts());
138+
assertEquals(30000, options.retryParams().getMaxRetryDelayMillis());
139+
assertEquals(120000, options.retryParams().getTotalRetryPeriodMillis());
140+
assertEquals(250, options.retryParams().getInitialRetryDelayMillis());
141+
}
142+
143+
@Test
144+
public void testCreateNoKey() {
145+
thrown.expect(RemoteGcsHelper.GcsHelperException.class);
146+
thrown.expectMessage(KEY_PATH + " (No such file or directory)");
147+
RemoteGcsHelper.create(PROJECT_ID, KEY_PATH);
148+
}
149+
150+
@Test
151+
public void testCreateNoKeyWithOption() {
152+
thrown.expect(RemoteGcsHelper.GcsHelperException.class);
153+
thrown.expectMessage(KEY_PATH + " not found in classpath");
154+
RemoteGcsHelper.create(PROJECT_ID, KEY_PATH, RemoteGcsHelper.Option.keyFromClassPath());
155+
}
156+
}

0 commit comments

Comments
 (0)