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