|
24 | 24 | import org.junit.Assume;
|
25 | 25 | import org.junit.Test;
|
26 | 26 | import org.assertj.core.api.Assertions;
|
| 27 | +import org.mockito.Mockito; |
27 | 28 |
|
28 | 29 | import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
|
29 | 30 | import org.apache.hadoop.fs.FileSystem;
|
| 31 | +import org.apache.hadoop.fs.azurebfs.contracts.exceptions.AbfsRestOperationException; |
30 | 32 | import org.apache.hadoop.fs.azurebfs.services.AbfsClient;
|
31 | 33 | import org.apache.hadoop.fs.azurebfs.services.AbfsRestOperation;
|
32 | 34 | import org.apache.hadoop.conf.Configuration;
|
33 | 35 | import org.apache.hadoop.fs.Path;
|
34 | 36 | import org.apache.hadoop.fs.azurebfs.enums.Trilean;
|
35 | 37 | import org.apache.hadoop.fs.azurebfs.utils.TracingContext;
|
36 | 38 |
|
| 39 | +import static java.net.HttpURLConnection.HTTP_BAD_REQUEST; |
| 40 | +import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR; |
| 41 | +import static java.net.HttpURLConnection.HTTP_NOT_FOUND; |
| 42 | +import static java.net.HttpURLConnection.HTTP_UNAVAILABLE; |
37 | 43 | import static org.mockito.ArgumentMatchers.any;
|
38 | 44 | import static org.mockito.ArgumentMatchers.anyString;
|
39 | 45 | import static org.mockito.Mockito.doReturn;
|
| 46 | +import static org.mockito.Mockito.doThrow; |
40 | 47 | import static org.mockito.Mockito.mock;
|
41 | 48 | import static org.mockito.Mockito.never;
|
42 | 49 | import static org.mockito.Mockito.times;
|
@@ -217,4 +224,45 @@ private AbfsClient callAbfsGetIsNamespaceEnabledAndReturnMockAbfsClient()
|
217 | 224 | return mockClient;
|
218 | 225 | }
|
219 | 226 |
|
| 227 | + @Test |
| 228 | + public void ensureGetAclDetermineHnsStatusAccurately() throws Exception { |
| 229 | + ensureGetAclDetermineHnsStatusAccuratelyInternal(HTTP_BAD_REQUEST, |
| 230 | + false, false); |
| 231 | + ensureGetAclDetermineHnsStatusAccuratelyInternal(HTTP_NOT_FOUND, |
| 232 | + true, true); |
| 233 | + ensureGetAclDetermineHnsStatusAccuratelyInternal(HTTP_INTERNAL_ERROR, |
| 234 | + true, true); |
| 235 | + ensureGetAclDetermineHnsStatusAccuratelyInternal(HTTP_UNAVAILABLE, |
| 236 | + true, true); |
| 237 | + } |
| 238 | + |
| 239 | + private void ensureGetAclDetermineHnsStatusAccuratelyInternal(int statusCode, |
| 240 | + boolean expectedValue, boolean isExceptionExpected) throws Exception { |
| 241 | + AzureBlobFileSystemStore store = Mockito.spy(getFileSystem().getAbfsStore()); |
| 242 | + AbfsClient mockClient = mock(AbfsClient.class); |
| 243 | + store.setNamespaceEnabled(Trilean.UNKNOWN); |
| 244 | + doReturn(mockClient).when(store).getClient(); |
| 245 | + AbfsRestOperationException ex = new AbfsRestOperationException( |
| 246 | + statusCode, null, Integer.toString(statusCode), null); |
| 247 | + doThrow(ex).when(mockClient).getAclStatus(anyString(), any(TracingContext.class)); |
| 248 | + |
| 249 | + if (isExceptionExpected) { |
| 250 | + try { |
| 251 | + store.getIsNamespaceEnabled(getTestTracingContext(getFileSystem(), false)); |
| 252 | + Assertions.fail( |
| 253 | + "Exception Should have been thrown with status code: " + statusCode); |
| 254 | + } catch (AbfsRestOperationException caughtEx) { |
| 255 | + Assertions.assertThat(caughtEx.getStatusCode()).isEqualTo(statusCode); |
| 256 | + Assertions.assertThat(caughtEx.getErrorMessage()).isEqualTo(ex.getErrorMessage()); |
| 257 | + } |
| 258 | + } |
| 259 | + // This should not trigger extra getAcl() call in case of exceptions. |
| 260 | + boolean isHnsEnabled = store.getIsNamespaceEnabled( |
| 261 | + getTestTracingContext(getFileSystem(), false)); |
| 262 | + Assertions.assertThat(isHnsEnabled).isEqualTo(expectedValue); |
| 263 | + |
| 264 | + // GetAcl() should be called only once to determine the HNS status. |
| 265 | + Mockito.verify(mockClient, times(1)) |
| 266 | + .getAclStatus(anyString(), any(TracingContext.class)); |
| 267 | + } |
220 | 268 | }
|
0 commit comments