29
29
import org .apache .hadoop .fs .CommonConfigurationKeysPublic ;
30
30
import org .apache .hadoop .fs .FileSystem ;
31
31
import org .apache .hadoop .fs .azurebfs .constants .AbfsServiceType ;
32
+ import org .apache .hadoop .fs .azurebfs .contracts .exceptions .AbfsDriverException ;
32
33
import org .apache .hadoop .fs .azurebfs .contracts .exceptions .AbfsRestOperationException ;
33
34
import org .apache .hadoop .fs .azurebfs .services .AbfsClient ;
34
35
import org .apache .hadoop .fs .azurebfs .services .AbfsDfsClient ;
@@ -159,7 +160,8 @@ public void testFailedRequestWhenFSNotExist() throws Exception {
159
160
+ testUri .substring (testUri .indexOf ("@" ));
160
161
config .setBoolean (FS_AZURE_ACCOUNT_IS_HNS_ENABLED , isUsingXNSAccount );
161
162
AzureBlobFileSystem fs = this .getFileSystem (nonExistingFsUrl );
162
- fs .getAbfsStore ().setNamespaceEnabled (Trilean .UNKNOWN );
163
+ fs .getAbfsStore ().getAbfsConfiguration ()
164
+ .setIsNamespaceEnabledAccountForTesting (Trilean .UNKNOWN );
163
165
164
166
FileNotFoundException ex = intercept (FileNotFoundException .class , ()-> {
165
167
fs .getFileStatus (new Path ("/" )); // Run a dummy FS call
@@ -207,7 +209,8 @@ public void testEnsureGetAclCallIsMadeOnceWhenConfigIsNotPresent()
207
209
private void ensureGetAclCallIsMadeOnceForInvalidConf (String invalidConf )
208
210
throws Exception {
209
211
this .getFileSystem ().getAbfsStore ()
210
- .setNamespaceEnabled (Trilean .getTrilean (invalidConf ));
212
+ .getAbfsConfiguration ()
213
+ .setIsNamespaceEnabledAccountForTesting (Trilean .getTrilean (invalidConf ));
211
214
AbfsClient mockClient =
212
215
callAbfsGetIsNamespaceEnabledAndReturnMockAbfsClient ();
213
216
verify (mockClient , times (1 ))
@@ -217,15 +220,17 @@ private void ensureGetAclCallIsMadeOnceForInvalidConf(String invalidConf)
217
220
private void ensureGetAclCallIsNeverMadeForValidConf (String validConf )
218
221
throws Exception {
219
222
this .getFileSystem ().getAbfsStore ()
220
- .setNamespaceEnabled (Trilean .getTrilean (validConf ));
223
+ .getAbfsConfiguration ()
224
+ .setIsNamespaceEnabledAccountForTesting (Trilean .getTrilean (validConf ));
221
225
AbfsClient mockClient =
222
226
callAbfsGetIsNamespaceEnabledAndReturnMockAbfsClient ();
223
227
verify (mockClient , never ())
224
228
.getAclStatus (anyString (), any (TracingContext .class ));
225
229
}
226
230
227
231
private void unsetConfAndEnsureGetAclCallIsMadeOnce () throws IOException {
228
- this .getFileSystem ().getAbfsStore ().setNamespaceEnabled (Trilean .UNKNOWN );
232
+ this .getFileSystem ().getAbfsStore ()
233
+ .getAbfsConfiguration ().setIsNamespaceEnabledAccountForTesting (Trilean .UNKNOWN );
229
234
AbfsClient mockClient =
230
235
callAbfsGetIsNamespaceEnabledAndReturnMockAbfsClient ();
231
236
verify (mockClient , times (1 ))
@@ -262,7 +267,7 @@ private void ensureGetAclDetermineHnsStatusAccuratelyInternal(int statusCode,
262
267
boolean expectedValue , boolean isExceptionExpected ) throws Exception {
263
268
AzureBlobFileSystemStore store = Mockito .spy (getFileSystem ().getAbfsStore ());
264
269
AbfsClient mockClient = mock (AbfsClient .class );
265
- store .setNamespaceEnabled (Trilean .UNKNOWN );
270
+ store .getAbfsConfiguration (). setIsNamespaceEnabledAccountForTesting (Trilean .UNKNOWN );
266
271
doReturn (mockClient ).when (store ).getClient (AbfsServiceType .DFS );
267
272
AbfsRestOperationException ex = new AbfsRestOperationException (
268
273
statusCode , null , Integer .toString (statusCode ), null );
@@ -354,9 +359,7 @@ public void testAccountSpecificConfig() throws Exception {
354
359
*/
355
360
@ Test
356
361
public void testNameSpaceConfig () throws Exception {
357
- Configuration configuration = getRawConfiguration ();
358
- configuration .unset (FS_AZURE_ACCOUNT_IS_HNS_ENABLED );
359
- configuration .unset (accountProperty (FS_AZURE_ACCOUNT_IS_HNS_ENABLED , this .getAccountName ()));
362
+ Configuration configuration = getConfigurationWithoutHnsConfig ();
360
363
AzureBlobFileSystem abfs = (AzureBlobFileSystem ) FileSystem .newInstance (configuration );
361
364
AbfsConfiguration abfsConfig = new AbfsConfiguration (configuration , "bogusAccountName" );
362
365
@@ -414,6 +417,63 @@ public void testNameSpaceConfig() throws Exception {
414
417
.isEqualTo (false );
415
418
}
416
419
420
+ /**
421
+ * Tests to check that the namespace configuration is set correctly
422
+ * during the initialization of the AzureBlobFileSystem.
423
+ *
424
+ *
425
+ * @throws Exception if any error occurs during configuration setup or evaluation
426
+ */
427
+ @ Test
428
+ public void testFsInitShouldSetNamespaceConfig () throws Exception {
429
+ // Mock the AzureBlobFileSystem and its dependencies
430
+ AzureBlobFileSystem mockFileSystem = Mockito .spy ((AzureBlobFileSystem )
431
+ FileSystem .newInstance (getConfigurationWithoutHnsConfig ()));
432
+ AzureBlobFileSystemStore mockStore = Mockito .spy (mockFileSystem .getAbfsStore ());
433
+ AbfsClient abfsClient = Mockito .spy (mockStore .getClient ());
434
+ Mockito .doReturn (abfsClient ).when (mockStore ).getClient ();
435
+ Mockito .doReturn (abfsClient ).when (mockStore ).getClient (any ());
436
+ abfsClient .getIsNamespaceEnabled ();
437
+ // Verify that getAclStatus is called once during initialization
438
+ Mockito .verify (abfsClient , times (0 ))
439
+ .getAclStatus (anyString (), any (TracingContext .class ));
440
+
441
+ mockStore .getAbfsConfiguration ().setIsNamespaceEnabledAccountForTesting (Trilean .UNKNOWN );
442
+ // In case someone wrongly configured the namespace in between the processing,
443
+ // abfsclient.getIsNamespaceEnabled() should throw an exception.
444
+ String errorMessage = intercept (AbfsDriverException .class , () -> {
445
+ abfsClient .getIsNamespaceEnabled ();
446
+ }).getMessage ();
447
+ Assertions .assertThat (errorMessage )
448
+ .describedAs ("Client should throw exception when namespace is unknown" )
449
+ .contains ("Failed to determine account type" );
450
+
451
+ // In case of unknown namespace, store's getIsNamespaceEnabled should call getAclStatus
452
+ // to determine the namespace status.
453
+ mockStore .getIsNamespaceEnabled (getTestTracingContext (mockFileSystem , false ));
454
+ Mockito .verify (abfsClient , times (1 ))
455
+ .getAclStatus (anyString (), any (TracingContext .class ));
456
+
457
+ abfsClient .getIsNamespaceEnabled ();
458
+ // Verify that client's getNamespaceEnabled will not call getAclStatus again
459
+ Mockito .verify (abfsClient , times (1 ))
460
+ .getAclStatus (anyString (), any (TracingContext .class ));
461
+ }
462
+
463
+ /**
464
+ * Returns the configuration without the HNS config set.
465
+ *
466
+ * @return Configuration without HNS config
467
+ */
468
+ private Configuration getConfigurationWithoutHnsConfig () {
469
+ Configuration configuration = getRawConfiguration ();
470
+ configuration .unset (FS_AZURE_ACCOUNT_IS_HNS_ENABLED );
471
+ configuration .unset (accountProperty (FS_AZURE_ACCOUNT_IS_HNS_ENABLED , this .getAccountName ()));
472
+ configuration .setBoolean (AZURE_CREATE_REMOTE_FILESYSTEM_DURING_INITIALIZATION , true );
473
+ configuration .set (CommonConfigurationKeysPublic .FS_DEFAULT_NAME_KEY , getTestUrl ());
474
+ return configuration ;
475
+ }
476
+
417
477
private void assertFileSystemInitWithExpectedHNSSettings (
418
478
Configuration configuration , boolean expectedIsHnsEnabledValue ) throws IOException {
419
479
try (AzureBlobFileSystem fs = (AzureBlobFileSystem ) FileSystem .newInstance (configuration )) {
0 commit comments