TokenAspect { synchronized void ensureTokenInitialized() throws IOException { if (!hasInitedToken || (action != null && !action.isValid())) { Token<?> token = fs.getDelegationToken(null); if (token != null) { fs.setDelegationToken(token); addRenewAction(fs); LOG.debug("Created new DT for {}", token.getService()); } hasInitedToken = true; } } TokenAspect(T fs, final Text serviceName, final Text kind); synchronized void reset();  }
@Test public void testCachedInitialization() throws IOException, URISyntaxException { Configuration conf = new Configuration(); DummyFs fs = spy(new DummyFs()); Token<TokenIdentifier> token = new Token<TokenIdentifier>(new byte[0], new byte[0], DummyFs.TOKEN_KIND, new Text("127.0.0.1:1234")); doReturn(token).when(fs).getDelegationToken(any()); doReturn(token).when(fs).getRenewToken(); fs.emulateSecurityEnabled = true; fs.initialize(new URI("dummyfs: fs.tokenAspect.ensureTokenInitialized(); verify(fs, times(1)).getDelegationToken(null); verify(fs, times(1)).setDelegationToken(token); fs.tokenAspect.ensureTokenInitialized(); verify(fs, times(1)).getDelegationToken(null); verify(fs, times(1)).setDelegationToken(token); }
@Test public void testGetRemoteToken() throws IOException, URISyntaxException { Configuration conf = new Configuration(); DummyFs fs = spy(new DummyFs()); Token<TokenIdentifier> token = new Token<TokenIdentifier>(new byte[0], new byte[0], DummyFs.TOKEN_KIND, new Text("127.0.0.1:1234")); doReturn(token).when(fs).getDelegationToken(any()); doReturn(token).when(fs).getRenewToken(); fs.initialize(new URI("dummyfs: fs.tokenAspect.ensureTokenInitialized(); verify(fs).setDelegationToken(token); assertNotNull(Whitebox.getInternalState(fs.tokenAspect, "dtRenewer")); assertNotNull(Whitebox.getInternalState(fs.tokenAspect, "action")); }
@Test public void testGetRemoteTokenFailure() throws IOException, URISyntaxException { Configuration conf = new Configuration(); DummyFs fs = spy(new DummyFs()); IOException e = new IOException(); doThrow(e).when(fs).getDelegationToken(anyString()); fs.emulateSecurityEnabled = true; fs.initialize(new URI("dummyfs: try { fs.tokenAspect.ensureTokenInitialized(); } catch (IOException exc) { assertEquals(e, exc); } }
@Test public void testInitWithNoTokens() throws IOException, URISyntaxException { Configuration conf = new Configuration(); DummyFs fs = spy(new DummyFs()); doReturn(null).when(fs).getDelegationToken(anyString()); fs.initialize(new URI("dummyfs: fs.tokenAspect.ensureTokenInitialized(); verify(fs, never()).setDelegationToken( Mockito.<Token<? extends TokenIdentifier>> any()); }
@Test public void testInitWithUGIToken() throws IOException, URISyntaxException { Configuration conf = new Configuration(); DummyFs fs = spy(new DummyFs()); doReturn(null).when(fs).getDelegationToken(anyString()); Token<TokenIdentifier> token = new Token<TokenIdentifier>(new byte[0], new byte[0], DummyFs.TOKEN_KIND, new Text("127.0.0.1:1234")); fs.ugi.addToken(token); fs.ugi.addToken(new Token<TokenIdentifier>(new byte[0], new byte[0], new Text("Other token"), new Text("127.0.0.1:8021"))); assertEquals("wrong tokens in user", 2, fs.ugi.getTokens().size()); fs.emulateSecurityEnabled = true; fs.initialize(new URI("dummyfs: fs.tokenAspect.ensureTokenInitialized(); verify(fs).setDelegationToken(token); verify(fs, never()).getDelegationToken(anyString()); assertNull(Whitebox.getInternalState(fs.tokenAspect, "dtRenewer")); assertNull(Whitebox.getInternalState(fs.tokenAspect, "action")); }