|
23 | 23 | import java.io.IOException;
|
24 | 24 | import java.lang.reflect.Field;
|
25 | 25 | import java.util.ArrayList;
|
| 26 | +import java.util.Collections; |
26 | 27 | import java.util.EnumSet;
|
27 | 28 | import java.util.List;
|
28 | 29 | import java.util.Random;
|
|
37 | 38 | import org.junit.Assume;
|
38 | 39 | import org.junit.Test;
|
39 | 40 | import org.mockito.Mockito;
|
| 41 | +import org.mockito.invocation.InvocationOnMock; |
| 42 | +import org.mockito.stubbing.Answer; |
40 | 43 |
|
41 | 44 | import org.apache.hadoop.conf.Configuration;
|
42 | 45 | import org.apache.hadoop.fs.CreateFlag;
|
|
54 | 57 | import org.apache.hadoop.fs.azurebfs.services.AbfsBlobClient;
|
55 | 58 | import org.apache.hadoop.fs.azurebfs.services.AbfsClient;
|
56 | 59 | import org.apache.hadoop.fs.azurebfs.services.AbfsClientHandler;
|
| 60 | +import org.apache.hadoop.fs.azurebfs.services.AbfsClientTestUtil; |
57 | 61 | import org.apache.hadoop.fs.azurebfs.services.AbfsHttpOperation;
|
58 | 62 | import org.apache.hadoop.fs.azurebfs.services.AbfsRestOperation;
|
| 63 | +import org.apache.hadoop.fs.azurebfs.services.AbfsRestOperationType; |
59 | 64 | import org.apache.hadoop.fs.azurebfs.services.ITestAbfsClient;
|
60 | 65 | import org.apache.hadoop.fs.azurebfs.services.RenameAtomicity;
|
61 | 66 | import org.apache.hadoop.fs.azurebfs.utils.DirectoryStateHelper;
|
|
85 | 90 | import static org.mockito.ArgumentMatchers.anyString;
|
86 | 91 | import static org.mockito.ArgumentMatchers.eq;
|
87 | 92 | import static org.mockito.ArgumentMatchers.nullable;
|
| 93 | +import static org.mockito.Mockito.doAnswer; |
88 | 94 | import static org.mockito.Mockito.doCallRealMethod;
|
89 | 95 | import static org.mockito.Mockito.doNothing;
|
90 | 96 | import static org.mockito.Mockito.doReturn;
|
@@ -729,6 +735,127 @@ public void testNegativeScenariosForCreateOverwriteDisabled()
|
729 | 735 | validateCreateFileException(AbfsRestOperationException.class, abfsStore);
|
730 | 736 | }
|
731 | 737 |
|
| 738 | + /** |
| 739 | + * Tests that the exception thrown during the creation of a marker is swallowed. |
| 740 | + * This test verifies that when an exception occurs during the creation of a marker, |
| 741 | + * it does not propagate and is handled internally and file creation still succeeds. |
| 742 | + * |
| 743 | + * @throws Throwable if an error occurs during the test execution |
| 744 | + */ |
| 745 | + @Test |
| 746 | + public void testCreateMarkerFailExceptionIsSwallowed() |
| 747 | + throws Throwable { |
| 748 | + |
| 749 | + final AzureBlobFileSystem currentFs = getFileSystem(); |
| 750 | + Configuration config = new Configuration(this.getRawConfiguration()); |
| 751 | + config.set("fs.azure.enable.conditional.create.overwrite", |
| 752 | + Boolean.toString(true)); |
| 753 | + |
| 754 | + final AzureBlobFileSystem fs = |
| 755 | + (AzureBlobFileSystem) FileSystem.newInstance(currentFs.getUri(), |
| 756 | + config); |
| 757 | + |
| 758 | + // Get mock AbfsClient with current config |
| 759 | + AbfsClient mockClient = Mockito.spy(fs.getAbfsClient()); |
| 760 | + AzureBlobFileSystemStore spiedStore = Mockito.spy(fs.getAbfsStore()); |
| 761 | + spiedStore.setClient(mockClient); |
| 762 | + |
| 763 | + Assume.assumeTrue(mockClient instanceof AbfsBlobClient); |
| 764 | + AbfsClientHandler clientHandler = Mockito.mock(AbfsClientHandler.class); |
| 765 | + when(clientHandler.getIngressClient()).thenReturn(mockClient); |
| 766 | + when(clientHandler.getClient(Mockito.any())).thenReturn(mockClient); |
| 767 | + Path testFolder = new Path("/dir1"); |
| 768 | + createAzCopyFolder(testFolder); |
| 769 | + |
| 770 | + AzureBlobFileSystemStore abfsStore = fs.getAbfsStore(); |
| 771 | + |
| 772 | + ReflectionUtils.setFinalField(AzureBlobFileSystemStore.class, abfsStore, |
| 773 | + "clientHandler", clientHandler); |
| 774 | + ReflectionUtils.setFinalField(AzureBlobFileSystemStore.class, abfsStore, |
| 775 | + "client", mockClient); |
| 776 | + |
| 777 | + AbfsRestOperation successOp = mock( |
| 778 | + AbfsRestOperation.class); |
| 779 | + AbfsHttpOperation http200Op = mock( |
| 780 | + AbfsHttpOperation.class); |
| 781 | + when(http200Op.getStatusCode()).thenReturn(HTTP_OK); |
| 782 | + when(successOp.getResult()).thenReturn(http200Op); |
| 783 | + |
| 784 | + AbfsRestOperationException preConditionResponseEx |
| 785 | + = getMockAbfsRestOperationException(HTTP_PRECON_FAILED); |
| 786 | + |
| 787 | + doCallRealMethod().when(mockClient) |
| 788 | + .conditionalCreateOverwriteFile(anyString(), |
| 789 | + Mockito.nullable(FileSystem.Statistics.class), |
| 790 | + Mockito.nullable(AzureBlobFileSystemStore.Permissions.class), |
| 791 | + anyBoolean(), |
| 792 | + Mockito.nullable(ContextEncryptionAdapter.class), |
| 793 | + Mockito.nullable(TracingContext.class)); |
| 794 | + |
| 795 | + doCallRealMethod().when((AbfsBlobClient) mockClient) |
| 796 | + .checkDirectoryAndCreateMarkersIfNeeded(anyString(), |
| 797 | + Mockito.nullable(AzureBlobFileSystemStore.Permissions.class), |
| 798 | + anyBoolean(), |
| 799 | + Mockito.nullable(String.class), |
| 800 | + Mockito.nullable(ContextEncryptionAdapter.class), |
| 801 | + Mockito.nullable(TracingContext.class)); |
| 802 | + |
| 803 | + doCallRealMethod().when((AbfsBlobClient) mockClient) |
| 804 | + .createParentMarkersIfNeeded(anyString(), |
| 805 | + Mockito.nullable(AzureBlobFileSystemStore.Permissions.class), |
| 806 | + anyBoolean(), |
| 807 | + Mockito.nullable(String.class), |
| 808 | + Mockito.nullable(ContextEncryptionAdapter.class), |
| 809 | + Mockito.nullable(TracingContext.class)); |
| 810 | + |
| 811 | + Mockito.doReturn(new ArrayList<>(Collections.singletonList(testFolder))) |
| 812 | + .when((AbfsBlobClient) mockClient) |
| 813 | + .getMarkerPathsTobeCreated(any(Path.class), |
| 814 | + Mockito.nullable(TracingContext.class)); |
| 815 | + |
| 816 | + doReturn(false).when((AbfsBlobClient) mockClient) |
| 817 | + .checkIsDirectoryPath(anyString(), |
| 818 | + Mockito.nullable(TracingContext.class)); |
| 819 | + |
| 820 | + // throw exception for first call of marker creation and return true for file creation |
| 821 | + doAnswer(new Answer<Void>() { |
| 822 | + private boolean firstCall = true; |
| 823 | + |
| 824 | + @Override |
| 825 | + public Void answer(InvocationOnMock invocation) throws Throwable { |
| 826 | + if (firstCall) { |
| 827 | + firstCall = false; |
| 828 | + throw preConditionResponseEx; |
| 829 | + } |
| 830 | + return null; |
| 831 | + } |
| 832 | + }).doCallRealMethod() |
| 833 | + .when((AbfsBlobClient) mockClient) |
| 834 | + .createPathRestOp(anyString(), anyBoolean(), anyBoolean(), |
| 835 | + anyBoolean(), Mockito.nullable(String.class), |
| 836 | + Mockito.nullable(ContextEncryptionAdapter.class), |
| 837 | + Mockito.nullable(TracingContext.class)); |
| 838 | + |
| 839 | + AbfsClientTestUtil.hookOnRestOpsForTracingContextSingularity(mockClient); |
| 840 | + |
| 841 | + doReturn(successOp) // Scn4: create overwrite=true fails with Http500 |
| 842 | + .when((AbfsBlobClient) mockClient) |
| 843 | + .getPathStatus(any(String.class), any(TracingContext.class), nullable( |
| 844 | + ContextEncryptionAdapter.class), eq(false)); |
| 845 | + |
| 846 | + FsPermission permission = new FsPermission(FsAction.ALL, FsAction.ALL, |
| 847 | + FsAction.ALL); |
| 848 | + FsPermission umask = new FsPermission(FsAction.NONE, FsAction.NONE, |
| 849 | + FsAction.NONE); |
| 850 | + Path testPath = new Path("/dir1/testFile"); |
| 851 | + abfsStore.createFile(testPath, null, true, permission, umask, |
| 852 | + getTestTracingContext(getFileSystem(), true)); |
| 853 | + Assertions.assertThat(fs.exists(testPath)) |
| 854 | + .describedAs("File not created when marker creation failed.") |
| 855 | + .isTrue(); |
| 856 | + |
| 857 | + } |
| 858 | + |
732 | 859 | private <E extends Throwable> void validateCreateFileException(final Class<E> exceptionClass, final AzureBlobFileSystemStore abfsStore)
|
733 | 860 | throws Exception {
|
734 | 861 | FsPermission permission = new FsPermission(FsAction.ALL, FsAction.ALL,
|
|
0 commit comments