Skip to content

Commit 070759f

Browse files
author
slfan1989
committed
YARN-11158. Add Junit Test.
1 parent 15ad090 commit 070759f

File tree

1 file changed

+39
-68
lines changed

1 file changed

+39
-68
lines changed

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/test/java/org/apache/hadoop/yarn/server/router/clientrm/TestFederationClientInterceptor.java

Lines changed: 39 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -38,72 +38,7 @@
3838
import org.apache.hadoop.test.LambdaTestUtils;
3939
import org.apache.hadoop.util.Time;
4040
import org.apache.hadoop.yarn.MockApps;
41-
import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationAttemptReportRequest;
42-
import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationAttemptReportResponse;
43-
import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationReportRequest;
44-
import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationReportResponse;
45-
import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsRequest;
46-
import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationsResponse;
47-
import org.apache.hadoop.yarn.api.protocolrecords.GetClusterMetricsRequest;
48-
import org.apache.hadoop.yarn.api.protocolrecords.GetClusterMetricsResponse;
49-
import org.apache.hadoop.yarn.api.protocolrecords.GetNewApplicationRequest;
50-
import org.apache.hadoop.yarn.api.protocolrecords.GetNewApplicationResponse;
51-
import org.apache.hadoop.yarn.api.protocolrecords.KillApplicationRequest;
52-
import org.apache.hadoop.yarn.api.protocolrecords.KillApplicationResponse;
53-
import org.apache.hadoop.yarn.api.protocolrecords.SubmitApplicationRequest;
54-
import org.apache.hadoop.yarn.api.protocolrecords.SubmitApplicationResponse;
55-
import org.apache.hadoop.yarn.api.protocolrecords.GetClusterNodesResponse;
56-
import org.apache.hadoop.yarn.api.protocolrecords.GetClusterNodesRequest;
57-
import org.apache.hadoop.yarn.api.protocolrecords.GetNodesToLabelsResponse;
58-
import org.apache.hadoop.yarn.api.protocolrecords.GetNodesToLabelsRequest;
59-
import org.apache.hadoop.yarn.api.protocolrecords.GetLabelsToNodesResponse;
60-
import org.apache.hadoop.yarn.api.protocolrecords.GetLabelsToNodesRequest;
61-
import org.apache.hadoop.yarn.api.protocolrecords.GetClusterNodeLabelsResponse;
62-
import org.apache.hadoop.yarn.api.protocolrecords.GetClusterNodeLabelsRequest;
63-
import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationAttemptsRequest;
64-
import org.apache.hadoop.yarn.api.protocolrecords.GetApplicationAttemptsResponse;
65-
import org.apache.hadoop.yarn.api.protocolrecords.GetQueueUserAclsInfoResponse;
66-
import org.apache.hadoop.yarn.api.protocolrecords.GetQueueUserAclsInfoRequest;
67-
import org.apache.hadoop.yarn.api.protocolrecords.ReservationListResponse;
68-
import org.apache.hadoop.yarn.api.protocolrecords.ReservationListRequest;
69-
import org.apache.hadoop.yarn.api.protocolrecords.GetContainersRequest;
70-
import org.apache.hadoop.yarn.api.protocolrecords.GetContainersResponse;
71-
import org.apache.hadoop.yarn.api.protocolrecords.GetContainerReportRequest;
72-
import org.apache.hadoop.yarn.api.protocolrecords.GetContainerReportResponse;
73-
import org.apache.hadoop.yarn.api.protocolrecords.GetAllResourceTypeInfoResponse;
74-
import org.apache.hadoop.yarn.api.protocolrecords.GetAllResourceTypeInfoRequest;
75-
import org.apache.hadoop.yarn.api.protocolrecords.FailApplicationAttemptRequest;
76-
import org.apache.hadoop.yarn.api.protocolrecords.FailApplicationAttemptResponse;
77-
import org.apache.hadoop.yarn.api.protocolrecords.UpdateApplicationPriorityRequest;
78-
import org.apache.hadoop.yarn.api.protocolrecords.UpdateApplicationPriorityResponse;
79-
import org.apache.hadoop.yarn.api.protocolrecords.UpdateApplicationTimeoutsRequest;
80-
import org.apache.hadoop.yarn.api.protocolrecords.UpdateApplicationTimeoutsResponse;
81-
import org.apache.hadoop.yarn.api.protocolrecords.SignalContainerRequest;
82-
import org.apache.hadoop.yarn.api.protocolrecords.SignalContainerResponse;
83-
import org.apache.hadoop.yarn.api.protocolrecords.MoveApplicationAcrossQueuesRequest;
84-
import org.apache.hadoop.yarn.api.protocolrecords.MoveApplicationAcrossQueuesResponse;
85-
import org.apache.hadoop.yarn.api.protocolrecords.GetQueueInfoRequest;
86-
import org.apache.hadoop.yarn.api.protocolrecords.GetQueueInfoResponse;
87-
import org.apache.hadoop.yarn.api.protocolrecords.GetAllResourceProfilesRequest;
88-
import org.apache.hadoop.yarn.api.protocolrecords.GetAllResourceProfilesResponse;
89-
import org.apache.hadoop.yarn.api.protocolrecords.GetResourceProfileRequest;
90-
import org.apache.hadoop.yarn.api.protocolrecords.GetResourceProfileResponse;
91-
import org.apache.hadoop.yarn.api.protocolrecords.GetAttributesToNodesRequest;
92-
import org.apache.hadoop.yarn.api.protocolrecords.GetAttributesToNodesResponse;
93-
import org.apache.hadoop.yarn.api.protocolrecords.GetClusterNodeAttributesRequest;
94-
import org.apache.hadoop.yarn.api.protocolrecords.GetClusterNodeAttributesResponse;
95-
import org.apache.hadoop.yarn.api.protocolrecords.GetNodesToAttributesRequest;
96-
import org.apache.hadoop.yarn.api.protocolrecords.GetNodesToAttributesResponse;
97-
import org.apache.hadoop.yarn.api.protocolrecords.GetNewReservationRequest;
98-
import org.apache.hadoop.yarn.api.protocolrecords.GetNewReservationResponse;
99-
import org.apache.hadoop.yarn.api.protocolrecords.ReservationSubmissionRequest;
100-
import org.apache.hadoop.yarn.api.protocolrecords.ReservationSubmissionResponse;
101-
import org.apache.hadoop.yarn.api.protocolrecords.ReservationUpdateRequest;
102-
import org.apache.hadoop.yarn.api.protocolrecords.ReservationUpdateResponse;
103-
import org.apache.hadoop.yarn.api.protocolrecords.ReservationDeleteRequest;
104-
import org.apache.hadoop.yarn.api.protocolrecords.ReservationDeleteResponse;
105-
import org.apache.hadoop.yarn.api.protocolrecords.GetDelegationTokenRequest;
106-
import org.apache.hadoop.yarn.api.protocolrecords.GetDelegationTokenResponse;
41+
import org.apache.hadoop.yarn.api.protocolrecords.*;
10742
import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
10843
import org.apache.hadoop.yarn.api.records.ApplicationId;
10944
import org.apache.hadoop.yarn.api.records.ApplicationSubmissionContext;
@@ -146,7 +81,9 @@
14681
import org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMAppState;
14782
import org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.RMAppAttemptState;
14883
import org.apache.hadoop.yarn.server.router.security.RouterDelegationTokenSecretManager;
84+
import org.apache.hadoop.yarn.server.utils.BuilderUtils;
14985
import org.apache.hadoop.yarn.util.ConverterUtils;
86+
import org.apache.hadoop.yarn.util.Records;
15087
import org.apache.hadoop.yarn.util.Times;
15188
import org.apache.hadoop.yarn.util.resource.Resources;
15289
import org.junit.Assert;
@@ -1615,9 +1552,43 @@ public void testGetDelegationToken() throws IOException, YarnException {
16151552
RouterRMDTSecretManagerState managerState = stateStore.getRouterRMSecretManagerState();
16161553
Map<RMDelegationTokenIdentifier, Long> delegationTokenState = managerState.getTokenState();
16171554
long tokenRenewInterval = this.getConf().getLong(
1618-
YarnConfiguration.RM_DELEGATION_TOKEN_RENEW_INTERVAL_KEY,
1619-
YarnConfiguration.RM_DELEGATION_TOKEN_RENEW_INTERVAL_DEFAULT);
1555+
YarnConfiguration.RM_DELEGATION_TOKEN_RENEW_INTERVAL_KEY,
1556+
YarnConfiguration.RM_DELEGATION_TOKEN_RENEW_INTERVAL_DEFAULT);
16201557
long renewDate = delegationTokenState.get(rMDelegationTokenIdentifier);
16211558
Assert.assertEquals((issueDate + tokenRenewInterval), renewDate);
16221559
}
1560+
1561+
@Test
1562+
public void testRenewDelegationToken() throws IOException, YarnException {
1563+
1564+
// We design such a unit test to check
1565+
// that the execution of the GetDelegationToken method is as expected
1566+
// 1. Call GetDelegationToken to apply for delegationToken.
1567+
// 2. Call renewDelegationToken to refresh delegationToken.
1568+
// By looking at the code of AbstractDelegationTokenSecretManager#renewToken,
1569+
// we know that renewTime is calculated as Math.min(id.getMaxDate(), now + tokenRenewInterval)
1570+
// so renewTime will be less than or equal to maxDate
1571+
1572+
GetDelegationTokenRequest request = mock(GetDelegationTokenRequest.class);
1573+
when(request.getRenewer()).thenReturn("renewer2");
1574+
GetDelegationTokenResponse response = interceptor.getDelegationToken(request);
1575+
Assert.assertNotNull(response);
1576+
Token delegationToken = response.getRMDelegationToken();
1577+
1578+
// Step2. Serialize the returned Token as RMDelegationTokenIdentifier.
1579+
org.apache.hadoop.security.token.Token<RMDelegationTokenIdentifier> token =
1580+
ConverterUtils.convertFromYarn(delegationToken, (Text) null);
1581+
RMDelegationTokenIdentifier rMDelegationTokenIdentifier = token.decodeIdentifier();
1582+
String renewer = rMDelegationTokenIdentifier.getRenewer().toString();
1583+
long maxDate = rMDelegationTokenIdentifier.getMaxDate();
1584+
Assert.assertEquals("renewer2", renewer);
1585+
1586+
RenewDelegationTokenRequest renewRequest = Records.newRecord(RenewDelegationTokenRequest.class);
1587+
renewRequest.setDelegationToken(delegationToken);
1588+
RenewDelegationTokenResponse renewResponse = interceptor.renewDelegationToken(renewRequest);
1589+
Assert.assertNotNull(renewResponse);
1590+
1591+
long expDate = renewResponse.getNextExpirationTime();
1592+
Assert.assertTrue(expDate <= maxDate);
1593+
}
16231594
}

0 commit comments

Comments
 (0)