HADOOP-13388. Clean up TestLocalFileSystemPermission. Contributed by Andras Bokor.

This commit is contained in:
Anu Engineer 2016-09-07 11:09:41 -07:00
parent 7fdfcd8a6c
commit f414d5e118
1 changed files with 60 additions and 89 deletions

View File

@ -18,24 +18,23 @@
package org.apache.hadoop.fs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.permission.*;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.test.GenericTestUtils;
import org.apache.log4j.Level;
import org.apache.hadoop.util.Shell;
import java.io.*;
import java.util.*;
import org.apache.log4j.Level;
import org.junit.Test;
import static org.apache.hadoop.test.PlatformAssumptions.assumeNotWindows;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import static org.apache.hadoop.test.PlatformAssumptions.assumeNotWindows;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.*;
/**
* This class tests the local file system via the FileSystem abstraction.
@ -60,18 +59,22 @@ public class TestLocalFileSystemPermission {
return f;
}
private Path writeFile(FileSystem fs, String name, FsPermission perm) throws IOException {
private Path writeFile(FileSystem fs, String name, FsPermission perm)
throws IOException {
Path f = new Path(TEST_PATH_PREFIX + name);
FSDataOutputStream stm = fs.create(f, perm, true, 2048, (short)1, 32 * 1024 * 1024, null);
FSDataOutputStream stm = fs.create(f, perm, true, 2048, (short)1,
32 * 1024 * 1024, null);
stm.writeBytes("42\n");
stm.close();
return f;
}
private void cleanup(FileSystem fs, Path name) throws IOException {
if (name!=null) {
assertTrue(fs.exists(name));
fs.delete(name, true);
assertTrue(!fs.exists(name));
assertFalse(fs.exists(name));
}
}
@Test
@ -82,39 +85,33 @@ public class TestLocalFileSystemPermission {
conf.set(CommonConfigurationKeys.FS_PERMISSIONS_UMASK_KEY, "044");
Path dir = new Path(TEST_PATH_PREFIX + "dir");
localfs.mkdirs(dir);
Path dir1 = new Path(TEST_PATH_PREFIX + "dir1");
Path dir2 = new Path(TEST_PATH_PREFIX + "dir2");
try {
FsPermission initialPermission = getPermission(localfs, dir);
assertEquals(
FsPermission.getDirDefault().applyUMask(FsPermission.getUMask(conf)),
FsPermission.getDirDefault()
.applyUMask(FsPermission.getUMask(conf)),
initialPermission);
} catch(Exception e) {
LOGGER.error("Cannot run test", e);
return;
}
FsPermission perm = new FsPermission((short)0755);
Path dir1 = new Path(TEST_PATH_PREFIX + "dir1");
localfs.mkdirs(dir1, perm);
try {
FsPermission initialPermission = getPermission(localfs, dir1);
assertEquals(perm.applyUMask(FsPermission.getUMask(conf)), initialPermission);
} catch(Exception e) {
LOGGER.error("Cannot run test", e);
return;
}
Path dir2 = new Path(TEST_PATH_PREFIX + "dir2");
localfs.mkdirs(dir1, perm);
initialPermission = getPermission(localfs, dir1);
assertEquals(perm.applyUMask(FsPermission.getUMask(conf)),
initialPermission);
localfs.mkdirs(dir2);
try {
FsPermission initialPermission = getPermission(localfs, dir2);
initialPermission = getPermission(localfs, dir2);
Path copyPath = new Path(TEST_PATH_PREFIX + "dir_copy");
localfs.rename(dir2, copyPath);
FsPermission copyPermission = getPermission(localfs, copyPath);
assertEquals(copyPermission, initialPermission);
assertEquals(initialPermission, copyPermission);
dir2 = copyPath;
} catch (Exception e) {
LOGGER.error("Cannot run test", e);
return;
} finally {
cleanup(localfs, dir);
cleanup(localfs, dir1);
@ -124,52 +121,42 @@ public class TestLocalFileSystemPermission {
}
}
/** Test LocalFileSystem.setPermission */
/** Test LocalFileSystem.setPermission. */
@Test
public void testLocalFSsetPermission() throws IOException {
assumeNotWindows();
Configuration conf = new Configuration();
conf.set(CommonConfigurationKeys.FS_PERMISSIONS_UMASK_KEY, "044");
LocalFileSystem localfs = FileSystem.getLocal(conf);
Path f = null;
Path f1 = null;
Path f2 = null;
String filename = "foo";
Path f = writeFile(localfs, filename);
String filename1 = "foo1";
String filename2 = "foo2";
FsPermission perm = new FsPermission((short)0755);
try {
f = writeFile(localfs, filename);
f1 = writeFile(localfs, filename1, perm);
f2 = writeFile(localfs, filename2);
FsPermission initialPermission = getPermission(localfs, f);
assertEquals(
FsPermission.getFileDefault().applyUMask(FsPermission.getUMask(conf)),
initialPermission);
} catch(Exception e) {
LOGGER.error("Cannot run test", e);
return;
}
String filename1 = "foo1";
FsPermission perm = new FsPermission((short)0755);
Path f1 = writeFile(localfs, filename1, perm);
try {
FsPermission initialPermission = getPermission(localfs, f1);
initialPermission = getPermission(localfs, f1);
assertEquals(
perm.applyUMask(FsPermission.getUMask(conf)), initialPermission);
} catch(Exception e) {
LOGGER.error("Cannot run test", e);
return;
}
String filename2 = "foo2";
Path f2 = writeFile(localfs, filename2);
try {
FsPermission initialPermission = getPermission(localfs, f2);
initialPermission = getPermission(localfs, f2);
Path copyPath = new Path(TEST_PATH_PREFIX + "/foo_copy");
localfs.rename(f2, copyPath);
FsPermission copyPermission = getPermission(localfs, copyPath);
assertEquals(copyPermission, initialPermission);
f2 = copyPath;
} catch (Exception e) {
LOGGER.error("Cannot run test", e);
return;
}
try {
// create files and manipulate them.
FsPermission all = new FsPermission((short)0777);
FsPermission none = new FsPermission((short)0);
@ -179,8 +166,7 @@ public class TestLocalFileSystemPermission {
localfs.setPermission(f, all);
assertEquals(all, getPermission(localfs, f));
}
finally {
} finally {
cleanup(localfs, f);
cleanup(localfs, f1);
if (localfs.exists(f2)) {
@ -196,33 +182,19 @@ public class TestLocalFileSystemPermission {
/** Test LocalFileSystem.setOwner. */
@Test
public void testLocalFSsetOwner() throws IOException {
if (Path.WINDOWS) {
LOGGER.info("Cannot run test for Windows");
return;
}
assumeNotWindows();
Configuration conf = new Configuration();
conf.set(CommonConfigurationKeys.FS_PERMISSIONS_UMASK_KEY, "044");
LocalFileSystem localfs = FileSystem.getLocal(conf);
String filename = "bar";
Path f = writeFile(localfs, filename);
List<String> groups = null;
List<String> groups;
try {
groups = getGroups();
LOGGER.info("{}: {}", filename, getPermission(localfs, f));
}
catch(IOException e) {
LOGGER.error("Cannot run test", e);
return;
}
if (groups == null || groups.size() < 1) {
LOGGER.error("Cannot run test: need at least one group. groups={}",
groups);
return;
}
// create files and manipulate them.
try {
String g0 = groups.get(0);
localfs.setOwner(f, null, g0);
assertEquals(g0, getGroup(localfs, f));
@ -235,8 +207,9 @@ public class TestLocalFileSystemPermission {
LOGGER.info("Not testing changing the group since user " +
"belongs to only one group.");
}
} finally {
cleanup(localfs, f);
}
finally {cleanup(localfs, f);}
}
/**
@ -250,10 +223,7 @@ public class TestLocalFileSystemPermission {
*/
@Test
public void testSetUmaskInRealTime() throws Exception {
if (Path.WINDOWS) {
LOGGER.info("Cannot run test for Windows");
return;
}
assumeNotWindows();
LocalFileSystem localfs = FileSystem.getLocal(new Configuration());
Configuration conf = localfs.getConf();
@ -289,9 +259,10 @@ public class TestLocalFileSystemPermission {
}
static List<String> getGroups() throws IOException {
List<String> a = new ArrayList<String>();
List<String> a = new ArrayList<>();
String s = Shell.execCommand(Shell.getGroupsCommand());
for(StringTokenizer t = new StringTokenizer(s); t.hasMoreTokens(); ) {
StringTokenizer t = new StringTokenizer(s);
while (t.hasMoreTokens()) {
a.add(t.nextToken());
}
return a;