1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.http;
20
21 import org.apache.hadoop.net.NetUtils;
22 import org.apache.hadoop.security.authorize.AccessControlList;
23 import org.junit.Assert;
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.hbase.http.HttpServer.Builder;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.net.ServerSocket;
31 import java.net.URI;
32 import java.net.URL;
33 import java.net.MalformedURLException;
34
35
36
37
38
39 public class HttpServerFunctionalTest extends Assert {
40
41 public static final String TEST_BUILD_WEBAPPS = "test.build.webapps";
42
43 private static final String BUILD_WEBAPPS_DIR = "build/test/webapps";
44
45
46 private static final String TEST = "test";
47
48
49
50
51
52
53
54
55
56
57 public static HttpServer createTestServer() throws IOException {
58 prepareTestWebapp();
59 return createServer(TEST);
60 }
61
62
63
64
65
66
67
68
69
70
71 public static HttpServer createTestServer(Configuration conf)
72 throws IOException {
73 prepareTestWebapp();
74 return createServer(TEST, conf);
75 }
76
77 public static HttpServer createTestServer(Configuration conf, AccessControlList adminsAcl)
78 throws IOException {
79 prepareTestWebapp();
80 return createServer(TEST, conf, adminsAcl);
81 }
82
83
84
85
86
87
88
89
90
91
92 public static HttpServer createTestServer(Configuration conf,
93 String[] pathSpecs) throws IOException {
94 prepareTestWebapp();
95 return createServer(TEST, conf, pathSpecs);
96 }
97
98 public static HttpServer createTestServerWithSecurity(Configuration conf) throws IOException {
99 prepareTestWebapp();
100 return localServerBuilder(TEST).setFindPort(true).setConf(conf).setSecurityEnabled(true)
101
102 .setUsernameConfKey(HttpServer.HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_KEY)
103 .setKeytabConfKey(HttpServer.HTTP_SPNEGO_AUTHENTICATION_KEYTAB_KEY)
104 .build();
105 }
106
107
108
109
110
111
112 protected static void prepareTestWebapp() {
113 String webapps = System.getProperty(TEST_BUILD_WEBAPPS, BUILD_WEBAPPS_DIR);
114 File testWebappDir = new File(webapps +
115 File.separatorChar + TEST);
116 try {
117 if (!testWebappDir.exists()) {
118 fail("Test webapp dir " + testWebappDir.getCanonicalPath() + " missing");
119 }
120 }
121 catch (IOException e) {
122 }
123 }
124
125
126
127
128
129
130
131
132 public static HttpServer createServer(String host, int port)
133 throws IOException {
134 prepareTestWebapp();
135 return new HttpServer.Builder().setName(TEST)
136 .addEndpoint(URI.create("http://" + host + ":" + port))
137 .setFindPort(true).build();
138 }
139
140
141
142
143
144
145
146 public static HttpServer createServer(String webapp) throws IOException {
147 return localServerBuilder(webapp).setFindPort(true).build();
148 }
149
150
151
152
153
154
155
156 public static HttpServer createServer(String webapp, Configuration conf)
157 throws IOException {
158 return localServerBuilder(webapp).setFindPort(true).setConf(conf).build();
159 }
160
161 public static HttpServer createServer(String webapp, Configuration conf, AccessControlList adminsAcl)
162 throws IOException {
163 return localServerBuilder(webapp).setFindPort(true).setConf(conf).setACL(adminsAcl).build();
164 }
165
166 private static Builder localServerBuilder(String webapp) {
167 return new HttpServer.Builder().setName(webapp).addEndpoint(
168 URI.create("http://localhost:0"));
169 }
170
171
172
173
174
175
176
177
178
179 public static HttpServer createServer(String webapp, Configuration conf,
180 String[] pathSpecs) throws IOException {
181 return localServerBuilder(webapp).setFindPort(true).setConf(conf).setPathSpec(pathSpecs).build();
182 }
183
184
185
186
187
188
189
190
191
192 public static HttpServer createAndStartTestServer() throws IOException {
193 HttpServer server = createTestServer();
194 server.start();
195 return server;
196 }
197
198
199
200
201
202
203 public static void stop(HttpServer server) throws Exception {
204 if (server != null) {
205 server.stop();
206 }
207 }
208
209
210
211
212
213
214
215 public static URL getServerURL(HttpServer server)
216 throws MalformedURLException {
217 assertNotNull("No server", server);
218 return new URL("http://"
219 + NetUtils.getHostPortString(server.getConnectorAddress(0)));
220 }
221
222
223
224
225
226
227
228 protected static String readOutput(URL url) throws IOException {
229 StringBuilder out = new StringBuilder();
230 InputStream in = url.openConnection().getInputStream();
231 byte[] buffer = new byte[64 * 1024];
232 int len = in.read(buffer);
233 while (len > 0) {
234 out.append(new String(buffer, 0, len));
235 len = in.read(buffer);
236 }
237 return out.toString();
238 }
239
240
241
242
243 protected static void deleteRecursively(File d) {
244 if (d.isDirectory()) {
245 for (String name : d.list()) {
246 File child = new File(d, name);
247 if (child.isFile()) {
248 child.delete();
249 } else {
250 deleteRecursively(d);
251 }
252 }
253 }
254 d.delete();
255 }
256
257
258
259
260 protected static int getFreePort() throws IOException {
261 ServerSocket s = new ServerSocket(0);
262 try {
263 s.setReuseAddress(true);
264 int port = s.getLocalPort();
265 return port;
266 } finally {
267 if (null != s) {
268 s.close();
269 }
270 }
271 }
272 }