HADOOP-15313. TestKMS should close providers.

(cherry picked from commit c22d62b338)
This commit is contained in:
Xiao Chen 2018-03-26 15:59:17 -07:00
parent eef5d1947e
commit 46edbedd99
2 changed files with 41 additions and 7 deletions

View File

@ -76,5 +76,15 @@ public class MultipleIOException extends IOException {
public IOException build() { public IOException build() {
return createIOException(exceptions); return createIOException(exceptions);
} }
/**
* @return whether any exception was added.
*/
public boolean isEmpty() {
if (exceptions == null) {
return true;
}
return exceptions.isEmpty();
}
} }
} }

View File

@ -35,6 +35,7 @@ import org.apache.hadoop.crypto.key.kms.LoadBalancingKMSClientProvider;
import org.apache.hadoop.crypto.key.kms.ValueQueue; import org.apache.hadoop.crypto.key.kms.ValueQueue;
import org.apache.hadoop.fs.CommonConfigurationKeysPublic; import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.MultipleIOException;
import org.apache.hadoop.minikdc.MiniKdc; import org.apache.hadoop.minikdc.MiniKdc;
import org.apache.hadoop.security.Credentials; import org.apache.hadoop.security.Credentials;
import org.apache.hadoop.security.SecurityUtil; import org.apache.hadoop.security.SecurityUtil;
@ -84,6 +85,7 @@ import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@ -111,6 +113,10 @@ public class TestKMS {
private SSLFactory sslFactory; private SSLFactory sslFactory;
// Keep track of all key providers created during a test case, so they can be
// closed at test tearDown.
private List<KeyProvider> providersCreated = new LinkedList<>();
@Rule @Rule
public final Timeout testTimeout = new Timeout(180000); public final Timeout testTimeout = new Timeout(180000);
@ -144,13 +150,17 @@ public class TestKMS {
protected KeyProvider createProvider(URI uri, Configuration conf) protected KeyProvider createProvider(URI uri, Configuration conf)
throws IOException { throws IOException {
return new LoadBalancingKMSClientProvider( final KeyProvider ret = new LoadBalancingKMSClientProvider(
new KMSClientProvider[] { new KMSClientProvider(uri, conf) }, conf); new KMSClientProvider[] {new KMSClientProvider(uri, conf)}, conf);
providersCreated.add(ret);
return ret;
} }
private KMSClientProvider createKMSClientProvider(URI uri, Configuration conf) private KMSClientProvider createKMSClientProvider(URI uri, Configuration conf)
throws IOException { throws IOException {
return new KMSClientProvider(uri, conf); final KMSClientProvider ret = new KMSClientProvider(uri, conf);
providersCreated.add(ret);
return ret;
} }
protected <T> T runServer(String keystore, String password, File confDir, protected <T> T runServer(String keystore, String password, File confDir,
@ -311,13 +321,28 @@ public class TestKMS {
} }
@After @After
public void tearDownMiniKdc() throws Exception { public void tearDown() throws Exception {
if (kdc != null) { if (kdc != null) {
kdc.stop(); kdc.stop();
kdc = null; kdc = null;
} }
UserGroupInformation.setShouldRenewImmediatelyForTests(false); UserGroupInformation.setShouldRenewImmediatelyForTests(false);
UserGroupInformation.reset(); UserGroupInformation.reset();
if (!providersCreated.isEmpty()) {
final MultipleIOException.Builder b = new MultipleIOException.Builder();
for (KeyProvider kp : providersCreated) {
try {
kp.close();
} catch (IOException e) {
LOG.error("Failed to close key provider.", e);
b.add(e);
}
}
providersCreated.clear();
if (!b.isEmpty()) {
throw b.build();
}
}
} }
private <T> T doAs(String user, final PrivilegedExceptionAction<T> action) private <T> T doAs(String user, final PrivilegedExceptionAction<T> action)
@ -449,6 +474,8 @@ public class TestKMS {
} }
} }
Assert.assertTrue("Reloader is not alive", reloaderThread.isAlive()); Assert.assertTrue("Reloader is not alive", reloaderThread.isAlive());
// Explicitly close the provider so we can verify the internal thread
// is shutdown
testKp.close(); testKp.close();
boolean reloaderStillAlive = true; boolean reloaderStillAlive = true;
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
@ -476,7 +503,6 @@ public class TestKMS {
.addDelegationTokens("myuser", new Credentials()); .addDelegationTokens("myuser", new Credentials());
Assert.assertEquals(1, tokens.length); Assert.assertEquals(1, tokens.length);
Assert.assertEquals("kms-dt", tokens[0].getKind().toString()); Assert.assertEquals("kms-dt", tokens[0].getKind().toString());
kp.close();
return null; return null;
} }
}); });
@ -494,7 +520,6 @@ public class TestKMS {
.addDelegationTokens("myuser", new Credentials()); .addDelegationTokens("myuser", new Credentials());
Assert.assertEquals(1, tokens.length); Assert.assertEquals(1, tokens.length);
Assert.assertEquals("kms-dt", tokens[0].getKind().toString()); Assert.assertEquals("kms-dt", tokens[0].getKind().toString());
kp.close();
} }
return null; return null;
} }
@ -2533,7 +2558,6 @@ public class TestKMS {
@Test @Test
public void testTGTRenewal() throws Exception { public void testTGTRenewal() throws Exception {
tearDownMiniKdc();
Properties kdcConf = MiniKdc.createConf(); Properties kdcConf = MiniKdc.createConf();
kdcConf.setProperty(MiniKdc.MAX_TICKET_LIFETIME, "3"); kdcConf.setProperty(MiniKdc.MAX_TICKET_LIFETIME, "3");
kdcConf.setProperty(MiniKdc.MIN_TICKET_LIFETIME, "3"); kdcConf.setProperty(MiniKdc.MIN_TICKET_LIFETIME, "3");