PageRenderTime 858ms CodeModel.GetById 12ms 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/timeline/webapp/TestTimelineWebServices.java

http://github.com/apache/hadoop-common
Java | 671 lines | 596 code | 48 blank | 27 comment | 0 complexity | 52ef8d0d61355eeda50f650cabe7e478 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  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.timeline.webapp;
  19. import static org.junit.Assert.assertEquals;
  20. import static org.mockito.Matchers.any;
  21. import static org.mockito.Mockito.doNothing;
  22. import static org.mockito.Mockito.mock;
  23. import static org.mockito.Mockito.spy;
  24. import static org.mockito.Mockito.when;
  25. import java.util.Enumeration;
  26. import java.util.HashMap;
  27. import java.util.HashSet;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.Set;
  31. import javax.servlet.FilterConfig;
  32. import javax.servlet.ServletContext;
  33. import javax.servlet.ServletException;
  34. import javax.ws.rs.core.MediaType;
  35. import org.apache.hadoop.conf.Configuration;
  36. import org.apache.hadoop.security.authentication.server.AuthenticationFilter;
  37. import org.apache.hadoop.security.authentication.server.PseudoAuthenticationHandler;
  38. import org.apache.hadoop.yarn.api.records.timeline.TimelineEntities;
  39. import org.apache.hadoop.yarn.api.records.timeline.TimelineEntity;
  40. import org.apache.hadoop.yarn.api.records.timeline.TimelineEvent;
  41. import org.apache.hadoop.yarn.api.records.timeline.TimelineEvents;
  42. import org.apache.hadoop.yarn.api.records.timeline.TimelinePutResponse;
  43. import org.apache.hadoop.yarn.api.records.timeline.TimelinePutResponse.TimelinePutError;
  44. import org.apache.hadoop.yarn.conf.YarnConfiguration;
  45. import org.apache.hadoop.yarn.security.AdminACLsManager;
  46. import org.apache.hadoop.yarn.server.timeline.TestMemoryTimelineStore;
  47. import org.apache.hadoop.yarn.server.timeline.TimelineDataManager;
  48. import org.apache.hadoop.yarn.server.timeline.TimelineStore;
  49. import org.apache.hadoop.yarn.server.timeline.security.TimelineACLsManager;
  50. import org.apache.hadoop.yarn.server.timeline.security.TimelineAuthenticationFilter;
  51. import org.apache.hadoop.yarn.webapp.GenericExceptionHandler;
  52. import org.apache.hadoop.yarn.webapp.YarnJacksonJaxbJsonProvider;
  53. import org.junit.Assert;
  54. import org.junit.Test;
  55. import com.google.inject.Guice;
  56. import com.google.inject.Injector;
  57. import com.google.inject.servlet.GuiceServletContextListener;
  58. import com.google.inject.servlet.ServletModule;
  59. import com.sun.jersey.api.client.ClientResponse;
  60. import com.sun.jersey.api.client.WebResource;
  61. import com.sun.jersey.api.client.config.DefaultClientConfig;
  62. import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
  63. import com.sun.jersey.test.framework.JerseyTest;
  64. import com.sun.jersey.test.framework.WebAppDescriptor;
  65. public class TestTimelineWebServices extends JerseyTest {
  66. private static TimelineStore store;
  67. private static TimelineACLsManager timelineACLsManager;
  68. private static AdminACLsManager adminACLsManager;
  69. private long beforeTime;
  70. private Injector injector = Guice.createInjector(new ServletModule() {
  71. @SuppressWarnings("unchecked")
  72. @Override
  73. protected void configureServlets() {
  74. bind(YarnJacksonJaxbJsonProvider.class);
  75. bind(TimelineWebServices.class);
  76. bind(GenericExceptionHandler.class);
  77. try{
  78. store = mockTimelineStore();
  79. } catch (Exception e) {
  80. Assert.fail();
  81. }
  82. Configuration conf = new YarnConfiguration();
  83. conf.setBoolean(YarnConfiguration.YARN_ACL_ENABLE, false);
  84. timelineACLsManager = new TimelineACLsManager(conf);
  85. conf.setBoolean(YarnConfiguration.YARN_ACL_ENABLE, true);
  86. conf.set(YarnConfiguration.YARN_ADMIN_ACL, "admin");
  87. adminACLsManager = new AdminACLsManager(conf);
  88. TimelineDataManager timelineDataManager =
  89. new TimelineDataManager(store, timelineACLsManager);
  90. bind(TimelineDataManager.class).toInstance(timelineDataManager);
  91. serve("/*").with(GuiceContainer.class);
  92. TimelineAuthenticationFilter taFilter = new TimelineAuthenticationFilter();
  93. FilterConfig filterConfig = mock(FilterConfig.class);
  94. when(filterConfig.getInitParameter(AuthenticationFilter.CONFIG_PREFIX))
  95. .thenReturn(null);
  96. when(filterConfig.getInitParameter(AuthenticationFilter.AUTH_TYPE))
  97. .thenReturn("simple");
  98. when(filterConfig.getInitParameter(
  99. PseudoAuthenticationHandler.ANONYMOUS_ALLOWED)).thenReturn("true");
  100. ServletContext context = mock(ServletContext.class);
  101. when(filterConfig.getServletContext()).thenReturn(context);
  102. Enumeration<Object> names = mock(Enumeration.class);
  103. when(names.hasMoreElements()).thenReturn(true, true, false);
  104. when(names.nextElement()).thenReturn(
  105. AuthenticationFilter.AUTH_TYPE,
  106. PseudoAuthenticationHandler.ANONYMOUS_ALLOWED);
  107. when(filterConfig.getInitParameterNames()).thenReturn(names);
  108. try {
  109. taFilter.init(filterConfig);
  110. } catch (ServletException e) {
  111. Assert.fail("Unable to initialize TimelineAuthenticationFilter: " +
  112. e.getMessage());
  113. }
  114. taFilter = spy(taFilter);
  115. try {
  116. doNothing().when(taFilter).init(any(FilterConfig.class));
  117. } catch (ServletException e) {
  118. Assert.fail("Unable to initialize TimelineAuthenticationFilter: " +
  119. e.getMessage());
  120. }
  121. filter("/*").through(taFilter);
  122. }
  123. });
  124. public class GuiceServletConfig extends GuiceServletContextListener {
  125. @Override
  126. protected Injector getInjector() {
  127. return injector;
  128. }
  129. }
  130. private TimelineStore mockTimelineStore()
  131. throws Exception {
  132. beforeTime = System.currentTimeMillis() - 1;
  133. TestMemoryTimelineStore store =
  134. new TestMemoryTimelineStore();
  135. store.setup();
  136. return store.getTimelineStore();
  137. }
  138. public TestTimelineWebServices() {
  139. super(new WebAppDescriptor.Builder(
  140. "org.apache.hadoop.yarn.server.applicationhistoryservice.webapp")
  141. .contextListenerClass(GuiceServletConfig.class)
  142. .filterClass(com.google.inject.servlet.GuiceFilter.class)
  143. .contextPath("jersey-guice-filter")
  144. .servletPath("/")
  145. .clientConfig(new DefaultClientConfig(YarnJacksonJaxbJsonProvider.class))
  146. .build());
  147. }
  148. @Test
  149. public void testAbout() throws Exception {
  150. WebResource r = resource();
  151. ClientResponse response = r.path("ws").path("v1").path("timeline")
  152. .accept(MediaType.APPLICATION_JSON)
  153. .get(ClientResponse.class);
  154. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  155. TimelineWebServices.AboutInfo about =
  156. response.getEntity(TimelineWebServices.AboutInfo.class);
  157. Assert.assertNotNull(about);
  158. Assert.assertEquals("Timeline API", about.getAbout());
  159. }
  160. private static void verifyEntities(TimelineEntities entities) {
  161. Assert.assertNotNull(entities);
  162. Assert.assertEquals(2, entities.getEntities().size());
  163. TimelineEntity entity1 = entities.getEntities().get(0);
  164. Assert.assertNotNull(entity1);
  165. Assert.assertEquals("id_1", entity1.getEntityId());
  166. Assert.assertEquals("type_1", entity1.getEntityType());
  167. Assert.assertEquals(123l, entity1.getStartTime().longValue());
  168. Assert.assertEquals(2, entity1.getEvents().size());
  169. Assert.assertEquals(4, entity1.getPrimaryFilters().size());
  170. Assert.assertEquals(4, entity1.getOtherInfo().size());
  171. TimelineEntity entity2 = entities.getEntities().get(1);
  172. Assert.assertNotNull(entity2);
  173. Assert.assertEquals("id_2", entity2.getEntityId());
  174. Assert.assertEquals("type_1", entity2.getEntityType());
  175. Assert.assertEquals(123l, entity2.getStartTime().longValue());
  176. Assert.assertEquals(2, entity2.getEvents().size());
  177. Assert.assertEquals(4, entity2.getPrimaryFilters().size());
  178. Assert.assertEquals(4, entity2.getOtherInfo().size());
  179. }
  180. @Test
  181. public void testGetEntities() throws Exception {
  182. WebResource r = resource();
  183. ClientResponse response = r.path("ws").path("v1").path("timeline")
  184. .path("type_1")
  185. .accept(MediaType.APPLICATION_JSON)
  186. .get(ClientResponse.class);
  187. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  188. verifyEntities(response.getEntity(TimelineEntities.class));
  189. }
  190. @Test
  191. public void testFromId() throws Exception {
  192. WebResource r = resource();
  193. ClientResponse response = r.path("ws").path("v1").path("timeline")
  194. .path("type_1").queryParam("fromId", "id_2")
  195. .accept(MediaType.APPLICATION_JSON)
  196. .get(ClientResponse.class);
  197. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  198. assertEquals(1, response.getEntity(TimelineEntities.class).getEntities()
  199. .size());
  200. response = r.path("ws").path("v1").path("timeline")
  201. .path("type_1").queryParam("fromId", "id_1")
  202. .accept(MediaType.APPLICATION_JSON)
  203. .get(ClientResponse.class);
  204. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  205. assertEquals(2, response.getEntity(TimelineEntities.class).getEntities()
  206. .size());
  207. }
  208. @Test
  209. public void testFromTs() throws Exception {
  210. WebResource r = resource();
  211. ClientResponse response = r.path("ws").path("v1").path("timeline")
  212. .path("type_1").queryParam("fromTs", Long.toString(beforeTime))
  213. .accept(MediaType.APPLICATION_JSON)
  214. .get(ClientResponse.class);
  215. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  216. assertEquals(0, response.getEntity(TimelineEntities.class).getEntities()
  217. .size());
  218. response = r.path("ws").path("v1").path("timeline")
  219. .path("type_1").queryParam("fromTs", Long.toString(
  220. System.currentTimeMillis()))
  221. .accept(MediaType.APPLICATION_JSON)
  222. .get(ClientResponse.class);
  223. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  224. assertEquals(2, response.getEntity(TimelineEntities.class).getEntities()
  225. .size());
  226. }
  227. @Test
  228. public void testPrimaryFilterString() {
  229. WebResource r = resource();
  230. ClientResponse response = r.path("ws").path("v1").path("timeline")
  231. .path("type_1").queryParam("primaryFilter", "user:username")
  232. .accept(MediaType.APPLICATION_JSON)
  233. .get(ClientResponse.class);
  234. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  235. verifyEntities(response.getEntity(TimelineEntities.class));
  236. }
  237. @Test
  238. public void testPrimaryFilterInteger() {
  239. WebResource r = resource();
  240. ClientResponse response = r.path("ws").path("v1").path("timeline")
  241. .path("type_1").queryParam("primaryFilter",
  242. "appname:" + Integer.toString(Integer.MAX_VALUE))
  243. .accept(MediaType.APPLICATION_JSON)
  244. .get(ClientResponse.class);
  245. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  246. verifyEntities(response.getEntity(TimelineEntities.class));
  247. }
  248. @Test
  249. public void testPrimaryFilterLong() {
  250. WebResource r = resource();
  251. ClientResponse response = r.path("ws").path("v1").path("timeline")
  252. .path("type_1").queryParam("primaryFilter",
  253. "long:" + Long.toString((long)Integer.MAX_VALUE + 1l))
  254. .accept(MediaType.APPLICATION_JSON)
  255. .get(ClientResponse.class);
  256. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  257. verifyEntities(response.getEntity(TimelineEntities.class));
  258. }
  259. @Test
  260. public void testPrimaryFilterNumericString() {
  261. // without quotes, 123abc is interpreted as the number 123,
  262. // which finds no entities
  263. WebResource r = resource();
  264. ClientResponse response = r.path("ws").path("v1").path("timeline")
  265. .path("type_1").queryParam("primaryFilter", "other:123abc")
  266. .accept(MediaType.APPLICATION_JSON)
  267. .get(ClientResponse.class);
  268. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  269. assertEquals(0, response.getEntity(TimelineEntities.class).getEntities()
  270. .size());
  271. }
  272. @Test
  273. public void testPrimaryFilterNumericStringWithQuotes() {
  274. WebResource r = resource();
  275. ClientResponse response = r.path("ws").path("v1").path("timeline")
  276. .path("type_1").queryParam("primaryFilter", "other:\"123abc\"")
  277. .accept(MediaType.APPLICATION_JSON)
  278. .get(ClientResponse.class);
  279. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  280. verifyEntities(response.getEntity(TimelineEntities.class));
  281. }
  282. @Test
  283. public void testSecondaryFilters() {
  284. WebResource r = resource();
  285. ClientResponse response = r.path("ws").path("v1").path("timeline")
  286. .path("type_1")
  287. .queryParam("secondaryFilter",
  288. "user:username,appname:" + Integer.toString(Integer.MAX_VALUE))
  289. .accept(MediaType.APPLICATION_JSON)
  290. .get(ClientResponse.class);
  291. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  292. verifyEntities(response.getEntity(TimelineEntities.class));
  293. }
  294. @Test
  295. public void testGetEntity() throws Exception {
  296. WebResource r = resource();
  297. ClientResponse response = r.path("ws").path("v1").path("timeline")
  298. .path("type_1").path("id_1")
  299. .accept(MediaType.APPLICATION_JSON)
  300. .get(ClientResponse.class);
  301. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  302. TimelineEntity entity = response.getEntity(TimelineEntity.class);
  303. Assert.assertNotNull(entity);
  304. Assert.assertEquals("id_1", entity.getEntityId());
  305. Assert.assertEquals("type_1", entity.getEntityType());
  306. Assert.assertEquals(123l, entity.getStartTime().longValue());
  307. Assert.assertEquals(2, entity.getEvents().size());
  308. Assert.assertEquals(4, entity.getPrimaryFilters().size());
  309. Assert.assertEquals(4, entity.getOtherInfo().size());
  310. }
  311. @Test
  312. public void testGetEntityFields1() throws Exception {
  313. WebResource r = resource();
  314. ClientResponse response = r.path("ws").path("v1").path("timeline")
  315. .path("type_1").path("id_1").queryParam("fields", "events,otherinfo")
  316. .accept(MediaType.APPLICATION_JSON)
  317. .get(ClientResponse.class);
  318. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  319. TimelineEntity entity = response.getEntity(TimelineEntity.class);
  320. Assert.assertNotNull(entity);
  321. Assert.assertEquals("id_1", entity.getEntityId());
  322. Assert.assertEquals("type_1", entity.getEntityType());
  323. Assert.assertEquals(123l, entity.getStartTime().longValue());
  324. Assert.assertEquals(2, entity.getEvents().size());
  325. Assert.assertEquals(0, entity.getPrimaryFilters().size());
  326. Assert.assertEquals(4, entity.getOtherInfo().size());
  327. }
  328. @Test
  329. public void testGetEntityFields2() throws Exception {
  330. WebResource r = resource();
  331. ClientResponse response = r.path("ws").path("v1").path("timeline")
  332. .path("type_1").path("id_1").queryParam("fields", "lasteventonly," +
  333. "primaryfilters,relatedentities")
  334. .accept(MediaType.APPLICATION_JSON)
  335. .get(ClientResponse.class);
  336. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  337. TimelineEntity entity = response.getEntity(TimelineEntity.class);
  338. Assert.assertNotNull(entity);
  339. Assert.assertEquals("id_1", entity.getEntityId());
  340. Assert.assertEquals("type_1", entity.getEntityType());
  341. Assert.assertEquals(123l, entity.getStartTime().longValue());
  342. Assert.assertEquals(1, entity.getEvents().size());
  343. Assert.assertEquals(4, entity.getPrimaryFilters().size());
  344. Assert.assertEquals(0, entity.getOtherInfo().size());
  345. }
  346. @Test
  347. public void testGetEvents() throws Exception {
  348. WebResource r = resource();
  349. ClientResponse response = r.path("ws").path("v1").path("timeline")
  350. .path("type_1").path("events")
  351. .queryParam("entityId", "id_1")
  352. .accept(MediaType.APPLICATION_JSON)
  353. .get(ClientResponse.class);
  354. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  355. TimelineEvents events = response.getEntity(TimelineEvents.class);
  356. Assert.assertNotNull(events);
  357. Assert.assertEquals(1, events.getAllEvents().size());
  358. TimelineEvents.EventsOfOneEntity partEvents = events.getAllEvents().get(0);
  359. Assert.assertEquals(2, partEvents.getEvents().size());
  360. TimelineEvent event1 = partEvents.getEvents().get(0);
  361. Assert.assertEquals(456l, event1.getTimestamp());
  362. Assert.assertEquals("end_event", event1.getEventType());
  363. Assert.assertEquals(1, event1.getEventInfo().size());
  364. TimelineEvent event2 = partEvents.getEvents().get(1);
  365. Assert.assertEquals(123l, event2.getTimestamp());
  366. Assert.assertEquals("start_event", event2.getEventType());
  367. Assert.assertEquals(0, event2.getEventInfo().size());
  368. }
  369. @Test
  370. public void testPostEntitiesWithPrimaryFilter() throws Exception {
  371. TimelineEntities entities = new TimelineEntities();
  372. TimelineEntity entity = new TimelineEntity();
  373. Map<String, Set<Object>> filters = new HashMap<String, Set<Object>>();
  374. filters.put(TimelineStore.SystemFilter.ENTITY_OWNER.toString(), new HashSet<Object>());
  375. entity.setPrimaryFilters(filters);
  376. entity.setEntityId("test id 6");
  377. entity.setEntityType("test type 6");
  378. entity.setStartTime(System.currentTimeMillis());
  379. entities.addEntity(entity);
  380. WebResource r = resource();
  381. ClientResponse response = r.path("ws").path("v1").path("timeline")
  382. .queryParam("user.name", "tester")
  383. .accept(MediaType.APPLICATION_JSON)
  384. .type(MediaType.APPLICATION_JSON)
  385. .post(ClientResponse.class, entities);
  386. TimelinePutResponse putResposne = response.getEntity(TimelinePutResponse.class);
  387. Assert.assertEquals(1, putResposne.getErrors().size());
  388. List<TimelinePutError> errors = putResposne.getErrors();
  389. Assert.assertEquals(TimelinePutResponse.TimelinePutError.SYSTEM_FILTER_CONFLICT,
  390. errors.get(0).getErrorCode());
  391. }
  392. @Test
  393. public void testPostEntities() throws Exception {
  394. TimelineEntities entities = new TimelineEntities();
  395. TimelineEntity entity = new TimelineEntity();
  396. entity.setEntityId("test id 1");
  397. entity.setEntityType("test type 1");
  398. entity.setStartTime(System.currentTimeMillis());
  399. entities.addEntity(entity);
  400. WebResource r = resource();
  401. // No owner, will be rejected
  402. ClientResponse response = r.path("ws").path("v1").path("timeline")
  403. .accept(MediaType.APPLICATION_JSON)
  404. .type(MediaType.APPLICATION_JSON)
  405. .post(ClientResponse.class, entities);
  406. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  407. assertEquals(ClientResponse.Status.FORBIDDEN,
  408. response.getClientResponseStatus());
  409. response = r.path("ws").path("v1").path("timeline")
  410. .queryParam("user.name", "tester")
  411. .accept(MediaType.APPLICATION_JSON)
  412. .type(MediaType.APPLICATION_JSON)
  413. .post(ClientResponse.class, entities);
  414. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  415. TimelinePutResponse putResposne = response.getEntity(TimelinePutResponse.class);
  416. Assert.assertNotNull(putResposne);
  417. Assert.assertEquals(0, putResposne.getErrors().size());
  418. // verify the entity exists in the store
  419. response = r.path("ws").path("v1").path("timeline")
  420. .path("test type 1").path("test id 1")
  421. .accept(MediaType.APPLICATION_JSON)
  422. .get(ClientResponse.class);
  423. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  424. entity = response.getEntity(TimelineEntity.class);
  425. Assert.assertNotNull(entity);
  426. Assert.assertEquals("test id 1", entity.getEntityId());
  427. Assert.assertEquals("test type 1", entity.getEntityType());
  428. }
  429. @Test
  430. public void testPostEntitiesWithYarnACLsEnabled() throws Exception {
  431. AdminACLsManager oldAdminACLsManager =
  432. timelineACLsManager.setAdminACLsManager(adminACLsManager);
  433. try {
  434. TimelineEntities entities = new TimelineEntities();
  435. TimelineEntity entity = new TimelineEntity();
  436. entity.setEntityId("test id 2");
  437. entity.setEntityType("test type 2");
  438. entity.setStartTime(System.currentTimeMillis());
  439. entities.addEntity(entity);
  440. WebResource r = resource();
  441. ClientResponse response = r.path("ws").path("v1").path("timeline")
  442. .queryParam("user.name", "tester")
  443. .accept(MediaType.APPLICATION_JSON)
  444. .type(MediaType.APPLICATION_JSON)
  445. .post(ClientResponse.class, entities);
  446. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  447. TimelinePutResponse putResponse = response.getEntity(TimelinePutResponse.class);
  448. Assert.assertNotNull(putResponse);
  449. Assert.assertEquals(0, putResponse.getErrors().size());
  450. // override/append timeline data in the same entity with different user
  451. response = r.path("ws").path("v1").path("timeline")
  452. .queryParam("user.name", "other")
  453. .accept(MediaType.APPLICATION_JSON)
  454. .type(MediaType.APPLICATION_JSON)
  455. .post(ClientResponse.class, entities);
  456. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  457. putResponse = response.getEntity(TimelinePutResponse.class);
  458. Assert.assertNotNull(putResponse);
  459. Assert.assertEquals(1, putResponse.getErrors().size());
  460. Assert.assertEquals(TimelinePutResponse.TimelinePutError.ACCESS_DENIED,
  461. putResponse.getErrors().get(0).getErrorCode());
  462. } finally {
  463. timelineACLsManager.setAdminACLsManager(oldAdminACLsManager);
  464. }
  465. }
  466. @Test
  467. public void testGetEntityWithYarnACLsEnabled() throws Exception {
  468. AdminACLsManager oldAdminACLsManager =
  469. timelineACLsManager.setAdminACLsManager(adminACLsManager);
  470. try {
  471. TimelineEntities entities = new TimelineEntities();
  472. TimelineEntity entity = new TimelineEntity();
  473. entity.setEntityId("test id 3");
  474. entity.setEntityType("test type 3");
  475. entity.setStartTime(System.currentTimeMillis());
  476. entities.addEntity(entity);
  477. WebResource r = resource();
  478. ClientResponse response = r.path("ws").path("v1").path("timeline")
  479. .queryParam("user.name", "tester")
  480. .accept(MediaType.APPLICATION_JSON)
  481. .type(MediaType.APPLICATION_JSON)
  482. .post(ClientResponse.class, entities);
  483. // verify the system data will not be exposed
  484. // 1. No field specification
  485. response = r.path("ws").path("v1").path("timeline")
  486. .path("test type 3").path("test id 3")
  487. .queryParam("user.name", "tester")
  488. .accept(MediaType.APPLICATION_JSON)
  489. .get(ClientResponse.class);
  490. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  491. entity = response.getEntity(TimelineEntity.class);
  492. Assert.assertNull(entity.getPrimaryFilters().get(
  493. TimelineStore.SystemFilter.ENTITY_OWNER.toString()));
  494. // 2. other field
  495. response = r.path("ws").path("v1").path("timeline")
  496. .path("test type 3").path("test id 3")
  497. .queryParam("fields", "relatedentities")
  498. .queryParam("user.name", "tester")
  499. .accept(MediaType.APPLICATION_JSON)
  500. .get(ClientResponse.class);
  501. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  502. entity = response.getEntity(TimelineEntity.class);
  503. Assert.assertNull(entity.getPrimaryFilters().get(
  504. TimelineStore.SystemFilter.ENTITY_OWNER.toString()));
  505. // 3. primaryfilters field
  506. response = r.path("ws").path("v1").path("timeline")
  507. .path("test type 3").path("test id 3")
  508. .queryParam("fields", "primaryfilters")
  509. .queryParam("user.name", "tester")
  510. .accept(MediaType.APPLICATION_JSON)
  511. .get(ClientResponse.class);
  512. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  513. entity = response.getEntity(TimelineEntity.class);
  514. Assert.assertNull(entity.getPrimaryFilters().get(
  515. TimelineStore.SystemFilter.ENTITY_OWNER.toString()));
  516. // get entity with other user
  517. response = r.path("ws").path("v1").path("timeline")
  518. .path("test type 3").path("test id 3")
  519. .queryParam("user.name", "other")
  520. .accept(MediaType.APPLICATION_JSON)
  521. .get(ClientResponse.class);
  522. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  523. assertEquals(ClientResponse.Status.NOT_FOUND,
  524. response.getClientResponseStatus());
  525. } finally {
  526. timelineACLsManager.setAdminACLsManager(oldAdminACLsManager);
  527. }
  528. }
  529. @Test
  530. public void testGetEntitiesWithYarnACLsEnabled() {
  531. AdminACLsManager oldAdminACLsManager =
  532. timelineACLsManager.setAdminACLsManager(adminACLsManager);
  533. try {
  534. TimelineEntities entities = new TimelineEntities();
  535. TimelineEntity entity = new TimelineEntity();
  536. entity.setEntityId("test id 4");
  537. entity.setEntityType("test type 4");
  538. entity.setStartTime(System.currentTimeMillis());
  539. entities.addEntity(entity);
  540. WebResource r = resource();
  541. ClientResponse response = r.path("ws").path("v1").path("timeline")
  542. .queryParam("user.name", "tester")
  543. .accept(MediaType.APPLICATION_JSON)
  544. .type(MediaType.APPLICATION_JSON)
  545. .post(ClientResponse.class, entities);
  546. entities = new TimelineEntities();
  547. entity = new TimelineEntity();
  548. entity.setEntityId("test id 5");
  549. entity.setEntityType("test type 4");
  550. entity.setStartTime(System.currentTimeMillis());
  551. entities.addEntity(entity);
  552. r = resource();
  553. response = r.path("ws").path("v1").path("timeline")
  554. .queryParam("user.name", "other")
  555. .accept(MediaType.APPLICATION_JSON)
  556. .type(MediaType.APPLICATION_JSON)
  557. .post(ClientResponse.class, entities);
  558. response = r.path("ws").path("v1").path("timeline")
  559. .queryParam("user.name", "other")
  560. .path("test type 4")
  561. .accept(MediaType.APPLICATION_JSON)
  562. .get(ClientResponse.class);
  563. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  564. entities = response.getEntity(TimelineEntities.class);
  565. assertEquals(1, entities.getEntities().size());
  566. assertEquals("test type 4", entities.getEntities().get(0).getEntityType());
  567. assertEquals("test id 5", entities.getEntities().get(0).getEntityId());
  568. } finally {
  569. timelineACLsManager.setAdminACLsManager(oldAdminACLsManager);
  570. }
  571. }
  572. @Test
  573. public void testGetEventsWithYarnACLsEnabled() {
  574. AdminACLsManager oldAdminACLsManager =
  575. timelineACLsManager.setAdminACLsManager(adminACLsManager);
  576. try {
  577. TimelineEntities entities = new TimelineEntities();
  578. TimelineEntity entity = new TimelineEntity();
  579. entity.setEntityId("test id 5");
  580. entity.setEntityType("test type 5");
  581. entity.setStartTime(System.currentTimeMillis());
  582. TimelineEvent event = new TimelineEvent();
  583. event.setEventType("event type 1");
  584. event.setTimestamp(System.currentTimeMillis());
  585. entity.addEvent(event);
  586. entities.addEntity(entity);
  587. WebResource r = resource();
  588. ClientResponse response = r.path("ws").path("v1").path("timeline")
  589. .queryParam("user.name", "tester")
  590. .accept(MediaType.APPLICATION_JSON)
  591. .type(MediaType.APPLICATION_JSON)
  592. .post(ClientResponse.class, entities);
  593. entities = new TimelineEntities();
  594. entity = new TimelineEntity();
  595. entity.setEntityId("test id 6");
  596. entity.setEntityType("test type 5");
  597. entity.setStartTime(System.currentTimeMillis());
  598. event = new TimelineEvent();
  599. event.setEventType("event type 2");
  600. event.setTimestamp(System.currentTimeMillis());
  601. entity.addEvent(event);
  602. entities.addEntity(entity);
  603. r = resource();
  604. response = r.path("ws").path("v1").path("timeline")
  605. .queryParam("user.name", "other")
  606. .accept(MediaType.APPLICATION_JSON)
  607. .type(MediaType.APPLICATION_JSON)
  608. .post(ClientResponse.class, entities);
  609. response = r.path("ws").path("v1").path("timeline")
  610. .path("test type 5").path("events")
  611. .queryParam("user.name", "other")
  612. .queryParam("entityId", "test id 5,test id 6")
  613. .accept(MediaType.APPLICATION_JSON)
  614. .get(ClientResponse.class);
  615. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  616. TimelineEvents events = response.getEntity(TimelineEvents.class);
  617. assertEquals(1, events.getAllEvents().size());
  618. assertEquals("test id 6", events.getAllEvents().get(0).getEntityId());
  619. } finally {
  620. timelineACLsManager.setAdminACLsManager(oldAdminACLsManager);
  621. }
  622. }
  623. }