Search in sources :

Example 11 with ParentNotDirectoryException

use of org.apache.hadoop.fs.ParentNotDirectoryException in project hadoop by apache.

the class TestFileCreation method testFileCreationNonRecursive.

// Worker method for testing non-recursive. Extracted to allow other
// FileSystem implementations to re-use the tests
public static void testFileCreationNonRecursive(FileSystem fs) throws IOException {
    final Path path = new Path("/" + Time.now() + "-testFileCreationNonRecursive");
    IOException expectedException = null;
    final String nonExistDir = "/non-exist-" + Time.now();
    fs.delete(new Path(nonExistDir), true);
    EnumSet<CreateFlag> createFlag = EnumSet.of(CreateFlag.CREATE);
    // Create a new file in root dir, should succeed
    assertNull(createNonRecursive(fs, path, 1, createFlag));
    // Create a file when parent dir exists as file, should fail
    expectedException = createNonRecursive(fs, new Path(path, "Create"), 1, createFlag);
    assertTrue("Create a file when parent directory exists as a file" + " should throw ParentNotDirectoryException ", expectedException != null && expectedException instanceof ParentNotDirectoryException);
    fs.delete(path, true);
    // Create a file in a non-exist directory, should fail
    final Path path2 = new Path(nonExistDir + "/testCreateNonRecursive");
    expectedException = createNonRecursive(fs, path2, 1, createFlag);
    assertTrue("Create a file in a non-exist dir using" + " createNonRecursive() should throw FileNotFoundException ", expectedException != null && expectedException instanceof FileNotFoundException);
    EnumSet<CreateFlag> overwriteFlag = EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE);
    // Overwrite a file in root dir, should succeed
    assertNull(createNonRecursive(fs, path, 1, overwriteFlag));
    // Overwrite a file when parent dir exists as file, should fail
    expectedException = createNonRecursive(fs, new Path(path, "Overwrite"), 1, overwriteFlag);
    assertTrue("Overwrite a file when parent directory exists as a file" + " should throw ParentNotDirectoryException ", expectedException != null && expectedException instanceof ParentNotDirectoryException);
    fs.delete(path, true);
    // Overwrite a file in a non-exist directory, should fail
    final Path path3 = new Path(nonExistDir + "/testOverwriteNonRecursive");
    expectedException = createNonRecursive(fs, path3, 1, overwriteFlag);
    assertTrue("Overwrite a file in a non-exist dir using" + " createNonRecursive() should throw FileNotFoundException ", expectedException != null && expectedException instanceof FileNotFoundException);
}
Also used : Path(org.apache.hadoop.fs.Path) CreateFlag(org.apache.hadoop.fs.CreateFlag) ParentNotDirectoryException(org.apache.hadoop.fs.ParentNotDirectoryException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 12 with ParentNotDirectoryException

use of org.apache.hadoop.fs.ParentNotDirectoryException in project hadoop by apache.

the class TestFSDirectory method testVerifyParentDir.

@Test
public void testVerifyParentDir() throws Exception {
    hdfs.mkdirs(new Path("/dir1/dir2"));
    hdfs.createNewFile(new Path("/dir1/file"));
    hdfs.createNewFile(new Path("/dir1/dir2/file"));
    INodesInPath iip = fsdir.resolvePath(null, "/", DirOp.READ);
    fsdir.verifyParentDir(iip);
    iip = fsdir.resolvePath(null, "/dir1", DirOp.READ);
    fsdir.verifyParentDir(iip);
    iip = fsdir.resolvePath(null, "/dir1/file", DirOp.READ);
    fsdir.verifyParentDir(iip);
    iip = fsdir.resolvePath(null, "/dir-nonexist/file", DirOp.READ);
    try {
        fsdir.verifyParentDir(iip);
        fail("expected FNF");
    } catch (FileNotFoundException fnf) {
    // expected.
    }
    iip = fsdir.resolvePath(null, "/dir1/dir2", DirOp.READ);
    fsdir.verifyParentDir(iip);
    iip = fsdir.resolvePath(null, "/dir1/dir2/file", DirOp.READ);
    fsdir.verifyParentDir(iip);
    iip = fsdir.resolvePath(null, "/dir1/dir-nonexist/file", DirOp.READ);
    try {
        fsdir.verifyParentDir(iip);
        fail("expected FNF");
    } catch (FileNotFoundException fnf) {
    // expected.
    }
    try {
        iip = fsdir.resolvePath(null, "/dir1/file/fail", DirOp.READ);
        fail("expected ACE");
    } catch (AccessControlException ace) {
        assertTrue(ace.getMessage().contains("is not a directory"));
    }
    try {
        iip = fsdir.resolvePath(null, "/dir1/file/fail", DirOp.WRITE);
        fail("expected ACE");
    } catch (AccessControlException ace) {
        assertTrue(ace.getMessage().contains("is not a directory"));
    }
    try {
        iip = fsdir.resolvePath(null, "/dir1/file/fail", DirOp.CREATE);
        fail("expected PNDE");
    } catch (ParentNotDirectoryException pnde) {
        assertTrue(pnde.getMessage().contains("is not a directory"));
    }
}
Also used : Path(org.apache.hadoop.fs.Path) ParentNotDirectoryException(org.apache.hadoop.fs.ParentNotDirectoryException) FileNotFoundException(java.io.FileNotFoundException) AccessControlException(org.apache.hadoop.security.AccessControlException) Test(org.junit.Test)

Aggregations

ParentNotDirectoryException (org.apache.hadoop.fs.ParentNotDirectoryException)12 Path (org.apache.hadoop.fs.Path)8 FileNotFoundException (java.io.FileNotFoundException)6 IOException (java.io.IOException)6 Test (org.junit.Test)5 FileAlreadyExistsException (org.apache.hadoop.fs.FileAlreadyExistsException)4 AccessControlException (org.apache.hadoop.security.AccessControlException)3 FileSystem (org.apache.hadoop.fs.FileSystem)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Configuration (org.apache.hadoop.conf.Configuration)1 CreateFlag (org.apache.hadoop.fs.CreateFlag)1 FileStatus (org.apache.hadoop.fs.FileStatus)1 InvalidPathException (org.apache.hadoop.fs.InvalidPathException)1 FsPermission (org.apache.hadoop.fs.permission.FsPermission)1 SnapshotAccessControlException (org.apache.hadoop.hdfs.protocol.SnapshotAccessControlException)1 BlockStoragePolicySuite (org.apache.hadoop.hdfs.server.blockmanagement.BlockStoragePolicySuite)1 ChunkedArrayList (org.apache.hadoop.util.ChunkedArrayList)1