Skip to content

Commit 8a14ea5

Browse files
authored
Add docker-composed based test fixture for GCS (#48902)
Similarly to what has be done for Azure in #48636, this commit adds a new :test:fixtures:gcs-fixture project which provides two docker-compose based fixtures that emulate a Google Cloud Storage service. Some code has been extracted from existing tests and placed into this new project so that it can be easily reused in other projects.
1 parent 293902c commit 8a14ea5

File tree

13 files changed

+559
-1009
lines changed

13 files changed

+559
-1009
lines changed

plugins/repository-gcs/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ dependencies {
5454
compile 'io.opencensus:opencensus-api:0.18.0'
5555
compile 'io.opencensus:opencensus-contrib-http-util:0.18.0'
5656
compile 'com.google.apis:google-api-services-storage:v1-rev20190426-1.28.0'
57+
58+
testCompile project(':test:fixtures:gcs-fixture')
5759
}
5860

5961
dependencyLicenses {

plugins/repository-gcs/qa/google-cloud-storage/build.gradle

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import org.elasticsearch.gradle.MavenFilteringHack
2222
import org.elasticsearch.gradle.info.BuildParams
23-
import org.elasticsearch.gradle.test.AntFixture
2423

2524
import java.nio.file.Files
2625
import java.security.KeyPair
@@ -30,12 +29,14 @@ import static org.elasticsearch.gradle.PropertyNormalization.IGNORE_VALUE
3029

3130
apply plugin: 'elasticsearch.standalone-rest-test'
3231
apply plugin: 'elasticsearch.rest-test'
32+
apply plugin: 'elasticsearch.test.fixtures'
3333

3434
// TODO think about flattening qa:google-cloud-storage project into parent
3535
dependencies {
3636
testCompile project(path: ':plugins:repository-gcs')
3737
}
3838

39+
testFixtures.useFixture(':test:fixtures:gcs-fixture')
3940
boolean useFixture = false
4041

4142
String gcsServiceAccount = System.getenv("google_storage_service_account")
@@ -45,7 +46,7 @@ String gcsBasePath = System.getenv("google_storage_base_path")
4546
File serviceAccountFile = null
4647
if (!gcsServiceAccount && !gcsBucket && !gcsBasePath) {
4748
serviceAccountFile = new File(project.buildDir, 'generated-resources/service_account_test.json')
48-
gcsBucket = 'bucket_test'
49+
gcsBucket = 'bucket'
4950
gcsBasePath = 'integration_test'
5051
useFixture = true
5152
} else if (!gcsServiceAccount || !gcsBucket || !gcsBasePath) {
@@ -58,12 +59,11 @@ def encodedCredentials = {
5859
Base64.encoder.encodeToString(Files.readAllBytes(serviceAccountFile.toPath()))
5960
}
6061

61-
/** A task to start the GoogleCloudStorageFixture which emulates a Google Cloud Storage service **/
62-
task googleCloudStorageFixture(type: AntFixture) {
63-
dependsOn testClasses
64-
env 'CLASSPATH', "${ -> project.sourceSets.test.runtimeClasspath.asPath }"
65-
executable = "${BuildParams.runtimeJavaHome}/bin/java"
66-
args 'org.elasticsearch.repositories.gcs.GoogleCloudStorageFixture', baseDir, 'bucket_test'
62+
def fixtureAddress = { fixture ->
63+
assert useFixture : 'closure should not be used without a fixture'
64+
int ephemeralPort = project(':test:fixtures:gcs-fixture').postProcessFixture.ext."test.fixtures.${fixture}.tcp.80"
65+
assert ephemeralPort > 0
66+
'http://127.0.0.1:' + ephemeralPort
6767
}
6868

6969
/** A service account file that points to the Google Cloud Storage service emulated by the fixture **/
@@ -87,6 +87,19 @@ task createServiceAccountFile() {
8787
}
8888

8989
task thirdPartyTest (type: Test) {
90+
if (useFixture) {
91+
thirdPartyTest.dependsOn createServiceAccountFile
92+
nonInputProperties.systemProperty 'test.google.endpoint', "${ -> fixtureAddress('gcs-fixture-third-party') }"
93+
nonInputProperties.systemProperty 'test.google.tokenURI', "${ -> fixtureAddress('gcs-fixture-third-party') }/o/oauth2/token"
94+
95+
gradle.taskGraph.whenReady {
96+
if (it.hasTask(gcsThirdPartyTests)) {
97+
throw new IllegalStateException("Tried to run third party tests but not all of the necessary environment variables " +
98+
"'google_storage_service_account', 'google_storage_bucket', 'google_storage_base_path' are set.")
99+
}
100+
}
101+
}
102+
90103
include '**/GoogleCloudStorageThirdPartyTests.class'
91104
systemProperty 'tests.security.manager', false
92105
systemProperty 'test.google.bucket', gcsBucket
@@ -98,32 +111,6 @@ task gcsThirdPartyTests {
98111
dependsOn check
99112
}
100113

101-
if (useFixture) {
102-
// TODO think about running the fixture in the same JVM as tests
103-
thirdPartyTest.dependsOn createServiceAccountFile, googleCloudStorageFixture
104-
thirdPartyTest.finalizedBy googleCloudStorageFixture.getStopTask()
105-
106-
def fixtureEndpoint = {
107-
"http://${googleCloudStorageFixture.addressAndPort}"
108-
}
109-
110-
def tokenURI = {
111-
"http://${googleCloudStorageFixture.addressAndPort}/o/oauth2/token"
112-
}
113-
114-
thirdPartyTest {
115-
nonInputProperties.systemProperty 'test.google.endpoint', "${ -> fixtureEndpoint.call() }"
116-
nonInputProperties.systemProperty 'test.google.tokenURI', "${ -> tokenURI.call() }"
117-
}
118-
119-
gradle.taskGraph.whenReady {
120-
if (it.hasTask(gcsThirdPartyTests)) {
121-
throw new IllegalStateException("Tried to run third party tests but not all of the necessary environment variables 'google_storage_service_account', " +
122-
"'google_storage_bucket', 'google_storage_base_path' are set.")
123-
}
124-
}
125-
}
126-
127114
integTest.mustRunAfter(thirdPartyTest)
128115
check.dependsOn thirdPartyTest
129116

@@ -147,10 +134,10 @@ testClusters.integTest {
147134
keystore 'gcs.client.integration_test.credentials_file', serviceAccountFile, IGNORE_VALUE
148135

149136
if (useFixture) {
150-
tasks.integTest.dependsOn createServiceAccountFile, googleCloudStorageFixture
137+
tasks.integTest.dependsOn createServiceAccountFile
151138
/* Use a closure on the string to delay evaluation until tests are executed */
152-
setting 'gcs.client.integration_test.endpoint', { "http://${googleCloudStorageFixture.addressAndPort}" }, IGNORE_VALUE
153-
setting 'gcs.client.integration_test.token_uri', { "http://${googleCloudStorageFixture.addressAndPort}/o/oauth2/token" }, IGNORE_VALUE
139+
setting 'gcs.client.integration_test.endpoint', { "${ -> fixtureAddress('gcs-fixture') }" }, IGNORE_VALUE
140+
setting 'gcs.client.integration_test.token_uri', { "${ -> fixtureAddress('gcs-fixture') }/o/oauth2/token" }, IGNORE_VALUE
154141
} else {
155142
println "Using an external service to test the repository-gcs plugin"
156143
}

0 commit comments

Comments
 (0)