PageRenderTime 2028ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/xiaoliuliu2050/hadoop
Java | 990 lines | 882 code | 63 blank | 45 comment | 4 complexity | 5d20a3f1b87b393d65b5c344473e6f1a 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.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.Collections;
  26. import java.util.Enumeration;
  27. import java.util.HashMap;
  28. import java.util.HashSet;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.Set;
  32. import javax.servlet.FilterConfig;
  33. import javax.servlet.ServletContext;
  34. import javax.servlet.ServletException;
  35. import javax.ws.rs.core.MediaType;
  36. import javax.ws.rs.core.Response.Status;
  37. import org.apache.hadoop.conf.Configuration;
  38. import org.apache.hadoop.security.authentication.server.AuthenticationFilter;
  39. import org.apache.hadoop.security.authentication.server.PseudoAuthenticationHandler;
  40. import org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticationHandler;
  41. import org.apache.hadoop.yarn.api.records.timeline.TimelineEntities;
  42. import org.apache.hadoop.yarn.api.records.timeline.TimelineEntity;
  43. import org.apache.hadoop.yarn.api.records.timeline.TimelineEvent;
  44. import org.apache.hadoop.yarn.api.records.timeline.TimelineEvents;
  45. import org.apache.hadoop.yarn.api.records.timeline.TimelineDomain;
  46. import org.apache.hadoop.yarn.api.records.timeline.TimelineDomains;
  47. import org.apache.hadoop.yarn.api.records.timeline.TimelinePutResponse;
  48. import org.apache.hadoop.yarn.api.records.timeline.TimelinePutResponse.TimelinePutError;
  49. import org.apache.hadoop.yarn.conf.YarnConfiguration;
  50. import org.apache.hadoop.yarn.security.AdminACLsManager;
  51. import org.apache.hadoop.yarn.security.client.TimelineDelegationTokenIdentifier;
  52. import org.apache.hadoop.yarn.server.timeline.TestMemoryTimelineStore;
  53. import org.apache.hadoop.yarn.server.timeline.TimelineDataManager;
  54. import org.apache.hadoop.yarn.server.timeline.TimelineStore;
  55. import org.apache.hadoop.yarn.server.timeline.security.TimelineACLsManager;
  56. import org.apache.hadoop.yarn.server.timeline.security.TimelineAuthenticationFilter;
  57. import org.apache.hadoop.yarn.webapp.GenericExceptionHandler;
  58. import org.apache.hadoop.yarn.webapp.YarnJacksonJaxbJsonProvider;
  59. import org.junit.Assert;
  60. import org.junit.Test;
  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.WebResource;
  67. import com.sun.jersey.api.client.config.DefaultClientConfig;
  68. import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
  69. import com.sun.jersey.test.framework.JerseyTest;
  70. import com.sun.jersey.test.framework.WebAppDescriptor;
  71. public class TestTimelineWebServices extends JerseyTest {
  72. private static TimelineStore store;
  73. private static TimelineACLsManager timelineACLsManager;
  74. private static AdminACLsManager adminACLsManager;
  75. private long beforeTime;
  76. private Injector injector = Guice.createInjector(new ServletModule() {
  77. @SuppressWarnings("unchecked")
  78. @Override
  79. protected void configureServlets() {
  80. bind(YarnJacksonJaxbJsonProvider.class);
  81. bind(TimelineWebServices.class);
  82. bind(GenericExceptionHandler.class);
  83. try {
  84. store = mockTimelineStore();
  85. } catch (Exception e) {
  86. Assert.fail();
  87. }
  88. Configuration conf = new YarnConfiguration();
  89. conf.setBoolean(YarnConfiguration.YARN_ACL_ENABLE, false);
  90. timelineACLsManager = new TimelineACLsManager(conf);
  91. timelineACLsManager.setTimelineStore(store);
  92. conf.setBoolean(YarnConfiguration.YARN_ACL_ENABLE, true);
  93. conf.set(YarnConfiguration.YARN_ADMIN_ACL, "admin");
  94. adminACLsManager = new AdminACLsManager(conf);
  95. TimelineDataManager timelineDataManager =
  96. new TimelineDataManager(store, timelineACLsManager);
  97. timelineDataManager.init(conf);
  98. timelineDataManager.start();
  99. bind(TimelineDataManager.class).toInstance(timelineDataManager);
  100. serve("/*").with(GuiceContainer.class);
  101. TimelineAuthenticationFilter taFilter =
  102. new TimelineAuthenticationFilter();
  103. FilterConfig filterConfig = mock(FilterConfig.class);
  104. when(filterConfig.getInitParameter(AuthenticationFilter.CONFIG_PREFIX))
  105. .thenReturn(null);
  106. when(filterConfig.getInitParameter(AuthenticationFilter.AUTH_TYPE))
  107. .thenReturn("simple");
  108. when(filterConfig.getInitParameter(
  109. PseudoAuthenticationHandler.ANONYMOUS_ALLOWED)).thenReturn("true");
  110. ServletContext context = mock(ServletContext.class);
  111. when(filterConfig.getServletContext()).thenReturn(context);
  112. Enumeration<Object> names = mock(Enumeration.class);
  113. when(names.hasMoreElements()).thenReturn(true, true, true, false);
  114. when(names.nextElement()).thenReturn(
  115. AuthenticationFilter.AUTH_TYPE,
  116. PseudoAuthenticationHandler.ANONYMOUS_ALLOWED,
  117. DelegationTokenAuthenticationHandler.TOKEN_KIND);
  118. when(filterConfig.getInitParameterNames()).thenReturn(names);
  119. when(filterConfig.getInitParameter(
  120. DelegationTokenAuthenticationHandler.TOKEN_KIND)).thenReturn(
  121. TimelineDelegationTokenIdentifier.KIND_NAME.toString());
  122. try {
  123. taFilter.init(filterConfig);
  124. } catch (ServletException e) {
  125. Assert.fail("Unable to initialize TimelineAuthenticationFilter: " +
  126. e.getMessage());
  127. }
  128. taFilter = spy(taFilter);
  129. try {
  130. doNothing().when(taFilter).init(any(FilterConfig.class));
  131. } catch (ServletException e) {
  132. Assert.fail("Unable to initialize TimelineAuthenticationFilter: " +
  133. e.getMessage());
  134. }
  135. filter("/*").through(taFilter);
  136. }
  137. });
  138. public class GuiceServletConfig extends GuiceServletContextListener {
  139. @Override
  140. protected Injector getInjector() {
  141. return injector;
  142. }
  143. }
  144. private TimelineStore mockTimelineStore()
  145. throws Exception {
  146. beforeTime = System.currentTimeMillis() - 1;
  147. TestMemoryTimelineStore store =
  148. new TestMemoryTimelineStore();
  149. store.setup();
  150. return store.getTimelineStore();
  151. }
  152. public TestTimelineWebServices() {
  153. super(new WebAppDescriptor.Builder(
  154. "org.apache.hadoop.yarn.server.applicationhistoryservice.webapp")
  155. .contextListenerClass(GuiceServletConfig.class)
  156. .filterClass(com.google.inject.servlet.GuiceFilter.class)
  157. .contextPath("jersey-guice-filter")
  158. .servletPath("/")
  159. .clientConfig(
  160. new DefaultClientConfig(YarnJacksonJaxbJsonProvider.class))
  161. .build());
  162. }
  163. @Test
  164. public void testAbout() throws Exception {
  165. WebResource r = resource();
  166. ClientResponse response = r.path("ws").path("v1").path("timeline")
  167. .accept(MediaType.APPLICATION_JSON)
  168. .get(ClientResponse.class);
  169. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  170. TimelineWebServices.AboutInfo about =
  171. response.getEntity(TimelineWebServices.AboutInfo.class);
  172. Assert.assertNotNull(about);
  173. Assert.assertEquals("Timeline API", about.getAbout());
  174. }
  175. private static void verifyEntities(TimelineEntities entities) {
  176. Assert.assertNotNull(entities);
  177. Assert.assertEquals(3, entities.getEntities().size());
  178. TimelineEntity entity1 = entities.getEntities().get(0);
  179. Assert.assertNotNull(entity1);
  180. Assert.assertEquals("id_1", entity1.getEntityId());
  181. Assert.assertEquals("type_1", entity1.getEntityType());
  182. Assert.assertEquals(123l, entity1.getStartTime().longValue());
  183. Assert.assertEquals(2, entity1.getEvents().size());
  184. Assert.assertEquals(4, entity1.getPrimaryFilters().size());
  185. Assert.assertEquals(4, entity1.getOtherInfo().size());
  186. TimelineEntity entity2 = entities.getEntities().get(1);
  187. Assert.assertNotNull(entity2);
  188. Assert.assertEquals("id_2", entity2.getEntityId());
  189. Assert.assertEquals("type_1", entity2.getEntityType());
  190. Assert.assertEquals(123l, entity2.getStartTime().longValue());
  191. Assert.assertEquals(2, entity2.getEvents().size());
  192. Assert.assertEquals(4, entity2.getPrimaryFilters().size());
  193. Assert.assertEquals(4, entity2.getOtherInfo().size());
  194. TimelineEntity entity3 = entities.getEntities().get(2);
  195. Assert.assertNotNull(entity2);
  196. Assert.assertEquals("id_6", entity3.getEntityId());
  197. Assert.assertEquals("type_1", entity3.getEntityType());
  198. Assert.assertEquals(61l, entity3.getStartTime().longValue());
  199. Assert.assertEquals(0, entity3.getEvents().size());
  200. Assert.assertEquals(4, entity3.getPrimaryFilters().size());
  201. Assert.assertEquals(4, entity3.getOtherInfo().size());
  202. }
  203. @Test
  204. public void testGetEntities() throws Exception {
  205. WebResource r = resource();
  206. ClientResponse response = r.path("ws").path("v1").path("timeline")
  207. .path("type_1")
  208. .accept(MediaType.APPLICATION_JSON)
  209. .get(ClientResponse.class);
  210. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  211. verifyEntities(response.getEntity(TimelineEntities.class));
  212. }
  213. @Test
  214. public void testFromId() throws Exception {
  215. WebResource r = resource();
  216. ClientResponse response = r.path("ws").path("v1").path("timeline")
  217. .path("type_1").queryParam("fromId", "id_2")
  218. .accept(MediaType.APPLICATION_JSON)
  219. .get(ClientResponse.class);
  220. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  221. assertEquals(2, response.getEntity(TimelineEntities.class).getEntities()
  222. .size());
  223. response = r.path("ws").path("v1").path("timeline")
  224. .path("type_1").queryParam("fromId", "id_1")
  225. .accept(MediaType.APPLICATION_JSON)
  226. .get(ClientResponse.class);
  227. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  228. assertEquals(3, response.getEntity(TimelineEntities.class).getEntities()
  229. .size());
  230. }
  231. @Test
  232. public void testFromTs() throws Exception {
  233. WebResource r = resource();
  234. ClientResponse response = r.path("ws").path("v1").path("timeline")
  235. .path("type_1").queryParam("fromTs", Long.toString(beforeTime))
  236. .accept(MediaType.APPLICATION_JSON)
  237. .get(ClientResponse.class);
  238. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  239. assertEquals(0, response.getEntity(TimelineEntities.class).getEntities()
  240. .size());
  241. response = r.path("ws").path("v1").path("timeline")
  242. .path("type_1").queryParam("fromTs", Long.toString(
  243. System.currentTimeMillis()))
  244. .accept(MediaType.APPLICATION_JSON)
  245. .get(ClientResponse.class);
  246. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  247. assertEquals(3, response.getEntity(TimelineEntities.class).getEntities()
  248. .size());
  249. }
  250. @Test
  251. public void testPrimaryFilterString() {
  252. WebResource r = resource();
  253. ClientResponse response = r.path("ws").path("v1").path("timeline")
  254. .path("type_1").queryParam("primaryFilter", "user:username")
  255. .accept(MediaType.APPLICATION_JSON)
  256. .get(ClientResponse.class);
  257. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  258. verifyEntities(response.getEntity(TimelineEntities.class));
  259. }
  260. @Test
  261. public void testPrimaryFilterInteger() {
  262. WebResource r = resource();
  263. ClientResponse response = r.path("ws").path("v1").path("timeline")
  264. .path("type_1").queryParam("primaryFilter",
  265. "appname:" + Integer.toString(Integer.MAX_VALUE))
  266. .accept(MediaType.APPLICATION_JSON)
  267. .get(ClientResponse.class);
  268. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  269. verifyEntities(response.getEntity(TimelineEntities.class));
  270. }
  271. @Test
  272. public void testPrimaryFilterLong() {
  273. WebResource r = resource();
  274. ClientResponse response = r.path("ws").path("v1").path("timeline")
  275. .path("type_1").queryParam("primaryFilter",
  276. "long:" + Long.toString((long) Integer.MAX_VALUE + 1l))
  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 testPrimaryFilterNumericString() {
  284. // without quotes, 123abc is interpreted as the number 123,
  285. // which finds no entities
  286. WebResource r = resource();
  287. ClientResponse response = r.path("ws").path("v1").path("timeline")
  288. .path("type_1").queryParam("primaryFilter", "other:123abc")
  289. .accept(MediaType.APPLICATION_JSON)
  290. .get(ClientResponse.class);
  291. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  292. assertEquals(0, response.getEntity(TimelineEntities.class).getEntities()
  293. .size());
  294. }
  295. @Test
  296. public void testPrimaryFilterNumericStringWithQuotes() {
  297. WebResource r = resource();
  298. ClientResponse response = r.path("ws").path("v1").path("timeline")
  299. .path("type_1").queryParam("primaryFilter", "other:\"123abc\"")
  300. .accept(MediaType.APPLICATION_JSON)
  301. .get(ClientResponse.class);
  302. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  303. verifyEntities(response.getEntity(TimelineEntities.class));
  304. }
  305. @Test
  306. public void testSecondaryFilters() {
  307. WebResource r = resource();
  308. ClientResponse response = r.path("ws").path("v1").path("timeline")
  309. .path("type_1")
  310. .queryParam("secondaryFilter",
  311. "user:username,appname:" + Integer.toString(Integer.MAX_VALUE))
  312. .accept(MediaType.APPLICATION_JSON)
  313. .get(ClientResponse.class);
  314. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  315. verifyEntities(response.getEntity(TimelineEntities.class));
  316. }
  317. @Test
  318. public void testGetEntity() throws Exception {
  319. WebResource r = resource();
  320. ClientResponse response = r.path("ws").path("v1").path("timeline")
  321. .path("type_1").path("id_1")
  322. .accept(MediaType.APPLICATION_JSON)
  323. .get(ClientResponse.class);
  324. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  325. TimelineEntity entity = response.getEntity(TimelineEntity.class);
  326. Assert.assertNotNull(entity);
  327. Assert.assertEquals("id_1", entity.getEntityId());
  328. Assert.assertEquals("type_1", entity.getEntityType());
  329. Assert.assertEquals(123l, entity.getStartTime().longValue());
  330. Assert.assertEquals(2, entity.getEvents().size());
  331. Assert.assertEquals(4, entity.getPrimaryFilters().size());
  332. Assert.assertEquals(4, entity.getOtherInfo().size());
  333. }
  334. @Test
  335. public void testGetEntityFields1() throws Exception {
  336. WebResource r = resource();
  337. ClientResponse response = r.path("ws").path("v1").path("timeline")
  338. .path("type_1").path("id_1").queryParam("fields", "events,otherinfo")
  339. .accept(MediaType.APPLICATION_JSON)
  340. .get(ClientResponse.class);
  341. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  342. TimelineEntity entity = response.getEntity(TimelineEntity.class);
  343. Assert.assertNotNull(entity);
  344. Assert.assertEquals("id_1", entity.getEntityId());
  345. Assert.assertEquals("type_1", entity.getEntityType());
  346. Assert.assertEquals(123l, entity.getStartTime().longValue());
  347. Assert.assertEquals(2, entity.getEvents().size());
  348. Assert.assertEquals(0, entity.getPrimaryFilters().size());
  349. Assert.assertEquals(4, entity.getOtherInfo().size());
  350. }
  351. @Test
  352. public void testGetEntityFields2() throws Exception {
  353. WebResource r = resource();
  354. ClientResponse response = r.path("ws").path("v1").path("timeline")
  355. .path("type_1").path("id_1").queryParam("fields", "lasteventonly," +
  356. "primaryfilters,relatedentities")
  357. .accept(MediaType.APPLICATION_JSON)
  358. .get(ClientResponse.class);
  359. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  360. TimelineEntity entity = response.getEntity(TimelineEntity.class);
  361. Assert.assertNotNull(entity);
  362. Assert.assertEquals("id_1", entity.getEntityId());
  363. Assert.assertEquals("type_1", entity.getEntityType());
  364. Assert.assertEquals(123l, entity.getStartTime().longValue());
  365. Assert.assertEquals(1, entity.getEvents().size());
  366. Assert.assertEquals(4, entity.getPrimaryFilters().size());
  367. Assert.assertEquals(0, entity.getOtherInfo().size());
  368. }
  369. @Test
  370. public void testGetEvents() throws Exception {
  371. WebResource r = resource();
  372. ClientResponse response = r.path("ws").path("v1").path("timeline")
  373. .path("type_1").path("events")
  374. .queryParam("entityId", "id_1")
  375. .accept(MediaType.APPLICATION_JSON)
  376. .get(ClientResponse.class);
  377. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  378. TimelineEvents events = response.getEntity(TimelineEvents.class);
  379. Assert.assertNotNull(events);
  380. Assert.assertEquals(1, events.getAllEvents().size());
  381. TimelineEvents.EventsOfOneEntity partEvents = events.getAllEvents().get(0);
  382. Assert.assertEquals(2, partEvents.getEvents().size());
  383. TimelineEvent event1 = partEvents.getEvents().get(0);
  384. Assert.assertEquals(456l, event1.getTimestamp());
  385. Assert.assertEquals("end_event", event1.getEventType());
  386. Assert.assertEquals(1, event1.getEventInfo().size());
  387. TimelineEvent event2 = partEvents.getEvents().get(1);
  388. Assert.assertEquals(123l, event2.getTimestamp());
  389. Assert.assertEquals("start_event", event2.getEventType());
  390. Assert.assertEquals(0, event2.getEventInfo().size());
  391. }
  392. @Test
  393. public void testPostEntitiesWithPrimaryFilter() throws Exception {
  394. TimelineEntities entities = new TimelineEntities();
  395. TimelineEntity entity = new TimelineEntity();
  396. Map<String, Set<Object>> filters = new HashMap<String, Set<Object>>();
  397. filters.put(TimelineStore.SystemFilter.ENTITY_OWNER.toString(),
  398. new HashSet<Object>());
  399. entity.setPrimaryFilters(filters);
  400. entity.setEntityId("test id 6");
  401. entity.setEntityType("test type 6");
  402. entity.setStartTime(System.currentTimeMillis());
  403. entities.addEntity(entity);
  404. WebResource r = resource();
  405. ClientResponse response = r.path("ws").path("v1").path("timeline")
  406. .queryParam("user.name", "tester")
  407. .accept(MediaType.APPLICATION_JSON)
  408. .type(MediaType.APPLICATION_JSON)
  409. .post(ClientResponse.class, entities);
  410. TimelinePutResponse putResposne =
  411. response.getEntity(TimelinePutResponse.class);
  412. Assert.assertEquals(0, putResposne.getErrors().size());
  413. }
  414. @Test
  415. public void testPostEntities() throws Exception {
  416. TimelineEntities entities = new TimelineEntities();
  417. TimelineEntity entity = new TimelineEntity();
  418. entity.setEntityId("test id 1");
  419. entity.setEntityType("test type 1");
  420. entity.setStartTime(System.currentTimeMillis());
  421. entity.setDomainId("domain_id_1");
  422. entities.addEntity(entity);
  423. WebResource r = resource();
  424. // No owner, will be rejected
  425. ClientResponse response = r.path("ws").path("v1").path("timeline")
  426. .accept(MediaType.APPLICATION_JSON)
  427. .type(MediaType.APPLICATION_JSON)
  428. .post(ClientResponse.class, entities);
  429. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  430. assertEquals(ClientResponse.Status.FORBIDDEN,
  431. response.getClientResponseStatus());
  432. response = r.path("ws").path("v1").path("timeline")
  433. .queryParam("user.name", "tester")
  434. .accept(MediaType.APPLICATION_JSON)
  435. .type(MediaType.APPLICATION_JSON)
  436. .post(ClientResponse.class, entities);
  437. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  438. TimelinePutResponse putResposne =
  439. response.getEntity(TimelinePutResponse.class);
  440. Assert.assertNotNull(putResposne);
  441. Assert.assertEquals(0, putResposne.getErrors().size());
  442. // verify the entity exists in the store
  443. response = r.path("ws").path("v1").path("timeline")
  444. .path("test type 1").path("test id 1")
  445. .accept(MediaType.APPLICATION_JSON)
  446. .get(ClientResponse.class);
  447. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  448. entity = response.getEntity(TimelineEntity.class);
  449. Assert.assertNotNull(entity);
  450. Assert.assertEquals("test id 1", entity.getEntityId());
  451. Assert.assertEquals("test type 1", entity.getEntityType());
  452. }
  453. @Test
  454. public void testPostEntitiesWithYarnACLsEnabled() throws Exception {
  455. AdminACLsManager oldAdminACLsManager =
  456. timelineACLsManager.setAdminACLsManager(adminACLsManager);
  457. try {
  458. TimelineEntities entities = new TimelineEntities();
  459. TimelineEntity entity = new TimelineEntity();
  460. entity.setEntityId("test id 2");
  461. entity.setEntityType("test type 2");
  462. entity.setStartTime(System.currentTimeMillis());
  463. entity.setDomainId("domain_id_1");
  464. entities.addEntity(entity);
  465. WebResource r = resource();
  466. ClientResponse response = r.path("ws").path("v1").path("timeline")
  467. .queryParam("user.name", "writer_user_1")
  468. .accept(MediaType.APPLICATION_JSON)
  469. .type(MediaType.APPLICATION_JSON)
  470. .post(ClientResponse.class, entities);
  471. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  472. TimelinePutResponse putResponse =
  473. response.getEntity(TimelinePutResponse.class);
  474. Assert.assertNotNull(putResponse);
  475. Assert.assertEquals(0, putResponse.getErrors().size());
  476. // override/append timeline data in the same entity with different user
  477. response = r.path("ws").path("v1").path("timeline")
  478. .queryParam("user.name", "writer_user_3")
  479. .accept(MediaType.APPLICATION_JSON)
  480. .type(MediaType.APPLICATION_JSON)
  481. .post(ClientResponse.class, entities);
  482. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  483. putResponse = response.getEntity(TimelinePutResponse.class);
  484. Assert.assertNotNull(putResponse);
  485. Assert.assertEquals(1, putResponse.getErrors().size());
  486. Assert.assertEquals(TimelinePutResponse.TimelinePutError.ACCESS_DENIED,
  487. putResponse.getErrors().get(0).getErrorCode());
  488. // Cross domain relationship will be rejected
  489. entities = new TimelineEntities();
  490. entity = new TimelineEntity();
  491. entity.setEntityId("test id 3");
  492. entity.setEntityType("test type 2");
  493. entity.setStartTime(System.currentTimeMillis());
  494. entity.setDomainId("domain_id_2");
  495. entity.setRelatedEntities(Collections.singletonMap(
  496. "test type 2", Collections.singleton("test id 2")));
  497. entities.addEntity(entity);
  498. r = resource();
  499. response = r.path("ws").path("v1").path("timeline")
  500. .queryParam("user.name", "writer_user_3")
  501. .accept(MediaType.APPLICATION_JSON)
  502. .type(MediaType.APPLICATION_JSON)
  503. .post(ClientResponse.class, entities);
  504. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  505. putResponse = response.getEntity(TimelinePutResponse.class);
  506. Assert.assertNotNull(putResponse);
  507. Assert.assertEquals(1, putResponse.getErrors().size());
  508. Assert.assertEquals(TimelinePutError.FORBIDDEN_RELATION,
  509. putResponse.getErrors().get(0).getErrorCode());
  510. // Make sure the entity has been added anyway even though the
  511. // relationship is been excluded
  512. response = r.path("ws").path("v1").path("timeline")
  513. .path("test type 2").path("test id 3")
  514. .queryParam("user.name", "reader_user_3")
  515. .accept(MediaType.APPLICATION_JSON)
  516. .get(ClientResponse.class);
  517. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  518. entity = response.getEntity(TimelineEntity.class);
  519. Assert.assertNotNull(entity);
  520. Assert.assertEquals("test id 3", entity.getEntityId());
  521. Assert.assertEquals("test type 2", entity.getEntityType());
  522. } finally {
  523. timelineACLsManager.setAdminACLsManager(oldAdminACLsManager);
  524. }
  525. }
  526. @Test
  527. public void testPostEntitiesToDefaultDomain() throws Exception {
  528. AdminACLsManager oldAdminACLsManager =
  529. timelineACLsManager.setAdminACLsManager(adminACLsManager);
  530. try {
  531. TimelineEntities entities = new TimelineEntities();
  532. TimelineEntity entity = new TimelineEntity();
  533. entity.setEntityId("test id 7");
  534. entity.setEntityType("test type 7");
  535. entity.setStartTime(System.currentTimeMillis());
  536. entities.addEntity(entity);
  537. WebResource r = resource();
  538. ClientResponse response = r.path("ws").path("v1").path("timeline")
  539. .queryParam("user.name", "anybody_1")
  540. .accept(MediaType.APPLICATION_JSON)
  541. .type(MediaType.APPLICATION_JSON)
  542. .post(ClientResponse.class, entities);
  543. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  544. TimelinePutResponse putResposne =
  545. response.getEntity(TimelinePutResponse.class);
  546. Assert.assertNotNull(putResposne);
  547. Assert.assertEquals(0, putResposne.getErrors().size());
  548. // verify the entity exists in the store
  549. response = r.path("ws").path("v1").path("timeline")
  550. .path("test type 7").path("test id 7")
  551. .queryParam("user.name", "any_body_2")
  552. .accept(MediaType.APPLICATION_JSON)
  553. .get(ClientResponse.class);
  554. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  555. entity = response.getEntity(TimelineEntity.class);
  556. Assert.assertNotNull(entity);
  557. Assert.assertEquals("test id 7", entity.getEntityId());
  558. Assert.assertEquals("test type 7", entity.getEntityType());
  559. Assert.assertEquals(TimelineDataManager.DEFAULT_DOMAIN_ID,
  560. entity.getDomainId());
  561. } finally {
  562. timelineACLsManager.setAdminACLsManager(oldAdminACLsManager);
  563. }
  564. }
  565. @Test
  566. public void testGetEntityWithYarnACLsEnabled() throws Exception {
  567. AdminACLsManager oldAdminACLsManager =
  568. timelineACLsManager.setAdminACLsManager(adminACLsManager);
  569. try {
  570. TimelineEntities entities = new TimelineEntities();
  571. TimelineEntity entity = new TimelineEntity();
  572. entity.setEntityId("test id 3");
  573. entity.setEntityType("test type 3");
  574. entity.setStartTime(System.currentTimeMillis());
  575. entity.setDomainId("domain_id_1");
  576. entities.addEntity(entity);
  577. WebResource r = resource();
  578. ClientResponse response = r.path("ws").path("v1").path("timeline")
  579. .queryParam("user.name", "writer_user_1")
  580. .accept(MediaType.APPLICATION_JSON)
  581. .type(MediaType.APPLICATION_JSON)
  582. .post(ClientResponse.class, entities);
  583. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  584. TimelinePutResponse putResponse =
  585. response.getEntity(TimelinePutResponse.class);
  586. Assert.assertEquals(0, putResponse.getErrors().size());
  587. // verify the system data will not be exposed
  588. // 1. No field specification
  589. response = r.path("ws").path("v1").path("timeline")
  590. .path("test type 3").path("test id 3")
  591. .queryParam("user.name", "reader_user_1")
  592. .accept(MediaType.APPLICATION_JSON)
  593. .get(ClientResponse.class);
  594. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  595. entity = response.getEntity(TimelineEntity.class);
  596. Assert.assertNull(entity.getPrimaryFilters().get(
  597. TimelineStore.SystemFilter.ENTITY_OWNER.toString()));
  598. // 2. other field
  599. response = r.path("ws").path("v1").path("timeline")
  600. .path("test type 3").path("test id 3")
  601. .queryParam("fields", "relatedentities")
  602. .queryParam("user.name", "reader_user_1")
  603. .accept(MediaType.APPLICATION_JSON)
  604. .get(ClientResponse.class);
  605. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  606. entity = response.getEntity(TimelineEntity.class);
  607. Assert.assertNull(entity.getPrimaryFilters().get(
  608. TimelineStore.SystemFilter.ENTITY_OWNER.toString()));
  609. // 3. primaryfilters field
  610. response = r.path("ws").path("v1").path("timeline")
  611. .path("test type 3").path("test id 3")
  612. .queryParam("fields", "primaryfilters")
  613. .queryParam("user.name", "reader_user_1")
  614. .accept(MediaType.APPLICATION_JSON)
  615. .get(ClientResponse.class);
  616. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  617. entity = response.getEntity(TimelineEntity.class);
  618. Assert.assertNull(entity.getPrimaryFilters().get(
  619. TimelineStore.SystemFilter.ENTITY_OWNER.toString()));
  620. // get entity with other user
  621. response = r.path("ws").path("v1").path("timeline")
  622. .path("test type 3").path("test id 3")
  623. .queryParam("user.name", "reader_user_2")
  624. .accept(MediaType.APPLICATION_JSON)
  625. .get(ClientResponse.class);
  626. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  627. assertEquals(ClientResponse.Status.NOT_FOUND,
  628. response.getClientResponseStatus());
  629. } finally {
  630. timelineACLsManager.setAdminACLsManager(oldAdminACLsManager);
  631. }
  632. }
  633. @Test
  634. public void testGetEntitiesWithYarnACLsEnabled() {
  635. AdminACLsManager oldAdminACLsManager =
  636. timelineACLsManager.setAdminACLsManager(adminACLsManager);
  637. try {
  638. // Put entity [4, 4] in domain 1
  639. TimelineEntities entities = new TimelineEntities();
  640. TimelineEntity entity = new TimelineEntity();
  641. entity.setEntityId("test id 4");
  642. entity.setEntityType("test type 4");
  643. entity.setStartTime(System.currentTimeMillis());
  644. entity.setDomainId("domain_id_1");
  645. entities.addEntity(entity);
  646. WebResource r = resource();
  647. ClientResponse response = r.path("ws").path("v1").path("timeline")
  648. .queryParam("user.name", "writer_user_1")
  649. .accept(MediaType.APPLICATION_JSON)
  650. .type(MediaType.APPLICATION_JSON)
  651. .post(ClientResponse.class, entities);
  652. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  653. TimelinePutResponse putResponse =
  654. response.getEntity(TimelinePutResponse.class);
  655. Assert.assertEquals(0, putResponse.getErrors().size());
  656. // Put entity [4, 5] in domain 2
  657. entities = new TimelineEntities();
  658. entity = new TimelineEntity();
  659. entity.setEntityId("test id 5");
  660. entity.setEntityType("test type 4");
  661. entity.setStartTime(System.currentTimeMillis());
  662. entity.setDomainId("domain_id_2");
  663. entities.addEntity(entity);
  664. r = resource();
  665. response = r.path("ws").path("v1").path("timeline")
  666. .queryParam("user.name", "writer_user_3")
  667. .accept(MediaType.APPLICATION_JSON)
  668. .type(MediaType.APPLICATION_JSON)
  669. .post(ClientResponse.class, entities);
  670. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  671. putResponse = response.getEntity(TimelinePutResponse.class);
  672. Assert.assertEquals(0, putResponse.getErrors().size());
  673. // Query entities of type 4
  674. response = r.path("ws").path("v1").path("timeline")
  675. .queryParam("user.name", "reader_user_1")
  676. .path("test type 4")
  677. .accept(MediaType.APPLICATION_JSON)
  678. .get(ClientResponse.class);
  679. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  680. entities = response.getEntity(TimelineEntities.class);
  681. // Reader 1 should just have the access to entity [4, 4]
  682. assertEquals(1, entities.getEntities().size());
  683. assertEquals("test type 4", entities.getEntities().get(0).getEntityType());
  684. assertEquals("test id 4", entities.getEntities().get(0).getEntityId());
  685. } finally {
  686. timelineACLsManager.setAdminACLsManager(oldAdminACLsManager);
  687. }
  688. }
  689. @Test
  690. public void testGetEventsWithYarnACLsEnabled() {
  691. AdminACLsManager oldAdminACLsManager =
  692. timelineACLsManager.setAdminACLsManager(adminACLsManager);
  693. try {
  694. // Put entity [5, 5] in domain 1
  695. TimelineEntities entities = new TimelineEntities();
  696. TimelineEntity entity = new TimelineEntity();
  697. entity.setEntityId("test id 5");
  698. entity.setEntityType("test type 5");
  699. entity.setStartTime(System.currentTimeMillis());
  700. entity.setDomainId("domain_id_1");
  701. TimelineEvent event = new TimelineEvent();
  702. event.setEventType("event type 1");
  703. event.setTimestamp(System.currentTimeMillis());
  704. entity.addEvent(event);
  705. entities.addEntity(entity);
  706. WebResource r = resource();
  707. ClientResponse response = r.path("ws").path("v1").path("timeline")
  708. .queryParam("user.name", "writer_user_1")
  709. .accept(MediaType.APPLICATION_JSON)
  710. .type(MediaType.APPLICATION_JSON)
  711. .post(ClientResponse.class, entities);
  712. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  713. TimelinePutResponse putResponse =
  714. response.getEntity(TimelinePutResponse.class);
  715. Assert.assertEquals(0, putResponse.getErrors().size());
  716. // Put entity [5, 6] in domain 2
  717. entities = new TimelineEntities();
  718. entity = new TimelineEntity();
  719. entity.setEntityId("test id 6");
  720. entity.setEntityType("test type 5");
  721. entity.setStartTime(System.currentTimeMillis());
  722. entity.setDomainId("domain_id_2");
  723. event = new TimelineEvent();
  724. event.setEventType("event type 2");
  725. event.setTimestamp(System.currentTimeMillis());
  726. entity.addEvent(event);
  727. entities.addEntity(entity);
  728. r = resource();
  729. response = r.path("ws").path("v1").path("timeline")
  730. .queryParam("user.name", "writer_user_3")
  731. .accept(MediaType.APPLICATION_JSON)
  732. .type(MediaType.APPLICATION_JSON)
  733. .post(ClientResponse.class, entities);
  734. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  735. putResponse = response.getEntity(TimelinePutResponse.class);
  736. Assert.assertEquals(0, putResponse.getErrors().size());
  737. // Query events belonging to the entities of type 4
  738. response = r.path("ws").path("v1").path("timeline")
  739. .path("test type 5").path("events")
  740. .queryParam("user.name", "reader_user_1")
  741. .queryParam("entityId", "test id 5,test id 6")
  742. .accept(MediaType.APPLICATION_JSON)
  743. .get(ClientResponse.class);
  744. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  745. TimelineEvents events = response.getEntity(TimelineEvents.class);
  746. // Reader 1 should just have the access to the events of entity [5, 5]
  747. assertEquals(1, events.getAllEvents().size());
  748. assertEquals("test id 5", events.getAllEvents().get(0).getEntityId());
  749. } finally {
  750. timelineACLsManager.setAdminACLsManager(oldAdminACLsManager);
  751. }
  752. }
  753. @Test
  754. public void testGetDomain() throws Exception {
  755. WebResource r = resource();
  756. ClientResponse response = r.path("ws").path("v1").path("timeline")
  757. .path("domain").path("domain_id_1")
  758. .accept(MediaType.APPLICATION_JSON)
  759. .get(ClientResponse.class);
  760. Assert.assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  761. TimelineDomain domain = response.getEntity(TimelineDomain.class);
  762. verifyDomain(domain, "domain_id_1");
  763. }
  764. @Test
  765. public void testGetDomainYarnACLsEnabled() {
  766. AdminACLsManager oldAdminACLsManager =
  767. timelineACLsManager.setAdminACLsManager(adminACLsManager);
  768. try {
  769. WebResource r = resource();
  770. ClientResponse response = r.path("ws").path("v1").path("timeline")
  771. .path("domain").path("domain_id_1")
  772. .queryParam("user.name", "owner_1")
  773. .accept(MediaType.APPLICATION_JSON)
  774. .get(ClientResponse.class);
  775. Assert.assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  776. TimelineDomain domain = response.getEntity(TimelineDomain.class);
  777. verifyDomain(domain, "domain_id_1");
  778. response = r.path("ws").path("v1").path("timeline")
  779. .path("domain").path("domain_id_1")
  780. .queryParam("user.name", "tester")
  781. .accept(MediaType.APPLICATION_JSON)
  782. .get(ClientResponse.class);
  783. Assert.assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  784. Assert.assertEquals(ClientResponse.Status.NOT_FOUND,
  785. response.getClientResponseStatus());
  786. } finally {
  787. timelineACLsManager.setAdminACLsManager(oldAdminACLsManager);
  788. }
  789. }
  790. @Test
  791. public void testGetDomains() throws Exception {
  792. WebResource r = resource();
  793. ClientResponse response = r.path("ws").path("v1").path("timeline")
  794. .path("domain")
  795. .queryParam("owner", "owner_1")
  796. .accept(MediaType.APPLICATION_JSON)
  797. .get(ClientResponse.class);
  798. Assert.assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  799. TimelineDomains domains = response.getEntity(TimelineDomains.class);
  800. Assert.assertEquals(2, domains.getDomains().size());
  801. for (int i = 0; i < domains.getDomains().size(); ++i) {
  802. verifyDomain(domains.getDomains().get(i),
  803. i == 0 ? "domain_id_4" : "domain_id_1");
  804. }
  805. }
  806. @Test
  807. public void testGetDomainsYarnACLsEnabled() throws Exception {
  808. AdminACLsManager oldAdminACLsManager =
  809. timelineACLsManager.setAdminACLsManager(adminACLsManager);
  810. try {
  811. WebResource r = resource();
  812. ClientResponse response = r.path("ws").path("v1").path("timeline")
  813. .path("domain")
  814. .queryParam("user.name", "owner_1")
  815. .accept(MediaType.APPLICATION_JSON)
  816. .get(ClientResponse.class);
  817. Assert.assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  818. TimelineDomains domains = response.getEntity(TimelineDomains.class);
  819. Assert.assertEquals(2, domains.getDomains().size());
  820. for (int i = 0; i < domains.getDomains().size(); ++i) {
  821. verifyDomain(domains.getDomains().get(i),
  822. i == 0 ? "domain_id_4" : "domain_id_1");
  823. }
  824. response = r.path("ws").path("v1").path("timeline")
  825. .path("domain")
  826. .queryParam("owner", "owner_1")
  827. .queryParam("user.name", "tester")
  828. .accept(MediaType.APPLICATION_JSON)
  829. .get(ClientResponse.class);
  830. Assert.assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  831. domains = response.getEntity(TimelineDomains.class);
  832. Assert.assertEquals(0, domains.getDomains().size());
  833. } finally {
  834. timelineACLsManager.setAdminACLsManager(oldAdminACLsManager);
  835. }
  836. }
  837. @Test
  838. public void testPutDomain() throws Exception {
  839. TimelineDomain domain = new TimelineDomain();
  840. domain.setId("test_domain_id");
  841. WebResource r = resource();
  842. // No owner, will be rejected
  843. ClientResponse response = r.path("ws").path("v1")
  844. .path("timeline").path("domain")
  845. .accept(MediaType.APPLICATION_JSON)
  846. .type(MediaType.APPLICATION_JSON)
  847. .put(ClientResponse.class, domain);
  848. assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  849. assertEquals(ClientResponse.Status.FORBIDDEN,
  850. response.getClientResponseStatus());
  851. response = r.path("ws").path("v1")
  852. .path("timeline").path("domain")
  853. .queryParam("user.name", "tester")
  854. .accept(MediaType.APPLICATION_JSON)
  855. .type(MediaType.APPLICATION_JSON)
  856. .put(ClientResponse.class, domain);
  857. assertEquals(Status.OK.getStatusCode(), response.getStatus());
  858. // Verify the domain exists
  859. response = r.path("ws").path("v1").path("timeline")
  860. .path("domain").path("test_domain_id")
  861. .accept(MediaType.APPLICATION_JSON)
  862. .get(ClientResponse.class);
  863. Assert.assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  864. domain = response.getEntity(TimelineDomain.class);
  865. Assert.assertNotNull(domain);
  866. Assert.assertEquals("test_domain_id", domain.getId());
  867. Assert.assertEquals("tester", domain.getOwner());
  868. Assert.assertEquals(null, domain.getDescription());
  869. // Update the domain
  870. domain.setDescription("test_description");
  871. response = r.path("ws").path("v1")
  872. .path("timeline").path("domain")
  873. .queryParam("user.name", "tester")
  874. .accept(MediaType.APPLICATION_JSON)
  875. .type(MediaType.APPLICATION_JSON)
  876. .put(ClientResponse.class, domain);
  877. assertEquals(Status.OK.getStatusCode(), response.getStatus());
  878. // Verify the domain is updated
  879. response = r.path("ws").path("v1").path("timeline")
  880. .path("domain").path("test_domain_id")
  881. .accept(MediaType.APPLICATION_JSON)
  882. .get(ClientResponse.class);
  883. Assert.assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
  884. domain = response.getEntity(TimelineDomain.class);
  885. Assert.assertNotNull(domain);
  886. Assert.assertEquals("test_domain_id", domain.getId());
  887. Assert.assertEquals("test_description", domain.getDescription());
  888. }
  889. @Test
  890. public void testPutDomainYarnACLsEnabled() throws Exception {
  891. AdminACLsManager oldAdminACLsManager =
  892. timelineACLsManager.setAdminACLsManager(adminACLsManager);
  893. try {
  894. TimelineDomain domain = new TimelineDomain();
  895. domain.setId("test_domain_id_acl");
  896. WebResource r = resource();
  897. ClientResponse response = r.path("ws").path("v1")
  898. .path("timeline").path("domain")
  899. .queryParam("user.name", "tester")
  900. .accept(MediaType.APPLICATION_JSON)
  901. .type(MediaType.APPLICATION_JSON)
  902. .put(ClientResponse.class, domain);
  903. assertEquals(Status.OK.getStatusCode(), response.getStatus());
  904. // Update the domain by another user
  905. response = r.path("ws").path("v1")
  906. .path("timeline").path("domain")
  907. .queryParam("user.name", "other")
  908. .accept(MediaType.APPLICATION_JSON)
  909. .type(MediaType.APPLICATION_JSON)
  910. .put(ClientResponse.class, domain);
  911. assertEquals(Status.FORBIDDEN.getStatusCode(), response.getStatus());
  912. } finally {
  913. timelineACLsManager.setAdminACLsManager(oldAdminACLsManager);
  914. }
  915. }
  916. private static void verifyDomain(TimelineDomain domain, String domainId) {
  917. Assert.assertNotNull(domain);
  918. Assert.assertEquals(domainId, domain.getId());
  919. // The specific values have been verified in TestMemoryTimelineStore
  920. Assert.assertNotNull(domain.getDescription());
  921. Assert.assertNotNull(domain.getOwner());
  922. Assert.assertNotNull(domain.getReaders());
  923. Assert.assertNotNull(domain.getWriters());
  924. Assert.assertNotNull(domain.getCreatedTime());
  925. Assert.assertNotNull(domain.getModifiedTime());
  926. }
  927. }