Skip to content

Instantly share code, notes, and snippets.

@jayunit100
Created April 7, 2014 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayunit100/10014317 to your computer and use it in GitHub Desktop.
Save jayunit100/10014317 to your computer and use it in GitHub Desktop.
enclosed HCFS Test
package org.apache.hadoop.fs.rawlocal;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import junit.framework.TestCase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FCStatisticsBaseTest;
import org.apache.hadoop.fs.FileAlreadyExistsException;
import org.apache.hadoop.fs.FileContext;
import org.apache.hadoop.fs.FileContextCreateMkdirBaseTest;
import org.apache.hadoop.fs.FileContextMainOperationsBaseTest;
import org.apache.hadoop.fs.FileContextTestHelper;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileSystem.Statistics;
import org.apache.hadoop.fs.FileSystemTestWrapper;
import org.apache.hadoop.fs.Options.Rename;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.UnsupportedFileSystemException;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.util.Shell;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
/**
* This class is designed to simpligy unit testing of any HCFS
* by providing a single point of entry to all unit tests.
*/
@RunWith(Enclosed.class)
public class TestRawLocalHCFS extends TestCase {
public static class TestLocalFsFCStatistics extends FCStatisticsBaseTest {
static final String LOCAL_FS_ROOT_URI = "file:///tmp/test";
@Before
public void setUp() throws Exception {
fc = FileContext.getLocalFSFileContext();
fc.mkdir(fileContextTestHelper.getTestRootPath(fc, "test"), FileContext.DEFAULT_PERM, true);
}
@After
public void tearDown() throws Exception {
fc.delete(fileContextTestHelper.getTestRootPath(fc, "test"), true);
}
@Override
protected void verifyReadBytes(Statistics stats) {
// one blockSize for read, one for pread
Assert.assertEquals(2*blockSize, stats.getBytesRead());
}
@Override
protected void verifyWrittenBytes(Statistics stats) {
//Extra 12 bytes are written apart from the block.
Assert.assertEquals(blockSize + 12, stats.getBytesWritten());
}
@Override
protected URI getFsUri() {
return URI.create(LOCAL_FS_ROOT_URI);
}
}
public static class TestLocalFSFileContextCreateMkdir extends FileContextCreateMkdirBaseTest {
@Override
@Before
public void setUp() throws Exception {
fc = FileContext.getLocalFSFileContext();
super.setUp();
}
}
public static class TestLocalFSFileContextMainOperations extends FileContextMainOperationsBaseTest {
@Override
@Before
public void setUp() throws Exception {
fc = FileContext.getLocalFSFileContext();
super.setUp();
}
static Path wd = null;
@Override
protected Path getDefaultWorkingDirectory() throws IOException {
if (wd == null)
wd = FileSystem.getLocal(new Configuration()).getWorkingDirectory();
return wd;
}
@Test
public void testFileContextNoCache() throws UnsupportedFileSystemException {
FileContext fc1 = FileContext.getLocalFSFileContext();
Assert.assertTrue(fc1 != fc);
}
@Override
protected boolean listCorruptedBlocksSupported() {
return false;
}
@Test
public void testDefaultFilePermission() throws IOException {
Path file = fileContextTestHelper.getTestRootPath(fc,
"testDefaultFilePermission");
FileContextTestHelper.createFile(fc, file);
FsPermission expect = FileContext.FILE_DEFAULT_PERM.applyUMask(fc.getUMask());
Assert.assertEquals(expect, fc.getFileStatus(file)
.getPermission());
}
}
public static class TestSymlinkLocalFSFileSystem extends TestSymlinkLocalFS {
@BeforeClass
public static void testSetup() throws Exception {
FileSystem filesystem = FileSystem.getLocal(new Configuration());
wrapper = new FileSystemTestWrapper(filesystem);
}
@Ignore("RawLocalFileSystem#mkdir does not treat existence of directory" +
" as an error")
@Override
@Test(timeout=1000)
public void testMkdirExistingLink() throws IOException {}
@Ignore("FileSystem#create defaults to creating parents," +
" throwing an IOException instead of FileNotFoundException")
@Override
@Test(timeout=1000)
public void testCreateFileViaDanglingLinkParent() throws IOException {}
@Ignore("RawLocalFileSystem does not throw an exception if the path" +
" already exists")
@Override
@Test(timeout=1000)
public void testCreateFileDirExistingLink() throws IOException {}
@Ignore("ChecksumFileSystem does not support append")
@Override
@Test(timeout=1000)
public void testAccessFileViaInterSymlinkAbsTarget() throws IOException {}
@Override
public void testRenameFileWithDestParentSymlink() throws IOException {
assumeTrue(!Shell.WINDOWS);
super.testRenameFileWithDestParentSymlink();
}
@Override
@Test(timeout=10000)
/** Rename a symlink to itself */
public void testRenameSymlinkToItself() throws IOException {
Path file = new Path(testBaseDir1(), "file");
createAndWriteFile(file);
Path link = new Path(testBaseDir1(), "linkToFile1");
wrapper.createSymlink(file, link, false);
try {
wrapper.rename(link, link);
fail("Failed to get expected IOException");
} catch (IOException e) {
assertTrue(unwrapException(e) instanceof FileAlreadyExistsException);
}
// Fails with overwrite as well
try {
wrapper.rename(link, link, Rename.OVERWRITE);
fail("Failed to get expected IOException");
} catch (IOException e) {
// Todo: Fix this test when HADOOP-9819 is fixed.
assertTrue(unwrapException(e) instanceof FileAlreadyExistsException ||
unwrapException(e) instanceof FileNotFoundException);
}
}
}
public static class TestSymlinkLocalFSFileContext extends TestSymlinkLocalFS {
@Override
public void testRenameFileWithDestParentSymlink() throws IOException {
assumeTrue(!Shell.WINDOWS);
super.testRenameFileWithDestParentSymlink();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment