You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by su...@apache.org on 2013/03/06 20:15:22 UTC
svn commit: r1453486 [7/7] - in
/hadoop/common/trunk/hadoop-common-project/hadoop-common: ./ src/main/bin/
src/main/conf/ src/main/docs/src/documentation/content/xdocs/
src/main/java/ src/main/java/org/apache/hadoop/fs/
src/main/java/org/apache/hadoop/...
Modified: hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/TestUserGroupInformation.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/TestUserGroupInformation.java?rev=1453486&r1=1453485&r2=1453486&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/TestUserGroupInformation.java (original)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/TestUserGroupInformation.java Wed Mar 6 19:15:18 2013
@@ -42,6 +42,7 @@ import org.apache.hadoop.security.token.
import org.apache.hadoop.security.token.TokenIdentifier;
import static org.apache.hadoop.test.MetricsAsserts.*;
import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
+import org.apache.hadoop.util.Shell;
public class TestUserGroupInformation {
final private static String USER_NAME = "user1@HADOOP.APACHE.ORG";
@@ -90,17 +91,17 @@ public class TestUserGroupInformation {
UserGroupInformation.setLoginUser(null);
}
- @Test
+ @Test (timeout = 30000)
public void testSimpleLogin() throws IOException {
tryLoginAuthenticationMethod(AuthenticationMethod.SIMPLE, true);
}
- @Test
+ @Test (timeout = 30000)
public void testTokenLogin() throws IOException {
tryLoginAuthenticationMethod(AuthenticationMethod.TOKEN, false);
}
- @Test
+ @Test (timeout = 30000)
public void testProxyLogin() throws IOException {
tryLoginAuthenticationMethod(AuthenticationMethod.PROXY, false);
}
@@ -129,7 +130,7 @@ public class TestUserGroupInformation {
}
}
- @Test
+ @Test (timeout = 30000)
public void testGetRealAuthenticationMethod() {
UserGroupInformation ugi = UserGroupInformation.createRemoteUser("user1");
ugi.setAuthenticationMethod(AuthenticationMethod.SIMPLE);
@@ -140,7 +141,7 @@ public class TestUserGroupInformation {
assertEquals(AuthenticationMethod.SIMPLE, ugi.getRealAuthenticationMethod());
}
/** Test login method */
- @Test
+ @Test (timeout = 30000)
public void testLogin() throws Exception {
// login from unix
UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
@@ -167,7 +168,7 @@ public class TestUserGroupInformation {
* given user name - get all the groups.
* Needs to happen before creating the test users
*/
- @Test
+ @Test (timeout = 30000)
public void testGetServerSideGroups() throws IOException,
InterruptedException {
// get the user name
@@ -175,19 +176,38 @@ public class TestUserGroupInformation {
BufferedReader br = new BufferedReader
(new InputStreamReader(pp.getInputStream()));
String userName = br.readLine().trim();
+ // If on windows domain, token format is DOMAIN\\user and we want to
+ // extract only the user name
+ if(Shell.WINDOWS) {
+ int sp = userName.lastIndexOf('\\');
+ if (sp != -1) {
+ userName = userName.substring(sp + 1);
+ }
+ // user names are case insensitive on Windows. Make consistent
+ userName = userName.toLowerCase();
+ }
// get the groups
- pp = Runtime.getRuntime().exec("id -Gn " + userName);
+ pp = Runtime.getRuntime().exec(Shell.WINDOWS ?
+ Shell.WINUTILS + " groups -F" : "id -Gn");
br = new BufferedReader(new InputStreamReader(pp.getInputStream()));
String line = br.readLine();
+
System.out.println(userName + ":" + line);
Set<String> groups = new LinkedHashSet<String> ();
- for(String s: line.split("[\\s]")) {
+ String[] tokens = line.split(Shell.TOKEN_SEPARATOR_REGEX);
+ for(String s: tokens) {
groups.add(s);
}
final UserGroupInformation login = UserGroupInformation.getCurrentUser();
- assertEquals(userName, login.getShortUserName());
+ String loginUserName = login.getShortUserName();
+ if(Shell.WINDOWS) {
+ // user names are case insensitive on Windows. Make consistent
+ loginUserName = loginUserName.toLowerCase();
+ }
+ assertEquals(userName, loginUserName);
+
String[] gi = login.getGroupNames();
assertEquals(groups.size(), gi.length);
for(int i=0; i < gi.length; i++) {
@@ -208,7 +228,7 @@ public class TestUserGroupInformation {
}
/** test constructor */
- @Test
+ @Test (timeout = 30000)
public void testConstructor() throws Exception {
UserGroupInformation ugi =
UserGroupInformation.createUserForTesting("user2/cron@HADOOP.APACHE.ORG",
@@ -234,7 +254,7 @@ public class TestUserGroupInformation {
assertTrue(gotException);
}
- @Test
+ @Test (timeout = 30000)
public void testEquals() throws Exception {
UserGroupInformation uugi =
UserGroupInformation.createUserForTesting(USER_NAME, GROUP_NAMES);
@@ -252,7 +272,7 @@ public class TestUserGroupInformation {
assertEquals(uugi.hashCode(), ugi3.hashCode());
}
- @Test
+ @Test (timeout = 30000)
public void testEqualsWithRealUser() throws Exception {
UserGroupInformation realUgi1 = UserGroupInformation.createUserForTesting(
"RealUser", GROUP_NAMES);
@@ -265,7 +285,7 @@ public class TestUserGroupInformation {
assertFalse(remoteUgi.equals(proxyUgi1));
}
- @Test
+ @Test (timeout = 30000)
public void testGettingGroups() throws Exception {
UserGroupInformation uugi =
UserGroupInformation.createUserForTesting(USER_NAME, GROUP_NAMES);
@@ -275,7 +295,7 @@ public class TestUserGroupInformation {
}
@SuppressWarnings("unchecked") // from Mockito mocks
- @Test
+ @Test (timeout = 30000)
public <T extends TokenIdentifier> void testAddToken() throws Exception {
UserGroupInformation ugi =
UserGroupInformation.createRemoteUser("someone");
@@ -313,7 +333,7 @@ public class TestUserGroupInformation {
}
@SuppressWarnings("unchecked") // from Mockito mocks
- @Test
+ @Test (timeout = 30000)
public <T extends TokenIdentifier> void testGetCreds() throws Exception {
UserGroupInformation ugi =
UserGroupInformation.createRemoteUser("someone");
@@ -339,7 +359,7 @@ public class TestUserGroupInformation {
}
@SuppressWarnings("unchecked") // from Mockito mocks
- @Test
+ @Test (timeout = 30000)
public <T extends TokenIdentifier> void testAddCreds() throws Exception {
UserGroupInformation ugi =
UserGroupInformation.createRemoteUser("someone");
@@ -364,7 +384,7 @@ public class TestUserGroupInformation {
assertSame(secret, ugi.getCredentials().getSecretKey(secretKey));
}
- @Test
+ @Test (timeout = 30000)
public <T extends TokenIdentifier> void testGetCredsNotSame()
throws Exception {
UserGroupInformation ugi =
@@ -392,7 +412,7 @@ public class TestUserGroupInformation {
}
@SuppressWarnings("unchecked") // from Mockito mocks
- @Test
+ @Test (timeout = 30000)
public <T extends TokenIdentifier> void testAddNamedToken() throws Exception {
UserGroupInformation ugi =
UserGroupInformation.createRemoteUser("someone");
@@ -413,7 +433,7 @@ public class TestUserGroupInformation {
}
@SuppressWarnings("unchecked") // from Mockito mocks
- @Test
+ @Test (timeout = 30000)
public <T extends TokenIdentifier> void testUGITokens() throws Exception {
UserGroupInformation ugi =
UserGroupInformation.createUserForTesting("TheDoctor",
@@ -459,7 +479,7 @@ public class TestUserGroupInformation {
assertTrue(otherSet.contains(t2));
}
- @Test
+ @Test (timeout = 30000)
public void testTokenIdentifiers() throws Exception {
UserGroupInformation ugi = UserGroupInformation.createUserForTesting(
"TheDoctor", new String[] { "TheTARDIS" });
@@ -487,7 +507,7 @@ public class TestUserGroupInformation {
assertEquals(2, otherSet.size());
}
- @Test
+ @Test (timeout = 30000)
public void testTestAuthMethod() throws Exception {
UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
// verify the reverse mappings works
@@ -499,7 +519,7 @@ public class TestUserGroupInformation {
}
}
- @Test
+ @Test (timeout = 30000)
public void testUGIAuthMethod() throws Exception {
final UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
final AuthenticationMethod am = AuthenticationMethod.KERBEROS;
@@ -515,7 +535,7 @@ public class TestUserGroupInformation {
});
}
- @Test
+ @Test (timeout = 30000)
public void testUGIAuthMethodInRealUser() throws Exception {
final UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
UserGroupInformation proxyUgi = UserGroupInformation.createProxyUser(
@@ -550,7 +570,7 @@ public class TestUserGroupInformation {
Assert.assertEquals(proxyUgi3, proxyUgi4);
}
- @Test
+ @Test (timeout = 30000)
public void testLoginObjectInSubject() throws Exception {
UserGroupInformation loginUgi = UserGroupInformation.getLoginUser();
UserGroupInformation anotherUgi = new UserGroupInformation(loginUgi
@@ -563,7 +583,7 @@ public class TestUserGroupInformation {
Assert.assertTrue(login1 == login2);
}
- @Test
+ @Test (timeout = 30000)
public void testLoginModuleCommit() throws Exception {
UserGroupInformation loginUgi = UserGroupInformation.getLoginUser();
User user1 = loginUgi.getSubject().getPrincipals(User.class).iterator()
@@ -597,7 +617,7 @@ public class TestUserGroupInformation {
* with it, but that Subject was not created by Hadoop (ie it has no
* associated User principal)
*/
- @Test
+ @Test (timeout = 30000)
public void testUGIUnderNonHadoopContext() throws Exception {
Subject nonHadoopSubject = new Subject();
Subject.doAs(nonHadoopSubject, new PrivilegedExceptionAction<Void>() {
@@ -611,7 +631,7 @@ public class TestUserGroupInformation {
}
/** Test hasSufficientTimeElapsed method */
- @Test
+ @Test (timeout = 30000)
public void testHasSufficientTimeElapsed() throws Exception {
// Make hasSufficientTimeElapsed public
Method method = UserGroupInformation.class
Modified: hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDiskChecker.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDiskChecker.java?rev=1453486&r1=1453485&r2=1453486&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDiskChecker.java (original)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDiskChecker.java Wed Mar 6 19:15:18 2013
@@ -30,24 +30,29 @@ import org.apache.hadoop.fs.LocalFileSys
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.util.DiskChecker.DiskErrorException;
+import org.apache.hadoop.util.Shell;
public class TestDiskChecker {
final FsPermission defaultPerm = new FsPermission("755");
final FsPermission invalidPerm = new FsPermission("000");
- @Test public void testMkdirs_dirExists() throws Throwable {
+ @Test (timeout = 30000)
+ public void testMkdirs_dirExists() throws Throwable {
_mkdirs(true, defaultPerm, defaultPerm);
}
- @Test public void testMkdirs_noDir() throws Throwable {
+ @Test (timeout = 30000)
+ public void testMkdirs_noDir() throws Throwable {
_mkdirs(false, defaultPerm, defaultPerm);
}
- @Test public void testMkdirs_dirExists_badUmask() throws Throwable {
+ @Test (timeout = 30000)
+ public void testMkdirs_dirExists_badUmask() throws Throwable {
_mkdirs(true, defaultPerm, invalidPerm);
}
- @Test public void testMkdirs_noDir_badUmask() throws Throwable {
+ @Test (timeout = 30000)
+ public void testMkdirs_noDir_badUmask() throws Throwable {
_mkdirs(false, defaultPerm, invalidPerm);
}
@@ -78,23 +83,28 @@ public class TestDiskChecker {
}
}
- @Test public void testCheckDir_normal() throws Throwable {
+ @Test (timeout = 30000)
+ public void testCheckDir_normal() throws Throwable {
_checkDirs(true, new FsPermission("755"), true);
}
- @Test public void testCheckDir_notDir() throws Throwable {
+ @Test (timeout = 30000)
+ public void testCheckDir_notDir() throws Throwable {
_checkDirs(false, new FsPermission("000"), false);
}
- @Test public void testCheckDir_notReadable() throws Throwable {
+ @Test (timeout = 30000)
+ public void testCheckDir_notReadable() throws Throwable {
_checkDirs(true, new FsPermission("000"), false);
}
- @Test public void testCheckDir_notWritable() throws Throwable {
+ @Test (timeout = 30000)
+ public void testCheckDir_notWritable() throws Throwable {
_checkDirs(true, new FsPermission("444"), false);
}
- @Test public void testCheckDir_notListable() throws Throwable {
+ @Test (timeout = 30000)
+ public void testCheckDir_notListable() throws Throwable {
_checkDirs(true, new FsPermission("666"), false); // not listable
}
@@ -130,27 +140,27 @@ public class TestDiskChecker {
* permission for result of mapper.
*/
- @Test
+ @Test (timeout = 30000)
public void testCheckDir_normal_local() throws Throwable {
_checkDirs(true, "755", true);
}
- @Test
+ @Test (timeout = 30000)
public void testCheckDir_notDir_local() throws Throwable {
_checkDirs(false, "000", false);
}
- @Test
+ @Test (timeout = 30000)
public void testCheckDir_notReadable_local() throws Throwable {
_checkDirs(true, "000", false);
}
- @Test
+ @Test (timeout = 30000)
public void testCheckDir_notWritable_local() throws Throwable {
_checkDirs(true, "444", false);
}
- @Test
+ @Test (timeout = 30000)
public void testCheckDir_notListable_local() throws Throwable {
_checkDirs(true, "666", false);
}
@@ -160,8 +170,8 @@ public class TestDiskChecker {
File localDir = File.createTempFile("test", "tmp");
localDir.delete();
localDir.mkdir();
- Runtime.getRuntime().exec(
- "chmod " + perm + " " + localDir.getAbsolutePath()).waitFor();
+ Shell.execCommand(Shell.getSetPermissionCommand(perm, false,
+ localDir.getAbsolutePath()));
try {
DiskChecker.checkDir(localDir);
assertTrue("checkDir success", success);
Modified: hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestGenericOptionsParser.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestGenericOptionsParser.java?rev=1453486&r1=1453485&r2=1453486&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestGenericOptionsParser.java (original)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestGenericOptionsParser.java Wed Mar 6 19:15:18 2013
@@ -44,7 +44,9 @@ public class TestGenericOptionsParser ex
String[] args = new String[2];
// pass a files option
args[0] = "-files";
- args[1] = tmpFile.toString();
+ // Convert a file to a URI as File.toString() is not a valid URI on
+ // all platforms and GenericOptionsParser accepts only valid URIs
+ args[1] = tmpFile.toURI().toString();
new GenericOptionsParser(conf, args);
String files = conf.get("tmpfiles");
assertNotNull("files is null", files);
@@ -53,7 +55,7 @@ public class TestGenericOptionsParser ex
// pass file as uri
Configuration conf1 = new Configuration();
- URI tmpURI = new URI(tmpFile.toString() + "#link");
+ URI tmpURI = new URI(tmpFile.toURI().toString() + "#link");
args[0] = "-files";
args[1] = tmpURI.toString();
new GenericOptionsParser(conf1, args);
@@ -148,7 +150,7 @@ public class TestGenericOptionsParser ex
String[] args = new String[2];
// pass a files option
args[0] = "-tokenCacheFile";
- args[1] = tmpFile.toString();
+ args[1] = tmpFile.toURI().toString();
// test non existing file
Throwable th = null;
Modified: hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestShell.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestShell.java?rev=1453486&r1=1453485&r2=1453486&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestShell.java (original)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestShell.java Wed Mar 6 19:15:18 2013
@@ -81,6 +81,10 @@ public class TestShell extends TestCase
}
public void testShellCommandTimeout() throws Throwable {
+ if(Shell.WINDOWS) {
+ // setExecutable does not work on Windows
+ return;
+ }
String rootDir = new File(System.getProperty(
"test.build.data", "/tmp")).getAbsolutePath();
File shellFile = new File(rootDir, "timeout.sh");
Modified: hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestStringUtils.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestStringUtils.java?rev=1453486&r1=1453485&r2=1453486&view=diff
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestStringUtils.java (original)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestStringUtils.java Wed Mar 6 19:15:18 2013
@@ -25,7 +25,10 @@ import static org.junit.Assert.assertEqu
import static org.junit.Assert.fail;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
+import java.util.regex.Pattern;
import org.apache.hadoop.test.UnitTestcaseTimeLimit;
import org.apache.hadoop.util.StringUtils.TraditionalBinaryPrefix;
@@ -43,7 +46,7 @@ public class TestStringUtils extends Uni
final private static String ESCAPED_STR_WITH_BOTH2 =
"\\,A\\\\\\,\\,B\\\\\\\\\\,";
- @Test
+ @Test (timeout = 30000)
public void testEscapeString() throws Exception {
assertEquals(NULL_STR, StringUtils.escapeString(NULL_STR));
assertEquals(EMPTY_STR, StringUtils.escapeString(EMPTY_STR));
@@ -57,7 +60,7 @@ public class TestStringUtils extends Uni
StringUtils.escapeString(STR_WITH_BOTH2));
}
- @Test
+ @Test (timeout = 30000)
public void testSplit() throws Exception {
assertEquals(NULL_STR, StringUtils.split(NULL_STR));
String[] splits = StringUtils.split(EMPTY_STR);
@@ -87,7 +90,7 @@ public class TestStringUtils extends Uni
assertEquals(ESCAPED_STR_WITH_BOTH2, splits[0]);
}
- @Test
+ @Test (timeout = 30000)
public void testSimpleSplit() throws Exception {
final String[] TO_TEST = {
"a/b/c",
@@ -103,7 +106,7 @@ public class TestStringUtils extends Uni
}
}
- @Test
+ @Test (timeout = 30000)
public void testUnescapeString() throws Exception {
assertEquals(NULL_STR, StringUtils.unEscapeString(NULL_STR));
assertEquals(EMPTY_STR, StringUtils.unEscapeString(EMPTY_STR));
@@ -135,7 +138,7 @@ public class TestStringUtils extends Uni
StringUtils.unEscapeString(ESCAPED_STR_WITH_BOTH2));
}
- @Test
+ @Test (timeout = 30000)
public void testTraditionalBinaryPrefix() throws Exception {
//test string2long(..)
String[] symbol = {"k", "m", "g", "t", "p", "e"};
@@ -261,7 +264,7 @@ public class TestStringUtils extends Uni
assertEquals("0.5430%", StringUtils.formatPercent(0.00543, 4));
}
- @Test
+ @Test (timeout = 30000)
public void testJoin() {
List<String> s = new ArrayList<String>();
s.add("a");
@@ -273,7 +276,7 @@ public class TestStringUtils extends Uni
assertEquals("a:b:c", StringUtils.join(":", s.subList(0, 3)));
}
- @Test
+ @Test (timeout = 30000)
public void testGetTrimmedStrings() throws Exception {
String compactDirList = "/spindle1/hdfs,/spindle2/hdfs,/spindle3/hdfs";
String spacedDirList = "/spindle1/hdfs, /spindle2/hdfs, /spindle3/hdfs";
@@ -295,7 +298,7 @@ public class TestStringUtils extends Uni
assertArrayEquals(emptyArray, estring);
}
- @Test
+ @Test (timeout = 30000)
public void testCamelize() {
// common use cases
assertEquals("Map", StringUtils.camelize("MAP"));
@@ -331,7 +334,7 @@ public class TestStringUtils extends Uni
assertEquals("Zz", StringUtils.camelize("zZ"));
}
- @Test
+ @Test (timeout = 30000)
public void testStringToURI() {
String[] str = new String[] { "file://" };
try {
@@ -342,7 +345,7 @@ public class TestStringUtils extends Uni
}
}
- @Test
+ @Test (timeout = 30000)
public void testSimpleHostName() {
assertEquals("Should return hostname when FQDN is specified",
"hadoop01",
@@ -355,6 +358,49 @@ public class TestStringUtils extends Uni
StringUtils.simpleHostname("10.10.5.68"));
}
+ @Test (timeout = 5000)
+ public void testReplaceTokensShellEnvVars() {
+ Pattern pattern = StringUtils.SHELL_ENV_VAR_PATTERN;
+ Map<String, String> replacements = new HashMap<String, String>();
+ replacements.put("FOO", "one");
+ replacements.put("BAZ", "two");
+ replacements.put("NUMBERS123", "one-two-three");
+ replacements.put("UNDER_SCORES", "___");
+
+ assertEquals("one", StringUtils.replaceTokens("$FOO", pattern,
+ replacements));
+ assertEquals("two", StringUtils.replaceTokens("$BAZ", pattern,
+ replacements));
+ assertEquals("", StringUtils.replaceTokens("$BAR", pattern, replacements));
+ assertEquals("", StringUtils.replaceTokens("", pattern, replacements));
+ assertEquals("one-two-three", StringUtils.replaceTokens("$NUMBERS123",
+ pattern, replacements));
+ assertEquals("___", StringUtils.replaceTokens("$UNDER_SCORES", pattern,
+ replacements));
+ assertEquals("//one//two//", StringUtils.replaceTokens("//$FOO/$BAR/$BAZ//",
+ pattern, replacements));
+ }
+
+ @Test (timeout = 5000)
+ public void testReplaceTokensWinEnvVars() {
+ Pattern pattern = StringUtils.WIN_ENV_VAR_PATTERN;
+ Map<String, String> replacements = new HashMap<String, String>();
+ replacements.put("foo", "zoo");
+ replacements.put("baz", "zaz");
+
+ assertEquals("zoo", StringUtils.replaceTokens("%foo%", pattern,
+ replacements));
+ assertEquals("zaz", StringUtils.replaceTokens("%baz%", pattern,
+ replacements));
+ assertEquals("", StringUtils.replaceTokens("%bar%", pattern,
+ replacements));
+ assertEquals("", StringUtils.replaceTokens("", pattern, replacements));
+ assertEquals("zoo__zaz", StringUtils.replaceTokens("%foo%_%bar%_%baz%",
+ pattern, replacements));
+ assertEquals("begin zoo__zaz end", StringUtils.replaceTokens(
+ "begin %foo%_%bar%_%baz% end", pattern, replacements));
+ }
+
// Benchmark for StringUtils split
public static void main(String []args) {
final String TO_SPLIT = "foo,bar,baz,blah,blah";
Added: hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestWinUtils.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestWinUtils.java?rev=1453486&view=auto
==============================================================================
--- hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestWinUtils.java (added)
+++ hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestWinUtils.java Wed Mar 6 19:15:18 2013
@@ -0,0 +1,355 @@
+/**
+ * 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.util;
+
+import static org.junit.Assert.*;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.fs.FileUtil;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test cases for helper Windows winutils.exe utility.
+ */
+public class TestWinUtils {
+
+ private static final Log LOG = LogFactory.getLog(TestWinUtils.class);
+ private static File TEST_DIR = new File(System.getProperty("test.build.data",
+ "/tmp"), TestWinUtils.class.getSimpleName());
+
+ @Before
+ public void setUp() {
+ TEST_DIR.mkdirs();
+ }
+
+ @After
+ public void tearDown() throws IOException {
+ FileUtil.fullyDelete(TEST_DIR);
+ }
+
+ // Helper routine that writes the given content to the file.
+ private void writeFile(File file, String content) throws IOException {
+ byte[] data = content.getBytes();
+ FileOutputStream os = new FileOutputStream(file);
+ os.write(data);
+ os.close();
+ }
+
+ // Helper routine that reads the first 100 bytes from the file.
+ private String readFile(File file) throws IOException {
+ FileInputStream fos = new FileInputStream(file);
+ byte[] b = new byte[100];
+ fos.read(b);
+ return b.toString();
+ }
+
+ @Test (timeout = 30000)
+ public void testLs() throws IOException {
+ if (!Shell.WINDOWS) {
+ // Not supported on non-Windows platforms
+ return;
+ }
+
+ final String content = "6bytes";
+ final int contentSize = content.length();
+ File testFile = new File(TEST_DIR, "file1");
+ writeFile(testFile, content);
+
+ // Verify permissions and file name return tokens
+ String output = Shell.execCommand(
+ Shell.WINUTILS, "ls", testFile.getCanonicalPath());
+ String[] outputArgs = output.split("[ \r\n]");
+ assertTrue(outputArgs[0].equals("-rwx------"));
+ assertTrue(outputArgs[outputArgs.length - 1]
+ .equals(testFile.getCanonicalPath()));
+
+ // Verify most tokens when using a formatted output (other tokens
+ // will be verified with chmod/chown)
+ output = Shell.execCommand(
+ Shell.WINUTILS, "ls", "-F", testFile.getCanonicalPath());
+ outputArgs = output.split("[|\r\n]");
+ assertEquals(9, outputArgs.length);
+ assertTrue(outputArgs[0].equals("-rwx------"));
+ assertEquals(contentSize, Long.parseLong(outputArgs[4]));
+ assertTrue(outputArgs[8].equals(testFile.getCanonicalPath()));
+
+ testFile.delete();
+ assertFalse(testFile.exists());
+ }
+
+ @Test (timeout = 30000)
+ public void testGroups() throws IOException {
+ if (!Shell.WINDOWS) {
+ // Not supported on non-Windows platforms
+ return;
+ }
+
+ String currentUser = System.getProperty("user.name");
+
+ // Verify that groups command returns information about the current user
+ // groups when invoked with no args
+ String outputNoArgs = Shell.execCommand(
+ Shell.WINUTILS, "groups").trim();
+ String output = Shell.execCommand(
+ Shell.WINUTILS, "groups", currentUser).trim();
+ assertEquals(output, outputNoArgs);
+
+ // Verify that groups command with the -F flag returns the same information
+ String outputFormat = Shell.execCommand(
+ Shell.WINUTILS, "groups", "-F", currentUser).trim();
+ outputFormat = outputFormat.replace("|", " ");
+ assertEquals(output, outputFormat);
+ }
+
+ private void chmod(String mask, File file) throws IOException {
+ Shell.execCommand(
+ Shell.WINUTILS, "chmod", mask, file.getCanonicalPath());
+ }
+
+ private void chmodR(String mask, File file) throws IOException {
+ Shell.execCommand(
+ Shell.WINUTILS, "chmod", "-R", mask, file.getCanonicalPath());
+ }
+
+ private String ls(File file) throws IOException {
+ return Shell.execCommand(
+ Shell.WINUTILS, "ls", file.getCanonicalPath());
+ }
+
+ private String lsF(File file) throws IOException {
+ return Shell.execCommand(
+ Shell.WINUTILS, "ls", "-F", file.getCanonicalPath());
+ }
+
+ private void assertPermissions(File file, String expected)
+ throws IOException {
+ String output = ls(file).split("[ \r\n]")[0];
+ assertEquals(expected, output);
+ }
+
+ @Test (timeout = 30000)
+ private void testChmodInternal(String mode, String expectedPerm)
+ throws IOException {
+ File a = new File(TEST_DIR, "file1");
+ assertTrue(a.createNewFile());
+
+ // Reset permissions on the file to default
+ chmod("700", a);
+
+ // Apply the mode mask
+ chmod(mode, a);
+
+ // Compare the output
+ assertPermissions(a, expectedPerm);
+
+ a.delete();
+ assertFalse(a.exists());
+ }
+
+ @Test (timeout = 30000)
+ private void testNewFileChmodInternal(String expectedPerm) throws IOException {
+ // Create a new directory
+ File dir = new File(TEST_DIR, "dir1");
+
+ assertTrue(dir.mkdir());
+
+ // Set permission use chmod
+ chmod("755", dir);
+
+ // Create a child file in the directory
+ File child = new File(dir, "file1");
+ assertTrue(child.createNewFile());
+
+ // Verify the child file has correct permissions
+ assertPermissions(child, expectedPerm);
+
+ child.delete();
+ dir.delete();
+ assertFalse(dir.exists());
+ }
+
+ @Test (timeout = 30000)
+ private void testChmodInternalR(String mode, String expectedPerm,
+ String expectedPermx) throws IOException {
+ // Setup test folder hierarchy
+ File a = new File(TEST_DIR, "a");
+ assertTrue(a.mkdir());
+ chmod("700", a);
+ File aa = new File(a, "a");
+ assertTrue(aa.createNewFile());
+ chmod("600", aa);
+ File ab = new File(a, "b");
+ assertTrue(ab.mkdir());
+ chmod("700", ab);
+ File aba = new File(ab, "a");
+ assertTrue(aba.mkdir());
+ chmod("700", aba);
+ File abb = new File(ab, "b");
+ assertTrue(abb.createNewFile());
+ chmod("600", abb);
+ File abx = new File(ab, "x");
+ assertTrue(abx.createNewFile());
+ chmod("u+x", abx);
+
+ // Run chmod recursive
+ chmodR(mode, a);
+
+ // Verify outcome
+ assertPermissions(a, "d" + expectedPermx);
+ assertPermissions(aa, "-" + expectedPerm);
+ assertPermissions(ab, "d" + expectedPermx);
+ assertPermissions(aba, "d" + expectedPermx);
+ assertPermissions(abb, "-" + expectedPerm);
+ assertPermissions(abx, "-" + expectedPermx);
+
+ assertTrue(FileUtil.fullyDelete(a));
+ }
+
+ @Test (timeout = 30000)
+ public void testBasicChmod() throws IOException {
+ if (!Shell.WINDOWS) {
+ // Not supported on non-Windows platforms
+ return;
+ }
+
+ // - Create a file.
+ // - Change mode to 377 so owner does not have read permission.
+ // - Verify the owner truly does not have the permissions to read.
+ File a = new File(TEST_DIR, "a");
+ a.createNewFile();
+ chmod("377", a);
+
+ try {
+ readFile(a);
+ assertFalse("readFile should have failed!", true);
+ } catch (IOException ex) {
+ LOG.info("Expected: Failed read from a file with permissions 377");
+ }
+ // restore permissions
+ chmod("700", a);
+
+ // - Create a file.
+ // - Change mode to 577 so owner does not have write permission.
+ // - Verify the owner truly does not have the permissions to write.
+ chmod("577", a);
+
+ try {
+ writeFile(a, "test");
+ assertFalse("writeFile should have failed!", true);
+ } catch (IOException ex) {
+ LOG.info("Expected: Failed write to a file with permissions 577");
+ }
+ // restore permissions
+ chmod("700", a);
+ assertTrue(a.delete());
+
+ // - Copy WINUTILS to a new executable file, a.exe.
+ // - Change mode to 677 so owner does not have execute permission.
+ // - Verify the owner truly does not have the permissions to execute the file.
+
+ File winutilsFile = new File(Shell.WINUTILS);
+ File aExe = new File(TEST_DIR, "a.exe");
+ FileUtils.copyFile(winutilsFile, aExe);
+ chmod("677", aExe);
+
+ try {
+ Shell.execCommand(aExe.getCanonicalPath(), "ls");
+ assertFalse("executing " + aExe + " should have failed!", true);
+ } catch (IOException ex) {
+ LOG.info("Expected: Failed to execute a file with permissions 677");
+ }
+ assertTrue(aExe.delete());
+ }
+
+ @Test (timeout = 30000)
+ public void testChmod() throws IOException {
+ if (!Shell.WINDOWS) {
+ // Not supported on non-Windows platforms
+ return;
+ }
+
+ testChmodInternal("7", "-------rwx");
+ testChmodInternal("70", "----rwx---");
+ testChmodInternal("u-x,g+r,o=g", "-rw-r--r--");
+ testChmodInternal("u-x,g+rw", "-rw-rw----");
+ testChmodInternal("u-x,g+rwx-x,o=u", "-rw-rw-rw-");
+ testChmodInternal("+", "-rwx------");
+
+ // Recursive chmod tests
+ testChmodInternalR("755", "rwxr-xr-x", "rwxr-xr-x");
+ testChmodInternalR("u-x,g+r,o=g", "rw-r--r--", "rw-r--r--");
+ testChmodInternalR("u-x,g+rw", "rw-rw----", "rw-rw----");
+ testChmodInternalR("u-x,g+rwx-x,o=u", "rw-rw-rw-", "rw-rw-rw-");
+ testChmodInternalR("a+rX", "rw-r--r--", "rwxr-xr-x");
+
+ // Test a new file created in a chmod'ed directory has expected permission
+ testNewFileChmodInternal("-rwx------");
+ }
+
+ private void chown(String userGroup, File file) throws IOException {
+ Shell.execCommand(
+ Shell.WINUTILS, "chown", userGroup, file.getCanonicalPath());
+ }
+
+ private void assertOwners(File file, String expectedUser,
+ String expectedGroup) throws IOException {
+ String [] args = lsF(file).trim().split("[\\|]");
+ assertEquals(expectedUser.toLowerCase(), args[2].toLowerCase());
+ assertEquals(expectedGroup.toLowerCase(), args[3].toLowerCase());
+ }
+
+ @Test (timeout = 30000)
+ public void testChown() throws IOException {
+ if (!Shell.WINDOWS) {
+ // Not supported on non-Windows platforms
+ return;
+ }
+
+ File a = new File(TEST_DIR, "a");
+ assertTrue(a.createNewFile());
+ String username = System.getProperty("user.name");
+ // username including the domain aka DOMAIN\\user
+ String qualifiedUsername = Shell.execCommand("whoami").trim();
+ String admins = "Administrators";
+ String qualifiedAdmins = "BUILTIN\\Administrators";
+
+ chown(username + ":" + admins, a);
+ assertOwners(a, qualifiedUsername, qualifiedAdmins);
+
+ chown(username, a);
+ chown(":" + admins, a);
+ assertOwners(a, qualifiedUsername, qualifiedAdmins);
+
+ chown(":" + admins, a);
+ chown(username + ":", a);
+ assertOwners(a, qualifiedUsername, qualifiedAdmins);
+
+ assertTrue(a.delete());
+ assertFalse(a.exists());
+ }
+}