Skip to content

Commit cea1cfd

Browse files
committed
simply unit test for jersey2 client testing
1 parent 97fe079 commit cea1cfd

File tree

1 file changed

+135
-0
lines changed
  • hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/test/java/org/apache/hadoop/yarn/server/applicationhistoryservice

1 file changed

+135
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
* <p>
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* <p>
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+
19+
package org.apache.hadoop.yarn.server.applicationhistoryservice;
20+
21+
import org.apache.hadoop.conf.Configuration;
22+
import org.apache.hadoop.yarn.api.records.timeline.TimelineEntity;
23+
import org.apache.hadoop.yarn.api.records.timeline.TimelinePutResponse;
24+
import org.apache.hadoop.yarn.client.api.TimelineClient;
25+
import org.apache.hadoop.yarn.conf.YarnConfiguration;
26+
import org.apache.hadoop.yarn.exceptions.YarnException;
27+
import org.apache.hadoop.yarn.server.timeline.MemoryTimelineStore;
28+
import org.apache.hadoop.yarn.server.timeline.TimelineStore;
29+
import org.apache.hadoop.yarn.webapp.YarnJacksonJaxbJsonProvider;
30+
import org.codehaus.jettison.json.JSONObject;
31+
import org.glassfish.jersey.client.ClientConfig;
32+
import org.glassfish.jersey.jettison.JettisonFeature;
33+
import org.junit.jupiter.api.AfterAll;
34+
import org.junit.jupiter.api.BeforeAll;
35+
import org.junit.jupiter.api.Test;
36+
import org.slf4j.Logger;
37+
import org.slf4j.LoggerFactory;
38+
39+
import javax.ws.rs.client.Client;
40+
import javax.ws.rs.client.ClientBuilder;
41+
import javax.ws.rs.client.WebTarget;
42+
import javax.ws.rs.core.MediaType;
43+
import javax.ws.rs.core.Response;
44+
import java.io.IOException;
45+
46+
import static org.apache.hadoop.yarn.conf.YarnConfiguration.TIMELINE_HTTP_AUTH_PREFIX;
47+
import static org.junit.jupiter.api.Assertions.*;
48+
49+
public class TestTimelineServerWithTezRequests {
50+
private static final Logger LOG = LoggerFactory.getLogger(TestTimelineServerWithTezRequests.class);
51+
52+
private static final String BASEDIR =
53+
System.getProperty("test.build.dir", "target/test-dir") + "/"
54+
+ TestTimelineServerWithTezRequests.class.getSimpleName();
55+
private static final String TIMELINE_SERVICE_WEBAPP_ADDRESS = "localhost:8188";
56+
private static final String TEZ_ENTITY_TYPE = "TEZ_APPLICATION";
57+
private static final String TEZ_ENTITY_ID = "tez_application_1_2";
58+
private static ApplicationHistoryServer testTimelineServer;
59+
private static Configuration conf;
60+
61+
@BeforeAll
62+
public static void setup() {
63+
try {
64+
testTimelineServer = new ApplicationHistoryServer();
65+
conf = new Configuration(false);
66+
conf.setStrings(TIMELINE_HTTP_AUTH_PREFIX + "type", "simple");
67+
68+
conf.setBoolean(YarnConfiguration.TIMELINE_SERVICE_ENABLED, true);
69+
conf.setClass(YarnConfiguration.TIMELINE_SERVICE_STORE, MemoryTimelineStore.class, TimelineStore.class);
70+
conf.set(YarnConfiguration.TIMELINE_SERVICE_WEBAPP_ADDRESS, TIMELINE_SERVICE_WEBAPP_ADDRESS);
71+
conf.setInt(YarnConfiguration.TIMELINE_SERVICE_CLIENT_MAX_RETRIES, 1);
72+
73+
testTimelineServer.init(conf);
74+
testTimelineServer.start();
75+
} catch (Exception e) {
76+
LOG.error("Failed to setup TimelineServer", e);
77+
fail("Couldn't setup TimelineServer");
78+
}
79+
}
80+
81+
@AfterAll
82+
public static void tearDown() throws Exception {
83+
if (testTimelineServer != null) {
84+
testTimelineServer.stop();
85+
}
86+
}
87+
88+
@Test
89+
void testPutAndGetTimelineEntity() throws Exception {
90+
putEntity();
91+
getEntity();
92+
}
93+
94+
private void putEntity() throws IOException, YarnException {
95+
try (TimelineClient client = createTimelineClient()) {
96+
TimelineEntity entityToStore = new TimelineEntity();
97+
entityToStore.setEntityType(TEZ_ENTITY_TYPE);
98+
entityToStore.setEntityId(TEZ_ENTITY_ID);
99+
entityToStore.setStartTime(System.currentTimeMillis());
100+
TimelinePutResponse putResponse = client.putEntities(entityToStore);
101+
if (!putResponse.getErrors().isEmpty()) {
102+
LOG.error("putResponse errors: {}", putResponse.getErrors());
103+
}
104+
assertTrue(putResponse.getErrors().isEmpty(), "There were some errors in the putResponse");
105+
TimelineEntity entityToRead =
106+
testTimelineServer.getTimelineStore().getEntity(TEZ_ENTITY_ID, TEZ_ENTITY_TYPE, null);
107+
assertNotNull(entityToRead, "Timeline entity should not be null");
108+
}
109+
}
110+
111+
private TimelineClient createTimelineClient() {
112+
TimelineClient client = TimelineClient.createTimelineClient();
113+
client.init(conf);
114+
client.start();
115+
return client;
116+
}
117+
118+
private void getEntity() {
119+
String appUrl = "http://" + TIMELINE_SERVICE_WEBAPP_ADDRESS + "/ws/v1/timeline/"
120+
+ TEZ_ENTITY_TYPE + "/" + TEZ_ENTITY_ID + "?user.name=foo";
121+
LOG.info("Getting timeline entity for tez application: " + appUrl);
122+
123+
ClientConfig cfg = new ClientConfig();
124+
cfg.register(new JettisonFeature()).register(YarnJacksonJaxbJsonProvider.class);
125+
Client client = ClientBuilder.newClient(cfg);
126+
WebTarget target = client.target(appUrl);
127+
128+
Response response = target.request(MediaType.APPLICATION_JSON).get();
129+
assertEquals(200, response.getStatus());
130+
assertTrue(MediaType.APPLICATION_JSON_TYPE.isCompatible(response.getMediaType()));
131+
132+
JSONObject entityStr = response.readEntity(JSONObject.class);
133+
LOG.info("Got entity from ATS: {}", entityStr);
134+
}
135+
}

0 commit comments

Comments
 (0)