PageRenderTime 4716ms CodeModel.GetById 13ms RepoModel.GetById 3ms app.codeStats 0ms

/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app/src/test/java/org/apache/hadoop/mapreduce/v2/app/webapp/TestAMWebServices.java

https://gitlab.com/xiaoliuliu2050/hadoop
Java | 345 lines | 282 code | 39 blank | 24 comment | 3 complexity | fd195c736ad45b6f47782f00bfb3e9dd MD5 | raw file
  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.mapreduce.v2.app.webapp;
  19. import static org.junit.Assert.assertEquals;
  20. import static org.junit.Assert.assertTrue;
  21. import static org.junit.Assert.fail;
  22. import java.io.StringReader;
  23. import java.util.Set;
  24. import javax.ws.rs.core.MediaType;
  25. import javax.xml.parsers.DocumentBuilder;
  26. import javax.xml.parsers.DocumentBuilderFactory;
  27. import org.apache.hadoop.conf.Configuration;
  28. import org.apache.hadoop.mapreduce.v2.app.AppContext;
  29. import org.apache.hadoop.mapreduce.v2.app.MockAppContext;
  30. import org.apache.hadoop.yarn.webapp.GenericExceptionHandler;
  31. import org.apache.hadoop.yarn.webapp.WebServicesTestUtils;
  32. import org.codehaus.jettison.json.JSONArray;
  33. import org.codehaus.jettison.json.JSONException;
  34. import org.codehaus.jettison.json.JSONObject;
  35. import org.junit.Before;
  36. import org.junit.Test;
  37. import org.w3c.dom.Document;
  38. import org.w3c.dom.Element;
  39. import org.w3c.dom.NodeList;
  40. import org.xml.sax.InputSource;
  41. import com.google.common.collect.Sets;
  42. import com.google.inject.Guice;
  43. import com.google.inject.Injector;
  44. import com.google.inject.servlet.GuiceServletContextListener;
  45. import com.google.inject.servlet.ServletModule;
  46. import com.sun.jersey.api.client.ClientResponse;
  47. import com.sun.jersey.api.client.ClientResponse.Status;
  48. import com.sun.jersey.api.client.UniformInterfaceException;
  49. import com.sun.jersey.api.client.WebResource;
  50. import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
  51. import com.sun.jersey.test.framework.JerseyTest;
  52. import com.sun.jersey.test.framework.WebAppDescriptor;
  53. /**
  54. * Test the MapReduce Application master info web services api's. Also test
  55. * non-existent urls.
  56. *
  57. * /ws/v1/mapreduce
  58. * /ws/v1/mapreduce/info
  59. */
  60. public class TestAMWebServices extends JerseyTest {
  61. private static Configuration conf = new Configuration();
  62. private static MockAppContext appContext;
  63. private Injector injector = Guice.createInjector(new ServletModule() {
  64. @Override
  65. protected void configureServlets() {
  66. appContext = new MockAppContext(0, 1, 1, 1);
  67. appContext.setBlacklistedNodes(Sets.newHashSet("badnode1", "badnode2"));
  68. bind(JAXBContextResolver.class);
  69. bind(AMWebServices.class);
  70. bind(GenericExceptionHandler.class);
  71. bind(AppContext.class).toInstance(appContext);
  72. bind(Configuration.class).toInstance(conf);
  73. serve("/*").with(GuiceContainer.class);
  74. }
  75. });
  76. public class GuiceServletConfig extends GuiceServletContextListener {
  77. @Override
  78. protected Injector getInjector() {
  79. return injector;
  80. }
  81. }
  82. @Before
  83. @Override
  84. public void setUp() throws Exception {
  85. super.setUp();
  86. }
  87. public TestAMWebServices() {
  88. super(new WebAppDescriptor.Builder(
  89. "org.apache.hadoop.mapreduce.v2.app.webapp")
  90. .contextListenerClass(GuiceServletConfig.class)
  91. .filterClass(com.google.inject.servlet.GuiceFilter.class)
  92. .contextPath("jersey-guice-filter").servletPath("/").build());
  93. }
  94. @Test
  95. public void testAM() throws JSONException, Exception {
  96. WebResource r = resource();
  97. ClientResponse response = r.path("ws").path("v1").path("mapreduce")
  98. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  99. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  100. JSONObject json = response.getEntity(JSONObject.class);
  101. assertEquals("incorrect number of elements", 1, json.length());
  102. verifyAMInfo(json.getJSONObject("info"), appContext);
  103. }
  104. @Test
  105. public void testAMSlash() throws JSONException, Exception {
  106. WebResource r = resource();
  107. ClientResponse response = r.path("ws").path("v1").path("mapreduce/")
  108. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  109. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  110. JSONObject json = response.getEntity(JSONObject.class);
  111. assertEquals("incorrect number of elements", 1, json.length());
  112. verifyAMInfo(json.getJSONObject("info"), appContext);
  113. }
  114. @Test
  115. public void testAMDefault() throws JSONException, Exception {
  116. WebResource r = resource();
  117. ClientResponse response = r.path("ws").path("v1").path("mapreduce/")
  118. .get(ClientResponse.class);
  119. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  120. JSONObject json = response.getEntity(JSONObject.class);
  121. assertEquals("incorrect number of elements", 1, json.length());
  122. verifyAMInfo(json.getJSONObject("info"), appContext);
  123. }
  124. @Test
  125. public void testAMXML() throws JSONException, Exception {
  126. WebResource r = resource();
  127. ClientResponse response = r.path("ws").path("v1").path("mapreduce")
  128. .accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
  129. assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
  130. String xml = response.getEntity(String.class);
  131. verifyAMInfoXML(xml, appContext);
  132. }
  133. @Test
  134. public void testInfo() throws JSONException, Exception {
  135. WebResource r = resource();
  136. ClientResponse response = r.path("ws").path("v1").path("mapreduce")
  137. .path("info").accept(MediaType.APPLICATION_JSON)
  138. .get(ClientResponse.class);
  139. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  140. JSONObject json = response.getEntity(JSONObject.class);
  141. assertEquals("incorrect number of elements", 1, json.length());
  142. verifyAMInfo(json.getJSONObject("info"), appContext);
  143. }
  144. @Test
  145. public void testInfoSlash() throws JSONException, Exception {
  146. WebResource r = resource();
  147. ClientResponse response = r.path("ws").path("v1").path("mapreduce")
  148. .path("info/").accept(MediaType.APPLICATION_JSON)
  149. .get(ClientResponse.class);
  150. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  151. JSONObject json = response.getEntity(JSONObject.class);
  152. assertEquals("incorrect number of elements", 1, json.length());
  153. verifyAMInfo(json.getJSONObject("info"), appContext);
  154. }
  155. @Test
  156. public void testInfoDefault() throws JSONException, Exception {
  157. WebResource r = resource();
  158. ClientResponse response = r.path("ws").path("v1").path("mapreduce")
  159. .path("info/").get(ClientResponse.class);
  160. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  161. JSONObject json = response.getEntity(JSONObject.class);
  162. assertEquals("incorrect number of elements", 1, json.length());
  163. verifyAMInfo(json.getJSONObject("info"), appContext);
  164. }
  165. @Test
  166. public void testInfoXML() throws JSONException, Exception {
  167. WebResource r = resource();
  168. ClientResponse response = r.path("ws").path("v1").path("mapreduce")
  169. .path("info/").accept(MediaType.APPLICATION_XML)
  170. .get(ClientResponse.class);
  171. assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
  172. String xml = response.getEntity(String.class);
  173. verifyAMInfoXML(xml, appContext);
  174. }
  175. @Test
  176. public void testInvalidUri() throws JSONException, Exception {
  177. WebResource r = resource();
  178. String responseStr = "";
  179. try {
  180. responseStr = r.path("ws").path("v1").path("mapreduce").path("bogus")
  181. .accept(MediaType.APPLICATION_JSON).get(String.class);
  182. fail("should have thrown exception on invalid uri");
  183. } catch (UniformInterfaceException ue) {
  184. ClientResponse response = ue.getResponse();
  185. assertEquals(Status.NOT_FOUND, response.getClientResponseStatus());
  186. WebServicesTestUtils.checkStringMatch(
  187. "error string exists and shouldn't", "", responseStr);
  188. }
  189. }
  190. @Test
  191. public void testInvalidUri2() throws JSONException, Exception {
  192. WebResource r = resource();
  193. String responseStr = "";
  194. try {
  195. responseStr = r.path("ws").path("v1").path("invalid")
  196. .accept(MediaType.APPLICATION_JSON).get(String.class);
  197. fail("should have thrown exception on invalid uri");
  198. } catch (UniformInterfaceException ue) {
  199. ClientResponse response = ue.getResponse();
  200. assertEquals(Status.NOT_FOUND, response.getClientResponseStatus());
  201. WebServicesTestUtils.checkStringMatch(
  202. "error string exists and shouldn't", "", responseStr);
  203. }
  204. }
  205. @Test
  206. public void testInvalidAccept() throws JSONException, Exception {
  207. WebResource r = resource();
  208. String responseStr = "";
  209. try {
  210. responseStr = r.path("ws").path("v1").path("mapreduce")
  211. .accept(MediaType.TEXT_PLAIN).get(String.class);
  212. fail("should have thrown exception on invalid uri");
  213. } catch (UniformInterfaceException ue) {
  214. ClientResponse response = ue.getResponse();
  215. assertEquals(Status.INTERNAL_SERVER_ERROR,
  216. response.getClientResponseStatus());
  217. WebServicesTestUtils.checkStringMatch(
  218. "error string exists and shouldn't", "", responseStr);
  219. }
  220. }
  221. @Test
  222. public void testBlacklistedNodes() throws JSONException, Exception {
  223. WebResource r = resource();
  224. ClientResponse response = r.path("ws").path("v1").path("mapreduce")
  225. .path("blacklistednodes").accept(MediaType.APPLICATION_JSON)
  226. .get(ClientResponse.class);
  227. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  228. JSONObject json = response.getEntity(JSONObject.class);
  229. assertEquals("incorrect number of elements", 1, json.length());
  230. verifyBlacklistedNodesInfo(json, appContext);
  231. }
  232. @Test
  233. public void testBlacklistedNodesXML() throws Exception {
  234. WebResource r = resource();
  235. ClientResponse response = r.path("ws").path("v1").path("mapreduce")
  236. .path("blacklistednodes").accept(MediaType.APPLICATION_XML)
  237. .get(ClientResponse.class);
  238. assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
  239. String xml = response.getEntity(String.class);
  240. verifyBlacklistedNodesInfoXML(xml, appContext);
  241. }
  242. public void verifyAMInfo(JSONObject info, AppContext ctx)
  243. throws JSONException {
  244. assertEquals("incorrect number of elements", 5, info.length());
  245. verifyAMInfoGeneric(ctx, info.getString("appId"), info.getString("user"),
  246. info.getString("name"), info.getLong("startedOn"),
  247. info.getLong("elapsedTime"));
  248. }
  249. public void verifyAMInfoXML(String xml, AppContext ctx)
  250. throws JSONException, Exception {
  251. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  252. DocumentBuilder db = dbf.newDocumentBuilder();
  253. InputSource is = new InputSource();
  254. is.setCharacterStream(new StringReader(xml));
  255. Document dom = db.parse(is);
  256. NodeList nodes = dom.getElementsByTagName("info");
  257. assertEquals("incorrect number of elements", 1, nodes.getLength());
  258. for (int i = 0; i < nodes.getLength(); i++) {
  259. Element element = (Element) nodes.item(i);
  260. verifyAMInfoGeneric(ctx,
  261. WebServicesTestUtils.getXmlString(element, "appId"),
  262. WebServicesTestUtils.getXmlString(element, "user"),
  263. WebServicesTestUtils.getXmlString(element, "name"),
  264. WebServicesTestUtils.getXmlLong(element, "startedOn"),
  265. WebServicesTestUtils.getXmlLong(element, "elapsedTime"));
  266. }
  267. }
  268. public void verifyAMInfoGeneric(AppContext ctx, String id, String user,
  269. String name, long startedOn, long elapsedTime) {
  270. WebServicesTestUtils.checkStringMatch("id", ctx.getApplicationID()
  271. .toString(), id);
  272. WebServicesTestUtils.checkStringMatch("user", ctx.getUser().toString(),
  273. user);
  274. WebServicesTestUtils.checkStringMatch("name", ctx.getApplicationName(),
  275. name);
  276. assertEquals("startedOn incorrect", ctx.getStartTime(), startedOn);
  277. assertTrue("elapsedTime not greater then 0", (elapsedTime > 0));
  278. }
  279. public void verifyBlacklistedNodesInfo(JSONObject blacklist, AppContext ctx)
  280. throws JSONException, Exception{
  281. JSONArray array = blacklist.getJSONArray("blacklistedNodes");
  282. assertEquals(array.length(), ctx.getBlacklistedNodes().size());
  283. for (int i = 0; i < array.length(); i++) {
  284. assertTrue(ctx.getBlacklistedNodes().contains(array.getString(i)));
  285. }
  286. }
  287. public void verifyBlacklistedNodesInfoXML(String xml, AppContext ctx)
  288. throws JSONException, Exception {
  289. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  290. DocumentBuilder db = dbf.newDocumentBuilder();
  291. InputSource is = new InputSource();
  292. is.setCharacterStream(new StringReader(xml));
  293. Document dom = db.parse(is);
  294. NodeList infonodes = dom.getElementsByTagName("blacklistednodesinfo");
  295. assertEquals("incorrect number of elements", 1, infonodes.getLength());
  296. NodeList nodes = dom.getElementsByTagName("blacklistedNodes");
  297. Set<String> blacklistedNodes = ctx.getBlacklistedNodes();
  298. assertEquals("incorrect number of elements", blacklistedNodes.size(),
  299. nodes.getLength());
  300. for (int i = 0; i < nodes.getLength(); i++) {
  301. Element element = (Element) nodes.item(i);
  302. assertTrue(
  303. blacklistedNodes.contains(element.getFirstChild().getNodeValue()));
  304. }
  305. }
  306. }