HADOOP-7389. Use of TestingGroups by tests causes subsequent tests to fail. Contributed by Aaron T. Myers.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1135820 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas White 2011-06-14 21:53:43 +00:00
parent 3d795b6ab4
commit c33c7fab2c
3 changed files with 35 additions and 28 deletions

View File

@ -306,6 +306,9 @@ Trunk (unreleased changes)
HADOOP-7356. RPM packages broke bin/hadoop script in developer environment. HADOOP-7356. RPM packages broke bin/hadoop script in developer environment.
(Eric Yang via todd) (Eric Yang via todd)
HADOOP-7389. Use of TestingGroups by tests causes subsequent tests to fail.
(atm via tomwhite)
Release 0.22.0 - Unreleased Release 0.22.0 - Unreleased
INCOMPATIBLE CHANGES INCOMPATIBLE CHANGES

View File

@ -878,17 +878,21 @@ public class UserGroupInformation {
private static class TestingGroups extends Groups { private static class TestingGroups extends Groups {
private final Map<String, List<String>> userToGroupsMapping = private final Map<String, List<String>> userToGroupsMapping =
new HashMap<String,List<String>>(); new HashMap<String,List<String>>();
private Groups underlyingImplementation;
private TestingGroups() { private TestingGroups(Groups underlyingImplementation) {
super(new org.apache.hadoop.conf.Configuration()); super(new org.apache.hadoop.conf.Configuration());
this.underlyingImplementation = underlyingImplementation;
} }
@Override @Override
public List<String> getGroups(String user) { public List<String> getGroups(String user) throws IOException {
List<String> result = userToGroupsMapping.get(user); List<String> result = userToGroupsMapping.get(user);
if (result == null) { if (result == null) {
result = new ArrayList<String>(); result = underlyingImplementation.getGroups(user);
} }
return result; return result;
} }
@ -910,7 +914,7 @@ public class UserGroupInformation {
UserGroupInformation ugi = createRemoteUser(user); UserGroupInformation ugi = createRemoteUser(user);
// make sure that the testing object is setup // make sure that the testing object is setup
if (!(groups instanceof TestingGroups)) { if (!(groups instanceof TestingGroups)) {
groups = new TestingGroups(); groups = new TestingGroups(groups);
} }
// add the user groups // add the user groups
((TestingGroups) groups).setUserGroups(ugi.getShortUserName(), userGroups); ((TestingGroups) groups).setUserGroups(ugi.getShortUserName(), userGroups);
@ -936,7 +940,7 @@ public class UserGroupInformation {
UserGroupInformation ugi = createProxyUser(user, realUser); UserGroupInformation ugi = createProxyUser(user, realUser);
// make sure that the testing object is setup // make sure that the testing object is setup
if (!(groups instanceof TestingGroups)) { if (!(groups instanceof TestingGroups)) {
groups = new TestingGroups(); groups = new TestingGroups(groups);
} }
// add the user groups // add the user groups
((TestingGroups) groups).setUserGroups(ugi.getShortUserName(), userGroups); ((TestingGroups) groups).setUserGroups(ugi.getShortUserName(), userGroups);

View File

@ -63,6 +63,29 @@ public class TestUserGroupInformation {
UserGroupInformation.setConfiguration(conf); UserGroupInformation.setConfiguration(conf);
} }
/** Test login method */
@Test
public void testLogin() throws Exception {
// login from unix
UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
assertEquals(UserGroupInformation.getCurrentUser(),
UserGroupInformation.getLoginUser());
assertTrue(ugi.getGroupNames().length >= 1);
// ensure that doAs works correctly
UserGroupInformation userGroupInfo =
UserGroupInformation.createUserForTesting(USER_NAME, GROUP_NAMES);
UserGroupInformation curUGI =
userGroupInfo.doAs(new PrivilegedExceptionAction<UserGroupInformation>(){
public UserGroupInformation run() throws IOException {
return UserGroupInformation.getCurrentUser();
}});
// make sure in the scope of the doAs, the right user is current
assertEquals(curUGI, userGroupInfo);
// make sure it is not the same as the login user
assertFalse(curUGI.equals(UserGroupInformation.getLoginUser()));
}
/** /**
* given user name - get all the groups. * given user name - get all the groups.
* Needs to happen before creating the test users * Needs to happen before creating the test users
@ -107,29 +130,6 @@ public class TestUserGroupInformation {
}}); }});
} }
/** Test login method */
@Test
public void testLogin() throws Exception {
// login from unix
UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
assertEquals(UserGroupInformation.getCurrentUser(),
UserGroupInformation.getLoginUser());
assertTrue(ugi.getGroupNames().length >= 1);
// ensure that doAs works correctly
UserGroupInformation userGroupInfo =
UserGroupInformation.createUserForTesting(USER_NAME, GROUP_NAMES);
UserGroupInformation curUGI =
userGroupInfo.doAs(new PrivilegedExceptionAction<UserGroupInformation>(){
public UserGroupInformation run() throws IOException {
return UserGroupInformation.getCurrentUser();
}});
// make sure in the scope of the doAs, the right user is current
assertEquals(curUGI, userGroupInfo);
// make sure it is not the same as the login user
assertFalse(curUGI.equals(UserGroupInformation.getLoginUser()));
}
/** test constructor */ /** test constructor */
@Test @Test
public void testConstructor() throws Exception { public void testConstructor() throws Exception {