PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

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