PageRenderTime 3055ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/webapp/TestNMWebServicesApps.java

https://gitlab.com/xiaoliuliu2050/hadoop
Java | 700 lines | 598 code | 79 blank | 23 comment | 5 complexity | adcdbb3d3ad003e1f36710a31871170a 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.nodemanager.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.File;
  23. import java.io.IOException;
  24. import java.io.StringReader;
  25. import java.util.HashMap;
  26. import javax.ws.rs.core.MediaType;
  27. import javax.xml.parsers.DocumentBuilder;
  28. import javax.xml.parsers.DocumentBuilderFactory;
  29. import org.apache.hadoop.conf.Configuration;
  30. import org.apache.hadoop.fs.FileUtil;
  31. import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
  32. import org.apache.hadoop.yarn.api.records.NodeId;
  33. import org.apache.hadoop.yarn.conf.YarnConfiguration;
  34. import org.apache.hadoop.yarn.event.AsyncDispatcher;
  35. import org.apache.hadoop.yarn.event.Dispatcher;
  36. import org.apache.hadoop.yarn.server.nodemanager.Context;
  37. import org.apache.hadoop.yarn.server.nodemanager.LocalDirsHandlerService;
  38. import org.apache.hadoop.yarn.server.nodemanager.NodeHealthCheckerService;
  39. import org.apache.hadoop.yarn.server.nodemanager.NodeManager;
  40. import org.apache.hadoop.yarn.server.nodemanager.ResourceView;
  41. import org.apache.hadoop.yarn.server.nodemanager.containermanager.application.Application;
  42. import org.apache.hadoop.yarn.server.nodemanager.containermanager.application.ApplicationState;
  43. import org.apache.hadoop.yarn.server.nodemanager.containermanager.container.Container;
  44. import org.apache.hadoop.yarn.server.nodemanager.webapp.WebServer.NMWebApp;
  45. import org.apache.hadoop.yarn.server.security.ApplicationACLsManager;
  46. import org.apache.hadoop.yarn.server.utils.BuilderUtils;
  47. import org.apache.hadoop.yarn.webapp.GenericExceptionHandler;
  48. import org.apache.hadoop.yarn.webapp.WebApp;
  49. import org.apache.hadoop.yarn.webapp.WebServicesTestUtils;
  50. import org.codehaus.jettison.json.JSONArray;
  51. import org.codehaus.jettison.json.JSONException;
  52. import org.codehaus.jettison.json.JSONObject;
  53. import org.junit.AfterClass;
  54. import org.junit.Before;
  55. import org.junit.Test;
  56. import org.w3c.dom.Document;
  57. import org.w3c.dom.Element;
  58. import org.w3c.dom.Node;
  59. import org.w3c.dom.NodeList;
  60. import org.xml.sax.InputSource;
  61. import com.google.inject.Guice;
  62. import com.google.inject.Injector;
  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. public class TestNMWebServicesApps extends JerseyTest {
  73. private static Context nmContext;
  74. private static ResourceView resourceView;
  75. private static ApplicationACLsManager aclsManager;
  76. private static LocalDirsHandlerService dirsHandler;
  77. private static WebApp nmWebApp;
  78. private static Configuration conf = new Configuration();
  79. private static final File testRootDir = new File("target",
  80. TestNMWebServicesApps.class.getSimpleName());
  81. private static File testLogDir = new File("target",
  82. TestNMWebServicesApps.class.getSimpleName() + "LogDir");
  83. private Injector injector = Guice.createInjector(new ServletModule() {
  84. @Override
  85. protected void configureServlets() {
  86. conf.set(YarnConfiguration.NM_LOCAL_DIRS, testRootDir.getAbsolutePath());
  87. conf.set(YarnConfiguration.NM_LOG_DIRS, testLogDir.getAbsolutePath());
  88. NodeHealthCheckerService healthChecker = new NodeHealthCheckerService();
  89. healthChecker.init(conf);
  90. dirsHandler = healthChecker.getDiskHandler();
  91. aclsManager = new ApplicationACLsManager(conf);
  92. nmContext = new NodeManager.NMContext(null, null, dirsHandler,
  93. aclsManager, null);
  94. NodeId nodeId = NodeId.newInstance("testhost.foo.com", 9999);
  95. ((NodeManager.NMContext)nmContext).setNodeId(nodeId);
  96. resourceView = new ResourceView() {
  97. @Override
  98. public long getVmemAllocatedForContainers() {
  99. // 15.5G in bytes
  100. return new Long("16642998272");
  101. }
  102. @Override
  103. public long getPmemAllocatedForContainers() {
  104. // 16G in bytes
  105. return new Long("17179869184");
  106. }
  107. @Override
  108. public long getVCoresAllocatedForContainers() {
  109. return new Long("4000");
  110. }
  111. @Override
  112. public boolean isVmemCheckEnabled() {
  113. return true;
  114. }
  115. @Override
  116. public boolean isPmemCheckEnabled() {
  117. return true;
  118. }
  119. };
  120. nmWebApp = new NMWebApp(resourceView, aclsManager, dirsHandler);
  121. bind(JAXBContextResolver.class);
  122. bind(NMWebServices.class);
  123. bind(GenericExceptionHandler.class);
  124. bind(Context.class).toInstance(nmContext);
  125. bind(WebApp.class).toInstance(nmWebApp);
  126. bind(ResourceView.class).toInstance(resourceView);
  127. bind(ApplicationACLsManager.class).toInstance(aclsManager);
  128. bind(LocalDirsHandlerService.class).toInstance(dirsHandler);
  129. serve("/*").with(GuiceContainer.class);
  130. }
  131. });
  132. public class GuiceServletConfig extends GuiceServletContextListener {
  133. @Override
  134. protected Injector getInjector() {
  135. return injector;
  136. }
  137. }
  138. @Before
  139. @Override
  140. public void setUp() throws Exception {
  141. super.setUp();
  142. testRootDir.mkdirs();
  143. testLogDir.mkdir();
  144. }
  145. @AfterClass
  146. static public void cleanup() {
  147. FileUtil.fullyDelete(testRootDir);
  148. FileUtil.fullyDelete(testLogDir);
  149. }
  150. public TestNMWebServicesApps() {
  151. super(new WebAppDescriptor.Builder(
  152. "org.apache.hadoop.yarn.server.nodemanager.webapp")
  153. .contextListenerClass(GuiceServletConfig.class)
  154. .filterClass(com.google.inject.servlet.GuiceFilter.class)
  155. .contextPath("jersey-guice-filter").servletPath("/").build());
  156. }
  157. @Test
  158. public void testNodeAppsNone() throws JSONException, Exception {
  159. WebResource r = resource();
  160. ClientResponse response = r.path("ws").path("v1").path("node").path("apps")
  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("apps isn't NULL", JSONObject.NULL, json.get("apps"));
  165. }
  166. private HashMap<String, String> addAppContainers(Application app)
  167. throws IOException {
  168. Dispatcher dispatcher = new AsyncDispatcher();
  169. ApplicationAttemptId appAttemptId = BuilderUtils.newApplicationAttemptId(
  170. app.getAppId(), 1);
  171. Container container1 = new MockContainer(appAttemptId, dispatcher, conf,
  172. app.getUser(), app.getAppId(), 1);
  173. Container container2 = new MockContainer(appAttemptId, dispatcher, conf,
  174. app.getUser(), app.getAppId(), 2);
  175. nmContext.getContainers()
  176. .put(container1.getContainerId(), container1);
  177. nmContext.getContainers()
  178. .put(container2.getContainerId(), container2);
  179. app.getContainers().put(container1.getContainerId(), container1);
  180. app.getContainers().put(container2.getContainerId(), container2);
  181. HashMap<String, String> hash = new HashMap<String, String>();
  182. hash.put(container1.getContainerId().toString(), container1
  183. .getContainerId().toString());
  184. hash.put(container2.getContainerId().toString(), container2
  185. .getContainerId().toString());
  186. return hash;
  187. }
  188. @Test
  189. public void testNodeApps() throws JSONException, Exception {
  190. testNodeHelper("apps", MediaType.APPLICATION_JSON);
  191. }
  192. @Test
  193. public void testNodeAppsSlash() throws JSONException, Exception {
  194. testNodeHelper("apps/", MediaType.APPLICATION_JSON);
  195. }
  196. // make sure default is json output
  197. @Test
  198. public void testNodeAppsDefault() throws JSONException, Exception {
  199. testNodeHelper("apps/", "");
  200. }
  201. public void testNodeHelper(String path, String media) throws JSONException,
  202. Exception {
  203. WebResource r = resource();
  204. Application app = new MockApp(1);
  205. nmContext.getApplications().put(app.getAppId(), app);
  206. HashMap<String, String> hash = addAppContainers(app);
  207. Application app2 = new MockApp(2);
  208. nmContext.getApplications().put(app2.getAppId(), app2);
  209. HashMap<String, String> hash2 = addAppContainers(app2);
  210. ClientResponse response = r.path("ws").path("v1").path("node").path(path)
  211. .accept(media).get(ClientResponse.class);
  212. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  213. JSONObject json = response.getEntity(JSONObject.class);
  214. JSONObject info = json.getJSONObject("apps");
  215. assertEquals("incorrect number of elements", 1, info.length());
  216. JSONArray appInfo = info.getJSONArray("app");
  217. assertEquals("incorrect number of elements", 2, appInfo.length());
  218. String id = appInfo.getJSONObject(0).getString("id");
  219. if (id.matches(app.getAppId().toString())) {
  220. verifyNodeAppInfo(appInfo.getJSONObject(0), app, hash);
  221. verifyNodeAppInfo(appInfo.getJSONObject(1), app2, hash2);
  222. } else {
  223. verifyNodeAppInfo(appInfo.getJSONObject(0), app2, hash2);
  224. verifyNodeAppInfo(appInfo.getJSONObject(1), app, hash);
  225. }
  226. }
  227. @Test
  228. public void testNodeAppsUser() throws JSONException, Exception {
  229. WebResource r = resource();
  230. Application app = new MockApp(1);
  231. nmContext.getApplications().put(app.getAppId(), app);
  232. HashMap<String, String> hash = addAppContainers(app);
  233. Application app2 = new MockApp("foo", 1234, 2);
  234. nmContext.getApplications().put(app2.getAppId(), app2);
  235. addAppContainers(app2);
  236. ClientResponse response = r.path("ws").path("v1").path("node").path("apps")
  237. .queryParam("user", "mockUser").accept(MediaType.APPLICATION_JSON)
  238. .get(ClientResponse.class);
  239. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  240. JSONObject json = response.getEntity(JSONObject.class);
  241. JSONObject info = json.getJSONObject("apps");
  242. assertEquals("incorrect number of elements", 1, info.length());
  243. JSONArray appInfo = info.getJSONArray("app");
  244. assertEquals("incorrect number of elements", 1, appInfo.length());
  245. verifyNodeAppInfo(appInfo.getJSONObject(0), app, hash);
  246. }
  247. @Test
  248. public void testNodeAppsUserNone() throws JSONException, Exception {
  249. WebResource r = resource();
  250. Application app = new MockApp(1);
  251. nmContext.getApplications().put(app.getAppId(), app);
  252. addAppContainers(app);
  253. Application app2 = new MockApp("foo", 1234, 2);
  254. nmContext.getApplications().put(app2.getAppId(), app2);
  255. addAppContainers(app2);
  256. ClientResponse response = r.path("ws").path("v1").path("node").path("apps")
  257. .queryParam("user", "george").accept(MediaType.APPLICATION_JSON)
  258. .get(ClientResponse.class);
  259. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  260. JSONObject json = response.getEntity(JSONObject.class);
  261. assertEquals("apps is not null", JSONObject.NULL, json.get("apps"));
  262. }
  263. @Test
  264. public void testNodeAppsUserEmpty() throws JSONException, Exception {
  265. WebResource r = resource();
  266. Application app = new MockApp(1);
  267. nmContext.getApplications().put(app.getAppId(), app);
  268. addAppContainers(app);
  269. Application app2 = new MockApp("foo", 1234, 2);
  270. nmContext.getApplications().put(app2.getAppId(), app2);
  271. addAppContainers(app2);
  272. try {
  273. r.path("ws").path("v1").path("node").path("apps").queryParam("user", "")
  274. .accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
  275. fail("should have thrown exception on invalid user query");
  276. } catch (UniformInterfaceException ue) {
  277. ClientResponse response = ue.getResponse();
  278. assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus());
  279. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  280. JSONObject msg = response.getEntity(JSONObject.class);
  281. JSONObject exception = msg.getJSONObject("RemoteException");
  282. assertEquals("incorrect number of elements", 3, exception.length());
  283. String message = exception.getString("message");
  284. String type = exception.getString("exception");
  285. String classname = exception.getString("javaClassName");
  286. WebServicesTestUtils
  287. .checkStringMatch(
  288. "exception message",
  289. "java.lang.Exception: Error: You must specify a non-empty string for the user",
  290. message);
  291. WebServicesTestUtils.checkStringMatch("exception type",
  292. "BadRequestException", type);
  293. WebServicesTestUtils.checkStringMatch("exception classname",
  294. "org.apache.hadoop.yarn.webapp.BadRequestException", classname);
  295. }
  296. }
  297. @Test
  298. public void testNodeAppsState() throws JSONException, Exception {
  299. WebResource r = resource();
  300. Application app = new MockApp(1);
  301. nmContext.getApplications().put(app.getAppId(), app);
  302. addAppContainers(app);
  303. MockApp app2 = new MockApp("foo", 1234, 2);
  304. nmContext.getApplications().put(app2.getAppId(), app2);
  305. HashMap<String, String> hash2 = addAppContainers(app2);
  306. app2.setState(ApplicationState.RUNNING);
  307. ClientResponse response = r.path("ws").path("v1").path("node").path("apps")
  308. .queryParam("state", ApplicationState.RUNNING.toString())
  309. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  310. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  311. JSONObject json = response.getEntity(JSONObject.class);
  312. JSONObject info = json.getJSONObject("apps");
  313. assertEquals("incorrect number of elements", 1, info.length());
  314. JSONArray appInfo = info.getJSONArray("app");
  315. assertEquals("incorrect number of elements", 1, appInfo.length());
  316. verifyNodeAppInfo(appInfo.getJSONObject(0), app2, hash2);
  317. }
  318. @Test
  319. public void testNodeAppsStateNone() throws JSONException, Exception {
  320. WebResource r = resource();
  321. Application app = new MockApp(1);
  322. nmContext.getApplications().put(app.getAppId(), app);
  323. addAppContainers(app);
  324. Application app2 = new MockApp("foo", 1234, 2);
  325. nmContext.getApplications().put(app2.getAppId(), app2);
  326. addAppContainers(app2);
  327. ClientResponse response = r.path("ws").path("v1").path("node").path("apps")
  328. .queryParam("state", ApplicationState.INITING.toString())
  329. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  330. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  331. JSONObject json = response.getEntity(JSONObject.class);
  332. assertEquals("apps is not null", JSONObject.NULL, json.get("apps"));
  333. }
  334. @Test
  335. public void testNodeAppsStateInvalid() throws JSONException, Exception {
  336. WebResource r = resource();
  337. Application app = new MockApp(1);
  338. nmContext.getApplications().put(app.getAppId(), app);
  339. addAppContainers(app);
  340. Application app2 = new MockApp("foo", 1234, 2);
  341. nmContext.getApplications().put(app2.getAppId(), app2);
  342. addAppContainers(app2);
  343. try {
  344. r.path("ws").path("v1").path("node").path("apps")
  345. .queryParam("state", "FOO_STATE").accept(MediaType.APPLICATION_JSON)
  346. .get(JSONObject.class);
  347. fail("should have thrown exception on invalid user query");
  348. } catch (UniformInterfaceException ue) {
  349. ClientResponse response = ue.getResponse();
  350. assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus());
  351. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  352. JSONObject msg = response.getEntity(JSONObject.class);
  353. JSONObject exception = msg.getJSONObject("RemoteException");
  354. assertEquals("incorrect number of elements", 3, exception.length());
  355. String message = exception.getString("message");
  356. String type = exception.getString("exception");
  357. String classname = exception.getString("javaClassName");
  358. verifyStateInvalidException(message, type, classname);
  359. }
  360. }
  361. // verify the exception object default format is JSON
  362. @Test
  363. public void testNodeAppsStateInvalidDefault() throws JSONException, Exception {
  364. WebResource r = resource();
  365. Application app = new MockApp(1);
  366. nmContext.getApplications().put(app.getAppId(), app);
  367. addAppContainers(app);
  368. Application app2 = new MockApp("foo", 1234, 2);
  369. nmContext.getApplications().put(app2.getAppId(), app2);
  370. addAppContainers(app2);
  371. try {
  372. r.path("ws").path("v1").path("node").path("apps")
  373. .queryParam("state", "FOO_STATE").get(JSONObject.class);
  374. fail("should have thrown exception on invalid user query");
  375. } catch (UniformInterfaceException ue) {
  376. ClientResponse response = ue.getResponse();
  377. assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus());
  378. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  379. JSONObject msg = response.getEntity(JSONObject.class);
  380. JSONObject exception = msg.getJSONObject("RemoteException");
  381. assertEquals("incorrect number of elements", 3, exception.length());
  382. String message = exception.getString("message");
  383. String type = exception.getString("exception");
  384. String classname = exception.getString("javaClassName");
  385. verifyStateInvalidException(message, type, classname);
  386. }
  387. }
  388. // test that the exception output also returns XML
  389. @Test
  390. public void testNodeAppsStateInvalidXML() throws JSONException, Exception {
  391. WebResource r = resource();
  392. Application app = new MockApp(1);
  393. nmContext.getApplications().put(app.getAppId(), app);
  394. addAppContainers(app);
  395. Application app2 = new MockApp("foo", 1234, 2);
  396. nmContext.getApplications().put(app2.getAppId(), app2);
  397. addAppContainers(app2);
  398. try {
  399. r.path("ws").path("v1").path("node").path("apps")
  400. .queryParam("state", "FOO_STATE").accept(MediaType.APPLICATION_XML)
  401. .get(JSONObject.class);
  402. fail("should have thrown exception on invalid user query");
  403. } catch (UniformInterfaceException ue) {
  404. ClientResponse response = ue.getResponse();
  405. assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus());
  406. assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
  407. String msg = response.getEntity(String.class);
  408. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  409. DocumentBuilder db = dbf.newDocumentBuilder();
  410. InputSource is = new InputSource();
  411. is.setCharacterStream(new StringReader(msg));
  412. Document dom = db.parse(is);
  413. NodeList nodes = dom.getElementsByTagName("RemoteException");
  414. Element element = (Element) nodes.item(0);
  415. String message = WebServicesTestUtils.getXmlString(element, "message");
  416. String type = WebServicesTestUtils.getXmlString(element, "exception");
  417. String classname = WebServicesTestUtils.getXmlString(element,
  418. "javaClassName");
  419. verifyStateInvalidException(message, type, classname);
  420. }
  421. }
  422. private void verifyStateInvalidException(String message, String type,
  423. String classname) {
  424. WebServicesTestUtils
  425. .checkStringContains(
  426. "exception message",
  427. "org.apache.hadoop.yarn.server.nodemanager.containermanager.application.ApplicationState.FOO_STATE",
  428. message);
  429. WebServicesTestUtils.checkStringMatch("exception type",
  430. "IllegalArgumentException", type);
  431. WebServicesTestUtils.checkStringMatch("exception classname",
  432. "java.lang.IllegalArgumentException", classname);
  433. }
  434. @Test
  435. public void testNodeSingleApps() throws JSONException, Exception {
  436. testNodeSingleAppHelper(MediaType.APPLICATION_JSON);
  437. }
  438. // make sure default is json output
  439. @Test
  440. public void testNodeSingleAppsDefault() throws JSONException, Exception {
  441. testNodeSingleAppHelper("");
  442. }
  443. public void testNodeSingleAppHelper(String media) throws JSONException,
  444. Exception {
  445. WebResource r = resource();
  446. Application app = new MockApp(1);
  447. nmContext.getApplications().put(app.getAppId(), app);
  448. HashMap<String, String> hash = addAppContainers(app);
  449. Application app2 = new MockApp(2);
  450. nmContext.getApplications().put(app2.getAppId(), app2);
  451. addAppContainers(app2);
  452. ClientResponse response = r.path("ws").path("v1").path("node").path("apps")
  453. .path(app.getAppId().toString()).accept(media)
  454. .get(ClientResponse.class);
  455. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  456. JSONObject json = response.getEntity(JSONObject.class);
  457. verifyNodeAppInfo(json.getJSONObject("app"), app, hash);
  458. }
  459. @Test
  460. public void testNodeSingleAppsSlash() throws JSONException, Exception {
  461. WebResource r = resource();
  462. Application app = new MockApp(1);
  463. nmContext.getApplications().put(app.getAppId(), app);
  464. HashMap<String, String> hash = addAppContainers(app);
  465. Application app2 = new MockApp(2);
  466. nmContext.getApplications().put(app2.getAppId(), app2);
  467. addAppContainers(app2);
  468. ClientResponse response = r.path("ws").path("v1").path("node").path("apps")
  469. .path(app.getAppId().toString() + "/")
  470. .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  471. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  472. JSONObject json = response.getEntity(JSONObject.class);
  473. verifyNodeAppInfo(json.getJSONObject("app"), app, hash);
  474. }
  475. @Test
  476. public void testNodeSingleAppsInvalid() throws JSONException, Exception {
  477. WebResource r = resource();
  478. Application app = new MockApp(1);
  479. nmContext.getApplications().put(app.getAppId(), app);
  480. addAppContainers(app);
  481. Application app2 = new MockApp(2);
  482. nmContext.getApplications().put(app2.getAppId(), app2);
  483. addAppContainers(app2);
  484. try {
  485. r.path("ws").path("v1").path("node").path("apps").path("app_foo_0000")
  486. .accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
  487. fail("should have thrown exception on invalid user query");
  488. } catch (UniformInterfaceException ue) {
  489. ClientResponse response = ue.getResponse();
  490. assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus());
  491. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  492. JSONObject msg = response.getEntity(JSONObject.class);
  493. JSONObject exception = msg.getJSONObject("RemoteException");
  494. assertEquals("incorrect number of elements", 3, exception.length());
  495. String message = exception.getString("message");
  496. String type = exception.getString("exception");
  497. String classname = exception.getString("javaClassName");
  498. WebServicesTestUtils.checkStringMatch("exception message",
  499. "For input string: \"foo\"", message);
  500. WebServicesTestUtils.checkStringMatch("exception type",
  501. "NumberFormatException", type);
  502. WebServicesTestUtils.checkStringMatch("exception classname",
  503. "java.lang.NumberFormatException", classname);
  504. }
  505. }
  506. @Test
  507. public void testNodeSingleAppsMissing() throws JSONException, Exception {
  508. WebResource r = resource();
  509. Application app = new MockApp(1);
  510. nmContext.getApplications().put(app.getAppId(), app);
  511. addAppContainers(app);
  512. Application app2 = new MockApp(2);
  513. nmContext.getApplications().put(app2.getAppId(), app2);
  514. addAppContainers(app2);
  515. try {
  516. r.path("ws").path("v1").path("node").path("apps")
  517. .path("application_1234_0009").accept(MediaType.APPLICATION_JSON)
  518. .get(JSONObject.class);
  519. fail("should have thrown exception on invalid user query");
  520. } catch (UniformInterfaceException ue) {
  521. ClientResponse response = ue.getResponse();
  522. assertEquals(Status.NOT_FOUND, response.getClientResponseStatus());
  523. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  524. JSONObject msg = response.getEntity(JSONObject.class);
  525. JSONObject exception = msg.getJSONObject("RemoteException");
  526. assertEquals("incorrect number of elements", 3, exception.length());
  527. String message = exception.getString("message");
  528. String type = exception.getString("exception");
  529. String classname = exception.getString("javaClassName");
  530. WebServicesTestUtils.checkStringMatch("exception message",
  531. "java.lang.Exception: app with id application_1234_0009 not found",
  532. message);
  533. WebServicesTestUtils.checkStringMatch("exception type",
  534. "NotFoundException", type);
  535. WebServicesTestUtils.checkStringMatch("exception classname",
  536. "org.apache.hadoop.yarn.webapp.NotFoundException", classname);
  537. }
  538. }
  539. @Test
  540. public void testNodeAppsXML() throws JSONException, Exception {
  541. WebResource r = resource();
  542. Application app = new MockApp(1);
  543. nmContext.getApplications().put(app.getAppId(), app);
  544. addAppContainers(app);
  545. Application app2 = new MockApp(2);
  546. nmContext.getApplications().put(app2.getAppId(), app2);
  547. addAppContainers(app2);
  548. ClientResponse response = r.path("ws").path("v1").path("node").path("apps")
  549. .accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
  550. assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
  551. String xml = response.getEntity(String.class);
  552. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  553. DocumentBuilder db = dbf.newDocumentBuilder();
  554. InputSource is = new InputSource();
  555. is.setCharacterStream(new StringReader(xml));
  556. Document dom = db.parse(is);
  557. NodeList nodes = dom.getElementsByTagName("app");
  558. assertEquals("incorrect number of elements", 2, nodes.getLength());
  559. }
  560. @Test
  561. public void testNodeSingleAppsXML() throws JSONException, Exception {
  562. WebResource r = resource();
  563. Application app = new MockApp(1);
  564. nmContext.getApplications().put(app.getAppId(), app);
  565. HashMap<String, String> hash = addAppContainers(app);
  566. Application app2 = new MockApp(2);
  567. nmContext.getApplications().put(app2.getAppId(), app2);
  568. addAppContainers(app2);
  569. ClientResponse response = r.path("ws").path("v1").path("node").path("apps")
  570. .path(app.getAppId().toString() + "/")
  571. .accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
  572. assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
  573. String xml = response.getEntity(String.class);
  574. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  575. DocumentBuilder db = dbf.newDocumentBuilder();
  576. InputSource is = new InputSource();
  577. is.setCharacterStream(new StringReader(xml));
  578. Document dom = db.parse(is);
  579. NodeList nodes = dom.getElementsByTagName("app");
  580. assertEquals("incorrect number of elements", 1, nodes.getLength());
  581. verifyNodeAppInfoXML(nodes, app, hash);
  582. }
  583. public void verifyNodeAppInfoXML(NodeList nodes, Application app,
  584. HashMap<String, String> hash) throws JSONException, Exception {
  585. for (int i = 0; i < nodes.getLength(); i++) {
  586. Element element = (Element) nodes.item(i);
  587. verifyNodeAppInfoGeneric(app,
  588. WebServicesTestUtils.getXmlString(element, "id"),
  589. WebServicesTestUtils.getXmlString(element, "state"),
  590. WebServicesTestUtils.getXmlString(element, "user"));
  591. NodeList ids = element.getElementsByTagName("containerids");
  592. for (int j = 0; j < ids.getLength(); j++) {
  593. Element line = (Element) ids.item(j);
  594. Node first = line.getFirstChild();
  595. String val = first.getNodeValue();
  596. assertEquals("extra containerid: " + val, val, hash.remove(val));
  597. }
  598. assertTrue("missing containerids", hash.isEmpty());
  599. }
  600. }
  601. public void verifyNodeAppInfo(JSONObject info, Application app,
  602. HashMap<String, String> hash) throws JSONException, Exception {
  603. assertEquals("incorrect number of elements", 4, info.length());
  604. verifyNodeAppInfoGeneric(app, info.getString("id"),
  605. info.getString("state"), info.getString("user"));
  606. JSONArray containerids = info.getJSONArray("containerids");
  607. for (int i = 0; i < containerids.length(); i++) {
  608. String id = containerids.getString(i);
  609. assertEquals("extra containerid: " + id, id, hash.remove(id));
  610. }
  611. assertTrue("missing containerids", hash.isEmpty());
  612. }
  613. public void verifyNodeAppInfoGeneric(Application app, String id,
  614. String state, String user) throws JSONException, Exception {
  615. WebServicesTestUtils.checkStringMatch("id", app.getAppId().toString(), id);
  616. WebServicesTestUtils.checkStringMatch("state", app.getApplicationState()
  617. .toString(), state);
  618. WebServicesTestUtils.checkStringMatch("user", app.getUser().toString(),
  619. user);
  620. }
  621. }