Skip to content

Commit 3eb5d26

Browse files
Andrey Ershovjkakavas
Andrey Ershov
authored andcommitted
Support fixture in repository-gcs:thirdPartyTest and fix GCS fixture (#44885)
It turns out that today :plugins:repository-gcs:thirdPartyTest can only run against real GCS. Moreover, thirdPartyTest is not a part of check, so these tests are not running on intake build. This commit addresses the issue and makes repository-gcs:thirdPartyTest work with both fixture and real GCS. To do that, except adjusting build and test itself, I had to make changes to the fixture, because previously it was ignoring BlobListOption.currentDirectory() in the list call.
1 parent f241e30 commit 3eb5d26

File tree

3 files changed

+79
-11
lines changed

3 files changed

+79
-11
lines changed

plugins/repository-gcs/build.gradle

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,49 @@ check {
125125
dependsOn 'qa:google-cloud-storage:check'
126126
}
127127

128+
test {
129+
exclude '**/GoogleCloudStorageThirdPartyTests.class'
130+
}
131+
132+
task thirdPartyTest(type: Test) {
133+
}
134+
128135
String gcsServiceAccount = System.getenv("google_storage_service_account")
129136
String gcsBucket = System.getenv("google_storage_bucket")
130137
String gcsBasePath = System.getenv("google_storage_base_path")
131138

132-
test {
133-
exclude '**/GoogleCloudStorageThirdPartyTests.class'
139+
if (!gcsServiceAccount && !gcsBucket && !gcsBasePath) {
140+
thirdPartyTest.dependsOn('qa:google-cloud-storage:createServiceAccountFile', 'qa:google-cloud-storage:googleCloudStorageFixture')
141+
thirdPartyTest.finalizedBy('qa:google-cloud-storage:googleCloudStorageFixture#stop')
142+
gcsServiceAccount = new File(project('qa:google-cloud-storage').buildDir, 'generated-resources/service_account_test.json').path
143+
gcsBucket = 'bucket_test'
144+
gcsBasePath = 'integration_test'
145+
146+
def fixtureEndpoint = {
147+
"http://${project('qa:google-cloud-storage').googleCloudStorageFixture.addressAndPort}"
148+
}
149+
150+
def tokenURI = {
151+
"http://${project('qa:google-cloud-storage').googleCloudStorageFixture.addressAndPort}/o/oauth2/token"
152+
}
153+
154+
thirdPartyTest {
155+
nonInputProperties.systemProperty 'test.google.endpoint', "${ -> fixtureEndpoint.call() }"
156+
nonInputProperties.systemProperty 'test.google.tokenURI', "${ -> tokenURI.call() }"
157+
}
158+
} else if (!gcsServiceAccount || !gcsBucket || !gcsBasePath) {
159+
throw new IllegalArgumentException("not all options specified to run thirdPartyTest against external GCS service are present")
134160
}
135161

136-
task thirdPartyTest(type: Test) {
162+
def encodedCredentials = {
163+
Base64.encoder.encodeToString(Files.readAllBytes(file(gcsServiceAccount).toPath()))
164+
}
165+
166+
thirdPartyTest {
137167
include '**/GoogleCloudStorageThirdPartyTests.class'
138-
systemProperty 'test.google.account', gcsServiceAccount ? Base64.encoder.encodeToString(Files.readAllBytes(file(gcsServiceAccount).toPath())) : ""
139-
systemProperty 'test.google.bucket', gcsBucket ? gcsBucket : ""
140-
systemProperty 'test.google.base', gcsBasePath ? gcsBasePath : "/"
168+
systemProperty 'test.google.bucket', gcsBucket
169+
systemProperty 'test.google.base', gcsBasePath
170+
nonInputProperties.systemProperty 'test.google.account', "${ -> encodedCredentials.call() }"
141171
}
142172

143-
if (gcsServiceAccount || gcsBucket || gcsBasePath) {
144-
check.dependsOn(thirdPartyTest)
145-
}
173+
check.dependsOn(thirdPartyTest)

plugins/repository-gcs/qa/google-cloud-storage/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageFixture.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@
3838
import java.util.ArrayList;
3939
import java.util.Arrays;
4040
import java.util.HashMap;
41+
import java.util.HashSet;
4142
import java.util.List;
4243
import java.util.Map;
4344
import java.util.Objects;
45+
import java.util.Set;
4446
import java.util.regex.Matcher;
4547
import java.util.regex.Pattern;
4648
import java.util.zip.GZIPInputStream;
@@ -343,18 +345,40 @@ private static PathTrie<RequestHandler> defaultHandlers(final Map<String, Bucket
343345
final XContentBuilder builder = jsonBuilder();
344346
builder.startObject();
345347
builder.field("kind", "storage#objects");
348+
final Set<String> prefixes = new HashSet<>();
346349
{
347350
builder.startArray("items");
348351

349352
final String prefixParam = request.getParam("prefix");
353+
final String delimiter = request.getParam("delimiter");
354+
350355
for (final Map.Entry<String, byte[]> object : bucket.objects.entrySet()) {
351-
if ((prefixParam != null) && (object.getKey().startsWith(prefixParam) == false)) {
356+
String objectKey = object.getKey();
357+
if ((prefixParam != null) && (objectKey.startsWith(prefixParam) == false)) {
352358
continue;
353359
}
354-
buildObjectResource(builder, bucket.name, object.getKey(), object.getValue());
360+
361+
if (Strings.isNullOrEmpty(delimiter)) {
362+
buildObjectResource(builder, bucket.name, objectKey, object.getValue());
363+
} else {
364+
int prefixLength = prefixParam.length();
365+
String rest = objectKey.substring(prefixLength);
366+
int delimiterPos;
367+
if ((delimiterPos = rest.indexOf(delimiter)) != -1) {
368+
String key = objectKey.substring(0, prefixLength + delimiterPos + 1);
369+
prefixes.add(key);
370+
} else {
371+
buildObjectResource(builder, bucket.name, objectKey, object.getValue());
372+
}
373+
}
355374
}
356375
builder.endArray();
357376
}
377+
{
378+
if (prefixes.isEmpty() == false) {
379+
builder.array("prefixes", prefixes.toArray());
380+
}
381+
}
358382
builder.endObject();
359383
return newResponse(RestStatus.OK, emptyMap(), builder);
360384
});

plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageThirdPartyTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.repositories.gcs;
2121

2222
import org.elasticsearch.action.support.master.AcknowledgedResponse;
23+
import org.elasticsearch.common.Strings;
2324
import org.elasticsearch.common.settings.MockSecureSettings;
2425
import org.elasticsearch.common.settings.SecureSettings;
2526
import org.elasticsearch.common.settings.Settings;
@@ -40,6 +41,21 @@ protected Collection<Class<? extends Plugin>> getPlugins() {
4041
return pluginList(GoogleCloudStoragePlugin.class);
4142
}
4243

44+
@Override
45+
protected Settings nodeSettings() {
46+
Settings.Builder builder = Settings.builder().put(super.nodeSettings());
47+
48+
if (Strings.isNullOrEmpty(System.getProperty("test.google.endpoint")) == false) {
49+
builder.put("gcs.client.default.endpoint", System.getProperty("test.google.endpoint"));
50+
}
51+
52+
if (Strings.isNullOrEmpty(System.getProperty("test.google.tokenURI")) == false) {
53+
builder.put("gcs.client.default.token_uri", System.getProperty("test.google.tokenURI"));
54+
}
55+
56+
return builder.build();
57+
}
58+
4359
@Override
4460
protected SecureSettings credentials() {
4561
assertThat(System.getProperty("test.google.account"), not(blankOrNullString()));

0 commit comments

Comments
 (0)