steveloughran commented on code in PR #5953:
URL: https://github.com/apache/hadoop/pull/5953#discussion_r1467059052
##########
hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azurebfs/oauth2/TestWorkloadIdentityTokenProvider.java:
##########
@@ -18,10 +18,15 @@
package org.apache.hadoop.fs.azurebfs.oauth2;
+import org.apache.commons.io.FileUtils;
import org.apache.hadoop.fs.azurebfs.AbstractAbfsTestWithTimeout;
import org.junit.Test;
import org.mockito.Mockito;
+import java.io.File;
Review Comment:
nit: this block of imports should go up first
##########
hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azurebfs/oauth2/TestWorkloadIdentityTokenProvider.java:
##########
@@ -77,4 +83,45 @@ public void testTokenDoesNotExpireTooSoon() {
assertFalse(provider.hasEnoughTimeElapsedSinceLastRefresh());
}
+
+ /**
+ * Test that the correct token is read from the token file.
+ *
+ * @throws IOException if the token file is empty or file I/O fails.
+ */
+ @Test
+ public void testGetToken() throws IOException {
+ long startTime = System.currentTimeMillis();
+ File tokenFile = File.createTempFile("azure-identity-token", "txt");
+ FileUtils.write(tokenFile, TOKEN, StandardCharsets.UTF_8);
+ AzureADToken azureAdToken = new AzureADToken();
+ WorkloadIdentityTokenProvider tokenProvider = Mockito.spy(
+ new WorkloadIdentityTokenProvider(AUTHORITY, TENANT_ID, CLIENT_ID,
tokenFile.getPath()));
+ Mockito.doReturn(azureAdToken)
+ .when(tokenProvider).getTokenUsingJWTAssertion(TOKEN);
+ assertEquals(azureAdToken, tokenProvider.getToken());
+ assertTrue("token fetch time was not set correctly",
tokenProvider.getTokenFetchTime() > startTime);
Review Comment:
use AssertJ especially here.
make test >= so that if the start time and load happens in same millisecond
by clock granularity, no test failure
##########
hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/AbfsConfiguration.java:
##########
@@ -884,6 +885,19 @@ public AccessTokenProvider getTokenProvider() throws
TokenAccessProviderExceptio
tokenProvider = new RefreshTokenBasedTokenProvider(authEndpoint,
clientId, refreshToken);
LOG.trace("RefreshTokenBasedTokenProvider initialized");
+ } else if (tokenProviderClass == WorkloadIdentityTokenProvider.class) {
+ String authority = getTrimmedPasswordString(
+ FS_AZURE_ACCOUNT_OAUTH_MSI_AUTHORITY,
+ AuthConfigurations.DEFAULT_FS_AZURE_ACCOUNT_OAUTH_MSI_AUTHORITY);
+ authority = appendSlashIfNeeded(authority);
+ String tenantId =
getPasswordString(FS_AZURE_ACCOUNT_OAUTH_MSI_TENANT);
Review Comment:
always good to trim this so if someone splits a value with newlines it is
trimmed properly
##########
hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azurebfs/oauth2/package-info.java:
##########
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
[email protected]
[email protected]
+package org.apache.hadoop.fs.azurebfs.oauth2;
Review Comment:
no need to worry about package files in test modules...is yetus complaining
about it?
##########
hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azurebfs/oauth2/TestWorkloadIdentityTokenProvider.java:
##########
@@ -77,4 +83,45 @@ public void testTokenDoesNotExpireTooSoon() {
assertFalse(provider.hasEnoughTimeElapsedSinceLastRefresh());
}
+
+ /**
+ * Test that the correct token is read from the token file.
+ *
+ * @throws IOException if the token file is empty or file I/O fails.
+ */
+ @Test
+ public void testGetToken() throws IOException {
+ long startTime = System.currentTimeMillis();
+ File tokenFile = File.createTempFile("azure-identity-token", "txt");
+ FileUtils.write(tokenFile, TOKEN, StandardCharsets.UTF_8);
+ AzureADToken azureAdToken = new AzureADToken();
+ WorkloadIdentityTokenProvider tokenProvider = Mockito.spy(
+ new WorkloadIdentityTokenProvider(AUTHORITY, TENANT_ID, CLIENT_ID,
tokenFile.getPath()));
+ Mockito.doReturn(azureAdToken)
+ .when(tokenProvider).getTokenUsingJWTAssertion(TOKEN);
+ assertEquals(azureAdToken, tokenProvider.getToken());
+ assertTrue("token fetch time was not set correctly",
tokenProvider.getTokenFetchTime() > startTime);
+ }
+
+ /**
+ * Test that an exception is thrown when the token file is empty.
+ *
+ * @throws IOException if file I/O fails.
+ */
+ @Test
+ public void testGetTokenThrowsWhenClientAssertionIsEmpty() throws
IOException {
+ File tokenFile = File.createTempFile("azure-identity-token", "txt");
+ AzureADToken azureAdToken = new AzureADToken();
+ WorkloadIdentityTokenProvider tokenProvider = Mockito.spy(
+ new WorkloadIdentityTokenProvider(AUTHORITY, TENANT_ID, CLIENT_ID,
tokenFile.getPath()));
+ Mockito.doReturn(azureAdToken)
+ .when(tokenProvider).getTokenUsingJWTAssertion(TOKEN);
+ boolean exception = false;
Review Comment:
Use LambdaTestUtils.intercept() here, ideally looking for the error string
as well as exception class.
##########
hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azurebfs/oauth2/TestWorkloadIdentityTokenProvider.java:
##########
@@ -77,4 +83,45 @@ public void testTokenDoesNotExpireTooSoon() {
assertFalse(provider.hasEnoughTimeElapsedSinceLastRefresh());
}
+
+ /**
+ * Test that the correct token is read from the token file.
+ *
+ * @throws IOException if the token file is empty or file I/O fails.
+ */
+ @Test
+ public void testGetToken() throws IOException {
+ long startTime = System.currentTimeMillis();
+ File tokenFile = File.createTempFile("azure-identity-token", "txt");
+ FileUtils.write(tokenFile, TOKEN, StandardCharsets.UTF_8);
+ AzureADToken azureAdToken = new AzureADToken();
+ WorkloadIdentityTokenProvider tokenProvider = Mockito.spy(
+ new WorkloadIdentityTokenProvider(AUTHORITY, TENANT_ID, CLIENT_ID,
tokenFile.getPath()));
+ Mockito.doReturn(azureAdToken)
+ .when(tokenProvider).getTokenUsingJWTAssertion(TOKEN);
+ assertEquals(azureAdToken, tokenProvider.getToken());
Review Comment:
Use assertJ assertions.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]