make forbiddenApisTest work for client and client-sniffer

This commit is contained in:
javanna 2016-06-09 23:44:34 +02:00 committed by Luca Cavanna
parent cf6e713d77
commit f38ce72004
5 changed files with 66 additions and 86 deletions

View File

@ -51,14 +51,10 @@ forbiddenApisMain {
signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')] signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')]
} }
//TODO would be nice to just exclude the classes where we use com.sun.net.httpserver.* classes forbiddenApisTest {
//excludes don't seem to work though and we don't want to have our own @SuppressForbidden
forbiddenApisTest.enabled=false
//forbiddenApisTest {
//client does not depend on core, so only jdk signatures should be checked //client does not depend on core, so only jdk signatures should be checked
//signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')] signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')]
//} }
//JarHell is part of es core, which we don't want to pull in //JarHell is part of es core, which we don't want to pull in
jarHell.enabled=false jarHell.enabled=false

View File

@ -31,6 +31,7 @@ import org.apache.http.Consts;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.SuppressForbidden;
import org.elasticsearch.client.Response; import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClient;
@ -54,6 +55,7 @@ import java.util.Set;
import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
@SuppressForbidden(reason = "uses sun HttpServer")
public class HostsSnifferTests extends LuceneTestCase { public class HostsSnifferTests extends LuceneTestCase {
private int sniffRequestTimeout; private int sniffRequestTimeout;
@ -109,13 +111,26 @@ public class HostsSnifferTests extends LuceneTestCase {
} }
} }
private static HttpServer createHttpServer(final SniffResponse sniffResponse, final int sniffTimeout) throws IOException { private static HttpServer createHttpServer(final SniffResponse sniffResponse, final int sniffTimeoutMillis) throws IOException {
HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0); HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
httpServer.createContext("/_nodes/http", new HttpHandler() { httpServer.createContext("/_nodes/http", new ResponseHandler(sniffTimeoutMillis, sniffResponse));
return httpServer;
}
@SuppressForbidden(reason = "uses sun HttpServer")
private static class ResponseHandler implements HttpHandler {
private final int sniffTimeoutMillis;
private final SniffResponse sniffResponse;
ResponseHandler(int sniffTimeoutMillis, SniffResponse sniffResponse) {
this.sniffTimeoutMillis = sniffTimeoutMillis;
this.sniffResponse = sniffResponse;
}
@Override @Override
public void handle(HttpExchange httpExchange) throws IOException { public void handle(HttpExchange httpExchange) throws IOException {
if (httpExchange.getRequestMethod().equals(HttpGet.METHOD_NAME)) { if (httpExchange.getRequestMethod().equals(HttpGet.METHOD_NAME)) {
if (httpExchange.getRequestURI().getRawQuery().equals("timeout=" + sniffTimeout + "ms")) { if (httpExchange.getRequestURI().getRawQuery().equals("timeout=" + sniffTimeoutMillis + "ms")) {
String nodesInfoBody = sniffResponse.nodesInfoBody; String nodesInfoBody = sniffResponse.nodesInfoBody;
httpExchange.sendResponseHeaders(sniffResponse.nodesInfoResponseCode, nodesInfoBody.length()); httpExchange.sendResponseHeaders(sniffResponse.nodesInfoResponseCode, nodesInfoBody.length());
try (OutputStream out = httpExchange.getResponseBody()) { try (OutputStream out = httpExchange.getResponseBody()) {
@ -127,8 +142,6 @@ public class HostsSnifferTests extends LuceneTestCase {
httpExchange.sendResponseHeaders(404, 0); httpExchange.sendResponseHeaders(404, 0);
httpExchange.close(); httpExchange.close();
} }
});
return httpServer;
} }
private static SniffResponse buildSniffResponse(HostsSniffer.Scheme scheme) throws IOException { private static SniffResponse buildSniffResponse(HostsSniffer.Scheme scheme) throws IOException {

View File

@ -49,14 +49,9 @@ forbiddenApisMain {
signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')] signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')]
} }
//TODO would be nice to just exclude the classes where we use com.sun.net.httpserver.* classes
//excludes don't seem to work though and we don't want to have our own @SuppressForbidden
forbiddenApisTest.enabled=false
forbiddenApisTest { forbiddenApisTest {
//client does not depend on core, so only jdk signatures should be checked //client does not depend on core, so only jdk signatures should be checked
signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')] signaturesURLs = [PrecommitTasks.getResource('/forbidden/jdk-signatures.txt')]
suppressAnnotations = ['**.SuppressForbidden']
} }
//JarHell is part of es core, which we don't want to pull in //JarHell is part of es core, which we don't want to pull in

View File

@ -32,6 +32,7 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.SuppressForbidden;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@ -83,7 +84,17 @@ public class RestClientIntegTests extends LuceneTestCase {
} }
private static void createStatusCodeContext(HttpServer httpServer, final int statusCode) { private static void createStatusCodeContext(HttpServer httpServer, final int statusCode) {
httpServer.createContext("/" + statusCode, new HttpHandler() { httpServer.createContext("/" + statusCode, new ResponseHandler(statusCode));
}
@SuppressForbidden(reason = "uses sun HttpServer")
private static class ResponseHandler implements HttpHandler {
private final int statusCode;
ResponseHandler(int statusCode) {
this.statusCode = statusCode;
}
@Override @Override
public void handle(HttpExchange httpExchange) throws IOException { public void handle(HttpExchange httpExchange) throws IOException {
StringBuilder body = new StringBuilder(); StringBuilder body = new StringBuilder();
@ -108,7 +119,6 @@ public class RestClientIntegTests extends LuceneTestCase {
} }
httpExchange.close(); httpExchange.close();
} }
});
} }
@AfterClass @AfterClass

View File

@ -1,34 +0,0 @@
/*
* 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.
*/
package org.elasticsearch.client;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation to suppress forbidden-apis errors inside a whole class, a method, or a field.
*/
@Retention(RetentionPolicy.CLASS)
@Target({ ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE })
public @interface SuppressForbidden {
String reason();
}