Skip to content

Commit

Permalink
HADOOP-15313. TestKMS should close providers.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiao-chen committed Mar 26, 2018
1 parent 27d60a1 commit c22d62b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,15 @@ public void add(Throwable t) {
public IOException build() {
return createIOException(exceptions);
}

/**
* @return whether any exception was added.
*/
public boolean isEmpty() {
if (exceptions == null) {
return true;
}
return exceptions.isEmpty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.hadoop.crypto.key.kms.ValueQueue;
import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.MultipleIOException;
import org.apache.hadoop.minikdc.MiniKdc;
import org.apache.hadoop.security.Credentials;
import org.apache.hadoop.security.SecurityUtil;
Expand Down Expand Up @@ -84,6 +85,7 @@
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -111,6 +113,10 @@ public class TestKMS {

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
public final Timeout testTimeout = new Timeout(180000);

Expand Down Expand Up @@ -144,13 +150,17 @@ protected URL getKMSUrl() {

protected KeyProvider createProvider(URI uri, Configuration conf)
throws IOException {
return new LoadBalancingKMSClientProvider(
new KMSClientProvider[] { new KMSClientProvider(uri, conf) }, conf);
final KeyProvider ret = new LoadBalancingKMSClientProvider(
new KMSClientProvider[] {new KMSClientProvider(uri, conf)}, conf);
providersCreated.add(ret);
return ret;
}

private KMSClientProvider createKMSClientProvider(URI uri, Configuration conf)
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,
Expand Down Expand Up @@ -311,13 +321,28 @@ private void setUpMiniKdc() throws Exception {
}

@After
public void tearDownMiniKdc() throws Exception {
public void tearDown() throws Exception {
if (kdc != null) {
kdc.stop();
kdc = null;
}
UserGroupInformation.setShouldRenewImmediatelyForTests(false);
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)
Expand Down Expand Up @@ -449,6 +474,8 @@ public Void call() throws Exception {
}
}
Assert.assertTrue("Reloader is not alive", reloaderThread.isAlive());
// Explicitly close the provider so we can verify the internal thread
// is shutdown
testKp.close();
boolean reloaderStillAlive = true;
for (int i = 0; i < 10; i++) {
Expand Down Expand Up @@ -476,7 +503,6 @@ public Void run() throws Exception {
.addDelegationTokens("myuser", new Credentials());
Assert.assertEquals(1, tokens.length);
Assert.assertEquals("kms-dt", tokens[0].getKind().toString());
kp.close();
return null;
}
});
Expand All @@ -494,7 +520,6 @@ public Void run() throws Exception {
.addDelegationTokens("myuser", new Credentials());
Assert.assertEquals(1, tokens.length);
Assert.assertEquals("kms-dt", tokens[0].getKind().toString());
kp.close();
}
return null;
}
Expand Down Expand Up @@ -2533,7 +2558,6 @@ public void testWebHDFSProxyUserSimple() throws Exception {

@Test
public void testTGTRenewal() throws Exception {
tearDownMiniKdc();
Properties kdcConf = MiniKdc.createConf();
kdcConf.setProperty(MiniKdc.MAX_TICKET_LIFETIME, "3");
kdcConf.setProperty(MiniKdc.MIN_TICKET_LIFETIME, "3");
Expand Down

0 comments on commit c22d62b

Please sign in to comment.