Skip to content

Commit 37d0371

Browse files
committed
Move repository-azure fixture test to QA project (#30253)
Similarly to what has been done in for the repository-s3 plugin, this pull request moves the fixture test into a dedicated repository-azure/qa/microsoft-azure-storage project. It also exposes some environment variables which allows to execute the integration tests against the real Azure Storage service. When the environment variables are not defined, the integration tests are executed using the fixture added in #29347. Closes #29349
1 parent d237402 commit 37d0371

File tree

8 files changed

+875
-88
lines changed

8 files changed

+875
-88
lines changed

plugins/repository-azure/build.gradle

+7-6
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ thirdPartyAudit.excludes = [
4242
'org.slf4j.LoggerFactory',
4343
]
4444

45+
check {
46+
// also execute the QA tests when testing the plugin
47+
dependsOn 'qa:microsoft-azure-storage:check'
48+
}
49+
4550
integTestCluster {
46-
setting 'cloud.azure.storage.my_account_test.account', 'cloudazureresource'
47-
setting 'cloud.azure.storage.my_account_test.key', 'abcdefgh'
48-
keystoreSetting 'azure.client.default.account', 'cloudazureresource'
49-
keystoreSetting 'azure.client.default.key', 'abcdefgh'
50-
keystoreSetting 'azure.client.secondary.account', 'cloudazureresource'
51-
keystoreSetting 'azure.client.secondary.key', 'abcdefgh'
51+
keystoreSetting 'azure.client.integration_test.account', 'azure_account'
52+
keystoreSetting 'azure.client.integration_test.key', 'azure_key'
5253
}

plugins/repository-azure/qa/build.gradle

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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-azure', configuration: 'runtime')
28+
}
29+
30+
integTestCluster {
31+
plugin ':plugins:repository-azure'
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 azureAccount = System.getenv("azure_storage_account")
43+
String azureKey = System.getenv("azure_storage_key")
44+
String azureContainer = System.getenv("azure_storage_container")
45+
String azureBasePath = System.getenv("azure_storage_base_path")
46+
47+
if (!azureAccount && !azureKey && !azureContainer && !azureBasePath) {
48+
azureAccount = 'azure_integration_test_account'
49+
azureKey = 'YXp1cmVfaW50ZWdyYXRpb25fdGVzdF9rZXk=' // The key is "azure_integration_test_key" encoded using base64
50+
azureContainer = 'container_test'
51+
azureBasePath = 'integration_test'
52+
useFixture = true
53+
}
54+
55+
/** A task to start the fixture which emulates an Azure Storage service **/
56+
task azureStorageFixture(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.azure.AzureStorageFixture', baseDir, azureContainer
61+
}
62+
63+
Map<String, Object> expansions = [
64+
'container': azureContainer,
65+
'base_path': azureBasePath
66+
]
67+
processTestResources {
68+
inputs.properties(expansions)
69+
MavenFilteringHack.filter(it, expansions)
70+
}
71+
72+
integTestCluster {
73+
keystoreSetting 'azure.client.integration_test.account', azureAccount
74+
keystoreSetting 'azure.client.integration_test.key', azureKey
75+
76+
if (useFixture) {
77+
dependsOn azureStorageFixture
78+
// Use a closure on the string to delay evaluation until tests are executed. The endpoint_suffix is used
79+
// in a hacky way to change the protocol and endpoint. We must fix that.
80+
setting 'azure.client.integration_test.endpoint_suffix',
81+
"ignored;DefaultEndpointsProtocol=http;BlobEndpoint=http://${ -> azureStorageFixture.addressAndPort }"
82+
} else {
83+
println "Using an external service to test the repository-azure plugin"
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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.azure;
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+
28+
import java.io.ByteArrayOutputStream;
29+
import java.io.IOException;
30+
import java.lang.management.ManagementFactory;
31+
import java.net.Inet6Address;
32+
import java.net.InetAddress;
33+
import java.net.InetSocketAddress;
34+
import java.net.SocketAddress;
35+
import java.nio.file.Files;
36+
import java.nio.file.Path;
37+
import java.nio.file.Paths;
38+
import java.nio.file.StandardCopyOption;
39+
import java.util.List;
40+
import java.util.Map;
41+
42+
import static java.util.Collections.singleton;
43+
import static java.util.Collections.singletonList;
44+
45+
/**
46+
* {@link AzureStorageFixture} is a fixture that emulates an Azure Storage service.
47+
* <p>
48+
* It starts an asynchronous socket server that binds to a random local port. The server parses
49+
* HTTP requests and uses a {@link AzureStorageTestServer} to handle them before returning
50+
* them to the client as HTTP responses.
51+
*/
52+
public class AzureStorageFixture {
53+
54+
public static void main(String[] args) throws Exception {
55+
if (args == null || args.length != 2) {
56+
throw new IllegalArgumentException("AzureStorageFixture <working directory> <container>");
57+
}
58+
59+
final InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
60+
final HttpServer httpServer = MockHttpServer.createHttp(socketAddress, 0);
61+
62+
try {
63+
final Path workingDirectory = workingDir(args[0]);
64+
/// Writes the PID of the current Java process in a `pid` file located in the working directory
65+
writeFile(workingDirectory, "pid", ManagementFactory.getRuntimeMXBean().getName().split("@")[0]);
66+
67+
final String addressAndPort = addressToString(httpServer.getAddress());
68+
// Writes the address and port of the http server in a `ports` file located in the working directory
69+
writeFile(workingDirectory, "ports", addressAndPort);
70+
71+
// Emulates Azure
72+
final String storageUrl = "http://" + addressAndPort;
73+
final AzureStorageTestServer testServer = new AzureStorageTestServer(storageUrl);
74+
testServer.createContainer(args[1]);
75+
76+
httpServer.createContext("/", new ResponseHandler(testServer));
77+
httpServer.start();
78+
79+
// Wait to be killed
80+
Thread.sleep(Long.MAX_VALUE);
81+
82+
} finally {
83+
httpServer.stop(0);
84+
}
85+
}
86+
87+
@SuppressForbidden(reason = "Paths#get is fine - we don't have environment here")
88+
private static Path workingDir(final String dir) {
89+
return Paths.get(dir);
90+
}
91+
92+
private static void writeFile(final Path dir, final String fileName, final String content) throws IOException {
93+
final Path tempPidFile = Files.createTempFile(dir, null, null);
94+
Files.write(tempPidFile, singleton(content));
95+
Files.move(tempPidFile, dir.resolve(fileName), StandardCopyOption.ATOMIC_MOVE);
96+
}
97+
98+
private static String addressToString(final SocketAddress address) {
99+
final InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
100+
if (inetSocketAddress.getAddress() instanceof Inet6Address) {
101+
return "[" + inetSocketAddress.getHostString() + "]:" + inetSocketAddress.getPort();
102+
} else {
103+
return inetSocketAddress.getHostString() + ":" + inetSocketAddress.getPort();
104+
}
105+
}
106+
107+
static class ResponseHandler implements HttpHandler {
108+
109+
private final AzureStorageTestServer server;
110+
111+
private ResponseHandler(final AzureStorageTestServer server) {
112+
this.server = server;
113+
}
114+
115+
@Override
116+
public void handle(HttpExchange exchange) throws IOException {
117+
String method = exchange.getRequestMethod();
118+
String path = server.getEndpoint() + exchange.getRequestURI().getRawPath();
119+
String query = exchange.getRequestURI().getRawQuery();
120+
Map<String, List<String>> headers = exchange.getRequestHeaders();
121+
ByteArrayOutputStream out = new ByteArrayOutputStream();
122+
Streams.copy(exchange.getRequestBody(), out);
123+
124+
final AzureStorageTestServer.Response response = server.handle(method, path, query, headers, out.toByteArray());
125+
126+
Map<String, List<String>> responseHeaders = exchange.getResponseHeaders();
127+
responseHeaders.put("Content-Type", singletonList(response.contentType));
128+
response.headers.forEach((k, v) -> responseHeaders.put(k, singletonList(v)));
129+
exchange.sendResponseHeaders(response.status.getStatus(), response.body.length);
130+
if (response.body.length > 0) {
131+
exchange.getResponseBody().write(response.body);
132+
}
133+
exchange.close();
134+
}
135+
}
136+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.azure;
21+
22+
import com.carrotsearch.randomizedtesting.annotations.Name;
23+
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
24+
import org.elasticsearch.common.settings.Settings;
25+
import org.elasticsearch.test.rest.ESRestTestCase;
26+
import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
27+
import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;
28+
29+
public class AzureStorageRepositoryClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase {
30+
31+
public AzureStorageRepositoryClientYamlTestSuiteIT(@Name("yaml") ClientYamlTestCandidate testCandidate) {
32+
super(testCandidate);
33+
}
34+
35+
@ParametersFactory
36+
public static Iterable<Object[]> parameters() throws Exception {
37+
return ESClientYamlSuiteTestCase.createParameters();
38+
}
39+
40+
@Override
41+
protected Settings restClientSettings() {
42+
// Give more time to repository-azure to complete the snapshot operations
43+
return Settings.builder().put(super.restClientSettings())
44+
.put(ESRestTestCase.CLIENT_RETRY_TIMEOUT, "60s")
45+
.put(ESRestTestCase.CLIENT_SOCKET_TIMEOUT, "60s")
46+
.build();
47+
}
48+
}

0 commit comments

Comments
 (0)