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 
19 package org.apache.hadoop.yarn.client.api.impl;
20 
21 import static org.mockito.Matchers.any;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.when;
24 
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 
30 import org.junit.Assert;
31 
32 import org.apache.hadoop.conf.Configuration;
33 import org.apache.hadoop.yarn.api.ApplicationHistoryProtocol;
34 import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationAttemptReportRequest;
35 import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationAttemptReportResponse;
36 import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationAttemptsRequest;
37 import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationAttemptsResponse;
38 import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationReportRequest;
39 import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationReportResponse;
40 import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsRequest;
41 import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsResponse;
42 import org.apache.hadoop.yarn.api.protocolrecords.GetContainerReportRequest;
43 import org.apache.hadoop.yarn.api.protocolrecords.GetContainerReportResponse;
44 import org.apache.hadoop.yarn.api.protocolrecords.GetContainersRequest;
45 import org.apache.hadoop.yarn.api.protocolrecords.GetContainersResponse;
46 import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
47 import org.apache.hadoop.yarn.api.records.ApplicationAttemptReport;
48 import org.apache.hadoop.yarn.api.records.ApplicationId;
49 import org.apache.hadoop.yarn.api.records.ApplicationReport;
50 import org.apache.hadoop.yarn.api.records.ContainerId;
51 import org.apache.hadoop.yarn.api.records.ContainerReport;
52 import org.apache.hadoop.yarn.api.records.ContainerState;
53 import org.apache.hadoop.yarn.api.records.FinalApplicationStatus;
54 import org.apache.hadoop.yarn.api.records.NodeId;
55 import org.apache.hadoop.yarn.api.records.Priority;
56 import org.apache.hadoop.yarn.api.records.YarnApplicationAttemptState;
57 import org.apache.hadoop.yarn.api.records.YarnApplicationState;
58 import org.apache.hadoop.yarn.client.api.AHSClient;
59 import org.apache.hadoop.yarn.exceptions.YarnException;
60 import org.junit.Test;
61 
62 public class TestAHSClient {
63 
64   @Test
testClientStop()65   public void testClientStop() {
66     Configuration conf = new Configuration();
67     AHSClient client = AHSClient.createAHSClient();
68     client.init(conf);
69     client.start();
70     client.stop();
71   }
72 
73   @Test(timeout = 10000)
testGetApplications()74   public void testGetApplications() throws YarnException, IOException {
75     Configuration conf = new Configuration();
76     final AHSClient client = new MockAHSClient();
77     client.init(conf);
78     client.start();
79 
80     List<ApplicationReport> expectedReports =
81         ((MockAHSClient) client).getReports();
82 
83     List<ApplicationReport> reports = client.getApplications();
84     Assert.assertEquals(reports, expectedReports);
85 
86     reports = client.getApplications();
87     Assert.assertEquals(reports.size(), 4);
88     client.stop();
89   }
90 
91   @Test(timeout = 10000)
testGetApplicationReport()92   public void testGetApplicationReport() throws YarnException, IOException {
93     Configuration conf = new Configuration();
94     final AHSClient client = new MockAHSClient();
95     client.init(conf);
96     client.start();
97 
98     List<ApplicationReport> expectedReports =
99         ((MockAHSClient) client).getReports();
100     ApplicationId applicationId = ApplicationId.newInstance(1234, 5);
101     ApplicationReport report = client.getApplicationReport(applicationId);
102     Assert.assertEquals(report, expectedReports.get(0));
103     Assert.assertEquals(report.getApplicationId().toString(), expectedReports
104       .get(0).getApplicationId().toString());
105     client.stop();
106   }
107 
108   @Test(timeout = 10000)
testGetApplicationAttempts()109   public void testGetApplicationAttempts() throws YarnException, IOException {
110     Configuration conf = new Configuration();
111     final AHSClient client = new MockAHSClient();
112     client.init(conf);
113     client.start();
114 
115     ApplicationId applicationId = ApplicationId.newInstance(1234, 5);
116     List<ApplicationAttemptReport> reports =
117         client.getApplicationAttempts(applicationId);
118     Assert.assertNotNull(reports);
119     Assert.assertEquals(reports.get(0).getApplicationAttemptId(),
120       ApplicationAttemptId.newInstance(applicationId, 1));
121     Assert.assertEquals(reports.get(1).getApplicationAttemptId(),
122       ApplicationAttemptId.newInstance(applicationId, 2));
123     client.stop();
124   }
125 
126   @Test(timeout = 10000)
testGetApplicationAttempt()127   public void testGetApplicationAttempt() throws YarnException, IOException {
128     Configuration conf = new Configuration();
129     final AHSClient client = new MockAHSClient();
130     client.init(conf);
131     client.start();
132 
133     List<ApplicationReport> expectedReports =
134         ((MockAHSClient) client).getReports();
135 
136     ApplicationId applicationId = ApplicationId.newInstance(1234, 5);
137     ApplicationAttemptId appAttemptId =
138         ApplicationAttemptId.newInstance(applicationId, 1);
139     ApplicationAttemptReport report =
140         client.getApplicationAttemptReport(appAttemptId);
141     Assert.assertNotNull(report);
142     Assert.assertEquals(report.getApplicationAttemptId().toString(),
143       expectedReports.get(0).getCurrentApplicationAttemptId().toString());
144     client.stop();
145   }
146 
147   @Test(timeout = 10000)
testGetContainers()148   public void testGetContainers() throws YarnException, IOException {
149     Configuration conf = new Configuration();
150     final AHSClient client = new MockAHSClient();
151     client.init(conf);
152     client.start();
153 
154     ApplicationId applicationId = ApplicationId.newInstance(1234, 5);
155     ApplicationAttemptId appAttemptId =
156         ApplicationAttemptId.newInstance(applicationId, 1);
157     List<ContainerReport> reports = client.getContainers(appAttemptId);
158     Assert.assertNotNull(reports);
159     Assert.assertEquals(reports.get(0).getContainerId(),
160       (ContainerId.newContainerId(appAttemptId, 1)));
161     Assert.assertEquals(reports.get(1).getContainerId(),
162       (ContainerId.newContainerId(appAttemptId, 2)));
163     client.stop();
164   }
165 
166   @Test(timeout = 10000)
testGetContainerReport()167   public void testGetContainerReport() throws YarnException, IOException {
168     Configuration conf = new Configuration();
169     final AHSClient client = new MockAHSClient();
170     client.init(conf);
171     client.start();
172 
173     List<ApplicationReport> expectedReports =
174         ((MockAHSClient) client).getReports();
175 
176     ApplicationId applicationId = ApplicationId.newInstance(1234, 5);
177     ApplicationAttemptId appAttemptId =
178         ApplicationAttemptId.newInstance(applicationId, 1);
179     ContainerId containerId = ContainerId.newContainerId(appAttemptId, 1);
180     ContainerReport report = client.getContainerReport(containerId);
181     Assert.assertNotNull(report);
182     Assert.assertEquals(report.getContainerId().toString(), (ContainerId
183       .newContainerId(expectedReports.get(0).getCurrentApplicationAttemptId(), 1))
184       .toString());
185     client.stop();
186   }
187 
188   private static class MockAHSClient extends AHSClientImpl {
189     // private ApplicationReport mockReport;
190     private List<ApplicationReport> reports =
191         new ArrayList<ApplicationReport>();
192     private HashMap<ApplicationId, List<ApplicationAttemptReport>> attempts =
193         new HashMap<ApplicationId, List<ApplicationAttemptReport>>();
194     private HashMap<ApplicationAttemptId, List<ContainerReport>> containers =
195         new HashMap<ApplicationAttemptId, List<ContainerReport>>();
196     GetApplicationsResponse mockAppResponse =
197         mock(GetApplicationsResponse.class);
198     GetApplicationReportResponse mockResponse =
199         mock(GetApplicationReportResponse.class);
200     GetApplicationAttemptsResponse mockAppAttemptsResponse =
201         mock(GetApplicationAttemptsResponse.class);
202     GetApplicationAttemptReportResponse mockAttemptResponse =
203         mock(GetApplicationAttemptReportResponse.class);
204     GetContainersResponse mockContainersResponse =
205         mock(GetContainersResponse.class);
206     GetContainerReportResponse mockContainerResponse =
207         mock(GetContainerReportResponse.class);
208 
MockAHSClient()209     public MockAHSClient() {
210       super();
211       createAppReports();
212     }
213 
214     @Override
start()215     public void start() {
216       ahsClient = mock(ApplicationHistoryProtocol.class);
217 
218       try {
219         when(
220           ahsClient
221             .getApplicationReport(any(GetApplicationReportRequest.class)))
222           .thenReturn(mockResponse);
223         when(ahsClient.getApplications(any(GetApplicationsRequest.class)))
224           .thenReturn(mockAppResponse);
225         when(
226           ahsClient
227             .getApplicationAttemptReport(any(GetApplicationAttemptReportRequest.class)))
228           .thenReturn(mockAttemptResponse);
229         when(
230           ahsClient
231             .getApplicationAttempts(any(GetApplicationAttemptsRequest.class)))
232           .thenReturn(mockAppAttemptsResponse);
233         when(ahsClient.getContainers(any(GetContainersRequest.class)))
234           .thenReturn(mockContainersResponse);
235 
236         when(ahsClient.getContainerReport(any(GetContainerReportRequest.class)))
237           .thenReturn(mockContainerResponse);
238 
239       } catch (YarnException e) {
240         Assert.fail("Exception is not expected.");
241       } catch (IOException e) {
242         Assert.fail("Exception is not expected.");
243       }
244     }
245 
246     @Override
getApplications()247     public List<ApplicationReport> getApplications() throws YarnException,
248         IOException {
249       when(mockAppResponse.getApplicationList()).thenReturn(reports);
250       return super.getApplications();
251     }
252 
253     @Override
getApplicationReport(ApplicationId appId)254     public ApplicationReport getApplicationReport(ApplicationId appId)
255         throws YarnException, IOException {
256       when(mockResponse.getApplicationReport()).thenReturn(getReport(appId));
257       return super.getApplicationReport(appId);
258     }
259 
260     @Override
getApplicationAttempts( ApplicationId appId)261     public List<ApplicationAttemptReport> getApplicationAttempts(
262         ApplicationId appId) throws YarnException, IOException {
263       when(mockAppAttemptsResponse.getApplicationAttemptList()).thenReturn(
264         getAttempts(appId));
265       return super.getApplicationAttempts(appId);
266     }
267 
268     @Override
getApplicationAttemptReport( ApplicationAttemptId appAttemptId)269     public ApplicationAttemptReport getApplicationAttemptReport(
270         ApplicationAttemptId appAttemptId) throws YarnException, IOException {
271       when(mockAttemptResponse.getApplicationAttemptReport()).thenReturn(
272         getAttempt(appAttemptId));
273       return super.getApplicationAttemptReport(appAttemptId);
274     }
275 
276     @Override
277     public List<ContainerReport>
getContainers(ApplicationAttemptId appAttemptId)278         getContainers(ApplicationAttemptId appAttemptId) throws YarnException,
279             IOException {
280       when(mockContainersResponse.getContainerList()).thenReturn(
281         getContainersReport(appAttemptId));
282       return super.getContainers(appAttemptId);
283     }
284 
285     @Override
getContainerReport(ContainerId containerId)286     public ContainerReport getContainerReport(ContainerId containerId)
287         throws YarnException, IOException {
288       when(mockContainerResponse.getContainerReport()).thenReturn(
289         getContainer(containerId));
290       return super.getContainerReport(containerId);
291     }
292 
293     @Override
stop()294     public void stop() {
295     }
296 
getReport(ApplicationId appId)297     public ApplicationReport getReport(ApplicationId appId) {
298       for (int i = 0; i < reports.size(); ++i) {
299         if (appId.toString().equalsIgnoreCase(
300           reports.get(i).getApplicationId().toString())) {
301           return reports.get(i);
302         }
303       }
304       return null;
305     }
306 
getAttempts(ApplicationId appId)307     public List<ApplicationAttemptReport> getAttempts(ApplicationId appId) {
308       return attempts.get(appId);
309     }
310 
311     public ApplicationAttemptReport
getAttempt(ApplicationAttemptId appAttemptId)312         getAttempt(ApplicationAttemptId appAttemptId) {
313       return attempts.get(appAttemptId.getApplicationId()).get(0);
314     }
315 
getContainersReport( ApplicationAttemptId appAttemptId)316     public List<ContainerReport> getContainersReport(
317         ApplicationAttemptId appAttemptId) {
318       return containers.get(appAttemptId);
319     }
320 
getContainer(ContainerId containerId)321     public ContainerReport getContainer(ContainerId containerId) {
322       return containers.get(containerId.getApplicationAttemptId()).get(0);
323     }
324 
getReports()325     public List<ApplicationReport> getReports() {
326       return this.reports;
327     }
328 
createAppReports()329     private void createAppReports() {
330       ApplicationId applicationId = ApplicationId.newInstance(1234, 5);
331       ApplicationReport newApplicationReport =
332           ApplicationReport.newInstance(applicationId,
333             ApplicationAttemptId.newInstance(applicationId, 1), "user",
334             "queue", "appname", "host", 124, null,
335             YarnApplicationState.RUNNING, "diagnostics", "url", 0, 0,
336             FinalApplicationStatus.SUCCEEDED, null, "N/A", 0.53789f, "YARN",
337             null);
338       List<ApplicationReport> applicationReports =
339           new ArrayList<ApplicationReport>();
340       applicationReports.add(newApplicationReport);
341       List<ApplicationAttemptReport> appAttempts =
342           new ArrayList<ApplicationAttemptReport>();
343       ApplicationAttemptReport attempt =
344           ApplicationAttemptReport.newInstance(
345             ApplicationAttemptId.newInstance(applicationId, 1),
346             "host",
347             124,
348             "url",
349             "oUrl",
350             "diagnostics",
351             YarnApplicationAttemptState.FINISHED,
352             ContainerId.newContainerId(
353               newApplicationReport.getCurrentApplicationAttemptId(), 1));
354       appAttempts.add(attempt);
355       ApplicationAttemptReport attempt1 =
356           ApplicationAttemptReport.newInstance(
357             ApplicationAttemptId.newInstance(applicationId, 2),
358             "host",
359             124,
360             "url",
361             "oUrl",
362             "diagnostics",
363             YarnApplicationAttemptState.FINISHED,
364             ContainerId.newContainerId(
365               newApplicationReport.getCurrentApplicationAttemptId(), 2));
366       appAttempts.add(attempt1);
367       attempts.put(applicationId, appAttempts);
368 
369       List<ContainerReport> containerReports = new ArrayList<ContainerReport>();
370       ContainerReport container =
371           ContainerReport.newInstance(
372             ContainerId.newContainerId(attempt.getApplicationAttemptId(), 1),
373             null, NodeId.newInstance("host", 1234), Priority.UNDEFINED, 1234,
374             5678, "diagnosticInfo", "logURL", 0, ContainerState.COMPLETE,
375             "http://" + NodeId.newInstance("host", 2345).toString());
376       containerReports.add(container);
377 
378       ContainerReport container1 =
379           ContainerReport.newInstance(
380             ContainerId.newContainerId(attempt.getApplicationAttemptId(), 2),
381             null, NodeId.newInstance("host", 1234), Priority.UNDEFINED, 1234,
382             5678, "diagnosticInfo", "logURL", 0, ContainerState.COMPLETE,
383             "http://" + NodeId.newInstance("host", 2345).toString());
384       containerReports.add(container1);
385       containers.put(attempt.getApplicationAttemptId(), containerReports);
386 
387       ApplicationId applicationId2 = ApplicationId.newInstance(1234, 6);
388       ApplicationReport newApplicationReport2 =
389           ApplicationReport.newInstance(applicationId2,
390             ApplicationAttemptId.newInstance(applicationId2, 2), "user2",
391             "queue2", "appname2", "host2", 125, null,
392             YarnApplicationState.FINISHED, "diagnostics2", "url2", 2, 2,
393             FinalApplicationStatus.SUCCEEDED, null, "N/A", 0.63789f,
394             "NON-YARN", null);
395       applicationReports.add(newApplicationReport2);
396 
397       ApplicationId applicationId3 = ApplicationId.newInstance(1234, 7);
398       ApplicationReport newApplicationReport3 =
399           ApplicationReport.newInstance(applicationId3,
400             ApplicationAttemptId.newInstance(applicationId3, 3), "user3",
401             "queue3", "appname3", "host3", 126, null,
402             YarnApplicationState.RUNNING, "diagnostics3", "url3", 3, 3,
403             FinalApplicationStatus.SUCCEEDED, null, "N/A", 0.73789f,
404             "MAPREDUCE", null);
405       applicationReports.add(newApplicationReport3);
406 
407       ApplicationId applicationId4 = ApplicationId.newInstance(1234, 8);
408       ApplicationReport newApplicationReport4 =
409           ApplicationReport.newInstance(applicationId4,
410             ApplicationAttemptId.newInstance(applicationId4, 4), "user4",
411             "queue4", "appname4", "host4", 127, null,
412             YarnApplicationState.FAILED, "diagnostics4", "url4", 4, 4,
413             FinalApplicationStatus.SUCCEEDED, null, "N/A", 0.83789f,
414             "NON-MAPREDUCE", null);
415       applicationReports.add(newApplicationReport4);
416       reports = applicationReports;
417     }
418   }
419 }
420