/** 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. Needs to happen before creating the test users */ @Test public void testGetServerSideGroups() throws IOException, InterruptedException { // get the user name Process pp = Runtime.getRuntime().exec("whoami"); BufferedReader br = new BufferedReader(new InputStreamReader(pp.getInputStream())); String userName = br.readLine().trim(); // get the groups pp = Runtime.getRuntime().exec("id -Gn"); br = new BufferedReader(new InputStreamReader(pp.getInputStream())); String line = br.readLine(); System.out.println(userName + ":" + line); List<String> groups = new ArrayList<String>(); for (String s : line.split("[\\s]")) { groups.add(s); } final UserGroupInformation login = UserGroupInformation.getCurrentUser(); assertEquals(userName, login.getShortUserName()); String[] gi = login.getGroupNames(); assertEquals(groups.size(), gi.length); for (int i = 0; i < gi.length; i++) { assertEquals(groups.get(i), gi[i]); } final UserGroupInformation fakeUser = UserGroupInformation.createRemoteUser("foo.bar"); fakeUser.doAs( new PrivilegedExceptionAction<Object>() { @Override public Object run() throws IOException { UserGroupInformation current = UserGroupInformation.getCurrentUser(); assertFalse(current.equals(login)); assertEquals(current, fakeUser); assertEquals(0, current.getGroupNames().length); return null; } }); }
@Test public void testGettingGroups() throws Exception { UserGroupInformation uugi = UserGroupInformation.createUserForTesting(USER_NAME, GROUP_NAMES); assertEquals(USER_NAME, uugi.getUserName()); assertArrayEquals(new String[] {GROUP1_NAME, GROUP2_NAME, GROUP3_NAME}, uugi.getGroupNames()); }