|
| 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 | +} |
0 commit comments