PageRenderTime 2535ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-hs/src/test/java/org/apache/hadoop/mapreduce/v2/hs/webapp/TestHsWebServicesJobConf.java

https://gitlab.com/xiaoliuliu2050/hadoop
Java | 279 lines | 211 code | 41 blank | 27 comment | 16 complexity | 61730ce0918907a5ef46bf875fc578da 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.hs.webapp;
  19. import static org.junit.Assert.assertEquals;
  20. import static org.junit.Assert.assertNotNull;
  21. import static org.junit.Assert.assertTrue;
  22. import static org.junit.Assert.fail;
  23. import static org.mockito.Mockito.mock;
  24. import static org.mockito.Mockito.when;
  25. import java.io.File;
  26. import java.io.IOException;
  27. import java.io.OutputStream;
  28. import java.io.StringReader;
  29. import java.util.Map;
  30. import javax.ws.rs.core.MediaType;
  31. import javax.xml.parsers.DocumentBuilder;
  32. import javax.xml.parsers.DocumentBuilderFactory;
  33. import org.apache.hadoop.conf.Configuration;
  34. import org.apache.hadoop.fs.FileSystem;
  35. import org.apache.hadoop.fs.FileUtil;
  36. import org.apache.hadoop.fs.Path;
  37. import org.apache.hadoop.mapreduce.MRJobConfig;
  38. import org.apache.hadoop.mapreduce.v2.api.records.JobId;
  39. import org.apache.hadoop.mapreduce.v2.app.AppContext;
  40. import org.apache.hadoop.mapreduce.v2.app.job.Job;
  41. import org.apache.hadoop.mapreduce.v2.hs.HistoryContext;
  42. import org.apache.hadoop.mapreduce.v2.hs.MockHistoryContext;
  43. import org.apache.hadoop.mapreduce.v2.util.MRApps;
  44. import org.apache.hadoop.yarn.webapp.GenericExceptionHandler;
  45. import org.apache.hadoop.yarn.webapp.WebApp;
  46. import org.apache.hadoop.yarn.webapp.WebServicesTestUtils;
  47. import org.codehaus.jettison.json.JSONArray;
  48. import org.codehaus.jettison.json.JSONException;
  49. import org.codehaus.jettison.json.JSONObject;
  50. import org.junit.AfterClass;
  51. import org.junit.Before;
  52. import org.junit.Test;
  53. import org.w3c.dom.Document;
  54. import org.w3c.dom.Element;
  55. import org.w3c.dom.NodeList;
  56. import org.xml.sax.InputSource;
  57. import com.google.inject.Guice;
  58. import com.google.inject.Injector;
  59. import com.google.inject.servlet.GuiceServletContextListener;
  60. import com.google.inject.servlet.ServletModule;
  61. import com.sun.jersey.api.client.ClientResponse;
  62. import com.sun.jersey.api.client.WebResource;
  63. import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
  64. import com.sun.jersey.test.framework.JerseyTest;
  65. import com.sun.jersey.test.framework.WebAppDescriptor;
  66. /**
  67. * Test the history server Rest API for getting the job conf. This
  68. * requires created a temporary configuration file.
  69. *
  70. * /ws/v1/history/mapreduce/jobs/{jobid}/conf
  71. */
  72. public class TestHsWebServicesJobConf extends JerseyTest {
  73. private static Configuration conf = new Configuration();
  74. private static HistoryContext appContext;
  75. private static HsWebApp webApp;
  76. private static File testConfDir = new File("target",
  77. TestHsWebServicesJobConf.class.getSimpleName() + "confDir");
  78. private Injector injector = Guice.createInjector(new ServletModule() {
  79. @Override
  80. protected void configureServlets() {
  81. Path confPath = new Path(testConfDir.toString(),
  82. MRJobConfig.JOB_CONF_FILE);
  83. Configuration config = new Configuration();
  84. FileSystem localFs;
  85. try {
  86. localFs = FileSystem.getLocal(config);
  87. confPath = localFs.makeQualified(confPath);
  88. OutputStream out = localFs.create(confPath);
  89. try {
  90. conf.writeXml(out);
  91. } finally {
  92. out.close();
  93. }
  94. if (!localFs.exists(confPath)) {
  95. fail("error creating config file: " + confPath);
  96. }
  97. } catch (IOException e) {
  98. fail("error creating config file: " + e.getMessage());
  99. }
  100. appContext = new MockHistoryContext(0, 2, 1, confPath);
  101. webApp = mock(HsWebApp.class);
  102. when(webApp.name()).thenReturn("hsmockwebapp");
  103. bind(JAXBContextResolver.class);
  104. bind(HsWebServices.class);
  105. bind(GenericExceptionHandler.class);
  106. bind(WebApp.class).toInstance(webApp);
  107. bind(AppContext.class).toInstance(appContext);
  108. bind(HistoryContext.class).toInstance(appContext);
  109. bind(Configuration.class).toInstance(conf);
  110. serve("/*").with(GuiceContainer.class);
  111. }
  112. });
  113. public class GuiceServletConfig extends GuiceServletContextListener {
  114. @Override
  115. protected Injector getInjector() {
  116. return injector;
  117. }
  118. }
  119. @Before
  120. @Override
  121. public void setUp() throws Exception {
  122. super.setUp();
  123. testConfDir.mkdir();
  124. }
  125. @AfterClass
  126. static public void stop() {
  127. FileUtil.fullyDelete(testConfDir);
  128. }
  129. public TestHsWebServicesJobConf() {
  130. super(new WebAppDescriptor.Builder(
  131. "org.apache.hadoop.mapreduce.v2.hs.webapp")
  132. .contextListenerClass(GuiceServletConfig.class)
  133. .filterClass(com.google.inject.servlet.GuiceFilter.class)
  134. .contextPath("jersey-guice-filter").servletPath("/").build());
  135. }
  136. @Test
  137. public void testJobConf() throws JSONException, Exception {
  138. WebResource r = resource();
  139. Map<JobId, Job> jobsMap = appContext.getAllJobs();
  140. for (JobId id : jobsMap.keySet()) {
  141. String jobId = MRApps.toString(id);
  142. ClientResponse response = r.path("ws").path("v1").path("history")
  143. .path("mapreduce")
  144. .path("jobs").path(jobId).path("conf")
  145. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  146. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  147. JSONObject json = response.getEntity(JSONObject.class);
  148. assertEquals("incorrect number of elements", 1, json.length());
  149. JSONObject info = json.getJSONObject("conf");
  150. verifyHsJobConf(info, jobsMap.get(id));
  151. }
  152. }
  153. @Test
  154. public void testJobConfSlash() throws JSONException, Exception {
  155. WebResource r = resource();
  156. Map<JobId, Job> jobsMap = appContext.getAllJobs();
  157. for (JobId id : jobsMap.keySet()) {
  158. String jobId = MRApps.toString(id);
  159. ClientResponse response = r.path("ws").path("v1").path("history").path("mapreduce")
  160. .path("jobs").path(jobId).path("conf/")
  161. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  162. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  163. JSONObject json = response.getEntity(JSONObject.class);
  164. assertEquals("incorrect number of elements", 1, json.length());
  165. JSONObject info = json.getJSONObject("conf");
  166. verifyHsJobConf(info, jobsMap.get(id));
  167. }
  168. }
  169. @Test
  170. public void testJobConfDefault() throws JSONException, Exception {
  171. WebResource r = resource();
  172. Map<JobId, Job> jobsMap = appContext.getAllJobs();
  173. for (JobId id : jobsMap.keySet()) {
  174. String jobId = MRApps.toString(id);
  175. ClientResponse response = r.path("ws").path("v1").path("history").path("mapreduce")
  176. .path("jobs").path(jobId).path("conf").get(ClientResponse.class);
  177. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  178. JSONObject json = response.getEntity(JSONObject.class);
  179. assertEquals("incorrect number of elements", 1, json.length());
  180. JSONObject info = json.getJSONObject("conf");
  181. verifyHsJobConf(info, jobsMap.get(id));
  182. }
  183. }
  184. @Test
  185. public void testJobConfXML() throws JSONException, Exception {
  186. WebResource r = resource();
  187. Map<JobId, Job> jobsMap = appContext.getAllJobs();
  188. for (JobId id : jobsMap.keySet()) {
  189. String jobId = MRApps.toString(id);
  190. ClientResponse response = r.path("ws").path("v1").path("history").path("mapreduce")
  191. .path("jobs").path(jobId).path("conf")
  192. .accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
  193. assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
  194. String xml = response.getEntity(String.class);
  195. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  196. DocumentBuilder db = dbf.newDocumentBuilder();
  197. InputSource is = new InputSource();
  198. is.setCharacterStream(new StringReader(xml));
  199. Document dom = db.parse(is);
  200. NodeList info = dom.getElementsByTagName("conf");
  201. verifyHsJobConfXML(info, jobsMap.get(id));
  202. }
  203. }
  204. public void verifyHsJobConf(JSONObject info, Job job) throws JSONException {
  205. assertEquals("incorrect number of elements", 2, info.length());
  206. WebServicesTestUtils.checkStringMatch("path", job.getConfFile().toString(),
  207. info.getString("path"));
  208. // just do simple verification of fields - not data is correct
  209. // in the fields
  210. JSONArray properties = info.getJSONArray("property");
  211. for (int i = 0; i < properties.length(); i++) {
  212. JSONObject prop = properties.getJSONObject(i);
  213. String name = prop.getString("name");
  214. String value = prop.getString("value");
  215. assertTrue("name not set", (name != null && !name.isEmpty()));
  216. assertTrue("value not set", (value != null && !value.isEmpty()));
  217. }
  218. }
  219. public void verifyHsJobConfXML(NodeList nodes, Job job) {
  220. assertEquals("incorrect number of elements", 1, nodes.getLength());
  221. for (int i = 0; i < nodes.getLength(); i++) {
  222. Element element = (Element) nodes.item(i);
  223. WebServicesTestUtils.checkStringMatch("path", job.getConfFile()
  224. .toString(), WebServicesTestUtils.getXmlString(element, "path"));
  225. // just do simple verification of fields - not data is correct
  226. // in the fields
  227. NodeList properties = element.getElementsByTagName("property");
  228. for (int j = 0; j < properties.getLength(); j++) {
  229. Element property = (Element) properties.item(j);
  230. assertNotNull("should have counters in the web service info", property);
  231. String name = WebServicesTestUtils.getXmlString(property, "name");
  232. String value = WebServicesTestUtils.getXmlString(property, "value");
  233. assertTrue("name not set", (name != null && !name.isEmpty()));
  234. assertTrue("name not set", (value != null && !value.isEmpty()));
  235. }
  236. }
  237. }
  238. }