2929import org .apache .hadoop .fs .CommonConfigurationKeysPublic ;
3030import org .apache .hadoop .fs .FileSystem ;
3131import org .apache .hadoop .fs .azurebfs .constants .AbfsServiceType ;
32+ import org .apache .hadoop .fs .azurebfs .contracts .exceptions .AbfsDriverException ;
3233import org .apache .hadoop .fs .azurebfs .contracts .exceptions .AbfsRestOperationException ;
3334import org .apache .hadoop .fs .azurebfs .services .AbfsClient ;
3435import org .apache .hadoop .fs .azurebfs .services .AbfsDfsClient ;
@@ -159,7 +160,8 @@ public void testFailedRequestWhenFSNotExist() throws Exception {
159160 + testUri .substring (testUri .indexOf ("@" ));
160161 config .setBoolean (FS_AZURE_ACCOUNT_IS_HNS_ENABLED , isUsingXNSAccount );
161162 AzureBlobFileSystem fs = this .getFileSystem (nonExistingFsUrl );
162- fs .getAbfsStore ().setNamespaceEnabled (Trilean .UNKNOWN );
163+ fs .getAbfsStore ().getAbfsConfiguration ()
164+ .setIsNamespaceEnabledAccountForTesting (Trilean .UNKNOWN );
163165
164166 FileNotFoundException ex = intercept (FileNotFoundException .class , ()-> {
165167 fs .getFileStatus (new Path ("/" )); // Run a dummy FS call
@@ -207,7 +209,8 @@ public void testEnsureGetAclCallIsMadeOnceWhenConfigIsNotPresent()
207209 private void ensureGetAclCallIsMadeOnceForInvalidConf (String invalidConf )
208210 throws Exception {
209211 this .getFileSystem ().getAbfsStore ()
210- .setNamespaceEnabled (Trilean .getTrilean (invalidConf ));
212+ .getAbfsConfiguration ()
213+ .setIsNamespaceEnabledAccountForTesting (Trilean .getTrilean (invalidConf ));
211214 AbfsClient mockClient =
212215 callAbfsGetIsNamespaceEnabledAndReturnMockAbfsClient ();
213216 verify (mockClient , times (1 ))
@@ -217,15 +220,17 @@ private void ensureGetAclCallIsMadeOnceForInvalidConf(String invalidConf)
217220 private void ensureGetAclCallIsNeverMadeForValidConf (String validConf )
218221 throws Exception {
219222 this .getFileSystem ().getAbfsStore ()
220- .setNamespaceEnabled (Trilean .getTrilean (validConf ));
223+ .getAbfsConfiguration ()
224+ .setIsNamespaceEnabledAccountForTesting (Trilean .getTrilean (validConf ));
221225 AbfsClient mockClient =
222226 callAbfsGetIsNamespaceEnabledAndReturnMockAbfsClient ();
223227 verify (mockClient , never ())
224228 .getAclStatus (anyString (), any (TracingContext .class ));
225229 }
226230
227231 private void unsetConfAndEnsureGetAclCallIsMadeOnce () throws IOException {
228- this .getFileSystem ().getAbfsStore ().setNamespaceEnabled (Trilean .UNKNOWN );
232+ this .getFileSystem ().getAbfsStore ()
233+ .getAbfsConfiguration ().setIsNamespaceEnabledAccountForTesting (Trilean .UNKNOWN );
229234 AbfsClient mockClient =
230235 callAbfsGetIsNamespaceEnabledAndReturnMockAbfsClient ();
231236 verify (mockClient , times (1 ))
@@ -262,7 +267,7 @@ private void ensureGetAclDetermineHnsStatusAccuratelyInternal(int statusCode,
262267 boolean expectedValue , boolean isExceptionExpected ) throws Exception {
263268 AzureBlobFileSystemStore store = Mockito .spy (getFileSystem ().getAbfsStore ());
264269 AbfsClient mockClient = mock (AbfsClient .class );
265- store .setNamespaceEnabled (Trilean .UNKNOWN );
270+ store .getAbfsConfiguration (). setIsNamespaceEnabledAccountForTesting (Trilean .UNKNOWN );
266271 doReturn (mockClient ).when (store ).getClient (AbfsServiceType .DFS );
267272 AbfsRestOperationException ex = new AbfsRestOperationException (
268273 statusCode , null , Integer .toString (statusCode ), null );
@@ -354,9 +359,7 @@ public void testAccountSpecificConfig() throws Exception {
354359 */
355360 @ Test
356361 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 ();
360363 AzureBlobFileSystem abfs = (AzureBlobFileSystem ) FileSystem .newInstance (configuration );
361364 AbfsConfiguration abfsConfig = new AbfsConfiguration (configuration , "bogusAccountName" );
362365
@@ -414,6 +417,63 @@ public void testNameSpaceConfig() throws Exception {
414417 .isEqualTo (false );
415418 }
416419
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+
417477 private void assertFileSystemInitWithExpectedHNSSettings (
418478 Configuration configuration , boolean expectedIsHnsEnabledValue ) throws IOException {
419479 try (AzureBlobFileSystem fs = (AzureBlobFileSystem ) FileSystem .newInstance (configuration )) {
0 commit comments