Skip to content

Commit 8b4d80a

Browse files
authored
Fix AntFixture waiting condition (#31272)
The AntFixture waiting condition is evaluated to false but it should be true.
1 parent 489db54 commit 8b4d80a

File tree

9 files changed

+98
-34
lines changed

9 files changed

+98
-34
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/test/AntFixture.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ public class AntFixture extends AntTask implements Fixture {
149149
}
150150

151151
// the process is started (has a pid) and is bound to a network interface
152-
// so now wait undil the waitCondition has been met
152+
// so now evaluates if the waitCondition is successful
153153
// TODO: change this to a loop?
154154
boolean success
155155
try {
156-
success = waitCondition(this, ant) == false
156+
success = waitCondition(this, ant)
157157
} catch (Exception e) {
158158
String msg = "Wait condition caught exception for ${name}"
159159
logger.error(msg, e)

modules/reindex/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ if (Os.isFamily(Os.FAMILY_WINDOWS)) {
121121
baseDir,
122122
unzip.temporaryDir,
123123
version == '090'
124+
waitCondition = { fixture, ant ->
125+
// the fixture writes the ports file when Elasticsearch's HTTP service
126+
// is ready, so we can just wait for the file to exist
127+
return fixture.portsFile.exists()
128+
}
124129
}
125130
integTest.dependsOn fixture
126131
integTestRunner {

modules/repository-url/src/test/java/org/elasticsearch/repositories/url/URLFixture.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.Map;
4040
import java.util.Objects;
4141

42+
import static java.nio.charset.StandardCharsets.UTF_8;
4243
import static java.util.Collections.emptyMap;
4344
import static java.util.Collections.singleton;
4445
import static java.util.Collections.singletonMap;
@@ -67,7 +68,6 @@ public static void main(String[] args) throws Exception {
6768
writeFile(workingDirectory, "ports", addressAndPort);
6869

6970
// Exposes the repository over HTTP
70-
final String url = "http://" + addressAndPort;
7171
httpServer.createContext("/", new ResponseHandler(dir(args[1])));
7272
httpServer.start();
7373

@@ -110,7 +110,13 @@ static class ResponseHandler implements HttpHandler {
110110
@Override
111111
public void handle(HttpExchange exchange) throws IOException {
112112
Response response;
113-
if ("GET".equalsIgnoreCase(exchange.getRequestMethod())) {
113+
114+
final String userAgent = exchange.getRequestHeaders().getFirst("User-Agent");
115+
if (userAgent != null && userAgent.startsWith("Apache Ant")) {
116+
// This is a request made by the AntFixture, just reply "OK"
117+
response = new Response(RestStatus.OK, emptyMap(), "text/plain; charset=utf-8", "OK".getBytes(UTF_8));
118+
119+
} else if ("GET".equalsIgnoreCase(exchange.getRequestMethod())) {
114120
String path = exchange.getRequestURI().toString();
115121
if (path.length() > 0 && path.charAt(0) == '/') {
116122
path = path.substring(1);
@@ -125,13 +131,13 @@ public void handle(HttpExchange exchange) throws IOException {
125131
Map<String, String> headers = singletonMap("Content-Length", String.valueOf(content.length));
126132
response = new Response(RestStatus.OK, headers, "application/octet-stream", content);
127133
} else {
128-
response = new Response(RestStatus.NOT_FOUND, emptyMap(), "text/plain", new byte[0]);
134+
response = new Response(RestStatus.NOT_FOUND, emptyMap(), "text/plain; charset=utf-8", new byte[0]);
129135
}
130136
} else {
131-
response = new Response(RestStatus.FORBIDDEN, emptyMap(), "text/plain", new byte[0]);
137+
response = new Response(RestStatus.FORBIDDEN, emptyMap(), "text/plain; charset=utf-8", new byte[0]);
132138
}
133139
} else {
134-
response = new Response(RestStatus.INTERNAL_SERVER_ERROR, emptyMap(), "text/plain",
140+
response = new Response(RestStatus.INTERNAL_SERVER_ERROR, emptyMap(), "text/plain; charset=utf-8",
135141
"Unsupported HTTP method".getBytes(StandardCharsets.UTF_8));
136142
}
137143
exchange.sendResponseHeaders(response.status.getStatus(), response.body.length);

plugins/examples/rest-handler/src/test/java/org/elasticsearch/example/resthandler/ExampleFixtureIT.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,41 @@
2323
import org.elasticsearch.test.ESTestCase;
2424

2525
import java.io.BufferedReader;
26+
import java.io.BufferedWriter;
2627
import java.io.InputStreamReader;
28+
import java.io.OutputStreamWriter;
2729
import java.net.InetAddress;
2830
import java.net.Socket;
2931
import java.net.URL;
3032
import java.nio.charset.StandardCharsets;
31-
import java.util.Objects;
33+
import java.util.ArrayList;
34+
import java.util.List;
35+
36+
import static org.hamcrest.Matchers.hasItems;
3237

3338
public class ExampleFixtureIT extends ESTestCase {
3439

3540
public void testExample() throws Exception {
36-
final String stringAddress = Objects.requireNonNull(System.getProperty("external.address"));
37-
final URL url = new URL("http://" + stringAddress);
41+
final String externalAddress = System.getProperty("external.address");
42+
assertNotNull("External address must not be null", externalAddress);
3843

44+
final URL url = new URL("http://" + externalAddress);
3945
final InetAddress address = InetAddress.getByName(url.getHost());
4046
try (
4147
Socket socket = new MockSocket(address, url.getPort());
48+
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8));
4249
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8))
4350
) {
44-
assertEquals("TEST", reader.readLine());
51+
writer.write("GET / HTTP/1.1\r\n");
52+
writer.write("Host: elastic.co\r\n\r\n");
53+
writer.flush();
54+
55+
final List<String> lines = new ArrayList<>();
56+
String line;
57+
while ((line = reader.readLine()) != null) {
58+
lines.add(line);
59+
}
60+
assertThat(lines, hasItems("HTTP/1.1 200 OK", "TEST"));
4561
}
4662
}
4763
}

plugins/repository-azure/qa/microsoft-azure-storage/src/test/java/org/elasticsearch/repositories/azure/AzureStorageFixture.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.elasticsearch.common.SuppressForbidden;
2525
import org.elasticsearch.common.io.Streams;
2626
import org.elasticsearch.mocksocket.MockHttpServer;
27+
import org.elasticsearch.repositories.azure.AzureStorageTestServer.Response;
28+
import org.elasticsearch.rest.RestStatus;
2729

2830
import java.io.ByteArrayOutputStream;
2931
import java.io.IOException;
@@ -39,6 +41,8 @@
3941
import java.util.List;
4042
import java.util.Map;
4143

44+
import static java.nio.charset.StandardCharsets.UTF_8;
45+
import static java.util.Collections.emptyMap;
4246
import static java.util.Collections.singleton;
4347
import static java.util.Collections.singletonList;
4448

@@ -121,7 +125,16 @@ public void handle(HttpExchange exchange) throws IOException {
121125
ByteArrayOutputStream out = new ByteArrayOutputStream();
122126
Streams.copy(exchange.getRequestBody(), out);
123127

124-
final AzureStorageTestServer.Response response = server.handle(method, path, query, headers, out.toByteArray());
128+
Response response = null;
129+
130+
final String userAgent = exchange.getRequestHeaders().getFirst("User-Agent");
131+
if (userAgent != null && userAgent.startsWith("Apache Ant")) {
132+
// This is a request made by the AntFixture, just reply "OK"
133+
response = new Response(RestStatus.OK, emptyMap(), "text/plain; charset=utf-8", "OK".getBytes(UTF_8));
134+
} else {
135+
// Otherwise simulate a S3 response
136+
response = server.handle(method, path, query, headers, out.toByteArray());
137+
}
125138

126139
Map<String, List<String>> responseHeaders = exchange.getResponseHeaders();
127140
responseHeaders.put("Content-Type", singletonList(response.contentType));

plugins/repository-gcs/qa/google-cloud-storage/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageFixture.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.core.internal.io.Streams;
2626
import org.elasticsearch.mocksocket.MockHttpServer;
2727
import org.elasticsearch.repositories.gcs.GoogleCloudStorageTestServer.Response;
28+
import org.elasticsearch.rest.RestStatus;
2829

2930
import java.io.ByteArrayOutputStream;
3031
import java.io.IOException;
@@ -40,6 +41,8 @@
4041
import java.util.List;
4142
import java.util.Map;
4243

44+
import static java.nio.charset.StandardCharsets.UTF_8;
45+
import static java.util.Collections.emptyMap;
4346
import static java.util.Collections.singleton;
4447
import static java.util.Collections.singletonList;
4548

@@ -123,7 +126,16 @@ public void handle(HttpExchange exchange) throws IOException {
123126
ByteArrayOutputStream out = new ByteArrayOutputStream();
124127
Streams.copy(exchange.getRequestBody(), out);
125128

126-
final Response storageResponse = storageServer.handle(method, path, query, headers, out.toByteArray());
129+
Response storageResponse = null;
130+
131+
final String userAgent = exchange.getRequestHeaders().getFirst("User-Agent");
132+
if (userAgent != null && userAgent.startsWith("Apache Ant")) {
133+
// This is a request made by the AntFixture, just reply "OK"
134+
storageResponse = new Response(RestStatus.OK, emptyMap(), "text/plain; charset=utf-8", "OK".getBytes(UTF_8));
135+
} else {
136+
// Otherwise simulate a S3 response
137+
storageResponse = storageServer.handle(method, path, query, headers, out.toByteArray());
138+
}
127139

128140
Map<String, List<String>> responseHeaders = exchange.getResponseHeaders();
129141
responseHeaders.put("Content-Type", singletonList(storageResponse.contentType));

plugins/repository-hdfs/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ for (String fixtureName : ['hdfsFixture', 'haHdfsFixture', 'secureHdfsFixture',
116116
dependsOn project.configurations.hdfsFixture
117117
executable = new File(project.runtimeJavaHome, 'bin/java')
118118
env 'CLASSPATH', "${ -> project.configurations.hdfsFixture.asPath }"
119+
waitCondition = { fixture, ant ->
120+
// the hdfs.MiniHDFS fixture writes the ports file when
121+
// it's ready, so we can just wait for the file to exist
122+
return fixture.portsFile.exists()
123+
}
119124

120125
final List<String> miniHDFSArgs = []
121126

plugins/repository-s3/qa/amazon-s3/src/test/java/org/elasticsearch/repositories/s3/AmazonS3Fixture.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.common.io.Streams;
2626
import org.elasticsearch.mocksocket.MockHttpServer;
2727
import org.elasticsearch.repositories.s3.AmazonS3TestServer.Response;
28+
import org.elasticsearch.rest.RestStatus;
2829

2930
import java.io.ByteArrayOutputStream;
3031
import java.io.IOException;
@@ -40,6 +41,8 @@
4041
import java.util.List;
4142
import java.util.Map;
4243

44+
import static java.nio.charset.StandardCharsets.UTF_8;
45+
import static java.util.Collections.emptyMap;
4346
import static java.util.Collections.singleton;
4447
import static java.util.Collections.singletonList;
4548

@@ -122,7 +125,16 @@ public void handle(HttpExchange exchange) throws IOException {
122125
ByteArrayOutputStream out = new ByteArrayOutputStream();
123126
Streams.copy(exchange.getRequestBody(), out);
124127

125-
final Response storageResponse = storageServer.handle(method, path, query, headers, out.toByteArray());
128+
Response storageResponse = null;
129+
130+
final String userAgent = exchange.getRequestHeaders().getFirst("User-Agent");
131+
if (userAgent != null && userAgent.startsWith("Apache Ant")) {
132+
// This is a request made by the AntFixture, just reply "OK"
133+
storageResponse = new Response(RestStatus.OK, emptyMap(), "text/plain; charset=utf-8", "OK".getBytes(UTF_8));
134+
} else {
135+
// Otherwise simulate a S3 response
136+
storageResponse = storageServer.handle(method, path, query, headers, out.toByteArray());
137+
}
126138

127139
Map<String, List<String>> responseHeaders = exchange.getResponseHeaders();
128140
responseHeaders.put("Content-Type", singletonList(storageResponse.contentType));

test/fixtures/example-fixture/src/main/java/example/ExampleTestFixture.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@
1919

2020
package example;
2121

22+
import com.sun.net.httpserver.HttpServer;
23+
2224
import java.lang.management.ManagementFactory;
2325
import java.net.Inet6Address;
2426
import java.net.InetAddress;
2527
import java.net.InetSocketAddress;
26-
import java.nio.ByteBuffer;
27-
import java.nio.channels.AsynchronousServerSocketChannel;
28-
import java.nio.channels.AsynchronousSocketChannel;
29-
import java.nio.channels.CompletionHandler;
3028
import java.nio.charset.StandardCharsets;
3129
import java.nio.file.Files;
3230
import java.nio.file.Path;
@@ -41,9 +39,9 @@ public static void main(String args[]) throws Exception {
4139
throw new IllegalArgumentException("ExampleTestFixture <logDirectory>");
4240
}
4341
Path dir = Paths.get(args[0]);
44-
AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel
45-
.open()
46-
.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
42+
43+
final InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
44+
final HttpServer httpServer = HttpServer.create(socketAddress, 0);
4745

4846
// write pid file
4947
Path tmp = Files.createTempFile(dir, null, null);
@@ -53,29 +51,26 @@ public static void main(String args[]) throws Exception {
5351

5452
// write port file
5553
tmp = Files.createTempFile(dir, null, null);
56-
InetSocketAddress bound = (InetSocketAddress) server.getLocalAddress();
54+
InetSocketAddress bound = httpServer.getAddress();
5755
if (bound.getAddress() instanceof Inet6Address) {
5856
Files.write(tmp, Collections.singleton("[" + bound.getHostString() + "]:" + bound.getPort()));
5957
} else {
6058
Files.write(tmp, Collections.singleton(bound.getHostString() + ":" + bound.getPort()));
6159
}
6260
Files.move(tmp, dir.resolve("ports"), StandardCopyOption.ATOMIC_MOVE);
6361

62+
final byte[] response = "TEST\n".getBytes(StandardCharsets.UTF_8);
63+
6464
// go time
65-
server.accept(null, new CompletionHandler<AsynchronousSocketChannel,Void>() {
66-
@Override
67-
public void completed(AsynchronousSocketChannel socket, Void attachment) {
68-
server.accept(null, this);
69-
try (AsynchronousSocketChannel ch = socket) {
70-
ch.write(ByteBuffer.wrap("TEST\n".getBytes(StandardCharsets.UTF_8))).get();
71-
} catch (Exception e) {
72-
throw new RuntimeException(e);
73-
}
65+
httpServer.createContext("/", exchange -> {
66+
try {
67+
exchange.sendResponseHeaders(200, response.length);
68+
exchange.getResponseBody().write(response);
69+
} finally {
70+
exchange.close();
7471
}
75-
76-
@Override
77-
public void failed(Throwable exc, Void attachment) {}
7872
});
73+
httpServer.start();
7974

8075
// wait forever, until you kill me
8176
Thread.sleep(Long.MAX_VALUE);

0 commit comments

Comments
 (0)