|
36 | 36 | import org.apache.hadoop.fs.permission.FsPermission; |
37 | 37 | import org.mockito.Mockito; |
38 | 38 | import org.testng.Assert; |
| 39 | +import org.testng.annotations.DataProvider; |
39 | 40 | import org.testng.annotations.Test; |
40 | 41 |
|
41 | 42 | import com.google.common.io.Files; |
@@ -343,6 +344,77 @@ public void testDisableSetPermissionStep() throws Exception { |
343 | 344 | Assert.assertEquals(fileSet.getFiles().size(), 2); // 1 files to copy + 1 pre publish step |
344 | 345 | } |
345 | 346 |
|
| 347 | + @DataProvider |
| 348 | + public Object[][] dirPermissionWithExpectedSetPermissionStepCount() { |
| 349 | + return new Object[][] { |
| 350 | + {"d---------", 2}, |
| 351 | + {"drw-rw--w-", 2}, |
| 352 | + {"d-w-r----x", 2}, |
| 353 | + {"drwxrwxrwx", 1}, |
| 354 | + {"dr-xr-xr-x", 1}, |
| 355 | + {"d--x------", 1}, |
| 356 | + }; |
| 357 | + } |
| 358 | + |
| 359 | + @Test(dataProvider = "dirPermissionWithExpectedSetPermissionStepCount") |
| 360 | + public void testSetPermissionWhenCopyingDirectoryWithOwnerExecutePermissionSetUnset(String dirPermission, int expectedCount) throws IOException, URISyntaxException { |
| 361 | + Path manifestPath = new Path(getClass().getClassLoader().getResource("manifestBasedDistcpTest/sampleManifestWithOnlyDirectory.json").getPath()); |
| 362 | + Properties props = new Properties(); |
| 363 | + props.setProperty(ConfigurationKeys.DATA_PUBLISHER_FINAL_DIR, "/"); |
| 364 | + props.setProperty("gobblin.copy.preserved.attributes", "rbugpvta"); |
| 365 | + try (FileSystem sourceFs = Mockito.mock(FileSystem.class); |
| 366 | + FileSystem manifestReadFs = Mockito.mock(FileSystem.class); |
| 367 | + FileSystem destFs = Mockito.mock(FileSystem.class)) { |
| 368 | + |
| 369 | + setSourceAndDestFsMocks(sourceFs, destFs, manifestPath, manifestReadFs, false); |
| 370 | + |
| 371 | + Mockito.when(destFs.exists(new Path("/tmp/dataset"))).thenReturn(false); |
| 372 | + Mockito.when(destFs.exists(new Path("/tmp"))).thenReturn(false); |
| 373 | + |
| 374 | + Mockito.when(destFs.getFileStatus(any(Path.class))).thenReturn(localFs.getFileStatus(new Path(tmpDir.toString()))); |
| 375 | + |
| 376 | + setFsMockPathWithPermissions(sourceFs, "/tmp/dataset", dirPermission, "owner1", "group1", true); |
| 377 | + setFsMockPathWithPermissions(sourceFs, "/tmp", "dr--r--r--", "owner2", "group2", true); |
| 378 | + |
| 379 | + Iterator<FileSet<CopyEntity>> fileSets = new ManifestBasedDataset(sourceFs, manifestReadFs, manifestPath, props).getFileSetIterator(destFs, |
| 380 | + CopyConfiguration.builder(destFs, props).build()); |
| 381 | + |
| 382 | + Assert.assertTrue(fileSets.hasNext()); |
| 383 | + FileSet<CopyEntity> fileSet = fileSets.next(); |
| 384 | + // 1 dir to copy + 1 pre publish step + 1 post publish step |
| 385 | + Assert.assertEquals(fileSet.getFiles().size(),3); |
| 386 | + |
| 387 | + CommitStep createDirectoryStep = ((PrePublishStep) fileSet.getFiles().get(1)).getStep(); |
| 388 | + Assert.assertTrue(createDirectoryStep instanceof CreateDirectoryWithPermissionsCommitStep); |
| 389 | + Map<String, List<OwnerAndPermission>> pathAndPermissions = ((CreateDirectoryWithPermissionsCommitStep) createDirectoryStep).getPathAndPermissions(); |
| 390 | + Assert.assertEquals(pathAndPermissions.size(), 1); |
| 391 | + |
| 392 | + Assert.assertTrue(pathAndPermissions.containsKey("/tmp")); |
| 393 | + Assert.assertEquals(pathAndPermissions.get("/tmp").size(), 1); |
| 394 | + Assert.assertEquals(pathAndPermissions.get("/tmp").get(0).getFsPermission(), FsPermission.valueOf("dr--r--r--")); |
| 395 | + Assert.assertEquals(pathAndPermissions.get("/tmp").get(0).getOwner(), "owner2"); |
| 396 | + Assert.assertEquals(pathAndPermissions.get("/tmp").get(0).getGroup(), "group2"); |
| 397 | + |
| 398 | + CommitStep setPermissionStep = ((PostPublishStep) fileSet.getFiles().get(2)).getStep(); |
| 399 | + Assert.assertTrue(setPermissionStep instanceof SetPermissionCommitStep); |
| 400 | + Map<String, OwnerAndPermission> ownerAndPermissionMap = ((SetPermissionCommitStep) setPermissionStep).getPathAndPermissions(); |
| 401 | + Assert.assertEquals(ownerAndPermissionMap.size(), expectedCount); |
| 402 | + |
| 403 | + List<String> sortedMapKeys = new ArrayList<>(ownerAndPermissionMap.keySet()); |
| 404 | + Assert.assertEquals(sortedMapKeys.get(0), "/tmp"); |
| 405 | + Assert.assertEquals(ownerAndPermissionMap.get("/tmp").getFsPermission(), FsPermission.valueOf("dr--r--r--")); |
| 406 | + Assert.assertEquals(ownerAndPermissionMap.get("/tmp").getOwner(), "owner2"); |
| 407 | + Assert.assertEquals(ownerAndPermissionMap.get("/tmp").getGroup(), "group2"); |
| 408 | + |
| 409 | + if (expectedCount > 1) { |
| 410 | + Assert.assertEquals(sortedMapKeys.get(1), "/tmp/dataset"); |
| 411 | + Assert.assertEquals(ownerAndPermissionMap.get("/tmp/dataset").getFsPermission(), FsPermission.valueOf(dirPermission)); |
| 412 | + Assert.assertEquals(ownerAndPermissionMap.get("/tmp/dataset").getOwner(), "owner1"); |
| 413 | + Assert.assertEquals(ownerAndPermissionMap.get("/tmp/dataset").getGroup(), "group1"); |
| 414 | + } |
| 415 | + } |
| 416 | + } |
| 417 | + |
346 | 418 | private void setSourceAndDestFsMocks(FileSystem sourceFs, FileSystem destFs, Path manifestPath, FileSystem manifestReadFs, boolean setFileStatusMock) throws IOException, URISyntaxException { |
347 | 419 | URI SRC_FS_URI = new URI("source", "the.source.org", "/", null); |
348 | 420 | URI MANIFEST_READ_FS_URI = new URI("manifest-read", "the.manifest-source.org", "/", null); |
|
0 commit comments