/** test constructor */ @Test public void testConstructor() throws Exception { UserGroupInformation ugi = UserGroupInformation.createUserForTesting("user2/[email protected]", GROUP_NAMES); // make sure the short and full user names are correct assertEquals("user2/[email protected]", ugi.getUserName()); assertEquals("user2", ugi.getShortUserName()); ugi = UserGroupInformation.createUserForTesting(USER_NAME, GROUP_NAMES); assertEquals("user1", ugi.getShortUserName()); // failure test testConstructorFailures(null); testConstructorFailures(""); }
/** 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; } }); }