PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/hbase-server/src/test/java/org/apache/hadoop/hbase/http/HttpServerFunctionalTest.java

http://github.com/apache/hbase
Java | 272 lines | 134 code | 22 blank | 116 comment | 10 complexity | 99455254b48a731a9e7b9d667c8652ad MD5 | raw file
Possible License(s): Apache-2.0, MIT
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. 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, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.hadoop.hbase.http;
  19. import org.apache.hadoop.net.NetUtils;
  20. import org.apache.hadoop.security.authorize.AccessControlList;
  21. import org.junit.Assert;
  22. import org.apache.hadoop.conf.Configuration;
  23. import org.apache.hadoop.hbase.http.HttpServer.Builder;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.net.ServerSocket;
  28. import java.net.URI;
  29. import java.net.URL;
  30. import java.net.MalformedURLException;
  31. /**
  32. * This is a base class for functional tests of the {@link HttpServer}.
  33. * The methods are static for other classes to import statically.
  34. */
  35. public class HttpServerFunctionalTest extends Assert {
  36. /** JVM property for the webapp test dir : {@value} */
  37. public static final String TEST_BUILD_WEBAPPS = "test.build.webapps";
  38. /** expected location of the test.build.webapps dir: {@value} */
  39. private static final String BUILD_WEBAPPS_DIR = "build/test/webapps";
  40. /** name of the test webapp: {@value} */
  41. private static final String TEST = "test";
  42. /**
  43. * Create but do not start the test webapp server. The test webapp dir is
  44. * prepared/checked in advance.
  45. *
  46. * @return the server instance
  47. *
  48. * @throws IOException if a problem occurs
  49. * @throws AssertionError if a condition was not met
  50. */
  51. public static HttpServer createTestServer() throws IOException {
  52. prepareTestWebapp();
  53. return createServer(TEST);
  54. }
  55. /**
  56. * Create but do not start the test webapp server. The test webapp dir is
  57. * prepared/checked in advance.
  58. * @param conf the server configuration to use
  59. * @return the server instance
  60. *
  61. * @throws IOException if a problem occurs
  62. * @throws AssertionError if a condition was not met
  63. */
  64. public static HttpServer createTestServer(Configuration conf)
  65. throws IOException {
  66. prepareTestWebapp();
  67. return createServer(TEST, conf);
  68. }
  69. public static HttpServer createTestServer(Configuration conf, AccessControlList adminsAcl)
  70. throws IOException {
  71. prepareTestWebapp();
  72. return createServer(TEST, conf, adminsAcl);
  73. }
  74. /**
  75. * Create but do not start the test webapp server. The test webapp dir is
  76. * prepared/checked in advance.
  77. * @param conf the server configuration to use
  78. * @return the server instance
  79. *
  80. * @throws IOException if a problem occurs
  81. * @throws AssertionError if a condition was not met
  82. */
  83. public static HttpServer createTestServer(Configuration conf,
  84. String[] pathSpecs) throws IOException {
  85. prepareTestWebapp();
  86. return createServer(TEST, conf, pathSpecs);
  87. }
  88. public static HttpServer createTestServerWithSecurity(Configuration conf) throws IOException {
  89. prepareTestWebapp();
  90. return localServerBuilder(TEST).setFindPort(true).setConf(conf).setSecurityEnabled(true)
  91. // InfoServer normally sets these for us
  92. .setUsernameConfKey(HttpServer.HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_KEY)
  93. .setKeytabConfKey(HttpServer.HTTP_SPNEGO_AUTHENTICATION_KEYTAB_KEY)
  94. .build();
  95. }
  96. /**
  97. * Prepare the test webapp by creating the directory from the test properties
  98. * fail if the directory cannot be created.
  99. * @throws AssertionError if a condition was not met
  100. */
  101. protected static void prepareTestWebapp() {
  102. String webapps = System.getProperty(TEST_BUILD_WEBAPPS, BUILD_WEBAPPS_DIR);
  103. File testWebappDir = new File(webapps +
  104. File.separatorChar + TEST);
  105. try {
  106. if (!testWebappDir.exists()) {
  107. fail("Test webapp dir " + testWebappDir.getCanonicalPath() + " missing");
  108. }
  109. }
  110. catch (IOException e) {
  111. }
  112. }
  113. /**
  114. * Create an HttpServer instance on the given address for the given webapp
  115. * @param host to bind
  116. * @param port to bind
  117. * @return the server
  118. * @throws IOException if it could not be created
  119. */
  120. public static HttpServer createServer(String host, int port)
  121. throws IOException {
  122. prepareTestWebapp();
  123. return new HttpServer.Builder().setName(TEST)
  124. .addEndpoint(URI.create("http://" + host + ":" + port))
  125. .setFindPort(true).build();
  126. }
  127. /**
  128. * Create an HttpServer instance for the given webapp
  129. * @param webapp the webapp to work with
  130. * @return the server
  131. * @throws IOException if it could not be created
  132. */
  133. public static HttpServer createServer(String webapp) throws IOException {
  134. return localServerBuilder(webapp).setFindPort(true).build();
  135. }
  136. /**
  137. * Create an HttpServer instance for the given webapp
  138. * @param webapp the webapp to work with
  139. * @param conf the configuration to use for the server
  140. * @return the server
  141. * @throws IOException if it could not be created
  142. */
  143. public static HttpServer createServer(String webapp, Configuration conf)
  144. throws IOException {
  145. return localServerBuilder(webapp).setFindPort(true).setConf(conf).build();
  146. }
  147. public static HttpServer createServer(String webapp, Configuration conf, AccessControlList adminsAcl)
  148. throws IOException {
  149. return localServerBuilder(webapp).setFindPort(true).setConf(conf).setACL(adminsAcl).build();
  150. }
  151. private static Builder localServerBuilder(String webapp) {
  152. return new HttpServer.Builder().setName(webapp).addEndpoint(
  153. URI.create("http://localhost:0"));
  154. }
  155. /**
  156. * Create an HttpServer instance for the given webapp
  157. * @param webapp the webapp to work with
  158. * @param conf the configuration to use for the server
  159. * @param pathSpecs the paths specifications the server will service
  160. * @return the server
  161. * @throws IOException if it could not be created
  162. */
  163. public static HttpServer createServer(String webapp, Configuration conf,
  164. String[] pathSpecs) throws IOException {
  165. return localServerBuilder(webapp).setFindPort(true).setConf(conf).setPathSpec(pathSpecs).build();
  166. }
  167. /**
  168. * Create and start a server with the test webapp
  169. *
  170. * @return the newly started server
  171. *
  172. * @throws IOException on any failure
  173. * @throws AssertionError if a condition was not met
  174. */
  175. public static HttpServer createAndStartTestServer() throws IOException {
  176. HttpServer server = createTestServer();
  177. server.start();
  178. return server;
  179. }
  180. /**
  181. * If the server is non null, stop it
  182. * @param server to stop
  183. * @throws Exception on any failure
  184. */
  185. public static void stop(HttpServer server) throws Exception {
  186. if (server != null) {
  187. server.stop();
  188. }
  189. }
  190. /**
  191. * Pass in a server, return a URL bound to localhost and its port
  192. * @param server server
  193. * @return a URL bonded to the base of the server
  194. * @throws MalformedURLException if the URL cannot be created.
  195. */
  196. public static URL getServerURL(HttpServer server)
  197. throws MalformedURLException {
  198. assertNotNull("No server", server);
  199. return new URL("http://"
  200. + NetUtils.getHostPortString(server.getConnectorAddress(0)));
  201. }
  202. /**
  203. * Read in the content from a URL
  204. * @param url URL To read
  205. * @return the text from the output
  206. * @throws IOException if something went wrong
  207. */
  208. protected static String readOutput(URL url) throws IOException {
  209. StringBuilder out = new StringBuilder();
  210. InputStream in = url.openConnection().getInputStream();
  211. byte[] buffer = new byte[64 * 1024];
  212. int len = in.read(buffer);
  213. while (len > 0) {
  214. out.append(new String(buffer, 0, len));
  215. len = in.read(buffer);
  216. }
  217. return out.toString();
  218. }
  219. /**
  220. * Recursively deletes a {@link File}.
  221. */
  222. protected static void deleteRecursively(File d) {
  223. if (d.isDirectory()) {
  224. for (String name : d.list()) {
  225. File child = new File(d, name);
  226. if (child.isFile()) {
  227. child.delete();
  228. } else {
  229. deleteRecursively(child);
  230. }
  231. }
  232. }
  233. d.delete();
  234. }
  235. /**
  236. * Picks a free port on the host by binding a Socket to '0'.
  237. */
  238. protected static int getFreePort() throws IOException {
  239. ServerSocket s = new ServerSocket(0);
  240. try {
  241. s.setReuseAddress(true);
  242. int port = s.getLocalPort();
  243. return port;
  244. } finally {
  245. if (null != s) {
  246. s.close();
  247. }
  248. }
  249. }
  250. }