2018-04-30 13:31:14 +02:00
|
|
|
/*
|
|
|
|
* Licensed to Elasticsearch under one or more contributor
|
|
|
|
* license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright
|
|
|
|
* ownership. Elasticsearch licenses this file to you under
|
|
|
|
* the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
|
2019-07-10 15:42:28 -07:00
|
|
|
|
2018-04-30 13:31:14 +02:00
|
|
|
import org.elasticsearch.gradle.MavenFilteringHack
|
|
|
|
import org.elasticsearch.gradle.test.AntFixture
|
|
|
|
|
2019-10-31 18:21:36 +03:00
|
|
|
import java.nio.file.Files
|
2018-04-30 13:31:14 +02:00
|
|
|
import java.security.KeyPair
|
|
|
|
import java.security.KeyPairGenerator
|
|
|
|
|
2019-07-10 15:42:28 -07:00
|
|
|
import static org.elasticsearch.gradle.PropertyNormalization.IGNORE_VALUE
|
|
|
|
|
2018-04-30 13:31:14 +02:00
|
|
|
apply plugin: 'elasticsearch.standalone-rest-test'
|
|
|
|
apply plugin: 'elasticsearch.rest-test'
|
|
|
|
|
2019-10-31 18:21:36 +03:00
|
|
|
// TODO think about flattening qa:google-cloud-storage project into parent
|
|
|
|
dependencies {
|
|
|
|
testCompile project(path: ':plugins:repository-gcs')
|
|
|
|
}
|
|
|
|
|
2018-04-30 13:31:14 +02:00
|
|
|
boolean useFixture = false
|
|
|
|
|
|
|
|
String gcsServiceAccount = System.getenv("google_storage_service_account")
|
|
|
|
String gcsBucket = System.getenv("google_storage_bucket")
|
|
|
|
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'
|
|
|
|
gcsBasePath = 'integration_test'
|
|
|
|
useFixture = true
|
2019-10-31 18:21:36 +03:00
|
|
|
} else if (!gcsServiceAccount || !gcsBucket || !gcsBasePath) {
|
|
|
|
throw new IllegalArgumentException("not all options specified to run tests against external GCS service are present")
|
2018-04-30 13:31:14 +02:00
|
|
|
} else {
|
|
|
|
serviceAccountFile = new File(gcsServiceAccount)
|
2019-10-31 18:21:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
def encodedCredentials = {
|
|
|
|
Base64.encoder.encodeToString(Files.readAllBytes(serviceAccountFile.toPath()))
|
2018-04-30 13:31:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** A task to start the GoogleCloudStorageFixture which emulates a Google Cloud Storage service **/
|
|
|
|
task googleCloudStorageFixture(type: AntFixture) {
|
2018-06-14 14:09:56 +02:00
|
|
|
dependsOn testClasses
|
2018-04-30 13:31:14 +02:00
|
|
|
env 'CLASSPATH', "${ -> project.sourceSets.test.runtimeClasspath.asPath }"
|
|
|
|
executable = new File(project.runtimeJavaHome, 'bin/java')
|
|
|
|
args 'org.elasticsearch.repositories.gcs.GoogleCloudStorageFixture', baseDir, 'bucket_test'
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A service account file that points to the Google Cloud Storage service emulated by the fixture **/
|
|
|
|
task createServiceAccountFile() {
|
|
|
|
doLast {
|
|
|
|
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA")
|
|
|
|
keyPairGenerator.initialize(1024)
|
|
|
|
KeyPair keyPair = keyPairGenerator.generateKeyPair()
|
|
|
|
String encodedKey = Base64.getEncoder().encodeToString(keyPair.private.getEncoded())
|
|
|
|
|
|
|
|
serviceAccountFile.parentFile.mkdirs()
|
|
|
|
serviceAccountFile.setText("{\n" +
|
|
|
|
' "type": "service_account",\n' +
|
|
|
|
' "project_id": "integration_test",\n' +
|
|
|
|
' "private_key_id": "' + UUID.randomUUID().toString() + '",\n' +
|
|
|
|
' "private_key": "-----BEGIN PRIVATE KEY-----\\n' + encodedKey + '\\n-----END PRIVATE KEY-----\\n",\n' +
|
|
|
|
' "client_email": "integration_test@appspot.gserviceaccount.com",\n' +
|
2018-05-15 18:22:58 +03:00
|
|
|
' "client_id": "123456789101112130594"\n' +
|
2018-04-30 13:31:14 +02:00
|
|
|
'}', 'UTF-8')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 18:21:36 +03:00
|
|
|
task thirdPartyTest (type: Test) {
|
|
|
|
include '**/GoogleCloudStorageThirdPartyTests.class'
|
|
|
|
systemProperty 'tests.security.manager', false
|
|
|
|
systemProperty 'test.google.bucket', gcsBucket
|
|
|
|
systemProperty 'test.google.base', gcsBasePath + "_third_party_tests_" + project.testSeed
|
|
|
|
nonInputProperties.systemProperty 'test.google.account', "${ -> encodedCredentials.call() }"
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-04-30 13:31:14 +02:00
|
|
|
Map<String, Object> expansions = [
|
|
|
|
'bucket': gcsBucket,
|
2019-10-01 08:39:55 +02:00
|
|
|
'base_path': gcsBasePath + "_integration_tests"
|
2018-04-30 13:31:14 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
processTestResources {
|
|
|
|
inputs.properties(expansions)
|
|
|
|
MavenFilteringHack.filter(it, expansions)
|
|
|
|
}
|
|
|
|
|
2019-04-26 18:34:03 +03:00
|
|
|
integTest {
|
|
|
|
dependsOn project(':plugins:repository-gcs').bundlePlugin
|
|
|
|
}
|
|
|
|
|
|
|
|
testClusters.integTest {
|
|
|
|
plugin file(project(':plugins:repository-gcs').bundlePlugin.archiveFile)
|
|
|
|
|
2019-07-10 15:42:28 -07:00
|
|
|
keystore 'gcs.client.integration_test.credentials_file', serviceAccountFile, IGNORE_VALUE
|
2018-04-30 13:31:14 +02:00
|
|
|
|
|
|
|
if (useFixture) {
|
2019-04-26 18:34:03 +03:00
|
|
|
tasks.integTest.dependsOn createServiceAccountFile, googleCloudStorageFixture
|
2018-04-30 13:31:14 +02:00
|
|
|
/* Use a closure on the string to delay evaluation until tests are executed */
|
2019-07-10 15:42:28 -07:00
|
|
|
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
|
2018-04-30 13:31:14 +02:00
|
|
|
} else {
|
|
|
|
println "Using an external service to test the repository-gcs plugin"
|
|
|
|
}
|
|
|
|
}
|