Skip to content

Commit

Permalink
Add docker-composed based test fixture for GCS (#48902)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tlrx authored Nov 7, 2019
1 parent 293902c commit 8a14ea5
Show file tree
Hide file tree
Showing 13 changed files with 559 additions and 1,009 deletions.
2 changes: 2 additions & 0 deletions plugins/repository-gcs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ dependencies {
compile 'io.opencensus:opencensus-api:0.18.0'
compile 'io.opencensus:opencensus-contrib-http-util:0.18.0'
compile 'com.google.apis:google-api-services-storage:v1-rev20190426-1.28.0'

testCompile project(':test:fixtures:gcs-fixture')
}

dependencyLicenses {
Expand Down
61 changes: 24 additions & 37 deletions plugins/repository-gcs/qa/google-cloud-storage/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.elasticsearch.gradle.MavenFilteringHack
import org.elasticsearch.gradle.info.BuildParams
import org.elasticsearch.gradle.test.AntFixture

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

apply plugin: 'elasticsearch.standalone-rest-test'
apply plugin: 'elasticsearch.rest-test'
apply plugin: 'elasticsearch.test.fixtures'

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

testFixtures.useFixture(':test:fixtures:gcs-fixture')
boolean useFixture = false

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

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

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

task thirdPartyTest (type: Test) {
if (useFixture) {
thirdPartyTest.dependsOn createServiceAccountFile
nonInputProperties.systemProperty 'test.google.endpoint', "${ -> fixtureAddress('gcs-fixture-third-party') }"
nonInputProperties.systemProperty 'test.google.tokenURI', "${ -> fixtureAddress('gcs-fixture-third-party') }/o/oauth2/token"

gradle.taskGraph.whenReady {
if (it.hasTask(gcsThirdPartyTests)) {
throw new IllegalStateException("Tried to run third party tests but not all of the necessary environment variables " +
"'google_storage_service_account', 'google_storage_bucket', 'google_storage_base_path' are set.")
}
}
}

include '**/GoogleCloudStorageThirdPartyTests.class'
systemProperty 'tests.security.manager', false
systemProperty 'test.google.bucket', gcsBucket
Expand All @@ -98,32 +111,6 @@ task gcsThirdPartyTests {
dependsOn check
}

if (useFixture) {
// TODO think about running the fixture in the same JVM as tests
thirdPartyTest.dependsOn createServiceAccountFile, googleCloudStorageFixture
thirdPartyTest.finalizedBy googleCloudStorageFixture.getStopTask()

def fixtureEndpoint = {
"http://${googleCloudStorageFixture.addressAndPort}"
}

def tokenURI = {
"http://${googleCloudStorageFixture.addressAndPort}/o/oauth2/token"
}

thirdPartyTest {
nonInputProperties.systemProperty 'test.google.endpoint', "${ -> fixtureEndpoint.call() }"
nonInputProperties.systemProperty 'test.google.tokenURI', "${ -> tokenURI.call() }"
}

gradle.taskGraph.whenReady {
if (it.hasTask(gcsThirdPartyTests)) {
throw new IllegalStateException("Tried to run third party tests but not all of the necessary environment variables 'google_storage_service_account', " +
"'google_storage_bucket', 'google_storage_base_path' are set.")
}
}
}

integTest.mustRunAfter(thirdPartyTest)
check.dependsOn thirdPartyTest

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

if (useFixture) {
tasks.integTest.dependsOn createServiceAccountFile, googleCloudStorageFixture
tasks.integTest.dependsOn createServiceAccountFile
/* Use a closure on the string to delay evaluation until tests are executed */
setting 'gcs.client.integration_test.endpoint', { "http://${googleCloudStorageFixture.addressAndPort}" }, IGNORE_VALUE
setting 'gcs.client.integration_test.token_uri', { "http://${googleCloudStorageFixture.addressAndPort}/o/oauth2/token" }, IGNORE_VALUE
setting 'gcs.client.integration_test.endpoint', { "${ -> fixtureAddress('gcs-fixture') }" }, IGNORE_VALUE
setting 'gcs.client.integration_test.token_uri', { "${ -> fixtureAddress('gcs-fixture') }/o/oauth2/token" }, IGNORE_VALUE
} else {
println "Using an external service to test the repository-gcs plugin"
}
Expand Down
Loading

0 comments on commit 8a14ea5

Please sign in to comment.