HADOOP-6394. Add a helper class to simplify FileContext related tests and improve code reusability. Contributed by Jitendra Nath Pandey.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@891511 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Suresh Srinivas 2009-12-17 01:30:33 +00:00
parent 6a1e8bf452
commit ea5db0c5ab
7 changed files with 418 additions and 372 deletions

View File

@ -63,6 +63,9 @@ Trunk (unreleased changes)
HADOOP-6222. Core doesn't have TestCommonCLI facility. (cos)
HADOOP-6394. Add a helper class to simplify FileContext related tests and
improve code reusability. (Jitendra Nath Pandey via suresh)
HADOOP-6426. Create ant build for running EC2 unit tests. (tomwhite)
OPTIMIZATIONS

View File

@ -19,14 +19,15 @@ package org.apache.hadoop.fs;
import java.io.IOException;
import java.util.EnumSet;
import org.apache.hadoop.fs.Options.CreateOpts;
import org.apache.hadoop.util.StringUtils;
import org.apache.log4j.Level;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.apache.hadoop.fs.FileContextTestHelper.*;
import org.apache.commons.logging.impl.Log4JLogger;
/**
* <p>
@ -48,31 +49,13 @@ import org.junit.Test;
* </p>
*/
public class FileContextCreateMkdirBaseTest {
public abstract class FileContextCreateMkdirBaseTest {
protected static FileContext fc;
static final String TEST_ROOT_DIR = new Path(System.getProperty(
"test.build.data", "build/test/data")).toString().replace(' ', '_')
+ "/test";
protected Path getTestRootRelativePath(String pathString) {
return fc.makeQualified(new Path(TEST_ROOT_DIR, pathString));
}
private Path rootPath = null;
protected Path getTestRoot() {
if (rootPath == null) {
rootPath = fc.makeQualified(new Path(TEST_ROOT_DIR));
}
return rootPath;
}
{
try {
((org.apache.commons.logging.impl.Log4JLogger)FileSystem.LOG).getLogger()
.setLevel(org.apache.log4j.Level.DEBUG);
((Log4JLogger)FileSystem.LOG).getLogger().setLevel(Level.DEBUG);
}
catch(Exception e) {
System.out.println("Cannot change log level\n"
@ -81,15 +64,14 @@ public class FileContextCreateMkdirBaseTest {
}
@Before
public void setUp() throws Exception {
fc.mkdir(getTestRoot(), FileContext.DEFAULT_PERM, true);
fc.mkdir(getTestRootPath(fc), FileContext.DEFAULT_PERM, true);
}
@After
public void tearDown() throws Exception {
fc.delete(getTestRoot(), true);
fc.delete(getTestRootPath(fc), true);
}
@ -100,7 +82,7 @@ public class FileContextCreateMkdirBaseTest {
@Test
public void testMkdirNonRecursiveWithExistingDir() throws IOException {
Path f = getTestRootRelativePath("aDir");
Path f = getTestRootPath(fc, "aDir");
fc.mkdir(f, FileContext.DEFAULT_PERM, false);
Assert.assertTrue(fc.isDirectory(f));
}
@ -108,7 +90,7 @@ public class FileContextCreateMkdirBaseTest {
@Test
public void testMkdirNonRecursiveWithNonExistingDir() {
try {
fc.mkdir(getTestRootRelativePath("NonExistant/aDir"),
fc.mkdir(getTestRootPath(fc,"NonExistant/aDir"),
FileContext.DEFAULT_PERM, false);
Assert.fail("Mkdir with non existing parent dir should have failed");
} catch (IOException e) {
@ -119,7 +101,7 @@ public class FileContextCreateMkdirBaseTest {
@Test
public void testMkdirRecursiveWithExistingDir() throws IOException {
Path f = getTestRootRelativePath("aDir");
Path f = getTestRootPath(fc, "aDir");
fc.mkdir(f, FileContext.DEFAULT_PERM, true);
Assert.assertTrue(fc.isDirectory(f));
}
@ -127,7 +109,7 @@ public class FileContextCreateMkdirBaseTest {
@Test
public void testMkdirRecursiveWithNonExistingDir() throws IOException {
Path f = getTestRootRelativePath("NonExistant2/aDir");
Path f = getTestRootPath(fc, "NonExistant2/aDir");
fc.mkdir(f, FileContext.DEFAULT_PERM, true);
Assert.assertTrue(fc.isDirectory(f));
}
@ -137,15 +119,15 @@ public class FileContextCreateMkdirBaseTest {
////////////////////////
@Test
public void testCreateNonRecursiveWithExistingDir() throws IOException {
Path f = getTestRootRelativePath("foo");
createFile(f);
Path f = getTestRootPath(fc, "foo");
createFile(fc, f);
Assert.assertTrue(fc.isFile(f));
}
@Test
public void testCreateNonRecursiveWithNonExistingDir() {
try {
createFile(getTestRootRelativePath("NonExisting/foo"));
createFileNonRecursive(fc, getTestRootPath(fc, "NonExisting/foo"));
Assert.fail("Create with non existing parent dir should have failed");
} catch (IOException e) {
// As expected
@ -155,36 +137,16 @@ public class FileContextCreateMkdirBaseTest {
@Test
public void testCreateRecursiveWithExistingDir() throws IOException {
Path f = getTestRootRelativePath("foo");
createFile(f, CreateOpts.createParent());
Path f = getTestRootPath(fc,"foo");
createFile(fc, f);
Assert.assertTrue(fc.isFile(f));
}
@Test
public void testCreateRecursiveWithNonExistingDir() throws IOException {
Path f = getTestRootRelativePath("NonExisting/foo");
createFile(f, CreateOpts.createParent());
Path f = getTestRootPath(fc,"NonExisting/foo");
createFile(fc, f);
Assert.assertTrue(fc.isFile(f));
}
protected static int getBlockSize() {
return 1024;
}
private static byte[] data = new byte[getBlockSize() * 2]; // two blocks of data
{
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (i % 10);
}
}
protected void createFile(Path path,
CreateOpts.CreateParent ... opt) throws IOException {
FSDataOutputStream out = fc.create(path,EnumSet.of(CreateFlag.CREATE), opt);
out.write(data, 0, data.length);
out.close();
}
}

View File

@ -30,6 +30,8 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.apache.hadoop.fs.FileContextTestHelper.*;
/**
* <p>
* A collection of tests for the {@link FileContext}.
@ -55,38 +57,10 @@ public abstract class FileContextMainOperationsBaseTest {
private static String TEST_DIR_AAA = "test/hadoop/aaa";
private static String TEST_DIR_AXA = "test/hadoop/axa";
private static String TEST_DIR_AXX = "test/hadoop/axx";
private static int numBlocks = 2;
static final String LOCAL_FS_ROOT_URI = "file:///tmp/test";
static final String TEST_ROOT_DIR =
System.getProperty("test.build.data", "build/test/data").replace(' ', '_');
/**
* we need to store the absRootDir because some tests may do a setWd and
* the TEST_ROOT_DIR may itself be relative.
*/
String absTestRootDir = null;
protected String getAbsoluteTestRootDir() throws IOException {
if (absTestRootDir == null) {
if (TEST_ROOT_DIR.startsWith("/")) {
absTestRootDir = TEST_ROOT_DIR;
} else {
absTestRootDir = getDefaultWorkingDirectory().toString() + "/" +
TEST_ROOT_DIR;
}
}
return absTestRootDir;
}
protected Path getTestRootDir() throws IOException {
return fc.makeQualified(new Path(getAbsoluteTestRootDir()));
}
protected Path getTestRootPath(String pathString) throws IOException {
return fc.makeQualified(new Path(getAbsoluteTestRootDir(), pathString));
}
protected static FileContext fc;
@ -106,31 +80,25 @@ public abstract class FileContextMainOperationsBaseTest {
}
};
private static byte[] data = new byte[getBlockSize() * 2]; // two blocks of data
{
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (i % 10);
}
}
private static byte[] data = getFileData(numBlocks,
getDefaultBlockSize());
@Before
public void setUp() throws Exception {
fc.mkdir(getTestRootPath("test"), FileContext.DEFAULT_PERM, true);
fc.mkdir(getTestRootPath(fc, "test"), FileContext.DEFAULT_PERM, true);
}
@After
public void tearDown() throws Exception {
fc.delete(getTestRootPath("test"), true);
fc.delete(getTestRootPath(fc, "test"), true);
fc.delete(new Path(LOCAL_FS_ROOT_URI), true);
}
protected static int getBlockSize() {
return 1024;
}
protected Path getDefaultWorkingDirectory() throws IOException {
return getTestRootPath("/user/" + System.getProperty("user.name")).makeQualified(
fc.getDefaultFileSystem().getUri(), fc.getWorkingDirectory());
return getTestRootPath(fc,
"/user/" + System.getProperty("user.name")).makeQualified(
fc.getDefaultFileSystem().getUri(), fc.getWorkingDirectory());
}
protected boolean renameSupported() {
@ -156,7 +124,7 @@ public abstract class FileContextMainOperationsBaseTest {
public void testWorkingDirectory() throws Exception {
// First we cd to our test root
Path workDir = new Path(getTestRootDir(), new Path("test"));
Path workDir = new Path(getTestRootDir(fc), new Path("test"));
fc.setWorkingDirectory(workDir);
Assert.assertEquals(workDir, fc.getWorkingDirectory());
@ -169,7 +137,7 @@ public abstract class FileContextMainOperationsBaseTest {
// cd using a relative path
// Go back to our test root
workDir = new Path(getTestRootDir(), new Path("test"));
workDir = getTestRootPath(fc, "test");
fc.setWorkingDirectory(workDir);
Assert.assertEquals(workDir, fc.getWorkingDirectory());
@ -177,10 +145,9 @@ public abstract class FileContextMainOperationsBaseTest {
Path absoluteDir = new Path(workDir,"existingDir1");
fc.mkdir(absoluteDir, FileContext.DEFAULT_PERM, true);
fc.setWorkingDirectory(relativeDir);
Assert.assertEquals(absoluteDir,
fc.getWorkingDirectory());
Assert.assertEquals(absoluteDir, fc.getWorkingDirectory());
// cd using a absolute path
absoluteDir = getTestRootPath("test/existingDir2");
absoluteDir = getTestRootPath(fc, "test/existingDir2");
fc.mkdir(absoluteDir, FileContext.DEFAULT_PERM, true);
fc.setWorkingDirectory(absoluteDir);
Assert.assertEquals(absoluteDir, fc.getWorkingDirectory());
@ -190,7 +157,7 @@ public abstract class FileContextMainOperationsBaseTest {
fc.create(absolutePath, EnumSet.of(CreateFlag.CREATE)).close();
fc.open(new Path("foo")).close();
absoluteDir = getTestRootPath("nonexistingPath");
absoluteDir = getTestRootPath(fc, "nonexistingPath");
try {
fc.setWorkingDirectory(absoluteDir);
Assert.fail("cd to non existing dir should have failed");
@ -209,7 +176,7 @@ public abstract class FileContextMainOperationsBaseTest {
@Test
public void testMkdirs() throws Exception {
Path testDir = getTestRootPath("test/hadoop");
Path testDir = getTestRootPath(fc, "test/hadoop");
Assert.assertFalse(fc.exists(testDir));
Assert.assertFalse(fc.isFile(testDir));
@ -235,14 +202,14 @@ public abstract class FileContextMainOperationsBaseTest {
@Test
public void testMkdirsFailsForSubdirectoryOfExistingFile() throws Exception {
Path testDir = getTestRootPath("test/hadoop");
Path testDir = getTestRootPath(fc, "test/hadoop");
Assert.assertFalse(fc.exists(testDir));
fc.mkdir(testDir, FsPermission.getDefault(), true);
Assert.assertTrue(fc.exists(testDir));
createFile(getTestRootPath("test/hadoop/file"));
createFile(getTestRootPath(fc, "test/hadoop/file"));
Path testSubDir = getTestRootPath("test/hadoop/file/subdir");
Path testSubDir = getTestRootPath(fc, "test/hadoop/file/subdir");
try {
fc.mkdir(testSubDir, FsPermission.getDefault(), true);
Assert.fail("Should throw IOException.");
@ -251,7 +218,7 @@ public abstract class FileContextMainOperationsBaseTest {
}
Assert.assertFalse(fc.exists(testSubDir));
Path testDeepSubDir = getTestRootPath("test/hadoop/file/deep/sub/dir");
Path testDeepSubDir = getTestRootPath(fc, "test/hadoop/file/deep/sub/dir");
try {
fc.mkdir(testDeepSubDir, FsPermission.getDefault(), true);
Assert.fail("Should throw IOException.");
@ -266,7 +233,7 @@ public abstract class FileContextMainOperationsBaseTest {
public void testGetFileStatusThrowsExceptionForNonExistentFile()
throws Exception {
try {
fc.getFileStatus(getTestRootPath("test/hadoop/file"));
fc.getFileStatus(getTestRootPath(fc, "test/hadoop/file"));
Assert.fail("Should throw FileNotFoundException");
} catch (FileNotFoundException e) {
// expected
@ -276,7 +243,7 @@ public abstract class FileContextMainOperationsBaseTest {
public void testListStatusThrowsExceptionForNonExistentFile()
throws Exception {
try {
fc.listStatus(getTestRootPath("test/hadoop/file"));
fc.listStatus(getTestRootPath(fc, "test/hadoop/file"));
Assert.fail("Should throw FileNotFoundException");
} catch (FileNotFoundException fnfe) {
// expected
@ -285,36 +252,41 @@ public abstract class FileContextMainOperationsBaseTest {
@Test
public void testListStatus() throws Exception {
Path[] testDirs = { getTestRootPath("test/hadoop/a"),
getTestRootPath("test/hadoop/b"),
getTestRootPath("test/hadoop/c/1"), };
Path[] testDirs = {
getTestRootPath(fc, "test/hadoop/a"),
getTestRootPath(fc, "test/hadoop/b"),
getTestRootPath(fc, "test/hadoop/c/1"), };
Assert.assertFalse(fc.exists(testDirs[0]));
for (Path path : testDirs) {
fc.mkdir(path, FsPermission.getDefault(), true);
}
FileStatus[] paths = fc.listStatus(getTestRootPath("test"));
FileStatus[] paths = fc.listStatus(getTestRootPath(fc, "test"));
Assert.assertEquals(1, paths.length);
Assert.assertEquals(getTestRootPath("test/hadoop"), paths[0].getPath());
Assert.assertEquals(getTestRootPath(fc, "test/hadoop"), paths[0].getPath());
paths = fc.listStatus(getTestRootPath("test/hadoop"));
paths = fc.listStatus(getTestRootPath(fc, "test/hadoop"));
Assert.assertEquals(3, paths.length);
Assert.assertTrue(containsPath(getTestRootPath("test/hadoop/a"), paths));
Assert.assertTrue(containsPath(getTestRootPath("test/hadoop/b"), paths));
Assert.assertTrue(containsPath(getTestRootPath("test/hadoop/c"), paths));
paths = fc.listStatus(getTestRootPath("test/hadoop/a"));
Assert.assertTrue(containsPath(getTestRootPath(fc, "test/hadoop/a"),
paths));
Assert.assertTrue(containsPath(getTestRootPath(fc, "test/hadoop/b"),
paths));
Assert.assertTrue(containsPath(getTestRootPath(fc, "test/hadoop/c"),
paths));
paths = fc.listStatus(getTestRootPath(fc, "test/hadoop/a"));
Assert.assertEquals(0, paths.length);
}
@Test
public void testListStatusFilterWithNoMatches() throws Exception {
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA2),
getTestRootPath(TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXA),
getTestRootPath(TEST_DIR_AXX), };
Path[] testDirs = {
getTestRootPath(fc, TEST_DIR_AAA2),
getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) {
@ -322,18 +294,19 @@ public abstract class FileContextMainOperationsBaseTest {
}
}
//listStatus with filters returns empty correctly
FileStatus[] filteredPaths = fc.util().listStatus(getTestRootPath("test"),
TEST_X_FILTER);
// listStatus with filters returns empty correctly
FileStatus[] filteredPaths = fc.util().listStatus(
getTestRootPath(fc, "test"), TEST_X_FILTER);
Assert.assertEquals(0,filteredPaths.length);
}
public void testListStatusFilterWithSomeMatches() throws Exception {
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXA),
getTestRootPath(TEST_DIR_AXX),
getTestRootPath(TEST_DIR_AAA2), };
Path[] testDirs = {
getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AAA2), };
if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) {
@ -341,19 +314,23 @@ public abstract class FileContextMainOperationsBaseTest {
}
}
//should return 2 paths ("/test/hadoop/axa" and "/test/hadoop/axx")
FileStatus[] filteredPaths = fc.util().listStatus(getTestRootPath("test/hadoop"),
TEST_X_FILTER);
// should return 2 paths ("/test/hadoop/axa" and "/test/hadoop/axx")
FileStatus[] filteredPaths = fc.util()
.listStatus(getTestRootPath(fc, "test/hadoop"),
TEST_X_FILTER);
Assert.assertEquals(2,filteredPaths.length);
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXA), filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXX), filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(fc,
TEST_DIR_AXA), filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(fc,
TEST_DIR_AXX), filteredPaths));
}
@Test
public void testGlobStatusThrowsExceptionForNonExistentFile() throws Exception {
try {
//This should throw a FileNotFoundException
fc.util().globStatus(getTestRootPath("test/hadoopfsdf/?"));
// This should throw a FileNotFoundException
fc.util().globStatus(
getTestRootPath(fc, "test/hadoopfsdf/?"));
Assert.fail("Should throw FileNotFoundException");
} catch (FileNotFoundException fnfe) {
// expected
@ -362,10 +339,11 @@ public abstract class FileContextMainOperationsBaseTest {
@Test
public void testGlobStatusWithNoMatchesInPath() throws Exception {
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXA),
getTestRootPath(TEST_DIR_AXX),
getTestRootPath(TEST_DIR_AAA2), };
Path[] testDirs = {
getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AAA2), };
if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) {
@ -373,17 +351,19 @@ public abstract class FileContextMainOperationsBaseTest {
}
}
//should return nothing
FileStatus[] paths = fc.util().globStatus(getTestRootPath("test/hadoop/?"));
// should return nothing
FileStatus[] paths = fc.util().globStatus(
getTestRootPath(fc, "test/hadoop/?"));
Assert.assertEquals(0, paths.length);
}
@Test
public void testGlobStatusSomeMatchesInDirectories() throws Exception {
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXA),
getTestRootPath(TEST_DIR_AXX),
getTestRootPath(TEST_DIR_AAA2), };
Path[] testDirs = {
getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AAA2), };
if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) {
@ -391,19 +371,23 @@ public abstract class FileContextMainOperationsBaseTest {
}
}
//Should return two items ("/test/hadoop" and "/test/hadoop2")
FileStatus[] paths = fc.util().globStatus(getTestRootPath("test/hadoop*"));
// Should return two items ("/test/hadoop" and "/test/hadoop2")
FileStatus[] paths = fc.util().globStatus(
getTestRootPath(fc, "test/hadoop*"));
Assert.assertEquals(2, paths.length);
Assert.assertTrue(containsPath(getTestRootPath("test/hadoop"), paths));
Assert.assertTrue(containsPath(getTestRootPath("test/hadoop2"), paths));
Assert.assertTrue(containsPath(getTestRootPath(fc,
"test/hadoop"), paths));
Assert.assertTrue(containsPath(getTestRootPath(fc,
"test/hadoop2"), paths));
}
@Test
public void testGlobStatusWithMultipleWildCardMatches() throws Exception {
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXA),
getTestRootPath(TEST_DIR_AXX),
getTestRootPath(TEST_DIR_AAA2), };
Path[] testDirs = {
getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AAA2), };
if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) {
@ -413,20 +397,22 @@ public abstract class FileContextMainOperationsBaseTest {
//Should return all 4 items ("/test/hadoop/aaa", "/test/hadoop/axa"
//"/test/hadoop/axx", and "/test/hadoop2/axx")
FileStatus[] paths = fc.util().globStatus(getTestRootPath("test/hadoop*/*"));
FileStatus[] paths = fc.util().globStatus(
getTestRootPath(fc, "test/hadoop*/*"));
Assert.assertEquals(4, paths.length);
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AAA), paths));
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXA), paths));
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXX), paths));
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AAA2), paths));
Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AAA), paths));
Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXA), paths));
Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXX), paths));
Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AAA2), paths));
}
@Test
public void testGlobStatusWithMultipleMatchesOfSingleChar() throws Exception {
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXA),
getTestRootPath(TEST_DIR_AXX),
getTestRootPath(TEST_DIR_AAA2), };
Path[] testDirs = {
getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AAA2), };
if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) {
@ -435,18 +421,22 @@ public abstract class FileContextMainOperationsBaseTest {
}
//Should return only 2 items ("/test/hadoop/axa", "/test/hadoop/axx")
FileStatus[] paths = fc.util().globStatus(getTestRootPath("test/hadoop/ax?"));
FileStatus[] paths = fc.util().globStatus(
getTestRootPath(fc, "test/hadoop/ax?"));
Assert.assertEquals(2, paths.length);
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXA), paths));
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXX), paths));
Assert.assertTrue(containsPath(getTestRootPath(fc,
TEST_DIR_AXA), paths));
Assert.assertTrue(containsPath(getTestRootPath(fc,
TEST_DIR_AXX), paths));
}
@Test
public void testGlobStatusFilterWithEmptyPathResults() throws Exception {
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXA),
getTestRootPath(TEST_DIR_AXX),
getTestRootPath(TEST_DIR_AXX), };
Path[] testDirs = {
getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) {
@ -455,17 +445,20 @@ public abstract class FileContextMainOperationsBaseTest {
}
//This should return an empty set
FileStatus[] filteredPaths = fc.util().globStatus(getTestRootPath("test/hadoop/?"),
DEFAULT_FILTER);
FileStatus[] filteredPaths = fc.util().globStatus(
getTestRootPath(fc, "test/hadoop/?"),
DEFAULT_FILTER);
Assert.assertEquals(0,filteredPaths.length);
}
@Test
public void testGlobStatusFilterWithSomePathMatchesAndTrivialFilter() throws Exception {
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXA),
getTestRootPath(TEST_DIR_AXX),
getTestRootPath(TEST_DIR_AXX), };
public void testGlobStatusFilterWithSomePathMatchesAndTrivialFilter()
throws Exception {
Path[] testDirs = {
getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) {
@ -474,20 +467,26 @@ public abstract class FileContextMainOperationsBaseTest {
}
//This should return all three (aaa, axa, axx)
FileStatus[] filteredPaths = fc.util().globStatus(getTestRootPath("test/hadoop/*"),
DEFAULT_FILTER);
Assert.assertEquals(3,filteredPaths.length);
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AAA), filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXA), filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXX), filteredPaths));
FileStatus[] filteredPaths = fc.util().globStatus(
getTestRootPath(fc, "test/hadoop/*"),
DEFAULT_FILTER);
Assert.assertEquals(3, filteredPaths.length);
Assert.assertTrue(containsPath(getTestRootPath(fc,
TEST_DIR_AAA), filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(fc,
TEST_DIR_AXA), filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(fc,
TEST_DIR_AXX), filteredPaths));
}
@Test
public void testGlobStatusFilterWithMultipleWildCardMatchesAndTrivialFilter() throws Exception {
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXA),
getTestRootPath(TEST_DIR_AXX),
getTestRootPath(TEST_DIR_AXX), };
public void testGlobStatusFilterWithMultipleWildCardMatchesAndTrivialFilter()
throws Exception {
Path[] testDirs = {
getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) {
@ -496,20 +495,26 @@ public abstract class FileContextMainOperationsBaseTest {
}
//This should return all three (aaa, axa, axx)
FileStatus[] filteredPaths = fc.util().globStatus(getTestRootPath("test/hadoop/a??"),
DEFAULT_FILTER);
Assert.assertEquals(3,filteredPaths.length);
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AAA), filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXA), filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXX), filteredPaths));
FileStatus[] filteredPaths = fc.util().globStatus(
getTestRootPath(fc, "test/hadoop/a??"),
DEFAULT_FILTER);
Assert.assertEquals(3, filteredPaths.length);
Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AAA),
filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXA),
filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXX),
filteredPaths));
}
@Test
public void testGlobStatusFilterWithMultiplePathMatchesAndNonTrivialFilter() throws Exception {
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXA),
getTestRootPath(TEST_DIR_AXX),
getTestRootPath(TEST_DIR_AXX), };
public void testGlobStatusFilterWithMultiplePathMatchesAndNonTrivialFilter()
throws Exception {
Path[] testDirs = {
getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) {
@ -518,19 +523,24 @@ public abstract class FileContextMainOperationsBaseTest {
}
//This should return two (axa, axx)
FileStatus[] filteredPaths = fc.util().globStatus(getTestRootPath("test/hadoop/*"),
TEST_X_FILTER);
Assert.assertEquals(2,filteredPaths.length);
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXA), filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXX), filteredPaths));
FileStatus[] filteredPaths = fc.util().globStatus(
getTestRootPath(fc, "test/hadoop/*"),
TEST_X_FILTER);
Assert.assertEquals(2, filteredPaths.length);
Assert.assertTrue(containsPath(getTestRootPath(fc,
TEST_DIR_AXA), filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(fc,
TEST_DIR_AXX), filteredPaths));
}
@Test
public void testGlobStatusFilterWithNoMatchingPathsAndNonTrivialFilter() throws Exception {
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXA),
getTestRootPath(TEST_DIR_AXX),
getTestRootPath(TEST_DIR_AXX), };
public void testGlobStatusFilterWithNoMatchingPathsAndNonTrivialFilter()
throws Exception {
Path[] testDirs = {
getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) {
@ -539,17 +549,20 @@ public abstract class FileContextMainOperationsBaseTest {
}
//This should return an empty set
FileStatus[] filteredPaths = fc.util().globStatus(getTestRootPath("test/hadoop/?"),
TEST_X_FILTER);
FileStatus[] filteredPaths = fc.util().globStatus(
getTestRootPath(fc, "test/hadoop/?"),
TEST_X_FILTER);
Assert.assertEquals(0,filteredPaths.length);
}
@Test
public void testGlobStatusFilterWithMultiplePathWildcardsAndNonTrivialFilter() throws Exception {
Path[] testDirs = { getTestRootPath(TEST_DIR_AAA),
getTestRootPath(TEST_DIR_AXA),
getTestRootPath(TEST_DIR_AXX),
getTestRootPath(TEST_DIR_AXX), };
public void testGlobStatusFilterWithMultiplePathWildcardsAndNonTrivialFilter()
throws Exception {
Path[] testDirs = {
getTestRootPath(fc, TEST_DIR_AAA),
getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) {
for (Path path : testDirs) {
@ -558,11 +571,14 @@ public abstract class FileContextMainOperationsBaseTest {
}
//This should return two (axa, axx)
FileStatus[] filteredPaths = fc.util().globStatus(getTestRootPath("test/hadoop/a??"),
TEST_X_FILTER);
Assert.assertEquals(2,filteredPaths.length);
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXA), filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(TEST_DIR_AXX), filteredPaths));
FileStatus[] filteredPaths = fc.util().globStatus(
getTestRootPath(fc, "test/hadoop/a??"),
TEST_X_FILTER);
Assert.assertEquals(2, filteredPaths.length);
Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXA),
filteredPaths));
Assert.assertTrue(containsPath(getTestRootPath(fc, TEST_DIR_AXX),
filteredPaths));
}
@Test
@ -572,31 +588,33 @@ public abstract class FileContextMainOperationsBaseTest {
@Test
public void testWriteReadAndDeleteHalfABlock() throws Exception {
writeReadAndDelete(getBlockSize() / 2);
writeReadAndDelete(getDefaultBlockSize() / 2);
}
@Test
public void testWriteReadAndDeleteOneBlock() throws Exception {
writeReadAndDelete(getBlockSize());
writeReadAndDelete(getDefaultBlockSize());
}
@Test
public void testWriteReadAndDeleteOneAndAHalfBlocks() throws Exception {
writeReadAndDelete(getBlockSize() + (getBlockSize() / 2));
int blockSize = getDefaultBlockSize();
writeReadAndDelete(blockSize + (blockSize / 2));
}
@Test
public void testWriteReadAndDeleteTwoBlocks() throws Exception {
writeReadAndDelete(getBlockSize() * 2);
writeReadAndDelete(getDefaultBlockSize() * 2);
}
private void writeReadAndDelete(int len) throws IOException {
Path path = getTestRootPath("test/hadoop/file");
Path path = getTestRootPath(fc, "test/hadoop/file");
fc.mkdir(path.getParent(), FsPermission.getDefault(), true);
FSDataOutputStream out = fc.create(path, EnumSet.of(CreateFlag.CREATE),
CreateOpts.repFac((short) 1), CreateOpts.blockSize(getBlockSize()));
FSDataOutputStream out = fc.create(path, EnumSet.of(CreateFlag.CREATE),
CreateOpts.repFac((short) 1), CreateOpts
.blockSize(getDefaultBlockSize()));
out.write(data, 0, len);
out.close();
@ -621,7 +639,7 @@ public abstract class FileContextMainOperationsBaseTest {
@Test
public void testOverwrite() throws IOException {
Path path = getTestRootPath("test/hadoop/file");
Path path = getTestRootPath(fc, "test/hadoop/file");
fc.mkdir(path.getParent(), FsPermission.getDefault(), true);
@ -648,7 +666,7 @@ public abstract class FileContextMainOperationsBaseTest {
@Test
public void testWriteInNonExistentDirectory() throws IOException {
Path path = getTestRootPath("test/hadoop/file");
Path path = getTestRootPath(fc, "test/hadoop/file");
Assert.assertFalse("Parent doesn't exist", fc.exists(path.getParent()));
createFile(path);
@ -659,16 +677,16 @@ public abstract class FileContextMainOperationsBaseTest {
@Test
public void testDeleteNonExistentFile() throws IOException {
Path path = getTestRootPath("test/hadoop/file");
Path path = getTestRootPath(fc, "test/hadoop/file");
Assert.assertFalse("Doesn't exist", fc.exists(path));
Assert.assertFalse("No deletion", fc.delete(path, true));
}
@Test
public void testDeleteRecursively() throws IOException {
Path dir = getTestRootPath("test/hadoop");
Path file = getTestRootPath("test/hadoop/file");
Path subdir = getTestRootPath("test/hadoop/subdir");
Path dir = getTestRootPath(fc, "test/hadoop");
Path file = getTestRootPath(fc, "test/hadoop/file");
Path subdir = getTestRootPath(fc, "test/hadoop/subdir");
createFile(file);
fc.mkdir(subdir,FsPermission.getDefault(), true);
@ -695,7 +713,7 @@ public abstract class FileContextMainOperationsBaseTest {
@Test
public void testDeleteEmptyDirectory() throws IOException {
Path dir = getTestRootPath("test/hadoop");
Path dir = getTestRootPath(fc, "test/hadoop");
fc.mkdir(dir, FsPermission.getDefault(), true);
Assert.assertTrue("Dir exists", fc.exists(dir));
Assert.assertTrue("Deleted", fc.delete(dir, false));
@ -705,8 +723,8 @@ public abstract class FileContextMainOperationsBaseTest {
@Test
public void testRenameNonExistentPath() throws Exception {
if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/nonExistent");
Path dst = getTestRootPath("test/new/newpath");
Path src = getTestRootPath(fc, "test/hadoop/nonExistent");
Path dst = getTestRootPath(fc, "test/new/newpath");
try {
rename(src, dst, false, false, false, Rename.NONE);
Assert.fail("Should throw FileNotFoundException");
@ -726,9 +744,9 @@ public abstract class FileContextMainOperationsBaseTest {
public void testRenameFileToNonExistentDirectory() throws Exception {
if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/file");
Path src = getTestRootPath(fc, "test/hadoop/file");
createFile(src);
Path dst = getTestRootPath("test/nonExistent/newfile");
Path dst = getTestRootPath(fc, "test/nonExistent/newfile");
try {
rename(src, dst, false, true, false, Rename.NONE);
@ -749,9 +767,9 @@ public abstract class FileContextMainOperationsBaseTest {
public void testRenameFileToDestinationWithParentFile() throws Exception {
if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/file");
Path src = getTestRootPath(fc, "test/hadoop/file");
createFile(src);
Path dst = getTestRootPath("test/parentFile/newfile");
Path dst = getTestRootPath(fc, "test/parentFile/newfile");
createFile(dst.getParent());
try {
@ -771,9 +789,9 @@ public abstract class FileContextMainOperationsBaseTest {
public void testRenameFileToExistingParent() throws Exception {
if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/file");
Path src = getTestRootPath(fc, "test/hadoop/file");
createFile(src);
Path dst = getTestRootPath("test/new/newfile");
Path dst = getTestRootPath(fc, "test/new/newfile");
fc.mkdir(dst.getParent(), FileContext.DEFAULT_PERM, true);
rename(src, dst, true, false, true, Rename.OVERWRITE);
}
@ -782,9 +800,9 @@ public abstract class FileContextMainOperationsBaseTest {
public void testRenameFileAsExistingFile() throws Exception {
if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/file");
Path src = getTestRootPath(fc, "test/hadoop/file");
createFile(src);
Path dst = getTestRootPath("test/new/existingFile");
Path dst = getTestRootPath(fc, "test/new/existingFile");
createFile(dst);
// Fails without overwrite option
@ -803,9 +821,9 @@ public abstract class FileContextMainOperationsBaseTest {
public void testRenameFileAsExistingDirectory() throws Exception {
if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/file");
Path src = getTestRootPath(fc, "test/hadoop/file");
createFile(src);
Path dst = getTestRootPath("test/new/existingDir");
Path dst = getTestRootPath(fc, "test/new/existingDir");
fc.mkdir(dst, FileContext.DEFAULT_PERM, true);
// Fails without overwrite option
@ -827,9 +845,9 @@ public abstract class FileContextMainOperationsBaseTest {
public void testRenameDirectoryToNonExistentParent() throws Exception {
if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/dir");
Path src = getTestRootPath(fc, "test/hadoop/dir");
fc.mkdir(src, FileContext.DEFAULT_PERM, true);
Path dst = getTestRootPath("test/nonExistent/newdir");
Path dst = getTestRootPath(fc, "test/nonExistent/newdir");
try {
rename(src, dst, false, true, false, Rename.NONE);
@ -856,37 +874,37 @@ public abstract class FileContextMainOperationsBaseTest {
private void testRenameDirectoryAsNonExistentDirectory(Rename... options) throws Exception {
if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/dir");
Path src = getTestRootPath(fc, "test/hadoop/dir");
fc.mkdir(src, FileContext.DEFAULT_PERM, true);
createFile(getTestRootPath("test/hadoop/dir/file1"));
createFile(getTestRootPath("test/hadoop/dir/subdir/file2"));
createFile(getTestRootPath(fc, "test/hadoop/dir/file1"));
createFile(getTestRootPath(fc, "test/hadoop/dir/subdir/file2"));
Path dst = getTestRootPath("test/new/newdir");
Path dst = getTestRootPath(fc, "test/new/newdir");
fc.mkdir(dst.getParent(), FileContext.DEFAULT_PERM, true);
rename(src, dst, true, false, true, options);
Assert.assertFalse("Nested file1 exists",
fc.exists(getTestRootPath("test/hadoop/dir/file1")));
Assert.assertFalse("Nested file2 exists",
fc.exists(getTestRootPath("test/hadoop/dir/subdir/file2")));
Assert.assertTrue("Renamed nested file1 exists",
fc.exists(getTestRootPath("test/new/newdir/file1")));
Assert.assertTrue("Renamed nested exists",
fc.exists(getTestRootPath("test/new/newdir/subdir/file2")));
Assert.assertFalse("Nested file1 exists",
fc.exists(getTestRootPath(fc, "test/hadoop/dir/file1")));
Assert.assertFalse("Nested file2 exists",
fc.exists(getTestRootPath(fc, "test/hadoop/dir/subdir/file2")));
Assert.assertTrue("Renamed nested file1 exists", fc
.exists(getTestRootPath(fc, "test/new/newdir/file1")));
Assert.assertTrue("Renamed nested exists",
fc.exists(getTestRootPath(fc, "test/new/newdir/subdir/file2")));
}
@Test
public void testRenameDirectoryAsNonEmptyDirectory() throws Exception {
if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/dir");
Path src = getTestRootPath(fc, "test/hadoop/dir");
fc.mkdir(src, FileContext.DEFAULT_PERM, true);
createFile(getTestRootPath("test/hadoop/dir/file1"));
createFile(getTestRootPath("test/hadoop/dir/subdir/file2"));
createFile(getTestRootPath(fc, "test/hadoop/dir/file1"));
createFile(getTestRootPath(fc, "test/hadoop/dir/subdir/file2"));
Path dst = getTestRootPath("test/new/newdir");
Path dst = getTestRootPath(fc, "test/new/newdir");
fc.mkdir(dst, FileContext.DEFAULT_PERM, true);
createFile(getTestRootPath("test/new/newdir/file1"));
createFile(getTestRootPath(fc, "test/new/newdir/file1"));
// Fails without overwrite option
try {
rename(src, dst, false, true, false, Rename.NONE);
@ -907,9 +925,9 @@ public abstract class FileContextMainOperationsBaseTest {
public void testRenameDirectoryAsFile() throws Exception {
if (!renameSupported()) return;
Path src = getTestRootPath("test/hadoop/dir");
Path src = getTestRootPath(fc, "test/hadoop/dir");
fc.mkdir(src, FileContext.DEFAULT_PERM, true);
Path dst = getTestRootPath("test/new/newfile");
Path dst = getTestRootPath(fc, "test/new/newfile");
createFile(dst);
// Fails without overwrite option
try {
@ -929,7 +947,7 @@ public abstract class FileContextMainOperationsBaseTest {
public void testInputStreamClosedTwice() throws IOException {
//HADOOP-4760 according to Closeable#close() closing already-closed
//streams should have no effect.
Path src = getTestRootPath("test/hadoop/file");
Path src = getTestRootPath(fc, "test/hadoop/file");
createFile(src);
FSDataInputStream in = fc.open(src);
in.close();
@ -940,7 +958,7 @@ public abstract class FileContextMainOperationsBaseTest {
public void testOutputStreamClosedTwice() throws IOException {
//HADOOP-4760 according to Closeable#close() closing already-closed
//streams should have no effect.
Path src = getTestRootPath("test/hadoop/file");
Path src = getTestRootPath(fc, "test/hadoop/file");
FSDataOutputStream out = fc.create(src, EnumSet.of(CreateFlag.CREATE),
Options.CreateOpts.createParent());
@ -955,9 +973,10 @@ public abstract class FileContextMainOperationsBaseTest {
out.write(data, 0, data.length);
out.close();
}
private void rename(Path src, Path dst, boolean renameShouldSucceed,
boolean srcExists, boolean dstExists, Rename... options) throws IOException {
boolean srcExists, boolean dstExists, Rename... options)
throws IOException {
fc.rename(src, dst, options);
if (!renameShouldSucceed)
Assert.fail("rename should have thrown exception");
@ -967,7 +986,8 @@ public abstract class FileContextMainOperationsBaseTest {
private boolean containsPath(Path path, FileStatus[] filteredPaths)
throws IOException {
for(int i = 0; i < filteredPaths.length; i ++) {
if(getTestRootPath(path.toString()).equals(filteredPaths[i].getPath()))
if (getTestRootPath(fc, path.toString()).equals(
filteredPaths[i].getPath()))
return true;
}
return false;

View File

@ -19,7 +19,6 @@ package org.apache.hadoop.fs;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.StringTokenizer;
@ -32,6 +31,8 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.apache.hadoop.fs.FileContextTestHelper.*;
/**
* <p>
* A collection of permission tests for the {@link FileContext}.
@ -51,24 +52,8 @@ import org.junit.Test;
* @AfterClass public static void ClusterShutdownAtEnd()
* </p>
*/
public class FileContextPermissionBase {
static final String TEST_ROOT_DIR = new Path(System.getProperty(
"test.build.data", "/tmp")).toString().replace(' ', '_')
+ "/" + TestLocalFileSystemPermission.class.getSimpleName() + "_";
public abstract class FileContextPermissionBase {
protected Path getTestRootRelativePath(String pathString) {
return fc.makeQualified(new Path(TEST_ROOT_DIR, pathString));
}
private Path rootPath = null;
protected Path getTestRootPath() {
if (rootPath == null) {
rootPath = fc.makeQualified(new Path(TEST_ROOT_DIR));
}
return rootPath;
}
{
try {
((org.apache.commons.logging.impl.Log4JLogger)FileSystem.LOG).getLogger()
@ -84,23 +69,14 @@ public class FileContextPermissionBase {
@Before
public void setUp() throws Exception {
fc.mkdir(getTestRootPath(), FileContext.DEFAULT_PERM, true);
fc.mkdir(getTestRootPath(fc), FileContext.DEFAULT_PERM, true);
}
@After
public void tearDown() throws Exception {
fc.delete(getTestRootPath(), true);
fc.delete(getTestRootPath(fc), true);
}
private Path writeFile(FileContext theFc, String name) throws IOException {
Path f = getTestRootRelativePath(name);
FSDataOutputStream stm = theFc.create(f, EnumSet.of(CreateFlag.CREATE));
stm.writeBytes("42\n");
stm.close();
return f;
}
private void cleanupFile(FileContext theFc, Path name) throws IOException {
Assert.assertTrue(theFc.exists(name));
theFc.delete(name, true);
@ -114,7 +90,7 @@ public class FileContextPermissionBase {
return;
}
String filename = "foo";
Path f = writeFile(fc, filename);
Path f = createFile(fc, filename);
doFilePermissionCheck(FileContext.DEFAULT_PERM.applyUMask(fc.getUMask()),
fc.getFileStatus(f).getPermission());
}
@ -128,7 +104,7 @@ public class FileContextPermissionBase {
}
String filename = "foo";
Path f = writeFile(fc, filename);
Path f = createFile(fc, filename);
try {
// create files and manipulate them.
@ -152,7 +128,7 @@ public class FileContextPermissionBase {
}
String filename = "bar";
Path f = writeFile(fc, filename);
Path f = createFile(fc, filename);
List<String> groups = null;
try {
groups = getGroups();

View File

@ -0,0 +1,119 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.fs;
import java.io.IOException;
import java.util.EnumSet;
import org.apache.hadoop.fs.Options.CreateOpts;
import org.apache.hadoop.fs.Options.CreateOpts.BlockSize;
/**
* Helper class for unit tests.
*/
public final class FileContextTestHelper {
private static final String TEST_ROOT_DIR = System.getProperty("test.build.data",
"build/test/data") + "/test";
private static final int DEFAULT_BLOCK_SIZE = 1024;
private static final int DEFAULT_NUM_BLOCKS = 2;
private static String absTestRootDir = null;
/** Hidden constructor */
private FileContextTestHelper() {}
public static int getDefaultBlockSize() {
return DEFAULT_BLOCK_SIZE;
}
public static byte[] getFileData(int numOfBlocks, long blockSize) {
byte[] data = new byte[(int) (numOfBlocks * blockSize)];
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (i % 10);
}
return data;
}
public static Path getTestRootPath(FileContext fc) {
return fc.makeQualified(new Path(TEST_ROOT_DIR));
}
public static Path getTestRootPath(FileContext fc, String pathString) {
return fc.makeQualified(new Path(TEST_ROOT_DIR, pathString));
}
public static String getAbsoluteTestRootDir(FileContext fc)
throws IOException {
if (absTestRootDir == null) {
if (TEST_ROOT_DIR.startsWith("/")) {
absTestRootDir = TEST_ROOT_DIR;
} else {
absTestRootDir = getDefaultWorkingDirectory(fc).toString() + "/"
+ TEST_ROOT_DIR;
}
}
return absTestRootDir;
}
public static Path getTestRootDir(FileContext fc) throws IOException {
return fc.makeQualified(new Path(getAbsoluteTestRootDir(fc)));
}
public static Path getDefaultWorkingDirectory(FileContext fc)
throws IOException {
return getTestRootPath(fc, "/user/" + System.getProperty("user.name"))
.makeQualified(fc.getDefaultFileSystem().getUri(),
fc.getWorkingDirectory());
}
/*
* Create files with numBlocks blocks each with block size blockSize.
*/
public static void createFile(FileContext fc, Path path, int numBlocks,
CreateOpts... options) throws IOException {
BlockSize blockSizeOpt =
(BlockSize) CreateOpts.getOpt(CreateOpts.BlockSize.class, options);
long blockSize = blockSizeOpt != null ? blockSizeOpt.getValue()
: DEFAULT_BLOCK_SIZE;
FSDataOutputStream out =
fc.create(path, EnumSet.of(CreateFlag.CREATE), options);
byte[] data = getFileData(numBlocks, blockSize);
out.write(data, 0, data.length);
out.close();
}
public static void createFile(FileContext fc, Path path, int numBlocks,
int blockSize) throws IOException {
createFile(fc, path, numBlocks, CreateOpts.blockSize(blockSize),
CreateOpts.createParent());
}
public static void createFile(FileContext fc, Path path) throws IOException {
createFile(fc, path, DEFAULT_NUM_BLOCKS, CreateOpts.createParent());
}
public static Path createFile(FileContext fc, String name) throws IOException {
Path path = getTestRootPath(fc, name);
createFile(fc, path);
return path;
}
public static void createFileNonRecursive(FileContext fc, Path path)
throws IOException {
createFile(fc, path, DEFAULT_NUM_BLOCKS, CreateOpts.donotCreateParent());
}
}

View File

@ -20,14 +20,16 @@ package org.apache.hadoop.fs;
import java.io.*;
import java.util.ArrayList;
import java.util.EnumSet;
import junit.framework.Assert;
import org.apache.hadoop.fs.permission.FsPermission;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.apache.hadoop.fs.FileContextTestHelper.*;
/**
* <p>
* A collection of tests for the {@link FileContext} to test path names passed
@ -49,32 +51,18 @@ import org.junit.Test;
*/
public abstract class FileContextURIBase {
private static final String basePath = System.getProperty("test.build.data",
"build/test/data") + "/testContextURI";
"build/test/data") + "/testContextURI";
private static final Path BASE = new Path(basePath);
protected FileContext fc1;
protected FileContext fc2;
private static int BLOCK_SIZE = 1024;
private static byte[] data = new byte[BLOCK_SIZE * 2]; // two blocks of data
{
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (i % 10);
}
}
//Helper method to make path qualified
protected Path qualifiedPath(String path, FileContext fc) {
return fc.makeQualified(new Path(BASE, path));
}
// Helper method to create file and write data to file
protected void createFile(Path path, FileContext fc) throws IOException {
FSDataOutputStream out = fc.create(path, EnumSet.of(CreateFlag.CREATE),
Options.CreateOpts.createParent());
out.write(data, 0, data.length);
out.close();
}
@Before
public void setUp() throws Exception { }
@After
public void tearDown() throws Exception {
@ -100,7 +88,7 @@ public abstract class FileContextURIBase {
Assert.assertFalse(fc2.exists(testPath));
// Now create file
createFile(testPath, fc1);
createFile(fc1, testPath);
// Ensure fc2 has the created file
Assert.assertTrue(fc2.exists(testPath));
}
@ -117,7 +105,7 @@ public abstract class FileContextURIBase {
Assert.assertFalse(fc2.exists(testPath));
// Create a file on fc2's file system using fc1
createFile(testPath, fc1);
createFile(fc1, testPath);
Assert.fail("Create file with null name should throw IllegalArgumentException.");
} catch (IllegalArgumentException e) {
// expected
@ -134,11 +122,11 @@ public abstract class FileContextURIBase {
Assert.assertFalse(fc2.exists(testPath));
// Create a file on fc2's file system using fc1
createFile(testPath, fc1);
createFile(fc1, testPath);
// Create same file with fc1
try {
createFile(testPath, fc2);
createFile(fc2, testPath);
Assert.fail("Create existing file should throw an IOException.");
} catch (IOException e) {
// expected
@ -158,7 +146,7 @@ public abstract class FileContextURIBase {
Assert.assertFalse(fc2.exists(testPath));
// Create a file on fc2's file system using fc1
createFile(testPath, fc1);
createFile(fc1, testPath);
// Ensure using fc2 that file is created
Assert.assertTrue(fc2.isDirectory(testPath.getParent()));
@ -240,7 +228,7 @@ public abstract class FileContextURIBase {
Assert.assertTrue(fc2.exists(testDir));
// Create file on fc1 using fc2 context
createFile(qualifiedPath("test/hadoop/file", fc2), fc1);
createFile(fc1, qualifiedPath("test/hadoop/file", fc2));
Path testSubDir = qualifiedPath("test/hadoop/file/subdir", fc2);
try {
@ -292,7 +280,7 @@ public abstract class FileContextURIBase {
Assert.assertFalse(fc2.exists(testPath));
// First create a file on file system using fc1
createFile(testPath, fc1);
createFile(fc1, testPath);
// Ensure file exist
Assert.assertTrue(fc2.exists(testPath));
@ -319,7 +307,7 @@ public abstract class FileContextURIBase {
// TestCase2 : Create , Delete , Delete file
// Create a file on fc2's file system using fc1
createFile(testPath, fc1);
createFile(fc1, testPath);
// Ensure file exist
Assert.assertTrue(fc2.exists(testPath));
@ -346,7 +334,7 @@ public abstract class FileContextURIBase {
// TestCase2 : Create , Delete , Delete file
// Create a file on fc2's file system using fc1
createFile(testPath, fc1);
createFile(fc1, testPath);
// Ensure file exist
Assert.assertTrue(fc2.exists(testPath));
@ -440,7 +428,7 @@ public abstract class FileContextURIBase {
Path testPath = qualifiedPath(testFile, fc2);
// Create a file on fc2's file system using fc1
createFile(testPath, fc1);
createFile(fc1, testPath);
// Get modification time using fc2 and fc1
fc1ModificationTime = fc1.getFileStatus(testPath).getModificationTime();
fc2ModificationTime = fc2.getFileStatus(testPath).getModificationTime();
@ -454,7 +442,7 @@ public abstract class FileContextURIBase {
Path path2 = fc2.makeQualified(new Path(BASE, fileName));
// Create a file on fc2's file system using fc1
createFile(path2, fc1);
createFile(fc1, path2);
FsStatus fc2Status = fc2.getFsStatus(path2);
// FsStatus , used, free and capacity are non-negative longs

View File

@ -18,29 +18,21 @@
package org.apache.hadoop.fs;
import java.io.IOException;
import java.util.EnumSet;
import java.util.Set;
import junit.framework.Assert;
import org.apache.hadoop.fs.Options.CreateOpts;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.apache.hadoop.fs.FileContextTestHelper.*;
/**
* Tests {@link FileContext.#deleteOnExit(Path)} functionality.
*/
public class TestFileContextDeleteOnExit {
private static String TEST_ROOT_DIR =
System.getProperty("test.build.data", "/tmp") + "/test";
private static byte[] data = new byte[1024 * 2]; // two blocks of data
{
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (i % 10);
}
}
private static int blockSize = 1024;
private static int numBlocks = 2;
private FileContext fc;
@ -51,23 +43,9 @@ public class TestFileContextDeleteOnExit {
@After
public void tearDown() throws IOException {
fc.delete(getTestRootPath(), true);
fc.delete(getTestRootPath(fc), true);
}
private Path getTestRootPath() {
return fc.makeQualified(new Path(TEST_ROOT_DIR));
}
private Path getTestPath(String pathString) {
return fc.makeQualified(new Path(TEST_ROOT_DIR, pathString));
}
private void createFile(FileContext fc, Path path) throws IOException {
FSDataOutputStream out = fc.create(path, EnumSet.of(CreateFlag.CREATE),
CreateOpts.createParent());
out.write(data, 0, data.length);
out.close();
}
private void checkDeleteOnExitData(int size, FileContext fc, Path... paths) {
Assert.assertEquals(size, FileContext.DELETE_ON_EXIT.size());
@ -81,21 +59,21 @@ public class TestFileContextDeleteOnExit {
@Test
public void testDeleteOnExit() throws Exception {
// Create deleteOnExit entries
Path file1 = getTestPath("file1");
createFile(fc, file1);
Path file1 = getTestRootPath(fc, "file1");
createFile(fc, file1, numBlocks, blockSize);
fc.deleteOnExit(file1);
checkDeleteOnExitData(1, fc, file1);
// Ensure shutdown hook is added
Assert.assertTrue(Runtime.getRuntime().removeShutdownHook(FileContext.FINALIZER));
Path file2 = getTestPath("dir1/file2");
createFile(fc, file2);
Path file2 = getTestRootPath(fc, "dir1/file2");
createFile(fc, file2, numBlocks, blockSize);
fc.deleteOnExit(file2);
checkDeleteOnExitData(1, fc, file1, file2);
Path dir = getTestPath("dir3/dir4/dir5/dir6");
createFile(fc, dir);
Path dir = getTestRootPath(fc, "dir3/dir4/dir5/dir6");
createFile(fc, dir, numBlocks, blockSize);
fc.deleteOnExit(dir);
checkDeleteOnExitData(1, fc, file1, file2, dir);