Skip to content

Commit c35b49c

Browse files
committed
Move repository-s3 fixture tests to QA test project (#29372)
This commit moves the repository-s3 fixture test added in #29296 in a new `repository-s3/qa/amazon-s3` project. This new project allows the REST integration tests to be executed using the real S3 service when all the required environment variables are provided. When no env var is provided, then the tests are executed using the fixture added in #29296. The REST tests located at the `repository-s3`plugin project now only verify that the plugin is correctly loaded. The REST tests have been adapted to allow a bucket name and a base path to be specified as env vars. This way it is possible to run the tests with different base paths (could be anything, like a CI job name or a branch name) without multiplicating buckets. Related to #29349
1 parent 0b10f75 commit c35b49c

File tree

13 files changed

+1001
-34
lines changed

13 files changed

+1001
-34
lines changed

plugins/repository-s3/build.gradle

+7-2
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,14 @@ test {
6464
exclude '**/*CredentialsTests.class'
6565
}
6666

67+
check {
68+
// also execute the QA tests when testing the plugin
69+
dependsOn 'qa:amazon-s3:check'
70+
}
71+
6772
integTestCluster {
68-
keystoreSetting 's3.client.default.access_key', 'myaccesskey'
69-
keystoreSetting 's3.client.default.secret_key', 'mysecretkey'
73+
keystoreSetting 's3.client.integration_test.access_key', "s3_integration_test_access_key"
74+
keystoreSetting 's3.client.integration_test.secret_key', "s3_integration_test_secret_key"
7075
}
7176

7277
thirdPartyAudit.excludes = [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import org.elasticsearch.gradle.MavenFilteringHack
21+
import org.elasticsearch.gradle.test.AntFixture
22+
23+
apply plugin: 'elasticsearch.standalone-rest-test'
24+
apply plugin: 'elasticsearch.rest-test'
25+
26+
dependencies {
27+
testCompile project(path: ':plugins:repository-s3', configuration: 'runtime')
28+
}
29+
30+
integTestCluster {
31+
plugin ':plugins:repository-s3'
32+
}
33+
34+
forbiddenApisTest {
35+
// we are using jdk-internal instead of jdk-non-portable to allow for com.sun.net.httpserver.* usage
36+
bundledSignatures -= 'jdk-non-portable'
37+
bundledSignatures += 'jdk-internal'
38+
}
39+
40+
boolean useFixture = false
41+
42+
String s3AccessKey = System.getenv("amazon_s3_access_key")
43+
String s3SecretKey = System.getenv("amazon_s3_secret_key")
44+
String s3Bucket = System.getenv("amazon_s3_bucket")
45+
String s3BasePath = System.getenv("amazon_s3_base_path")
46+
47+
if (!s3AccessKey && !s3SecretKey && !s3Bucket && !s3BasePath) {
48+
s3AccessKey = 's3_integration_test_access_key'
49+
s3SecretKey = 's3_integration_test_secret_key'
50+
s3Bucket = 'bucket_test'
51+
s3BasePath = 'integration_test'
52+
useFixture = true
53+
}
54+
55+
/** A task to start the AmazonS3Fixture which emulates a S3 service **/
56+
task s3Fixture(type: AntFixture) {
57+
dependsOn compileTestJava
58+
env 'CLASSPATH', "${ -> project.sourceSets.test.runtimeClasspath.asPath }"
59+
executable = new File(project.runtimeJavaHome, 'bin/java')
60+
args 'org.elasticsearch.repositories.s3.AmazonS3Fixture', baseDir, s3Bucket
61+
}
62+
63+
Map<String, Object> expansions = [
64+
'bucket': s3Bucket,
65+
'base_path': s3BasePath
66+
]
67+
processTestResources {
68+
inputs.properties(expansions)
69+
MavenFilteringHack.filter(it, expansions)
70+
}
71+
72+
integTestCluster {
73+
keystoreSetting 's3.client.integration_test.access_key', s3AccessKey
74+
keystoreSetting 's3.client.integration_test.secret_key', s3SecretKey
75+
76+
if (useFixture) {
77+
dependsOn s3Fixture
78+
/* Use a closure on the string to delay evaluation until tests are executed */
79+
setting 's3.client.integration_test.endpoint', "http://${-> s3Fixture.addressAndPort}"
80+
} else {
81+
println "Using an external service to test the repository-s3 plugin"
82+
}
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.repositories.s3;
20+
21+
import com.sun.net.httpserver.HttpExchange;
22+
import com.sun.net.httpserver.HttpHandler;
23+
import com.sun.net.httpserver.HttpServer;
24+
import org.elasticsearch.common.SuppressForbidden;
25+
import org.elasticsearch.common.io.Streams;
26+
import org.elasticsearch.mocksocket.MockHttpServer;
27+
import org.elasticsearch.repositories.s3.AmazonS3TestServer.Response;
28+
29+
import java.io.ByteArrayOutputStream;
30+
import java.io.IOException;
31+
import java.lang.management.ManagementFactory;
32+
import java.net.Inet6Address;
33+
import java.net.InetAddress;
34+
import java.net.InetSocketAddress;
35+
import java.net.SocketAddress;
36+
import java.nio.file.Files;
37+
import java.nio.file.Path;
38+
import java.nio.file.Paths;
39+
import java.nio.file.StandardCopyOption;
40+
import java.util.List;
41+
import java.util.Map;
42+
43+
import static java.util.Collections.singleton;
44+
import static java.util.Collections.singletonList;
45+
46+
/**
47+
* {@link AmazonS3Fixture} is a fixture that emulates a S3 service.
48+
* <p>
49+
* It starts an asynchronous socket server that binds to a random local port. The server parses
50+
* HTTP requests and uses a {@link AmazonS3TestServer} to handle them before returning
51+
* them to the client as HTTP responses.
52+
*/
53+
public class AmazonS3Fixture {
54+
55+
public static void main(String[] args) throws Exception {
56+
if (args == null || args.length != 2) {
57+
throw new IllegalArgumentException("AmazonS3Fixture <working directory> <bucket>");
58+
}
59+
60+
final InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
61+
final HttpServer httpServer = MockHttpServer.createHttp(socketAddress, 0);
62+
63+
try {
64+
final Path workingDirectory = workingDir(args[0]);
65+
/// Writes the PID of the current Java process in a `pid` file located in the working directory
66+
writeFile(workingDirectory, "pid", ManagementFactory.getRuntimeMXBean().getName().split("@")[0]);
67+
68+
final String addressAndPort = addressToString(httpServer.getAddress());
69+
// Writes the address and port of the http server in a `ports` file located in the working directory
70+
writeFile(workingDirectory, "ports", addressAndPort);
71+
72+
// Emulates S3
73+
final String storageUrl = "http://" + addressAndPort;
74+
final AmazonS3TestServer storageTestServer = new AmazonS3TestServer(storageUrl);
75+
storageTestServer.createBucket(args[1]);
76+
77+
httpServer.createContext("/", new ResponseHandler(storageTestServer));
78+
httpServer.start();
79+
80+
// Wait to be killed
81+
Thread.sleep(Long.MAX_VALUE);
82+
83+
} finally {
84+
httpServer.stop(0);
85+
}
86+
}
87+
88+
@SuppressForbidden(reason = "Paths#get is fine - we don't have environment here")
89+
private static Path workingDir(final String dir) {
90+
return Paths.get(dir);
91+
}
92+
93+
private static void writeFile(final Path dir, final String fileName, final String content) throws IOException {
94+
final Path tempPidFile = Files.createTempFile(dir, null, null);
95+
Files.write(tempPidFile, singleton(content));
96+
Files.move(tempPidFile, dir.resolve(fileName), StandardCopyOption.ATOMIC_MOVE);
97+
}
98+
99+
private static String addressToString(final SocketAddress address) {
100+
final InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
101+
if (inetSocketAddress.getAddress() instanceof Inet6Address) {
102+
return "[" + inetSocketAddress.getHostString() + "]:" + inetSocketAddress.getPort();
103+
} else {
104+
return inetSocketAddress.getHostString() + ":" + inetSocketAddress.getPort();
105+
}
106+
}
107+
108+
static class ResponseHandler implements HttpHandler {
109+
110+
private final AmazonS3TestServer storageServer;
111+
112+
private ResponseHandler(final AmazonS3TestServer storageServer) {
113+
this.storageServer = storageServer;
114+
}
115+
116+
@Override
117+
public void handle(HttpExchange exchange) throws IOException {
118+
String method = exchange.getRequestMethod();
119+
String path = storageServer.getEndpoint() + exchange.getRequestURI().getRawPath();
120+
String query = exchange.getRequestURI().getRawQuery();
121+
Map<String, List<String>> headers = exchange.getRequestHeaders();
122+
ByteArrayOutputStream out = new ByteArrayOutputStream();
123+
Streams.copy(exchange.getRequestBody(), out);
124+
125+
final Response storageResponse = storageServer.handle(method, path, query, headers, out.toByteArray());
126+
127+
Map<String, List<String>> responseHeaders = exchange.getResponseHeaders();
128+
responseHeaders.put("Content-Type", singletonList(storageResponse.contentType));
129+
storageResponse.headers.forEach((k, v) -> responseHeaders.put(k, singletonList(v)));
130+
exchange.sendResponseHeaders(storageResponse.status.getStatus(), storageResponse.body.length);
131+
if (storageResponse.body.length > 0) {
132+
exchange.getResponseBody().write(storageResponse.body);
133+
}
134+
exchange.close();
135+
}
136+
}
137+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.repositories.s3;
21+
22+
import com.carrotsearch.randomizedtesting.annotations.Name;
23+
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
24+
import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
25+
import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;
26+
27+
public class AmazonS3RepositoryClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase {
28+
29+
public AmazonS3RepositoryClientYamlTestSuiteIT(@Name("yaml") ClientYamlTestCandidate testCandidate) {
30+
super(testCandidate);
31+
}
32+
33+
@ParametersFactory
34+
public static Iterable<Object[]> parameters() throws Exception {
35+
return ESClientYamlSuiteTestCase.createParameters();
36+
}
37+
}

0 commit comments

Comments
 (0)