|
@@ -22,6 +22,7 @@ import java.io.File;
|
|
|
import java.io.FileNotFoundException;
|
|
|
import java.io.IOException;
|
|
|
import java.util.EnumSet;
|
|
|
+import java.util.NoSuchElementException;
|
|
|
|
|
|
import org.apache.commons.lang.RandomStringUtils;
|
|
|
import org.apache.hadoop.HadoopIllegalArgumentException;
|
|
@@ -30,6 +31,7 @@ import org.apache.hadoop.fs.Options.Rename;
|
|
|
import org.apache.hadoop.fs.permission.FsPermission;
|
|
|
import org.junit.After;
|
|
|
import org.junit.Assert;
|
|
|
+import static org.junit.Assert.*;
|
|
|
import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
|
|
@@ -92,7 +94,7 @@ public abstract class FileContextMainOperationsBaseTest {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- private static byte[] data = getFileData(numBlocks,
|
|
|
+ private static final byte[] data = getFileData(numBlocks,
|
|
|
getDefaultBlockSize());
|
|
|
|
|
|
@Before
|
|
@@ -107,7 +109,8 @@ public abstract class FileContextMainOperationsBaseTest {
|
|
|
|
|
|
@After
|
|
|
public void tearDown() throws Exception {
|
|
|
- fc.delete(new Path(fileContextTestHelper.getAbsoluteTestRootPath(fc), new Path("test")), true);
|
|
|
+ boolean del = fc.delete(new Path(fileContextTestHelper.getAbsoluteTestRootPath(fc), new Path("test")), true);
|
|
|
+ assertTrue(del);
|
|
|
fc.delete(localFsRootPath, true);
|
|
|
}
|
|
|
|
|
@@ -194,6 +197,14 @@ public abstract class FileContextMainOperationsBaseTest {
|
|
|
fc.setWorkingDirectory(absoluteDir);
|
|
|
Assert.assertEquals(absoluteDir, fc.getWorkingDirectory());
|
|
|
|
|
|
+ Path aRegularFile = new Path("aRegularFile");
|
|
|
+ createFile(aRegularFile);
|
|
|
+ try {
|
|
|
+ fc.setWorkingDirectory(aRegularFile);
|
|
|
+ fail("An IOException expected.");
|
|
|
+ } catch (IOException ioe) {
|
|
|
+ // okay
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -1195,6 +1206,136 @@ public abstract class FileContextMainOperationsBaseTest {
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testOpen2() throws IOException {
|
|
|
+ final Path rootPath = getTestRootPath(fc, "test");
|
|
|
+ //final Path rootPath = getAbsoluteTestRootPath(fc);
|
|
|
+ final Path path = new Path(rootPath, "zoo");
|
|
|
+ createFile(path);
|
|
|
+ final long length = fc.getFileStatus(path).getLen();
|
|
|
+ FSDataInputStream fsdis = fc.open(path, 2048);
|
|
|
+ try {
|
|
|
+ byte[] bb = new byte[(int)length];
|
|
|
+ fsdis.readFully(bb);
|
|
|
+ assertArrayEquals(data, bb);
|
|
|
+ } finally {
|
|
|
+ fsdis.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testSetVerifyChecksum() throws IOException {
|
|
|
+ final Path rootPath = getTestRootPath(fc, "test");
|
|
|
+ final Path path = new Path(rootPath, "zoo");
|
|
|
+
|
|
|
+ FSDataOutputStream out = fc.create(path, EnumSet.of(CREATE),
|
|
|
+ Options.CreateOpts.createParent());
|
|
|
+ try {
|
|
|
+ // instruct FS to verify checksum through the FileContext:
|
|
|
+ fc.setVerifyChecksum(true, path);
|
|
|
+ out.write(data, 0, data.length);
|
|
|
+ } finally {
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ // NB: underlying FS may be different (this is an abstract test),
|
|
|
+ // so we cannot assert .zoo.crc existence.
|
|
|
+ // Instead, we check that the file is read correctly:
|
|
|
+ FileStatus fileStatus = fc.getFileStatus(path);
|
|
|
+ final long len = fileStatus.getLen();
|
|
|
+ assertTrue(len == data.length);
|
|
|
+ byte[] bb = new byte[(int)len];
|
|
|
+ FSDataInputStream fsdis = fc.open(path);
|
|
|
+ try {
|
|
|
+ fsdis.read(bb);
|
|
|
+ } finally {
|
|
|
+ fsdis.close();
|
|
|
+ }
|
|
|
+ assertArrayEquals(data, bb);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testListCorruptFileBlocks() throws IOException {
|
|
|
+ final Path rootPath = getTestRootPath(fc, "test");
|
|
|
+ final Path path = new Path(rootPath, "zoo");
|
|
|
+ createFile(path);
|
|
|
+ try {
|
|
|
+ final RemoteIterator<Path> remoteIterator = fc
|
|
|
+ .listCorruptFileBlocks(path);
|
|
|
+ if (listCorruptedBlocksSupported()) {
|
|
|
+ assertTrue(remoteIterator != null);
|
|
|
+ Path p;
|
|
|
+ while (remoteIterator.hasNext()) {
|
|
|
+ p = remoteIterator.next();
|
|
|
+ System.out.println("corrupted block: " + p);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ remoteIterator.next();
|
|
|
+ fail();
|
|
|
+ } catch (NoSuchElementException nsee) {
|
|
|
+ // okay
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ fail();
|
|
|
+ }
|
|
|
+ } catch (UnsupportedOperationException uoe) {
|
|
|
+ if (listCorruptedBlocksSupported()) {
|
|
|
+ fail(uoe.toString());
|
|
|
+ } else {
|
|
|
+ // okay
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected abstract boolean listCorruptedBlocksSupported();
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testDeleteOnExitUnexisting() throws IOException {
|
|
|
+ final Path rootPath = getTestRootPath(fc, "test");
|
|
|
+ final Path path = new Path(rootPath, "zoo");
|
|
|
+ boolean registered = fc.deleteOnExit(path);
|
|
|
+ // because "zoo" does not exist:
|
|
|
+ assertTrue(!registered);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testFileContextStatistics() throws IOException {
|
|
|
+ FileContext.clearStatistics();
|
|
|
+
|
|
|
+ final Path rootPath = getTestRootPath(fc, "test");
|
|
|
+ final Path path = new Path(rootPath, "zoo");
|
|
|
+ createFile(path);
|
|
|
+ byte[] bb = new byte[data.length];
|
|
|
+ FSDataInputStream fsdis = fc.open(path);
|
|
|
+ try {
|
|
|
+ fsdis.read(bb);
|
|
|
+ } finally {
|
|
|
+ fsdis.close();
|
|
|
+ }
|
|
|
+ assertArrayEquals(data, bb);
|
|
|
+
|
|
|
+ FileContext.printStatistics();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ /*
|
|
|
+ * Test method
|
|
|
+ * org.apache.hadoop.fs.FileContext.getFileContext(AbstractFileSystem)
|
|
|
+ */
|
|
|
+ public void testGetFileContext1() throws IOException {
|
|
|
+ final Path rootPath = getTestRootPath(fc, "test");
|
|
|
+ AbstractFileSystem asf = fc.getDefaultFileSystem();
|
|
|
+ // create FileContext using the protected #getFileContext(1) method:
|
|
|
+ FileContext fc2 = FileContext.getFileContext(asf);
|
|
|
+ // Now just check that this context can do something reasonable:
|
|
|
+ final Path path = new Path(rootPath, "zoo");
|
|
|
+ FSDataOutputStream out = fc2.create(path, EnumSet.of(CREATE),
|
|
|
+ Options.CreateOpts.createParent());
|
|
|
+ out.close();
|
|
|
+ Path pathResolved = fc2.resolvePath(path);
|
|
|
+ assertEquals(pathResolved.toUri().getPath(), path.toUri().getPath());
|
|
|
}
|
|
|
|
|
|
private Path getTestRootPath(FileContext fc, String pathString) {
|