PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/test/java/org/apache/hadoop/yarn/server/applicationhistoryservice/webapp/TestAHSWebServices.java

https://gitlab.com/xiaoliuliu2050/hadoop
Java | 376 lines | 330 code | 29 blank | 17 comment | 11 complexity | 5996f0b0e439b8871c95ed524e8d24e5 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.yarn.server.applicationhistoryservice.webapp;
  19. import static org.junit.Assert.assertEquals;
  20. import static org.junit.Assert.fail;
  21. import java.util.Arrays;
  22. import java.util.Collection;
  23. import java.util.Properties;
  24. import javax.servlet.FilterConfig;
  25. import javax.servlet.ServletException;
  26. import javax.ws.rs.core.MediaType;
  27. import javax.ws.rs.core.Response;
  28. import org.apache.hadoop.conf.Configuration;
  29. import org.apache.hadoop.http.lib.StaticUserWebFilter.StaticUserFilter;
  30. import org.apache.hadoop.security.authentication.server.AuthenticationFilter;
  31. import org.apache.hadoop.security.authentication.server.KerberosAuthenticationHandler;
  32. import org.apache.hadoop.security.authentication.server.PseudoAuthenticationHandler;
  33. import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
  34. import org.apache.hadoop.yarn.api.records.ApplicationId;
  35. import org.apache.hadoop.yarn.api.records.ContainerId;
  36. import org.apache.hadoop.yarn.api.records.ContainerState;
  37. import org.apache.hadoop.yarn.api.records.FinalApplicationStatus;
  38. import org.apache.hadoop.yarn.api.records.NodeId;
  39. import org.apache.hadoop.yarn.api.records.YarnApplicationAttemptState;
  40. import org.apache.hadoop.yarn.api.records.YarnApplicationState;
  41. import org.apache.hadoop.yarn.conf.YarnConfiguration;
  42. import org.apache.hadoop.yarn.server.api.ApplicationContext;
  43. import org.apache.hadoop.yarn.server.applicationhistoryservice.ApplicationHistoryManagerOnTimelineStore;
  44. import org.apache.hadoop.yarn.server.applicationhistoryservice.TestApplicationHistoryManagerOnTimelineStore;
  45. import org.apache.hadoop.yarn.server.security.ApplicationACLsManager;
  46. import org.apache.hadoop.yarn.server.timeline.TimelineDataManager;
  47. import org.apache.hadoop.yarn.server.timeline.TimelineStore;
  48. import org.apache.hadoop.yarn.server.timeline.security.TimelineACLsManager;
  49. import org.apache.hadoop.yarn.webapp.GenericExceptionHandler;
  50. import org.apache.hadoop.yarn.webapp.WebServicesTestUtils;
  51. import org.apache.hadoop.yarn.webapp.util.WebAppUtils;
  52. import org.codehaus.jettison.json.JSONArray;
  53. import org.codehaus.jettison.json.JSONException;
  54. import org.codehaus.jettison.json.JSONObject;
  55. import org.junit.AfterClass;
  56. import org.junit.BeforeClass;
  57. import org.junit.Test;
  58. import org.junit.runner.RunWith;
  59. import org.junit.runners.Parameterized;
  60. import com.google.inject.Guice;
  61. import com.google.inject.Injector;
  62. import com.google.inject.Singleton;
  63. import com.google.inject.servlet.GuiceServletContextListener;
  64. import com.google.inject.servlet.ServletModule;
  65. import com.sun.jersey.api.client.ClientResponse;
  66. import com.sun.jersey.api.client.ClientResponse.Status;
  67. import com.sun.jersey.api.client.UniformInterfaceException;
  68. import com.sun.jersey.api.client.WebResource;
  69. import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
  70. import com.sun.jersey.test.framework.JerseyTest;
  71. import com.sun.jersey.test.framework.WebAppDescriptor;
  72. @RunWith(Parameterized.class)
  73. public class TestAHSWebServices extends JerseyTest {
  74. private static ApplicationHistoryManagerOnTimelineStore historyManager;
  75. private static final String[] USERS = new String[] { "foo" , "bar" };
  76. @BeforeClass
  77. public static void setupClass() throws Exception {
  78. Configuration conf = new YarnConfiguration();
  79. TimelineStore store =
  80. TestApplicationHistoryManagerOnTimelineStore.createStore(5);
  81. TimelineACLsManager aclsManager = new TimelineACLsManager(conf);
  82. TimelineDataManager dataManager =
  83. new TimelineDataManager(store, aclsManager);
  84. conf.setBoolean(YarnConfiguration.YARN_ACL_ENABLE, true);
  85. conf.set(YarnConfiguration.YARN_ADMIN_ACL, "foo");
  86. ApplicationACLsManager appAclsManager = new ApplicationACLsManager(conf);
  87. historyManager =
  88. new ApplicationHistoryManagerOnTimelineStore(dataManager, appAclsManager);
  89. historyManager.init(conf);
  90. historyManager.start();
  91. }
  92. @AfterClass
  93. public static void tearDownClass() throws Exception {
  94. if (historyManager != null) {
  95. historyManager.stop();
  96. }
  97. }
  98. @Parameterized.Parameters
  99. public static Collection<Object[]> rounds() {
  100. return Arrays.asList(new Object[][] { { 0 }, { 1 } });
  101. }
  102. private Injector injector = Guice.createInjector(new ServletModule() {
  103. @Override
  104. protected void configureServlets() {
  105. bind(JAXBContextResolver.class);
  106. bind(AHSWebServices.class);
  107. bind(GenericExceptionHandler.class);
  108. bind(ApplicationContext.class).toInstance(historyManager);
  109. serve("/*").with(GuiceContainer.class);
  110. filter("/*").through(TestSimpleAuthFilter.class);
  111. }
  112. });
  113. @Singleton
  114. public static class TestSimpleAuthFilter extends AuthenticationFilter {
  115. @Override
  116. protected Properties getConfiguration(String configPrefix,
  117. FilterConfig filterConfig) throws ServletException {
  118. Properties properties =
  119. super.getConfiguration(configPrefix, filterConfig);
  120. properties.put(AuthenticationFilter.AUTH_TYPE, "simple");
  121. properties.put(PseudoAuthenticationHandler.ANONYMOUS_ALLOWED, "false");
  122. return properties;
  123. }
  124. }
  125. public class GuiceServletConfig extends GuiceServletContextListener {
  126. @Override
  127. protected Injector getInjector() {
  128. return injector;
  129. }
  130. }
  131. private int round;
  132. public TestAHSWebServices(int round) {
  133. super(new WebAppDescriptor.Builder(
  134. "org.apache.hadoop.yarn.server.applicationhistoryservice.webapp")
  135. .contextListenerClass(GuiceServletConfig.class)
  136. .filterClass(com.google.inject.servlet.GuiceFilter.class)
  137. .contextPath("jersey-guice-filter").servletPath("/").build());
  138. this.round = round;
  139. }
  140. @Test
  141. public void testInvalidUri() throws JSONException, Exception {
  142. WebResource r = resource();
  143. String responseStr = "";
  144. try {
  145. responseStr =
  146. r.path("ws").path("v1").path("applicationhistory").path("bogus")
  147. .queryParam("user.name", USERS[round])
  148. .accept(MediaType.APPLICATION_JSON).get(String.class);
  149. fail("should have thrown exception on invalid uri");
  150. } catch (UniformInterfaceException ue) {
  151. ClientResponse response = ue.getResponse();
  152. assertEquals(Status.NOT_FOUND, response.getClientResponseStatus());
  153. WebServicesTestUtils.checkStringMatch(
  154. "error string exists and shouldn't", "", responseStr);
  155. }
  156. }
  157. @Test
  158. public void testInvalidUri2() throws JSONException, Exception {
  159. WebResource r = resource();
  160. String responseStr = "";
  161. try {
  162. responseStr = r.queryParam("user.name", USERS[round])
  163. .accept(MediaType.APPLICATION_JSON).get(String.class);
  164. fail("should have thrown exception on invalid uri");
  165. } catch (UniformInterfaceException ue) {
  166. ClientResponse response = ue.getResponse();
  167. assertEquals(Status.NOT_FOUND, response.getClientResponseStatus());
  168. WebServicesTestUtils.checkStringMatch(
  169. "error string exists and shouldn't", "", responseStr);
  170. }
  171. }
  172. @Test
  173. public void testInvalidAccept() throws JSONException, Exception {
  174. WebResource r = resource();
  175. String responseStr = "";
  176. try {
  177. responseStr =
  178. r.path("ws").path("v1").path("applicationhistory")
  179. .queryParam("user.name", USERS[round])
  180. .accept(MediaType.TEXT_PLAIN).get(String.class);
  181. fail("should have thrown exception on invalid uri");
  182. } catch (UniformInterfaceException ue) {
  183. ClientResponse response = ue.getResponse();
  184. assertEquals(Status.INTERNAL_SERVER_ERROR,
  185. response.getClientResponseStatus());
  186. WebServicesTestUtils.checkStringMatch(
  187. "error string exists and shouldn't", "", responseStr);
  188. }
  189. }
  190. @Test
  191. public void testAppsQuery() throws Exception {
  192. WebResource r = resource();
  193. ClientResponse response =
  194. r.path("ws").path("v1").path("applicationhistory").path("apps")
  195. .queryParam("state", YarnApplicationState.FINISHED.toString())
  196. .queryParam("user.name", USERS[round])
  197. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  198. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  199. JSONObject json = response.getEntity(JSONObject.class);
  200. assertEquals("incorrect number of elements", 1, json.length());
  201. JSONObject apps = json.getJSONObject("apps");
  202. assertEquals("incorrect number of elements", 1, apps.length());
  203. JSONArray array = apps.getJSONArray("app");
  204. assertEquals("incorrect number of elements", 5, array.length());
  205. }
  206. @Test
  207. public void testSingleApp() throws Exception {
  208. ApplicationId appId = ApplicationId.newInstance(0, 1);
  209. WebResource r = resource();
  210. ClientResponse response =
  211. r.path("ws").path("v1").path("applicationhistory").path("apps")
  212. .path(appId.toString())
  213. .queryParam("user.name", USERS[round])
  214. .accept(MediaType.APPLICATION_JSON)
  215. .get(ClientResponse.class);
  216. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  217. JSONObject json = response.getEntity(JSONObject.class);
  218. assertEquals("incorrect number of elements", 1, json.length());
  219. JSONObject app = json.getJSONObject("app");
  220. assertEquals(appId.toString(), app.getString("appId"));
  221. assertEquals("test app", app.get("name"));
  222. assertEquals(round == 0 ? "test diagnostics info" : "",
  223. app.get("diagnosticsInfo"));
  224. assertEquals("test queue", app.get("queue"));
  225. assertEquals("user1", app.get("user"));
  226. assertEquals("test app type", app.get("type"));
  227. assertEquals(FinalApplicationStatus.UNDEFINED.toString(),
  228. app.get("finalAppStatus"));
  229. assertEquals(YarnApplicationState.FINISHED.toString(), app.get("appState"));
  230. }
  231. @Test
  232. public void testMultipleAttempts() throws Exception {
  233. ApplicationId appId = ApplicationId.newInstance(0, 1);
  234. WebResource r = resource();
  235. ClientResponse response =
  236. r.path("ws").path("v1").path("applicationhistory").path("apps")
  237. .path(appId.toString()).path("appattempts")
  238. .queryParam("user.name", USERS[round])
  239. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  240. if (round == 1) {
  241. assertEquals(
  242. Status.FORBIDDEN, response.getClientResponseStatus());
  243. return;
  244. }
  245. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  246. JSONObject json = response.getEntity(JSONObject.class);
  247. assertEquals("incorrect number of elements", 1, json.length());
  248. JSONObject appAttempts = json.getJSONObject("appAttempts");
  249. assertEquals("incorrect number of elements", 1, appAttempts.length());
  250. JSONArray array = appAttempts.getJSONArray("appAttempt");
  251. assertEquals("incorrect number of elements", 5, array.length());
  252. }
  253. @Test
  254. public void testSingleAttempt() throws Exception {
  255. ApplicationId appId = ApplicationId.newInstance(0, 1);
  256. ApplicationAttemptId appAttemptId =
  257. ApplicationAttemptId.newInstance(appId, 1);
  258. WebResource r = resource();
  259. ClientResponse response =
  260. r.path("ws").path("v1").path("applicationhistory").path("apps")
  261. .path(appId.toString()).path("appattempts")
  262. .path(appAttemptId.toString())
  263. .queryParam("user.name", USERS[round])
  264. .accept(MediaType.APPLICATION_JSON)
  265. .get(ClientResponse.class);
  266. if (round == 1) {
  267. assertEquals(
  268. Status.FORBIDDEN, response.getClientResponseStatus());
  269. return;
  270. }
  271. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  272. JSONObject json = response.getEntity(JSONObject.class);
  273. assertEquals("incorrect number of elements", 1, json.length());
  274. JSONObject appAttempt = json.getJSONObject("appAttempt");
  275. assertEquals(appAttemptId.toString(), appAttempt.getString("appAttemptId"));
  276. assertEquals("test host", appAttempt.getString("host"));
  277. assertEquals("test diagnostics info",
  278. appAttempt.getString("diagnosticsInfo"));
  279. assertEquals("test tracking url", appAttempt.getString("trackingUrl"));
  280. assertEquals(YarnApplicationAttemptState.FINISHED.toString(),
  281. appAttempt.get("appAttemptState"));
  282. }
  283. @Test
  284. public void testMultipleContainers() throws Exception {
  285. ApplicationId appId = ApplicationId.newInstance(0, 1);
  286. ApplicationAttemptId appAttemptId =
  287. ApplicationAttemptId.newInstance(appId, 1);
  288. WebResource r = resource();
  289. ClientResponse response =
  290. r.path("ws").path("v1").path("applicationhistory").path("apps")
  291. .path(appId.toString()).path("appattempts")
  292. .path(appAttemptId.toString()).path("containers")
  293. .queryParam("user.name", USERS[round])
  294. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  295. if (round == 1) {
  296. assertEquals(
  297. Status.FORBIDDEN, response.getClientResponseStatus());
  298. return;
  299. }
  300. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  301. JSONObject json = response.getEntity(JSONObject.class);
  302. assertEquals("incorrect number of elements", 1, json.length());
  303. JSONObject containers = json.getJSONObject("containers");
  304. assertEquals("incorrect number of elements", 1, containers.length());
  305. JSONArray array = containers.getJSONArray("container");
  306. assertEquals("incorrect number of elements", 5, array.length());
  307. }
  308. @Test
  309. public void testSingleContainer() throws Exception {
  310. ApplicationId appId = ApplicationId.newInstance(0, 1);
  311. ApplicationAttemptId appAttemptId =
  312. ApplicationAttemptId.newInstance(appId, 1);
  313. ContainerId containerId = ContainerId.newContainerId(appAttemptId, 1);
  314. WebResource r = resource();
  315. ClientResponse response =
  316. r.path("ws").path("v1").path("applicationhistory").path("apps")
  317. .path(appId.toString()).path("appattempts")
  318. .path(appAttemptId.toString()).path("containers")
  319. .path(containerId.toString())
  320. .queryParam("user.name", USERS[round])
  321. .accept(MediaType.APPLICATION_JSON)
  322. .get(ClientResponse.class);
  323. if (round == 1) {
  324. assertEquals(
  325. Status.FORBIDDEN, response.getClientResponseStatus());
  326. return;
  327. }
  328. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  329. JSONObject json = response.getEntity(JSONObject.class);
  330. assertEquals("incorrect number of elements", 1, json.length());
  331. JSONObject container = json.getJSONObject("container");
  332. assertEquals(containerId.toString(), container.getString("containerId"));
  333. assertEquals("test diagnostics info", container.getString("diagnosticsInfo"));
  334. assertEquals("-1", container.getString("allocatedMB"));
  335. assertEquals("-1", container.getString("allocatedVCores"));
  336. assertEquals(NodeId.newInstance("test host", 100).toString(),
  337. container.getString("assignedNodeId"));
  338. assertEquals("-1", container.getString("priority"));
  339. Configuration conf = new YarnConfiguration();
  340. assertEquals(WebAppUtils.getHttpSchemePrefix(conf) +
  341. WebAppUtils.getAHSWebAppURLWithoutScheme(conf) +
  342. "/applicationhistory/logs/test host:100/container_0_0001_01_000001/" +
  343. "container_0_0001_01_000001/user1", container.getString("logUrl"));
  344. assertEquals(ContainerState.COMPLETE.toString(),
  345. container.getString("containerState"));
  346. }
  347. }