@@ -21,9 +21,12 @@ import java.security.PrivilegedExceptionAction
2121
2222import scala .util .Random
2323
24- import org .apache .hadoop .fs .FileStatus
24+ import org .apache .hadoop .fs .{ FileStatus , FileSystem , Path }
2525import org .apache .hadoop .fs .permission .{FsAction , FsPermission }
26- import org .apache .hadoop .security .UserGroupInformation
26+ import org .apache .hadoop .security .{AccessControlException , UserGroupInformation }
27+ import org .mockito
28+ import org .mockito .internal .stubbing .answers .DoesNothing
29+ import org .mockito .Mockito ._
2730import org .scalatest .Matchers
2831
2932import org .apache .spark .SparkFunSuite
@@ -72,6 +75,14 @@ class SparkHadoopUtilSuite extends SparkFunSuite with Matchers {
7275 sparkHadoopUtil.checkAccessPermission(status, READ ) should be(false )
7376 sparkHadoopUtil.checkAccessPermission(status, WRITE ) should be(false )
7477
78+ status = mockedStatus(otherUser, otherGroup, READ_WRITE , READ_WRITE , NONE , true )
79+ sparkHadoopUtil.checkAccessPermission(status, READ ) should be(true )
80+ sparkHadoopUtil.checkAccessPermission(status, WRITE ) should be(true )
81+
82+ status = mockedStatus(otherUser, otherGroup, READ_WRITE , READ_WRITE , NONE , false )
83+ sparkHadoopUtil.checkAccessPermission(status, READ ) should be(false )
84+ sparkHadoopUtil.checkAccessPermission(status, WRITE ) should be(false )
85+
7586 null
7687 }
7788 })
@@ -94,4 +105,26 @@ class SparkHadoopUtilSuite extends SparkFunSuite with Matchers {
94105 group,
95106 null )
96107 }
108+
109+ private def mockedStatus (
110+ owner : String ,
111+ group : String ,
112+ userAction : FsAction ,
113+ groupAction : FsAction ,
114+ otherAction : FsAction ,
115+ accessGranted : Boolean ): FileStatus = {
116+ val mockedFs = mock(classOf [FileSystem ])
117+ val stub = when(mockedFs.access(mockito.Matchers .any(), mockito.Matchers .any()))
118+ if (accessGranted) {
119+ stub.thenAnswer(new DoesNothing )
120+ } else {
121+ stub.thenThrow(new AccessControlException )
122+ }
123+ val mockedPath = mock(classOf [Path ])
124+ when(mockedPath.getFileSystem(mockito.Matchers .any())).thenReturn(mockedFs)
125+ // This status has no access permission, here we modify only the Path it returns
126+ val mockedStatus = spy(fileStatus(owner, group, userAction, groupAction, otherAction))
127+ when(mockedStatus.getPath).thenReturn(mockedPath)
128+ mockedStatus
129+ }
97130}
0 commit comments