-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add integration test for Ip2GeoProcessor #306
Merged
heemin32
merged 1 commit into
opensearch-project:feature/ip2geo
from
heemin32:ip2geo-integ
May 15, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/main/java/org/opensearch/geospatial/exceptions/ResourceInUseException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.geospatial.exceptions; | ||
|
||
import java.io.IOException; | ||
|
||
import org.opensearch.OpenSearchException; | ||
import org.opensearch.common.io.stream.StreamInput; | ||
import org.opensearch.rest.RestStatus; | ||
|
||
/** | ||
* Generic ResourceInUseException corresponding to the {@link RestStatus#BAD_REQUEST} status code | ||
*/ | ||
public class ResourceInUseException extends OpenSearchException { | ||
|
||
public ResourceInUseException(String msg, Object... args) { | ||
super(msg, args); | ||
} | ||
|
||
public ResourceInUseException(String msg, Throwable cause, Object... args) { | ||
super(msg, cause, args); | ||
} | ||
|
||
public ResourceInUseException(StreamInput in) throws IOException { | ||
super(in); | ||
} | ||
|
||
@Override | ||
public final RestStatus status() { | ||
return RestStatus.BAD_REQUEST; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
src/test/java/org/opensearch/geospatial/ip2geo/Ip2GeoDataServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.geospatial.ip2geo; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.InetSocketAddress; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
import lombok.SneakyThrows; | ||
import lombok.extern.log4j.Log4j2; | ||
|
||
import org.opensearch.common.SuppressForbidden; | ||
|
||
import com.sun.net.httpserver.HttpExchange; | ||
import com.sun.net.httpserver.HttpHandler; | ||
import com.sun.net.httpserver.HttpServer; | ||
|
||
/** | ||
* Simple http server to serve static files under test/java/resources/ip2geo/server for integration testing | ||
*/ | ||
@Log4j2 | ||
@SuppressForbidden(reason = "used only for testing") | ||
public class Ip2GeoDataServer { | ||
private static final String SYS_PROPERTY_KEY_CLUSTER_ENDPOINT = "tests.rest.cluster"; | ||
private static final String LOCAL_CLUSTER_ENDPOINT = "127.0.0.1"; | ||
private static final String ROOT = "ip2geo/server"; | ||
private static final int PORT = 8001; | ||
private static final String EXTERNAL_ENDPOINT_PREFIX = | ||
"https://github.com/opensearch-project/geospatial/blob/main/src/test/resources/ip2geo/server"; | ||
|
||
private static HttpServer server; | ||
private static volatile int counter = 0; | ||
private static String endpointPrefix = "http://localhost:" + PORT; | ||
private static String cityFilePath = endpointPrefix + "/city/manifest_local.json"; | ||
private static String countryFilePath = endpointPrefix + "/country/manifest_local.json"; | ||
|
||
/** | ||
* Return an endpoint to a manifest file for a sample city data | ||
* The sample data should contain three lines as follows | ||
* | ||
* cidr,city,country | ||
* 10.0.0.0/8,Seattle,USA | ||
* 127.0.0.0/12,Vancouver,Canada | ||
* fd12:2345:6789:1::/64,Bengaluru,India | ||
* | ||
*/ | ||
public static String getEndpointCity() { | ||
return cityFilePath; | ||
} | ||
|
||
/** | ||
* Return an endpoint to a manifest file for a sample country data | ||
* The sample data should contain three lines as follows | ||
* | ||
* cidr,country | ||
* 10.0.0.0/8,USA | ||
* 127.0.0.0/12,Canada | ||
* fd12:2345:6789:1::/64,India | ||
* | ||
*/ | ||
public static String getEndpointCountry() { | ||
return countryFilePath; | ||
} | ||
|
||
@SneakyThrows | ||
synchronized public static void start() { | ||
log.info("Start server is called"); | ||
// If it is remote cluster test, use external endpoint and do not launch local server | ||
if (System.getProperty(SYS_PROPERTY_KEY_CLUSTER_ENDPOINT).contains(LOCAL_CLUSTER_ENDPOINT) == false) { | ||
log.info("Remote cluster[{}] testing. Skip launching local server", System.getProperty(SYS_PROPERTY_KEY_CLUSTER_ENDPOINT)); | ||
cityFilePath = EXTERNAL_ENDPOINT_PREFIX + "/city/manifest.json"; | ||
countryFilePath = EXTERNAL_ENDPOINT_PREFIX + "/country/manifest.json"; | ||
return; | ||
} | ||
|
||
counter++; | ||
if (server != null) { | ||
log.info("Server has started already"); | ||
return; | ||
} | ||
server = HttpServer.create(new InetSocketAddress("localhost", PORT), 0); | ||
server.createContext("/", new Ip2GeoHttpHandler()); | ||
server.start(); | ||
log.info("Local file server started on port {}", PORT); | ||
} | ||
|
||
synchronized public static void stop() { | ||
log.info("Stop server is called"); | ||
if (server == null) { | ||
log.info("Server has stopped already"); | ||
return; | ||
} | ||
counter--; | ||
if (counter > 0) { | ||
log.info("[{}] processors are still using the server", counter); | ||
return; | ||
} | ||
|
||
server.stop(0); | ||
server = null; | ||
log.info("Server stopped"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as for server start, please add logging before server stop call |
||
} | ||
|
||
@SuppressForbidden(reason = "used only for testing") | ||
private static class Ip2GeoHttpHandler implements HttpHandler { | ||
@Override | ||
public void handle(final HttpExchange exchange) throws IOException { | ||
try { | ||
byte[] data = Files.readAllBytes( | ||
Paths.get(this.getClass().getClassLoader().getResource(ROOT + exchange.getRequestURI().getPath()).toURI()) | ||
); | ||
exchange.sendResponseHeaders(200, data.length); | ||
OutputStream outputStream = exchange.getResponseBody(); | ||
outputStream.write(data); | ||
outputStream.flush(); | ||
outputStream.close(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add similar log for before start event? in case server failed to start such log message can help in troubleshooting